{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"colab":{"provenance":[],"gpuType":"T4"},"accelerator":"GPU","kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":31154,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip  install kaggle\n!pip install py7zr","metadata":{"id":"NSKK7cGTnAVb","outputId":"916017ad-89e2-42ba-8b8a-f77693049ddf","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:24:11.571162Z","iopub.execute_input":"2025-10-13T04:24:11.571418Z","iopub.status.idle":"2025-10-13T04:24:17.879653Z","shell.execute_reply.started":"2025-10-13T04:24:11.5714Z","shell.execute_reply":"2025-10-13T04:24:17.878922Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom sklearn.model_selection import train_test_split\n","metadata":{"id":"kgz_bHUknNUi","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:34:49.56555Z","iopub.execute_input":"2025-10-13T04:34:49.56612Z","iopub.status.idle":"2025-10-13T04:34:49.569819Z","shell.execute_reply.started":"2025-10-13T04:34:49.566096Z","shell.execute_reply":"2025-10-13T04:34:49.569098Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import py7zr\n\narchive = py7zr.SevenZipFile('/kaggle/input/cifar-10/train.7z', mode='r')\narchive.extractall()\narchive.close()","metadata":{"id":"KgU0DKVynPbN","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:25:03.619774Z","iopub.execute_input":"2025-10-13T04:25:03.620012Z","iopub.status.idle":"2025-10-13T04:25:23.724478Z","shell.execute_reply.started":"2025-10-13T04:25:03.619997Z","shell.execute_reply":"2025-10-13T04:25:23.723663Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import py7zr\n\narchive = py7zr.SevenZipFile('/kaggle/input/cifar-10/test.7z', mode='r')\narchive.extractall()\narchive.close()","metadata":{"id":"g4jIy-LA9DiP","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:25:47.945387Z","iopub.execute_input":"2025-10-13T04:25:47.945892Z","iopub.status.idle":"2025-10-13T04:27:47.092223Z","shell.execute_reply.started":"2025-10-13T04:25:47.945857Z","shell.execute_reply":"2025-10-13T04:27:47.091292Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"filenames = os.listdir('/kaggle/working/train')","metadata":{"id":"ClghNzWenRP1","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:32:59.728134Z","iopub.execute_input":"2025-10-13T04:32:59.728858Z","iopub.status.idle":"2025-10-13T04:32:59.755628Z","shell.execute_reply.started":"2025-10-13T04:32:59.728826Z","shell.execute_reply":"2025-10-13T04:32:59.754866Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels_df = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv')","metadata":{"id":"__jqeRqknVWU","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:33:00.509175Z","iopub.execute_input":"2025-10-13T04:33:00.509438Z","iopub.status.idle":"2025-10-13T04:33:00.523916Z","shell.execute_reply.started":"2025-10-13T04:33:00.50942Z","shell.execute_reply":"2025-10-13T04:33:00.523389Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels_dictionary = {'airplane':0, 'automobile':1, 'bird':2, 'cat':3, 'deer':4, 'dog':5, 'frog':6, 'horse':7, 'ship':8, 'truck':9}\n\nlabels = [labels_dictionary[i] for i in labels_df['label']]","metadata":{"id":"t2tSVxL1nXav","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:33:02.558966Z","iopub.execute_input":"2025-10-13T04:33:02.559283Z","iopub.status.idle":"2025-10-13T04:33:02.569893Z","shell.execute_reply.started":"2025-10-13T04:33:02.55926Z","shell.execute_reply":"2025-10-13T04:33:02.569018Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\nfrom google.colab.patches import cv2_imshow\n\nimg = cv2.imread('/kaggle/working/train/2.png')\ncv2_imshow(img)","metadata":{"id":"tO5RC6xInZwb","outputId":"ccbd98c8-3c17-4b7b-fffd-eff2ccaa67e6","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:33:22.735017Z","iopub.execute_input":"2025-10-13T04:33:22.735709Z","iopub.status.idle":"2025-10-13T04:33:22.793752Z","shell.execute_reply.started":"2025-10-13T04:33:22.735686Z","shell.execute_reply":"2025-10-13T04:33:22.793108Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"id_list = list(labels_df['id'])\nid_list","metadata":{"id":"NRYd19LZndob","outputId":"fdbe691a-4510-46eb-82ca-4d53587bf84c","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:33:25.454524Z","iopub.execute_input":"2025-10-13T04:33:25.454811Z","iopub.status.idle":"2025-10-13T04:33:25.470378Z","shell.execute_reply.started":"2025-10-13T04:33:25.454777Z","shell.execute_reply":"2025-10-13T04:33:25.469581Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data_folder = '/kaggle/working/train/'\n\ndata = []\n\nfor id in id_list:\n  image = Image.open(train_data_folder +str(id)+ '.png')\n\n  image = np.array(image)\n  data.append(image)","metadata":{"id":"TTUKlOHsngBG","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:35:23.076875Z","iopub.execute_input":"2025-10-13T04:35:23.077199Z","iopub.status.idle":"2025-10-13T04:35:29.998452Z","shell.execute_reply.started":"2025-10-13T04:35:23.07717Z","shell.execute_reply":"2025-10-13T04:35:29.997835Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = np.array(data)\nY = np.array(labels)","metadata":{"id":"Pxpz-6LvnhoW","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:35:33.276275Z","iopub.execute_input":"2025-10-13T04:35:33.276535Z","iopub.status.idle":"2025-10-13T04:35:33.359219Z","shell.execute_reply.started":"2025-10-13T04:35:33.276517Z","shell.execute_reply":"2025-10-13T04:35:33.358616Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_test, Y_train, Y_test = train_test_split(X,Y, test_size=0.2, random_state=2)","metadata":{"id":"Lb8qDQsvnjh1","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:35:35.485376Z","iopub.execute_input":"2025-10-13T04:35:35.485636Z","iopub.status.idle":"2025-10-13T04:35:35.548488Z","shell.execute_reply.started":"2025-10-13T04:35:35.485616Z","shell.execute_reply":"2025-10-13T04:35:35.547887Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_scaled = X_train/255\n\nX_test_scaled = X_test/255","metadata":{"id":"FkS8XlecnlhZ","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:35:37.51149Z","iopub.execute_input":"2025-10-13T04:35:37.512263Z","iopub.status.idle":"2025-10-13T04:35:37.898652Z","shell.execute_reply.started":"2025-10-13T04:35:37.512228Z","shell.execute_reply":"2025-10-13T04:35:37.897869Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\n","metadata":{"id":"rfvWYjcRnnOe","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:35:39.550712Z","iopub.execute_input":"2025-10-13T04:35:39.551208Z","iopub.status.idle":"2025-10-13T04:35:51.131752Z","shell.execute_reply.started":"2025-10-13T04:35:39.551184Z","shell.execute_reply":"2025-10-13T04:35:51.131161Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_of_classes = 10\n\nmodel = keras.Sequential([\n\n    keras.layers.Flatten(input_shape=(32,32,3)),\n    keras.layers.Dense(64, activation='relu'),\n    keras.layers.Dense(num_of_classes, activation='softmax')\n])","metadata":{"id":"9BBFH3mSnrRm","outputId":"95432378-e3fe-4ddf-db74-ad1b39d321d6","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:35:51.132756Z","iopub.execute_input":"2025-10-13T04:35:51.133266Z","iopub.status.idle":"2025-10-13T04:35:53.117663Z","shell.execute_reply.started":"2025-10-13T04:35:51.133247Z","shell.execute_reply":"2025-10-13T04:35:53.117075Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['acc'])","metadata":{"id":"cTTLYoC3nupo","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:35:58.382259Z","iopub.execute_input":"2025-10-13T04:35:58.382774Z","iopub.status.idle":"2025-10-13T04:35:58.396451Z","shell.execute_reply.started":"2025-10-13T04:35:58.382753Z","shell.execute_reply":"2025-10-13T04:35:58.395651Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.fit(X_train_scaled, Y_train, validation_split=0.1, epochs=5)","metadata":{"id":"iTV28qE1nw6S","outputId":"5aba8133-09c9-4b8f-ffd9-cf52a9e65220","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:36:04.355566Z","iopub.execute_input":"2025-10-13T04:36:04.355843Z","iopub.status.idle":"2025-10-13T04:36:21.41097Z","shell.execute_reply.started":"2025-10-13T04:36:04.355823Z","shell.execute_reply":"2025-10-13T04:36:21.410341Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras import Sequential, models, layers\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten\nfrom tensorflow.keras.layers import BatchNormalization\nfrom tensorflow.keras.models import load_model\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.applications.resnet50 import ResNet50\nfrom tensorflow.keras import optimizers","metadata":{"id":"psbQFOu5nzlV","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:36:23.421913Z","iopub.execute_input":"2025-10-13T04:36:23.422505Z","iopub.status.idle":"2025-10-13T04:36:23.43393Z","shell.execute_reply.started":"2025-10-13T04:36:23.422481Z","shell.execute_reply":"2025-10-13T04:36:23.433192Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"convolutional_base = ResNet50(weights='imagenet', include_top=False, input_shape=(256,256,3))\nconvolutional_base.summary()","metadata":{"id":"VTWrx9y1n2CT","outputId":"92036414-5e6e-4ce5-d78e-fb140d844cda","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:36:25.919452Z","iopub.execute_input":"2025-10-13T04:36:25.919909Z","iopub.status.idle":"2025-10-13T04:36:27.771934Z","shell.execute_reply.started":"2025-10-13T04:36:25.91989Z","shell.execute_reply":"2025-10-13T04:36:27.771293Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(layers.UpSampling2D((2,2)))\nmodel.add(layers.UpSampling2D((2,2)))\nmodel.add(layers.UpSampling2D((2,2)))\nmodel.add(convolutional_base)\nmodel.add(layers.Flatten())\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dense(128,activation='relu'))\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dense(128,activation='relu'))\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dense(num_of_classes, activation = 'softmax'))","metadata":{"id":"yPnbOS1in4st","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:36:37.974953Z","iopub.execute_input":"2025-10-13T04:36:37.97531Z","iopub.status.idle":"2025-10-13T04:36:37.992809Z","shell.execute_reply.started":"2025-10-13T04:36:37.975287Z","shell.execute_reply":"2025-10-13T04:36:37.992002Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer=optimizers.RMSprop(learning_rate=2e-5), loss='sparse_categorical_crossentropy', metrics=['accuracy'])","metadata":{"id":"FTF9uStwn6na","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:36:41.063923Z","iopub.execute_input":"2025-10-13T04:36:41.064242Z","iopub.status.idle":"2025-10-13T04:36:41.075299Z","shell.execute_reply.started":"2025-10-13T04:36:41.06422Z","shell.execute_reply":"2025-10-13T04:36:41.074646Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(X_train_scaled, Y_train, validation_split=0.1, epochs=5)","metadata":{"id":"QUJxB5Xan86M","outputId":"5d55183a-0051-4fb2-8b1b-10a86a1797d0","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:36:47.443684Z","iopub.execute_input":"2025-10-13T04:36:47.443979Z","iopub.status.idle":"2025-10-13T05:10:38.441008Z","shell.execute_reply.started":"2025-10-13T04:36:47.443958Z","shell.execute_reply":"2025-10-13T05:10:38.440392Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"h = history\nplt.plot(h.history['loss'], label='train loss')\nplt.plot(h.history['val_loss'], label='val loss')\nplt.legend()\nplt.show()","metadata":{"id":"0yLqldjQn-wl","outputId":"cd70ac64-2d6e-4dce-e23c-6b48bcd5a2a5","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T05:10:46.484799Z","iopub.execute_input":"2025-10-13T05:10:46.485098Z","iopub.status.idle":"2025-10-13T05:10:46.716412Z","shell.execute_reply.started":"2025-10-13T05:10:46.485077Z","shell.execute_reply":"2025-10-13T05:10:46.715626Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv('/content/sampleSubmission.csv')","metadata":{"id":"wpqqBlO54_7F","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:24:17.925665Z","iopub.status.idle":"2025-10-13T04:24:17.925933Z","shell.execute_reply.started":"2025-10-13T04:24:17.925809Z","shell.execute_reply":"2025-10-13T04:24:17.925822Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df","metadata":{"id":"WFscHURi5UxT","outputId":"a17a566e-44dc-4cb7-c48c-c7f2ca78baea","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T04:24:17.926808Z","iopub.status.idle":"2025-10-13T04:24:17.927022Z","shell.execute_reply.started":"2025-10-13T04:24:17.92692Z","shell.execute_reply":"2025-10-13T04:24:17.926929Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\n\n\ntest_dir = \"test/\"\n\n\nlabels_dictionary = {\n    'airplane': 0, 'automobile': 1, 'bird': 2, 'cat': 3, 'deer': 4,\n    'dog': 5, 'frog': 6, 'horse': 7, 'ship': 8, 'truck': 9\n}\nreverse_labels = {v: k for k, v in labels_dictionary.items()}\n\n\nIMG_SIZE = (32, 32)\n\ndef preprocess_image(img_path):\n    img = load_img(img_path, target_size=IMG_SIZE)\n    img = img_to_array(img)\n    img = img / 255.0\n    return img\n\n\ntest_images = sorted(os.listdir(test_dir), key=lambda x: int(os.path.splitext(x)[0]))\nX_test_submit = np.array([preprocess_image(os.path.join(test_dir, fname)) for fname in test_images])\n\nprint(f\"Loaded {len(X_test_submit)} test images.\")\n\n\npred_prob = model.predict(X_test_submit, verbose=1)\npred_idx = pred_prob.argmax(axis=1)\n\n\npred_labels = [reverse_labels[i] for i in pred_idx]\n\n\nsubmission = pd.DataFrame({\n    \"id\": [int(os.path.splitext(fname)[0]) for fname in test_images],\n    \"label\": pred_labels\n})\n\n\nsubmission.to_csv(\"submission.csv\", index=False)\nprint(\"✅ submission1.csv created successfully!\")","metadata":{"id":"IxRYPxUF5oeN","trusted":true,"execution":{"iopub.status.busy":"2025-10-13T05:36:05.72597Z","iopub.execute_input":"2025-10-13T05:36:05.72657Z","iopub.status.idle":"2025-10-13T05:52:27.803498Z","shell.execute_reply.started":"2025-10-13T05:36:05.726548Z","shell.execute_reply":"2025-10-13T05:52:27.802697Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"id":"StSNCn9b6rBv","trusted":true},"outputs":[],"execution_count":null}]}