{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30747,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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)\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":"2024-07-27T05:57:06.831467Z","iopub.execute_input":"2024-07-27T05:57:06.831815Z","iopub.status.idle":"2024-07-27T05:57:07.216331Z","shell.execute_reply.started":"2024-07-27T05:57:06.831784Z","shell.execute_reply":"2024-07-27T05:57:07.215347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:57:07.303912Z","iopub.execute_input":"2024-07-27T05:57:07.304367Z","iopub.status.idle":"2024-07-27T05:57:19.618059Z","shell.execute_reply.started":"2024-07-27T05:57:07.304338Z","shell.execute_reply":"2024-07-27T05:57:19.616894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import py7zr","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:57:19.620528Z","iopub.execute_input":"2024-07-27T05:57:19.620866Z","iopub.status.idle":"2024-07-27T05:57:19.776209Z","shell.execute_reply.started":"2024-07-27T05:57:19.620832Z","shell.execute_reply":"2024-07-27T05:57:19.775222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_folder = py7zr.SevenZipFile(\"/kaggle/input/cifar-10/train.7z\",mode=\"r\")\nimage_folder.extractall()\nimage_folder.close()","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:57:19.782614Z","iopub.execute_input":"2024-07-27T05:57:19.783274Z","iopub.status.idle":"2024-07-27T05:57:46.285986Z","shell.execute_reply.started":"2024-07-27T05:57:19.783236Z","shell.execute_reply":"2024-07-27T05:57:46.284897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:57:46.287344Z","iopub.execute_input":"2024-07-27T05:57:46.287726Z","iopub.status.idle":"2024-07-27T05:57:46.735230Z","shell.execute_reply.started":"2024-07-27T05:57:46.287685Z","shell.execute_reply":"2024-07-27T05:57:46.734334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_files = os.listdir(\"train\")","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:57:46.736331Z","iopub.execute_input":"2024-07-27T05:57:46.736592Z","iopub.status.idle":"2024-07-27T05:57:46.768690Z","shell.execute_reply.started":"2024-07-27T05:57:46.736568Z","shell.execute_reply":"2024-07-27T05:57:46.767960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(image_files)","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:57:46.769741Z","iopub.execute_input":"2024-07-27T05:57:46.770017Z","iopub.status.idle":"2024-07-27T05:57:46.778905Z","shell.execute_reply.started":"2024-07-27T05:57:46.769992Z","shell.execute_reply":"2024-07-27T05:57:46.778009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_lables = pd.read_csv(\"/kaggle/input/cifar-10/trainLabels.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:57:46.779980Z","iopub.execute_input":"2024-07-27T05:57:46.780696Z","iopub.status.idle":"2024-07-27T05:57:46.813663Z","shell.execute_reply.started":"2024-07-27T05:57:46.780657Z","shell.execute_reply":"2024-07-27T05:57:46.812578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_lables.head(2)","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:57:46.814933Z","iopub.execute_input":"2024-07-27T05:57:46.815251Z","iopub.status.idle":"2024-07-27T05:57:46.827589Z","shell.execute_reply.started":"2024-07-27T05:57:46.815225Z","shell.execute_reply":"2024-07-27T05:57:46.826448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_lables['label'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:57:46.833598Z","iopub.execute_input":"2024-07-27T05:57:46.834038Z","iopub.status.idle":"2024-07-27T05:57:46.844391Z","shell.execute_reply.started":"2024-07-27T05:57:46.834003Z","shell.execute_reply":"2024-07-27T05:57:46.843170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dict = {\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}","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:57:46.845451Z","iopub.execute_input":"2024-07-27T05:57:46.845728Z","iopub.status.idle":"2024-07-27T05:57:46.851123Z","shell.execute_reply.started":"2024-07-27T05:57:46.845704Z","shell.execute_reply":"2024-07-27T05:57:46.850166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = [labels_dict[i] for i in train_lables['label']]","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:57:46.852259Z","iopub.execute_input":"2024-07-27T05:57:46.852564Z","iopub.status.idle":"2024-07-27T05:57:46.871120Z","shell.execute_reply.started":"2024-07-27T05:57:46.852539Z","shell.execute_reply":"2024-07-27T05:57:46.870167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_list = train_lables['id']","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:57:46.872296Z","iopub.execute_input":"2024-07-27T05:57:46.872560Z","iopub.status.idle":"2024-07-27T05:57:46.879833Z","shell.execute_reply.started":"2024-07-27T05:57:46.872538Z","shell.execute_reply":"2024-07-27T05:57:46.878878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Image Processing","metadata":{}},{"cell_type":"code","source":"# Converting the Images into Numpy Arrays\n\ntrain_data_dir = 'train/'\ndata = []\nfor id in id_list:\n    image = Image.open(train_data_dir+str(id)+'.png')\n    image = np.array(image)\n    data.append(image)\n    ","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:57:46.880957Z","iopub.execute_input":"2024-07-27T05:57:46.881529Z","iopub.status.idle":"2024-07-27T05:57:55.186763Z","shell.execute_reply.started":"2024-07-27T05:57:46.881494Z","shell.execute_reply":"2024-07-27T05:57:55.185910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(data)","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:57:55.187982Z","iopub.execute_input":"2024-07-27T05:57:55.188301Z","iopub.status.idle":"2024-07-27T05:57:55.194498Z","shell.execute_reply.started":"2024-07-27T05:57:55.188274Z","shell.execute_reply":"2024-07-27T05:57:55.193573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data[0].shape","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:57:55.195629Z","iopub.execute_input":"2024-07-27T05:57:55.195872Z","iopub.status.idle":"2024-07-27T05:57:55.205448Z","shell.execute_reply.started":"2024-07-27T05:57:55.195850Z","shell.execute_reply":"2024-07-27T05:57:55.204487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Converting both images_list and labels_list to numpy arrays\n\nX = np.array(data)\ny = np.array(labels)","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:57:55.206950Z","iopub.execute_input":"2024-07-27T05:57:55.207351Z","iopub.status.idle":"2024-07-27T05:57:55.303244Z","shell.execute_reply.started":"2024-07-27T05:57:55.207316Z","shell.execute_reply":"2024-07-27T05:57:55.302239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train Test Split","metadata":{}},{"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":{"execution":{"iopub.status.busy":"2024-07-27T05:57:55.304369Z","iopub.execute_input":"2024-07-27T05:57:55.304656Z","iopub.status.idle":"2024-07-27T05:57:55.365327Z","shell.execute_reply.started":"2024-07-27T05:57:55.304630Z","shell.execute_reply":"2024-07-27T05:57:55.364289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Scaling the Images Data\n\nX_train_scaled = X_train/255\nX_test_scaled = X_test/255\n","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:57:55.366929Z","iopub.execute_input":"2024-07-27T05:57:55.367612Z","iopub.status.idle":"2024-07-27T05:57:55.744195Z","shell.execute_reply.started":"2024-07-27T05:57:55.367566Z","shell.execute_reply":"2024-07-27T05:57:55.743117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Nerual Network Build","metadata":{}},{"cell_type":"code","source":"# import tensorflow as tf\n# from tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:57:55.745387Z","iopub.execute_input":"2024-07-27T05:57:55.745685Z","iopub.status.idle":"2024-07-27T05:57:55.750470Z","shell.execute_reply.started":"2024-07-27T05:57:55.745659Z","shell.execute_reply":"2024-07-27T05:57:55.749582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# label_calsses = 10\n\n# model = keras.Sequential([\n#     keras.layers.Flatten(input_shape=(32,32,3)),\n#     keras.layers.Dense(64,activation='relu'),\n#     keras.layers.Dense(label_calsses,activation='softmax')\n# ])","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:57:55.751815Z","iopub.execute_input":"2024-07-27T05:57:55.752462Z","iopub.status.idle":"2024-07-27T05:57:55.760624Z","shell.execute_reply.started":"2024-07-27T05:57:55.752427Z","shell.execute_reply":"2024-07-27T05:57:55.759753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Compile the Neural Network\n\n# model.compile(optimizer='adam',\n#              loss='sparse_categorical_crossentropy',\n#              metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:57:55.761975Z","iopub.execute_input":"2024-07-27T05:57:55.762264Z","iopub.status.idle":"2024-07-27T05:57:55.771278Z","shell.execute_reply.started":"2024-07-27T05:57:55.762240Z","shell.execute_reply":"2024-07-27T05:57:55.770380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Model Training\n\n# model.fit(X_train_scaled,Y_train,validation_split=0.2,epochs=10)","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:57:55.772273Z","iopub.execute_input":"2024-07-27T05:57:55.772536Z","iopub.status.idle":"2024-07-27T05:57:55.781070Z","shell.execute_reply.started":"2024-07-27T05:57:55.772513Z","shell.execute_reply":"2024-07-27T05:57:55.780147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-07-27T05:57:55.782373Z","iopub.execute_input":"2024-07-27T05:57:55.782698Z","iopub.status.idle":"2024-07-27T05:57:59.065691Z","shell.execute_reply.started":"2024-07-27T05:57:55.782666Z","shell.execute_reply":"2024-07-27T05:57:59.064865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base = ResNet50(weights='imagenet',include_top=False,input_shape=(256,256,3))\nconv_base.summary()","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:57:59.066745Z","iopub.execute_input":"2024-07-27T05:57:59.067299Z","iopub.status.idle":"2024-07-27T05:58:01.256145Z","shell.execute_reply.started":"2024-07-27T05:57:59.067271Z","shell.execute_reply":"2024-07-27T05:58:01.255289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_calsses = 10\nmodel = models.Sequential()\nmodel.add(layers.UpSampling2D((2,2)))\nmodel.add(layers.UpSampling2D((2,2)))\nmodel.add(layers.UpSampling2D((2,2)))\nmodel.add(conv_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(64,activation='relu'))\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dense(label_calsses,activation='relu'))","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:58:01.257777Z","iopub.execute_input":"2024-07-27T05:58:01.258130Z","iopub.status.idle":"2024-07-27T05:58:01.286227Z","shell.execute_reply.started":"2024-07-27T05:58:01.258084Z","shell.execute_reply":"2024-07-27T05:58:01.284977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=optimizers.RMSprop(learning_rate=2e-5),loss='sparse_categorical_crossentropy',metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:58:01.287504Z","iopub.execute_input":"2024-07-27T05:58:01.287854Z","iopub.status.idle":"2024-07-27T05:58:01.306140Z","shell.execute_reply.started":"2024-07-27T05:58:01.287827Z","shell.execute_reply":"2024-07-27T05:58:01.304964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train_scaled,Y_train,validation_split=0.2,epochs=25)","metadata":{"execution":{"iopub.status.busy":"2024-07-27T05:58:01.311501Z","iopub.execute_input":"2024-07-27T05:58:01.311812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss,accuracy = model.evaluate(X_test_scaled,Y_test)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"h=history\n\n#plotting the loss value\n\nplt.plot(h.history['loss'],label='train loss')\nplt.plot(h.history['val_loss'],label='validation loss')\nplt.legend()\nplt.show()\n\n#plotting the Accuracy Value\n\nplt.plot(h.history['acc'],label='train Accuracy')\nplt.plot(h.history['val_acc'],label='validation Accuracy')\nplt.legend()\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predictions Time","metadata":{}},{"cell_type":"code","source":"image_folder = py7zr.SevenZipFile(\"/kaggle/input/cifar-10/test.7z\",mode=\"r\")\nimage_folder.extractall()\nimage_folder.close()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loading test images\ntest_images = os.listdir(\"test/\")\ntest_data = []\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_images)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for image_file in test_images:\n    image = Image.open(os.path.join(\"test\", image_file))\n#     image = image.resize((256, 256))  # Resize if needed\n    image = np.array(image)\n    test_data.append(image)\n\n# Convert test images to numpy array and scale\nX_test_final = np.array(test_data) / 255","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(X_test_final)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_labels = np.argmax(predictions, axis=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Submission ","metadata":{}},{"cell_type":"code","source":"submission_df = pd.DataFrame({\n    'id': [file.split('.')[0] for file in test_images],  # Assuming IDs are in file names\n    'label': predicted_labels\n})\n\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}