{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport time\nfrom PIL import Image\nimport tensorflow as tf\n\nfrom tensorflow.keras import layers, Sequential\nfrom tensorflow.keras.models import Sequential, load_model\nfrom tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-19T18:59:42.280747Z","iopub.execute_input":"2023-09-19T18:59:42.281975Z","iopub.status.idle":"2023-09-19T18:59:42.293081Z","shell.execute_reply.started":"2023-09-19T18:59:42.281930Z","shell.execute_reply":"2023-09-19T18:59:42.292143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2023-09-19T18:59:42.294745Z","iopub.execute_input":"2023-09-19T18:59:42.295185Z","iopub.status.idle":"2023-09-19T18:59:51.656648Z","shell.execute_reply.started":"2023-09-19T18:59:42.295152Z","shell.execute_reply":"2023-09-19T18:59:51.655552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import py7zr\n\n# Define the paths to the input .7z files and the directory where you want to extract them\ninput_dir = '/kaggle/input/cifar-10'\noutput_dir = '/kaggle/working/cifar-10'  \n\n# Create the output directory if it doesn't exist\nos.makedirs(output_dir, exist_ok=True)\n# Extract the test and train data\n# with py7zr.SevenZipFile(os.path.join(input_dir, 'test.7z'), mode='r') as z:\n#     z.extractall(path=output_dir)\n\n\nprint(\"\\nExtraction of Train_Data started\")\nwith py7zr.SevenZipFile(os.path.join(input_dir, 'train.7z'), mode='r') as z:\n    z.extractall(path=output_dir)\nprint(\"Train_Data Extraction completed successfully.\")\n\nprint(\"Extraction of Test_Data started\")\nwith py7zr.SevenZipFile(os.path.join(input_dir, 'test.7z'), mode='r') as z:\n    z.extractall(path=output_dir)\nprint(\"Test_Data Extraction completed successfully.\")","metadata":{"execution":{"iopub.status.busy":"2023-09-19T18:59:51.658086Z","iopub.execute_input":"2023-09-19T18:59:51.658396Z","iopub.status.idle":"2023-09-19T19:22:15.965188Z","shell.execute_reply.started":"2023-09-19T18:59:51.658364Z","shell.execute_reply":"2023-09-19T19:22:15.964164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define paths\nTRAIN_DIR = \"/kaggle/working/cifar-10/train\"\nTRAIN_LABELS_CSV = \"/kaggle/input/cifar-10/trainLabels.csv\"\n\n\n# Load train labels from CSV\ntrain_labels_df = pd.read_csv(TRAIN_LABELS_CSV)\nlabel_mapping = dict(zip(train_labels_df['id'].values, train_labels_df['label'].values))\nlabel_to_index = {label: index for index, label in enumerate(train_labels_df['label'].unique())}\n\n# Explicitly specify the index for each label\nlabel_to_index = {\n    \"airplane\": 0,\n    \"automobile\": 1,\n    \"bird\": 2,\n    \"cat\": 3,\n    \"deer\": 4,\n    \"dog\": 5,\n    \"frog\": 6,\n    \"horse\": 7,\n    \"ship\": 8,\n    \"truck\": 9\n}\n\n# Print the label categories\nprint(\"Label categories:\", list(label_to_index.keys()))\n\ndef load_images_from_directory(directory, label_provided=True):\n    images_list = []\n    labels_list = []\n    for filename in sorted(os.listdir(directory), key=lambda x: int(x.split('.')[0])):  # Sort by ID\n        with Image.open(os.path.join(directory, filename)) as img:\n            images_list.append(np.array(img))\n            if label_provided:\n                image_id = int(filename.split('.')[0])\n                labels_list.append(label_to_index[label_mapping[image_id]])\n    images = np.array(images_list)\n    labels = np.array(labels_list) if label_provided else None\n    return images, labels","metadata":{"execution":{"iopub.status.busy":"2023-09-19T19:22:15.967382Z","iopub.execute_input":"2023-09-19T19:22:15.967688Z","iopub.status.idle":"2023-09-19T19:22:16.023703Z","shell.execute_reply.started":"2023-09-19T19:22:15.967661Z","shell.execute_reply":"2023-09-19T19:22:16.022739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n# Load the data\nTrain_Data, Train_Label = load_images_from_directory(TRAIN_DIR)\nprint('Shape of Test_Data',Train_Data.shape)\n\n# Normalize the data\nx_train = Train_Data.astype('float32') / 255.0\ny_train = tf.keras.utils.to_categorical(Train_Label, 10)\n\n# connect to a TPU and instantiate a distribution strategy\ntpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()\ntpu_strategy = tf.distribute.TPUStrategy(tpu)\nprint('Number of replicas:', tpu_strategy.num_replicas_in_sync)\n\n# Define the input shape and number of classes\ninput_shape = (32, 32, 3)\nnum_classes = 10  # Adjust this based on your dataset\n# Open a strategy scope.\nwith tpu_strategy.scope():\n    # Everything that creates variables should be under the strategy scope.\n    # In general this is only model's `build`, `compile`.\n    \n\n# Define your model within the TPU strategy scope\n    model = Sequential([\n        layers.Conv2D(32, (3, 3), activation='relu', input_shape=input_shape, padding='same'),\n        layers.BatchNormalization(),\n        layers.Conv2D(32, (3, 3), activation='relu', padding='same'),\n        layers.BatchNormalization(),\n        layers.MaxPooling2D((2, 2)),\n        \n        layers.Conv2D(64, (3, 3), activation='relu', padding='same'),\n        layers.BatchNormalization(),\n        layers.Conv2D(64, (3, 3), activation='relu', padding='same'),\n        layers.BatchNormalization(),\n        layers.MaxPooling2D((2, 2)),\n        \n        layers.Conv2D(128, (3, 3), activation='relu', padding='same'),\n        layers.BatchNormalization(),\n        layers.Conv2D(128, (3, 3), activation='relu', padding='same'),\n        layers.BatchNormalization(),\n        layers.MaxPooling2D((2, 2)),\n        \n        layers.Flatten(),\n        layers.Dropout(0.5),  # Add dropout for regularization\n        layers.Dense(256, activation='relu'),\n        layers.BatchNormalization(),\n        layers.Dropout(0.5),  # Add dropout for regularization\n        layers.Dense(num_classes, activation='softmax')\n    ])\n\n\n\n\n\n\n\n# Compile the model\nmodel.compile(optimizer='adam',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T19:22:16.025205Z","iopub.execute_input":"2023-09-19T19:22:16.025587Z","iopub.status.idle":"2023-09-19T19:22:37.444649Z","shell.execute_reply.started":"2023-09-19T19:22:16.025557Z","shell.execute_reply":"2023-09-19T19:22:37.443422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\nhistory = model.fit(x_train, y_train, epochs=20, batch_size=128, validation_split=0.2)\n\n# To evaluate on validation data you can use:\nvalidation_loss, validation_accuracy = model.evaluate(x_train[int(0.8*len(x_train)):], y_train[int(0.8*len(y_train)):])\nprint(f\"Validation Accuracy: {validation_accuracy * 100:.2f}%\")\n\nAccuracy = str(validation_accuracy*100)[:2]\nprint(\"Accuracy\",Accuracy)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T19:22:37.445936Z","iopub.execute_input":"2023-09-19T19:22:37.446277Z","iopub.status.idle":"2023-09-19T19:24:44.739392Z","shell.execute_reply.started":"2023-09-19T19:22:37.446247Z","shell.execute_reply":"2023-09-19T19:24:44.738083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the model \nmodel.save(f\"cifar10_model_0.{Accuracy}.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-09-19T19:24:44.740748Z","iopub.execute_input":"2023-09-19T19:24:44.741222Z","iopub.status.idle":"2023-09-19T19:24:45.102028Z","shell.execute_reply.started":"2023-09-19T19:24:44.741188Z","shell.execute_reply":"2023-09-19T19:24:45.100774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the model\n\n\nloaded_model = load_model(f\"cifar10_model_0.{Accuracy}.h5\")\n\nTEST_DIR = \"/kaggle/working/cifar-10/test\"\n\n# Load the test data\nTest_Data, _ = load_images_from_directory(TEST_DIR, label_provided=False)\nprint('Shape of Test_Data',Test_Data.shape)\n\n# Normalize the test data\ntest_images = Test_Data.astype('float32') / 255.0\n\n# Predict using the test images\npredictions = loaded_model.predict(test_images)\npredicted_labels = np.argmax(predictions, axis=1)\n\nclass_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']\npredicted_labels_str = [class_names[label] for label in predicted_labels]\n\n# Extract IDs from filenames\nids = [int(filename.split('.')[0]) for filename in sorted(os.listdir(TEST_DIR), key=lambda x: int(x.split('.')[0]))]\n\n# Generate submission DataFrame\nsubmission = pd.DataFrame({\n    'id': ids,\n    'label': predicted_labels_str\n})\n\n# Save submission to CSV\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-09-19T19:24:45.103180Z","iopub.execute_input":"2023-09-19T19:24:45.103457Z","iopub.status.idle":"2023-09-19T19:27:43.052035Z","shell.execute_reply.started":"2023-09-19T19:24:45.103432Z","shell.execute_reply":"2023-09-19T19:27:43.050778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n# Plot training & validation accuracy and loss\nplt.figure(figsize=(12, 4))\nplt.subplot(1, 2, 1)\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('Model accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend(['Train', 'Validation'], loc='upper left')\n\nplt.subplot(1, 2, 2)\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('Model loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend(['Train', 'Validation'], loc='upper left')\n\nplt.tight_layout()\nplt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T19:27:43.053404Z","iopub.execute_input":"2023-09-19T19:27:43.053741Z","iopub.status.idle":"2023-09-19T19:27:45.696277Z","shell.execute_reply.started":"2023-09-19T19:27:43.053711Z","shell.execute_reply":"2023-09-19T19:27:45.694990Z"},"trusted":true},"execution_count":null,"outputs":[]}]}