{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.12.13"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"isGpuEnabled":false,"isInternetEnabled":false,"language":"python","sourceType":"notebook"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.execute_input":"2026-06-19T18:35:55.673899Z","iopub.status.busy":"2026-06-19T18:35:55.673239Z","iopub.status.idle":"2026-06-19T18:35:59.829705Z","shell.execute_reply":"2026-06-19T18:35:59.828657Z","shell.execute_reply.started":"2026-06-19T18:35:55.673863Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import py7zr\nfrom pathlib import Path\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport PIL.Image as Image\nimport tensorflow.keras as tf\nfrom tensorflow.keras.layers import Dense, BatchNormalization, Dropout\nfrom tensorflow.python.client import device_lib\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nimport shutil","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2026-06-19T18:35:59.831893Z","iopub.status.busy":"2026-06-19T18:35:59.831540Z","iopub.status.idle":"2026-06-19T18:36:09.701417Z","shell.execute_reply":"2026-06-19T18:36:09.700726Z","shell.execute_reply.started":"2026-06-19T18:35:59.831859Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Detecting GPU\ndevices = device_lib.list_local_devices()\nprint(\"GPU details:\")\nfor d in devices:\n    if d.device_type == \"GPU\":\n        print(d.physical_device_desc)","metadata":{"execution":{"iopub.execute_input":"2026-06-19T18:36:09.702707Z","iopub.status.busy":"2026-06-19T18:36:09.702253Z","iopub.status.idle":"2026-06-19T18:36:10.669220Z","shell.execute_reply":"2026-06-19T18:36:10.668350Z","shell.execute_reply.started":"2026-06-19T18:36:09.702682Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"base_dir = Path(\"/kaggle/input/competitions/cifar-10/\")\n\n# Extracting train folder\nwith py7zr.SevenZipFile(base_dir/\"train.7z\") as archive:\n    archive.extractall(path = \"extracted_train\")\n\n# Extracting test folder\nwith py7zr.SevenZipFile(base_dir/\"test.7z\") as archive:\n    archive.extractall(path = \"extracted_test\")","metadata":{"execution":{"iopub.execute_input":"2026-06-19T18:36:10.671201Z","iopub.status.busy":"2026-06-19T18:36:10.670889Z","iopub.status.idle":"2026-06-19T18:36:33.872624Z","shell.execute_reply":"2026-06-19T18:36:33.871222Z","shell.execute_reply.started":"2026-06-19T18:36:10.671176Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Reading labels of train data\ntrain_data = pd.read_csv(base_dir/\"trainLabels.csv\")\nprint(train_data.shape)\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2026-06-19T18:36:33.873654Z","iopub.status.idle":"2026-06-19T18:36:33.873986Z","shell.execute_reply":"2026-06-19T18:36:33.873867Z","shell.execute_reply.started":"2026-06-19T18:36:33.873849Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_paths = Path(\"/kaggle/working\")\n\n# y label\nle = LabelEncoder()\ny = le.fit_transform(train_data.label)\nnum_classes = len(le.classes_)\n\n# x label\nx = []\nfor i in range(1, train_data.shape[0] + 1):\n    img = Image.open(image_paths/f\"extracted_train/train/{i}.png\").resize((96, 96))\n    img = np.array(img)\n    x.append(img)\n\nx = np.array(x)\nx = tf.applications.mobilenet_v2.preprocess_input(x * 255)\nx.shape","metadata":{"execution":{"iopub.status.busy":"2026-06-19T18:36:33.874909Z","iopub.status.idle":"2026-06-19T18:36:33.875937Z","shell.execute_reply":"2026-06-19T18:36:33.875758Z","shell.execute_reply.started":"2026-06-19T18:36:33.875739Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train Test split\nx_train, x_valid, y_train, y_valid = train_test_split(x, y, train_size = 0.9, stratify = y, random_state = 42)","metadata":{"execution":{"iopub.status.busy":"2026-06-19T18:36:33.876818Z","iopub.status.idle":"2026-06-19T18:36:33.877190Z","shell.execute_reply":"2026-06-19T18:36:33.877025Z","shell.execute_reply.started":"2026-06-19T18:36:33.876998Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Loading a pre-trained model\nfeature_extractor = tf.applications.MobileNetV2(\n    input_shape = (96, 96, 3),\n    include_top = False,\n    weights = \"imagenet\",\n    pooling = \"avg\"\n)\n\nfeature_extractor.trainable = False\n\nmodel = tf.Sequential([\n    feature_extractor,\n    BatchNormalization(),\n    Dense(256, activation = \"relu\"),\n    Dropout(0.1),\n    Dense(num_classes, activation = \"softmax\", name = \"Output\")\n])","metadata":{"execution":{"iopub.status.busy":"2026-06-19T18:36:33.880151Z","iopub.status.idle":"2026-06-19T18:36:33.880990Z","shell.execute_reply":"2026-06-19T18:36:33.880799Z","shell.execute_reply.started":"2026-06-19T18:36:33.880757Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer = \"adam\", loss = \"sparse_categorical_crossentropy\", metrics = [\"accuracy\"])\nhistory = model.fit(x, y, epochs = 5, batch_size = 64, validation_data = (x_valid, y_valid))","metadata":{"execution":{"iopub.status.busy":"2026-06-19T18:36:33.882134Z","iopub.status.idle":"2026-06-19T18:36:33.882544Z","shell.execute_reply":"2026-06-19T18:36:33.882430Z","shell.execute_reply.started":"2026-06-19T18:36:33.882413Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize = (20, 10))\nplt.rcParams.update({\"font.size\": 20})\nplt.title(\"Loss Curve\", fontsize = 30)\nplt.xlabel(\"Epochs\", fontsize = 25)\nplt.ylabel(\"Loss\", fontsize = 25)\nplt.plot(history.history[\"loss\"], color = \"royalblue\", linewidth = 3, label = \"Loss\")\nplt.plot(history.history[\"val_loss\"], color = \"darkorange\", linewidth = 3, label = \"Validation Loss\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2026-06-19T18:36:33.883825Z","iopub.status.idle":"2026-06-19T18:36:33.884113Z","shell.execute_reply":"2026-06-19T18:36:33.883967Z","shell.execute_reply.started":"2026-06-19T18:36:33.883953Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"shutil.rmtree('/kaggle/working/extracted_train')\ndel x_train, x_valid, y_train, y_valid\n\ntotal_images = 300000\nbatch_size = 500\nresults = []\n\nfor start in range(1, total_images + 1, batch_size):\n    end = min(start + batch_size, total_images + 1)\n    batch_img = []\n    batch_ids = []\n    for j in range(start, end):\n        img = Image.open(image_paths/f\"extracted_test/test/{j}.png\").resize((96, 96))\n        img = np.array(img)\n        batch_img.append(img)\n        batch_ids.append(j)\n\n    batch_arr = np.array(batch_img).astype('float32')\n    batch_arr = tf.applications.mobilenet_v2.preprocess_input(batch_arr)\n    pred = np.argmax(model.predict(batch_arr, verbose=0), axis=1)\n    y_pred = le.inverse_transform(pred)\n\n    results.extend(zip(batch_ids, y_pred))\n    del batch_ids, batch_img, batch_arr, pred, y_pred\n\nshutil.rmtree('/kaggle/working/extracted_test')\nsubmission = pd.DataFrame(results, columns=[\"id\", \"label\"])\nsubmission.to_csv(\"submission_v_1.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2026-06-19T18:36:33.885565Z","iopub.status.idle":"2026-06-19T18:36:33.886152Z","shell.execute_reply":"2026-06-19T18:36:33.885966Z","shell.execute_reply.started":"2026-06-19T18:36:33.885942Z"},"trusted":true},"outputs":[],"execution_count":null}]}