{"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)\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-08-27T05:13:11.983846Z","iopub.execute_input":"2023-08-27T05:13:11.984133Z","iopub.status.idle":"2023-08-27T05:13:12.000477Z","shell.execute_reply.started":"2023-08-27T05:13:11.984107Z","shell.execute_reply":"2023-08-27T05:13:11.999536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.datasets import cifar10\nfrom keras.models import load_model\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras import models,layers\nfrom tensorflow.keras.layers import Dense,Dropout,BatchNormalization,Conv2D,MaxPool2D,Flatten\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.optimizers.experimental import SGD,Adam\n\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-08-27T05:13:15.204588Z","iopub.execute_input":"2023-08-27T05:13:15.205043Z","iopub.status.idle":"2023-08-27T05:13:30.602615Z","shell.execute_reply.started":"2023-08-27T05:13:15.205005Z","shell.execute_reply":"2023-08-27T05:13:30.601617Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# function to change the data type and normalize the data\ndef pre_process(data):\n    #data = data.astype('float32')\n    return data/255\n\n# function to chnage the labels into categorical data\ndef to_cat(data):\n    return to_categorical(data,num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2023-08-27T05:15:07.420576Z","iopub.execute_input":"2023-08-27T05:15:07.421096Z","iopub.status.idle":"2023-08-27T05:15:07.432521Z","shell.execute_reply.started":"2023-08-27T05:15:07.421051Z","shell.execute_reply":"2023-08-27T05:15:07.431558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load data\n(X_train,y_train),(X_test,y_test) = cifar10.load_data()\n# preprocess\nX_train = pre_process(X_train)\nX_test = pre_process(X_test)\ny_train = to_cat(y_train)\ny_test = to_cat(y_test)\n\nin_shape = X_train[0].shape\n# Model \nmodel = models.Sequential()\n\n# 3 block vgg style model \n# Base of the model\n\n# block1\nmodel.add(Conv2D(32,3,activation='relu',padding='same',input_shape = in_shape))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(32,3,activation='relu',padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(2))\nmodel.add(Dropout(0.2))  \n\n# block 2\nmodel.add(Conv2D(64,3,activation='relu',padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(64,3,activation='relu',padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(2))\nmodel.add(Dropout(0.3))\n\n# block 3\nmodel.add(Conv2D(128,3,activation='relu',padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(128,3,activation='relu',padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(2))\nmodel.add(Dropout(0.4))\n\n# Head of the model\nmodel.add(Flatten())\nmodel.add(Dense(128,activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10,activation='softmax'))\n\n# compile\n#opt = SGD(learning_rate=0.01,momentum=0.9)\nmodel.compile(optimizer= 'adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# callback\nearly_stoping = tf.keras.callbacks.EarlyStopping(patience=20, restore_best_weights=True)\n\n# Data Augementation\ndatagen = ImageDataGenerator(rotation_range=10,\n                             horizontal_flip=True,\n                             width_shift_range=0.1,\n                             height_shift_range=0.1\n                                )\ntrain_gen = datagen.flow(X_train,y_train,batch_size=64)\n\n\n# Model fit\nmodel_history = model.fit(train_gen,\n                          validation_data = (X_test,y_test),\n                          epochs = 100,\n                          callbacks=[early_stoping],\n                          verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2023-08-27T05:15:57.638338Z","iopub.execute_input":"2023-08-27T05:15:57.638699Z","iopub.status.idle":"2023-08-27T06:14:20.017790Z","shell.execute_reply.started":"2023-08-27T05:15:57.638668Z","shell.execute_reply":"2023-08-27T06:14:20.016827Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluvate\nloss3,acc3 = model.evaluate(X_test,y_test,batch_size = 64, verbose = 0)\n\nprint('The accuracy of the Base Model is:',acc3*100)\nprint('The Loss of the Base Model is:',loss3)\n\n# plots for accuracy and Loss with epochs\n\nerror = pd.DataFrame(model_history.history)\n\nplt.figure(figsize=(18,5),dpi=200)\n\nplt.subplot(121)\nplt.title('Cross Entropy Loss',fontsize=15)\nplt.xlabel('Epochs',fontsize=12)\nplt.ylabel('Loss',fontsize=12)\nplt.plot(error['loss'])\nplt.plot(error['val_loss'])\n\nplt.subplot(122)\nplt.title('Classification Accuracy',fontsize=15)\nplt.xlabel('Epochs',fontsize=12)\nplt.ylabel('Accuracy',fontsize=12)\nplt.plot(error['accuracy'])\nplt.plot(error['val_accuracy'])\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-27T06:15:16.829616Z","iopub.execute_input":"2023-08-27T06:15:16.830004Z","iopub.status.idle":"2023-08-27T06:15:18.694497Z","shell.execute_reply.started":"2023-08-27T06:15:16.829972Z","shell.execute_reply":"2023-08-27T06:15:18.693024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr;","metadata":{"execution":{"iopub.status.busy":"2023-08-27T06:15:27.975765Z","iopub.execute_input":"2023-08-27T06:15:27.976191Z","iopub.status.idle":"2023-08-27T06:15:45.491753Z","shell.execute_reply.started":"2023-08-27T06:15:27.976140Z","shell.execute_reply":"2023-08-27T06:15:45.490596Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python -m py7zr x ../input/cifar-10/test.7z /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2023-08-27T06:15:56.780530Z","iopub.execute_input":"2023-08-27T06:15:56.780908Z","iopub.status.idle":"2023-08-27T06:35:47.110606Z","shell.execute_reply.started":"2023-08-27T06:15:56.780872Z","shell.execute_reply":"2023-08-27T06:35:47.109197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls ../working","metadata":{"execution":{"iopub.status.busy":"2023-08-27T06:36:41.885925Z","iopub.execute_input":"2023-08-27T06:36:41.886328Z","iopub.status.idle":"2023-08-27T06:36:42.960079Z","shell.execute_reply.started":"2023-08-27T06:36:41.886295Z","shell.execute_reply":"2023-08-27T06:36:42.958915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom PIL import Image\nimport os\n\n\nimage_directory = \"/kaggle/working/test\"\n\nimage_list = [Image.open(os.path.join(image_directory, img)) for img in os.listdir(image_directory)]","metadata":{"execution":{"iopub.status.busy":"2023-08-27T06:36:48.483201Z","iopub.execute_input":"2023-08-27T06:36:48.485166Z","iopub.status.idle":"2023-08-27T06:37:23.170564Z","shell.execute_reply.started":"2023-08-27T06:36:48.485114Z","shell.execute_reply":"2023-08-27T06:37:23.169514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_array_list = [np.array(img) for img in image_list]","metadata":{"execution":{"iopub.status.busy":"2023-08-27T06:38:19.861458Z","iopub.execute_input":"2023-08-27T06:38:19.861846Z","iopub.status.idle":"2023-08-27T06:38:52.856883Z","shell.execute_reply.started":"2023-08-27T06:38:19.861813Z","shell.execute_reply":"2023-08-27T06:38:52.855834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_array = np.stack(image_array_list)","metadata":{"execution":{"iopub.status.busy":"2023-08-27T06:39:10.638952Z","iopub.execute_input":"2023-08-27T06:39:10.639316Z","iopub.status.idle":"2023-08-27T06:39:12.321213Z","shell.execute_reply.started":"2023-08-27T06:39:10.639288Z","shell.execute_reply":"2023-08-27T06:39:12.320158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_array.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-27T06:39:53.183498Z","iopub.execute_input":"2023-08-27T06:39:53.184106Z","iopub.status.idle":"2023-08-27T06:39:53.190724Z","shell.execute_reply.started":"2023-08-27T06:39:53.184068Z","shell.execute_reply":"2023-08-27T06:39:53.189728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = final_array/255","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model.predict(test)\n\nclass_name = [\"airplane\", \"automobile\", \"bird\",\"cat\", \"deer\", \"dog\",\"frog\",\"horse\", \"ship\",\"truck\"]\n\nprediction = [class_name[np.argmax(i)] for i in prediction]\nprediction","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit = pd.DataFrame({\"id\": range(1, len(prediction)+1),\n                       \"label\": prediction})\nsubmit.to_csv('sub.csv')","metadata":{},"execution_count":null,"outputs":[]}]}