{"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":"import tensorflow as tf\nimport numpy as np\nimport pandas as pd\nimport glob\nimport os\nimport matplotlib.pyplot as plt\nimport cv2\nimport random as rd","metadata":{"execution":{"iopub.status.busy":"2022-11-11T16:43:39.341035Z","iopub.execute_input":"2022-11-11T16:43:39.341510Z","iopub.status.idle":"2022-11-11T16:43:45.157790Z","shell.execute_reply.started":"2022-11-11T16:43:39.341438Z","shell.execute_reply":"2022-11-11T16:43:45.156670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Extracting Data","metadata":{}},{"cell_type":"code","source":"data_path = '../input/cifar-10'\nIMG_SIZE = 48","metadata":{"execution":{"iopub.status.busy":"2022-11-11T16:43:45.161266Z","iopub.execute_input":"2022-11-11T16:43:45.162359Z","iopub.status.idle":"2022-11-11T16:43:45.167278Z","shell.execute_reply.started":"2022-11-11T16:43:45.162321Z","shell.execute_reply":"2022-11-11T16:43:45.166311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(*glob.glob('../input/cifar-10/*'), sep='\\n\\n')","metadata":{"execution":{"iopub.status.busy":"2022-11-11T16:43:45.168473Z","iopub.execute_input":"2022-11-11T16:43:45.168813Z","iopub.status.idle":"2022-11-11T16:43:45.180349Z","shell.execute_reply.started":"2022-11-11T16:43:45.168787Z","shell.execute_reply":"2022-11-11T16:43:45.179362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"For extracting ```Data``` from .7z zip file we need to install ```py7zr``` package","metadata":{}},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2022-11-11T16:43:45.183133Z","iopub.execute_input":"2022-11-11T16:43:45.183502Z","iopub.status.idle":"2022-11-11T16:43:59.295571Z","shell.execute_reply.started":"2022-11-11T16:43:45.183462Z","shell.execute_reply":"2022-11-11T16:43:59.294344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import py7zr\ntemp_path = '/kaggle/temp/'\nif not os.path.exists(temp_path):\n    os.mkdir(temp_path)\ntrain_file_path = '../input/cifar-10/train.7z'\narchive = py7zr.SevenZipFile(train_file_path, mode='r')\narchive.extractall(path=temp_path)\narchive.close()","metadata":{"execution":{"iopub.status.busy":"2022-11-11T16:43:59.297699Z","iopub.execute_input":"2022-11-11T16:43:59.298457Z","iopub.status.idle":"2022-11-11T16:44:45.543931Z","shell.execute_reply.started":"2022-11-11T16:43:59.298402Z","shell.execute_reply":"2022-11-11T16:44:45.542932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check how many images are present in that file\nimages = glob.glob('/kaggle/temp/train/*.png')\nlen(images)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T16:44:45.545531Z","iopub.execute_input":"2022-11-11T16:44:45.545979Z","iopub.status.idle":"2022-11-11T16:44:45.735612Z","shell.execute_reply.started":"2022-11-11T16:44:45.545886Z","shell.execute_reply":"2022-11-11T16:44:45.734517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = pd.read_csv('../input/cifar-10/trainLabels.csv')\ntrain_labels","metadata":{"execution":{"iopub.status.busy":"2022-11-11T16:44:45.736715Z","iopub.execute_input":"2022-11-11T16:44:45.737345Z","iopub.status.idle":"2022-11-11T16:44:46.249468Z","shell.execute_reply.started":"2022-11-11T16:44:45.737306Z","shell.execute_reply":"2022-11-11T16:44:46.248541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels['label'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-11-11T16:44:46.250938Z","iopub.execute_input":"2022-11-11T16:44:46.251801Z","iopub.status.idle":"2022-11-11T16:44:46.265718Z","shell.execute_reply.started":"2022-11-11T16:44:46.251763Z","shell.execute_reply":"2022-11-11T16:44:46.264625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LABELS = ['frog', 'truck', 'deer', 'automobile', 'bird', 'horse', 'ship',\n          'cat', 'dog', 'airplane']\n","metadata":{"execution":{"iopub.status.busy":"2022-11-11T16:44:46.267265Z","iopub.execute_input":"2022-11-11T16:44:46.268090Z","iopub.status.idle":"2022-11-11T16:44:46.273520Z","shell.execute_reply.started":"2022-11-11T16:44:46.268053Z","shell.execute_reply":"2022-11-11T16:44:46.272491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Preprocessing","metadata":{}},{"cell_type":"code","source":"X_train = []\nY_train = []\n\nX_val = []\nY_val = []","metadata":{"execution":{"iopub.status.busy":"2022-11-11T16:44:46.274907Z","iopub.execute_input":"2022-11-11T16:44:46.275715Z","iopub.status.idle":"2022-11-11T16:44:46.284359Z","shell.execute_reply.started":"2022-11-11T16:44:46.275681Z","shell.execute_reply":"2022-11-11T16:44:46.283384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img in images:\n    prob = rd.random()\n    label = train_labels.iloc[int(img[19:-4])-1]['label']\n    img_arr = cv2.imread(img)\n    img_arr = cv2.resize(img_arr, (IMG_SIZE, IMG_SIZE))\n    if prob > 0.8:\n        X_val.append(list(img_arr))\n        Y_val.append(LABELS.index(label))\n    else:\n        X_train.append(list(img_arr))\n        Y_train.append(LABELS.index(label))\n#     print(label)\n#     print(img_arr)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T16:44:46.288097Z","iopub.execute_input":"2022-11-11T16:44:46.288357Z","iopub.status.idle":"2022-11-11T16:45:00.898008Z","shell.execute_reply.started":"2022-11-11T16:44:46.288334Z","shell.execute_reply":"2022-11-11T16:45:00.896992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.array(X_train, dtype=np.float32) / 255\n\nY_train = np.array(Y_train)\n\nX_val = np.array(X_val, dtype=np.float32) / 255\nY_val = np.array(Y_val)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T16:45:00.899400Z","iopub.execute_input":"2022-11-11T16:45:00.900424Z","iopub.status.idle":"2022-11-11T16:45:02.630685Z","shell.execute_reply.started":"2022-11-11T16:45:00.900385Z","shell.execute_reply":"2022-11-11T16:45:02.629449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Take a Quick lookup at our data","metadata":{}},{"cell_type":"code","source":"print(f'Type : {type(X_train)}\\nShape : {X_train.shape}\\nFirst Image Array: \\n{X_train[0]}')\nprint(f'\\nType : {type(X_val)}\\nShape : {X_val.shape}')","metadata":{"execution":{"iopub.status.busy":"2022-11-11T16:45:02.635716Z","iopub.execute_input":"2022-11-11T16:45:02.638063Z","iopub.status.idle":"2022-11-11T16:45:02.650021Z","shell.execute_reply.started":"2022-11-11T16:45:02.638025Z","shell.execute_reply":"2022-11-11T16:45:02.648939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training parameters\nBATCH_SIZE = 32 \n\nEPOCHS = 500\n\nCOLORS = 3","metadata":{"execution":{"iopub.status.busy":"2022-11-11T16:45:48.482042Z","iopub.execute_input":"2022-11-11T16:45:48.482410Z","iopub.status.idle":"2022-11-11T16:45:48.488793Z","shell.execute_reply.started":"2022-11-11T16:45:48.482380Z","shell.execute_reply":"2022-11-11T16:45:48.485825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CNN-Arcitecture","metadata":{}},{"cell_type":"code","source":"from tensorflow import keras\nfrom keras.layers import Dense, Conv2D\nfrom keras.layers import BatchNormalization, Flatten, Dropout, MaxPooling2D\nfrom keras.layers import RandomFlip, RandomHeight, RandomCrop, RandomRotation, RandomZoom, RandomWidth\nfrom keras import Sequential\nfrom keras.losses import SparseCategoricalCrossentropy, CategoricalCrossentropy\n","metadata":{"execution":{"iopub.status.busy":"2022-11-11T16:45:50.556731Z","iopub.execute_input":"2022-11-11T16:45:50.557087Z","iopub.status.idle":"2022-11-11T16:45:50.563200Z","shell.execute_reply.started":"2022-11-11T16:45:50.557055Z","shell.execute_reply":"2022-11-11T16:45:50.562234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n\n    Conv2D(64,\n           kernel_size=(3, 3),\n           strides=(1, 1),\n           activation=\"relu\", input_shape=(IMG_SIZE, IMG_SIZE, COLORS), padding='same'),\n    BatchNormalization(),\n\n    Conv2D(64,\n           kernel_size=(3, 3),\n           strides=(1, 1),\n           activation=\"relu\"),\n    BatchNormalization(),\n\n    Conv2D(64,\n           kernel_size=(3, 3),\n           strides=(1, 1),\n           activation=\"relu\"),\n    BatchNormalization(),\n\n    MaxPooling2D((2, 2)),\n    Dropout(0.25),\n\n    Conv2D(128,\n           kernel_size=(3, 3),\n           strides=(1, 1),\n           activation=\"relu\", padding='same'),\n    BatchNormalization(),\n\n    Conv2D(128,\n           kernel_size=(3, 3),\n           strides=(1, 1),\n           activation=\"relu\"),\n    BatchNormalization(),\n\n\n    Conv2D(128,\n           kernel_size=(3, 3),\n           strides=(1, 1),\n           activation=\"relu\"),\n    BatchNormalization(),\n\n    Dropout(0.30),\n    MaxPooling2D((2, 2)),\n\n    Conv2D(256,\n           kernel_size=(3, 3),\n           strides=(1, 1),\n           activation=\"relu\", padding='same'),\n    BatchNormalization(),\n\n    Conv2D(256,\n           kernel_size=(3, 3),\n           strides=(1, 1),\n           activation=\"relu\"),\n    BatchNormalization(),\n\n    Conv2D(256,\n           kernel_size=(3, 3),\n           strides=(1, 1),\n           activation=\"relu\"),\n    BatchNormalization(),\n\n\n    MaxPooling2D((4, 4)),\n    Dropout(0.35),\n\n#     Flatten(),\n#     Dense(512, activation=\"relu\"),\n#     BatchNormalization(),\n#     Dropout(0.5),\n\n#     Flatten(),\n#     Dense(1024, activation=\"relu\"),\n#     BatchNormalization(),\n#     Dropout(0.5),\n\n    Flatten(),\n    Dense(512, activation=\"relu\"),\n    BatchNormalization(),\n    Dropout(0.5),\n\n    Flatten(),\n    Dense(256, activation=\"relu\"),\n    BatchNormalization(),\n    Dropout(0.5),\n\n    Flatten(),\n    Dense(128, activation=\"relu\"),\n    BatchNormalization(),\n    Dropout(0.5),\n\n    Flatten(),\n    Dense(64, activation=\"relu\"),\n    BatchNormalization(),\n    Dropout(0.5),\n\n    Dense(10, activation='softmax')\n])\n","metadata":{"execution":{"iopub.status.busy":"2022-11-11T16:45:53.223049Z","iopub.execute_input":"2022-11-11T16:45:53.223406Z","iopub.status.idle":"2022-11-11T16:45:56.360162Z","shell.execute_reply.started":"2022-11-11T16:45:53.223375Z","shell.execute_reply":"2022-11-11T16:45:56.359260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='sparse_categorical_crossentropy', optimizer='Adam', metrics=['accuracy'])\n\nprint(model.summary())","metadata":{"execution":{"iopub.status.busy":"2022-11-11T16:46:19.443418Z","iopub.execute_input":"2022-11-11T16:46:19.443929Z","iopub.status.idle":"2022-11-11T16:46:19.484771Z","shell.execute_reply.started":"2022-11-11T16:46:19.443893Z","shell.execute_reply":"2022-11-11T16:46:19.483639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def lr_decay(epoch):\n    lr = 0.001\n    \n    if epoch > 30: \n        for e in range(epoch - 30):\n            lr*=0.98\n            \n    return lr","metadata":{"execution":{"iopub.status.busy":"2022-11-11T16:46:28.262757Z","iopub.execute_input":"2022-11-11T16:46:28.263120Z","iopub.status.idle":"2022-11-11T16:46:28.268461Z","shell.execute_reply.started":"2022-11-11T16:46:28.263089Z","shell.execute_reply":"2022-11-11T16:46:28.267208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, LearningRateScheduler","metadata":{"execution":{"iopub.status.busy":"2022-11-11T16:46:45.562481Z","iopub.execute_input":"2022-11-11T16:46:45.562968Z","iopub.status.idle":"2022-11-11T16:46:45.568098Z","shell.execute_reply.started":"2022-11-11T16:46:45.562928Z","shell.execute_reply":"2022-11-11T16:46:45.567097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stop = EarlyStopping(\n    monitor='val_loss',\n    verbose=0,\n    mode='min',\n    patience=20\n)\n\nlr_schedular = LearningRateScheduler(lr_decay)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T16:47:10.962420Z","iopub.execute_input":"2022-11-11T16:47:10.963383Z","iopub.status.idle":"2022-11-11T16:47:10.969025Z","shell.execute_reply.started":"2022-11-11T16:47:10.963338Z","shell.execute_reply":"2022-11-11T16:47:10.967869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [lr_schedular, early_stop]","metadata":{"execution":{"iopub.status.busy":"2022-11-11T16:47:14.562487Z","iopub.execute_input":"2022-11-11T16:47:14.562912Z","iopub.status.idle":"2022-11-11T16:47:14.567966Z","shell.execute_reply.started":"2022-11-11T16:47:14.562874Z","shell.execute_reply":"2022-11-11T16:47:14.566793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train, Y_train, epochs=EPOCHS,verbose=1,\n          batch_size=BATCH_SIZE,  validation_data=(X_val, Y_val), callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T16:47:18.432234Z","iopub.execute_input":"2022-11-11T16:47:18.433329Z","iopub.status.idle":"2022-11-11T17:02:24.063867Z","shell.execute_reply.started":"2022-11-11T16:47:18.433279Z","shell.execute_reply":"2022-11-11T17:02:24.062948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the the training loss\nplt.plot(model.history.history[\"loss\"], label=\"loss\")\n\n# Plot the the validation loss\nplt.plot(model.history.history[\"val_loss\"], label=\"val_loss\")\n\n# Name the x and y axises\nplt.xlabel(\"epochs\")\nplt.ylabel(\"Loss\")\n# Put legend table\nplt.legend()\nplt.grid()\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-11T17:03:00.743711Z","iopub.execute_input":"2022-11-11T17:03:00.744076Z","iopub.status.idle":"2022-11-11T17:03:00.963261Z","shell.execute_reply.started":"2022-11-11T17:03:00.744045Z","shell.execute_reply":"2022-11-11T17:03:00.962418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the the training loss\nplt.plot(model.history.history[\"accuracy\"], label=\"accuracy\")\n\n# Plot the the validation loss\nplt.plot(model.history.history[\"val_accuracy\"], label=\"val_acc\")\n\n# Name the x and y axises\nplt.xlabel(\"epochs\")\nplt.ylabel(\"Accuracy\")\n# Put legend table\nplt.legend()\nplt.grid()\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-11T17:03:28.482093Z","iopub.execute_input":"2022-11-11T17:03:28.482449Z","iopub.status.idle":"2022-11-11T17:03:28.695488Z","shell.execute_reply.started":"2022-11-11T17:03:28.482417Z","shell.execute_reply":"2022-11-11T17:03:28.694586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_dir = 'results'\nif not os.path.exists(result_dir):\n    os.mkdir(result_dir)\n\nprint('save the architecture of a model')\njson_string = model.to_json()\nopen(os.path.join(result_dir,'cifar10_model.json'), 'w').write(json_string)\nprint('save weights')\nmodel.save_weights(os.path.join(result_dir,'cifar10_model_weights.h5'))","metadata":{"execution":{"iopub.status.busy":"2022-11-11T17:04:06.118371Z","iopub.execute_input":"2022-11-11T17:04:06.118773Z","iopub.status.idle":"2022-11-11T17:04:06.209031Z","shell.execute_reply.started":"2022-11-11T17:04:06.118740Z","shell.execute_reply":"2022-11-11T17:04:06.207964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nfrom py7zr import unpack_7zarchive\nshutil.register_unpack_format('7zip',['.7z'],unpack_7zarchive)\nshutil.unpack_archive('/kaggle/input/cifar-10/test.7z','/kaggle/temp/test')\nshutil.unregister_unpack_format('7zip')\ntest_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-11T18:58:39.262943Z","iopub.execute_input":"2022-11-11T18:58:39.263323Z","iopub.status.idle":"2022-11-11T19:12:42.133165Z","shell.execute_reply.started":"2022-11-11T18:58:39.263290Z","shell.execute_reply":"2022-11-11T19:12:42.132152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gen = test_datagen.flow_from_directory(directory='/kaggle/temp/test',target_size=(IMG_SIZE,IMG_SIZE),batch_size=64,class_mode=None,shuffle=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-11T19:13:52.251377Z","iopub.execute_input":"2022-11-11T19:13:52.251778Z","iopub.status.idle":"2022-11-11T19:13:59.608360Z","shell.execute_reply.started":"2022-11-11T19:13:52.251744Z","shell.execute_reply":"2022-11-11T19:13:59.607390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gen.reset()\npredictions_vecs = model.predict(test_gen)\n\npredictions_final = np.argmax(predictions_vecs, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T19:14:08.002045Z","iopub.execute_input":"2022-11-11T19:14:08.002410Z","iopub.status.idle":"2022-11-11T19:15:57.974224Z","shell.execute_reply.started":"2022-11-11T19:14:08.002372Z","shell.execute_reply":"2022-11-11T19:15:57.973226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cifar10_classes = [\"airplane\", \"automobile\", \"bird\", \"cat\", \"deer\", \"dog\", \"frog\", \"horse\", \"ship\", \"truck\"]\nprint(cifar10_classes)\npredicted_classes=np.empty(shape=300000,dtype=np.dtype('U20'))\n\nind=0\nfor i in predictions_final.tolist():\n    predicted_classes[ind]=cifar10_classes[i]\n    ind=ind+1\n    \nfilenames_wo_ext = []\nfor fname in test_gen.filenames:\n    filenames_wo_ext.append(int(fname.split(sep=\"/\")[1].split(sep=\".\")[0])-1)\n\npredicted_classes_final = np.empty(shape=300000,dtype=np.dtype('U20'))\npredicted_classes_final[filenames_wo_ext]=predicted_classes","metadata":{"execution":{"iopub.status.busy":"2022-11-11T19:16:06.152830Z","iopub.execute_input":"2022-11-11T19:16:06.153199Z","iopub.status.idle":"2022-11-11T19:16:06.515904Z","shell.execute_reply.started":"2022-11-11T19:16:06.153168Z","shell.execute_reply":"2022-11-11T19:16:06.514807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('../input/cifar-10/sampleSubmission.csv',header='infer')\nsub.info()\n\nsub['label'] = predicted_classes_final\nsub.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T19:16:19.692489Z","iopub.execute_input":"2022-11-11T19:16:19.693239Z","iopub.status.idle":"2022-11-11T19:16:20.081705Z","shell.execute_reply.started":"2022-11-11T19:16:19.693199Z","shell.execute_reply":"2022-11-11T19:16:20.080608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}