{"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":"markdown","source":"CIFAR-10  is an established computer-vision dataset used for object recognition. It is a subset of the 80 million tiny images dataset and consists of 60,000 32x32 color images containing one of 10 object classes, with 6000 images per class. It was collected by Alex Krizhevsky, Vinod Nair, and Geoffrey Hinton.","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\nimport pandas as pd\nfrom tensorflow import keras\nfrom keras.preprocessing.image import ImageDataGenerator\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\n!pip install py7zr\nfrom keras.preprocessing.image import load_img,img_to_array\nfrom py7zr import unpack_7zarchive\nimport shutil\nimport os\nshutil.register_unpack_format('7zip', ['.7z'], unpack_7zarchive)\n","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2021-08-03T12:44:16.731649Z","iopub.execute_input":"2021-08-03T12:44:16.732097Z","iopub.status.idle":"2021-08-03T12:44:37.097940Z","shell.execute_reply.started":"2021-08-03T12:44:16.731994Z","shell.execute_reply":"2021-08-03T12:44:37.096328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.unpack_archive('/kaggle/input/cifar-10/train.7z', '/kaggle/working')","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:44:45.062468Z","iopub.execute_input":"2021-08-03T12:44:45.063012Z","iopub.status.idle":"2021-08-03T12:45:36.608560Z","shell.execute_reply.started":"2021-08-03T12:44:45.062972Z","shell.execute_reply":"2021-08-03T12:45:36.607394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = os.listdir(\"./train\");\ntrain_dir_len = len(train_dir)\nprint(\".\\\\train:\\t\",train_dir_len)\nprint(\"files:\\t\\t\",train_dir[:3])","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:45:45.902558Z","iopub.execute_input":"2021-08-03T12:45:45.902930Z","iopub.status.idle":"2021-08-03T12:45:45.939450Z","shell.execute_reply.started":"2021-08-03T12:45:45.902899Z","shell.execute_reply":"2021-08-03T12:45:45.938204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv',dtype=str)\ntrain_images = pd.DataFrame(columns = ['id','label','path'],dtype=str)\ntest_labels = pd.read_csv('/kaggle/input/cifar-10/sampleSubmission.csv')\ntrain_labels.info()","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:45:52.012479Z","iopub.execute_input":"2021-08-03T12:45:52.012842Z","iopub.status.idle":"2021-08-03T12:45:52.222917Z","shell.execute_reply.started":"2021-08-03T12:45:52.012813Z","shell.execute_reply":"2021-08-03T12:45:52.221817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_base = '/kaggle/working/train/'\n\nfor index in range(0,train_dir_len):\n    path = path_base + str(index+1)+'.png'\n    if os.path.exists(path):\n        train_images = train_images.append([{ 'id': str(train_labels['id'].iloc[index]),'path': path, 'label':train_labels['label'].iloc[index]}])\n        \ntrain_images.head(2)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:45:59.470441Z","iopub.execute_input":"2021-08-03T12:45:59.471089Z","iopub.status.idle":"2021-08-03T12:48:23.386541Z","shell.execute_reply.started":"2021-08-03T12:45:59.471040Z","shell.execute_reply":"2021-08-03T12:48:23.385750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images.head(2)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:48:52.482767Z","iopub.execute_input":"2021-08-03T12:48:52.483462Z","iopub.status.idle":"2021-08-03T12:48:52.495324Z","shell.execute_reply.started":"2021-08-03T12:48:52.483409Z","shell.execute_reply":"2021-08-03T12:48:52.494558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_groupby = train_images.groupby(['label']).count()\ndisplay_groupby.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:50:12.677657Z","iopub.execute_input":"2021-08-03T12:50:12.678311Z","iopub.status.idle":"2021-08-03T12:50:12.710753Z","shell.execute_reply.started":"2021-08-03T12:50:12.678255Z","shell.execute_reply":"2021-08-03T12:50:12.709692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = ['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck']\nfor name in  class_names:\n    index = class_names.index(name)\n    train_images.loc[train_images.label==name,'label'] = str(index)\n        \ndisplay_groupby = train_images.groupby(['label']).count()\ndisplay_groupby.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:50:24.852692Z","iopub.execute_input":"2021-08-03T12:50:24.853296Z","iopub.status.idle":"2021-08-03T12:50:24.972042Z","shell.execute_reply.started":"2021-08-03T12:50:24.853239Z","shell.execute_reply":"2021-08-03T12:50:24.970847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_base = '/kaggle/working/train'\nbatch_size = 64\ntrain_data_generator = ImageDataGenerator(\n            rescale=1./255.,\n            validation_split=0.2,\n            horizontal_flip=True\n            )\ntrain_generator = train_data_generator.flow_from_dataframe(dataframe=train_images,\n            directory=\"./train/\",\n            x_col=\"path\",\n            y_col=\"label\",\n            subset=\"training\",\n            batch_size=batch_size,\n            shuffle=True,\n            target_size=(32,32),\n            class_mode=\"categorical\")","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:50:30.221992Z","iopub.execute_input":"2021-08-03T12:50:30.222391Z","iopub.status.idle":"2021-08-03T12:50:30.744254Z","shell.execute_reply.started":"2021-08-03T12:50:30.222354Z","shell.execute_reply":"2021-08-03T12:50:30.743128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes  = 10","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:50:36.692259Z","iopub.execute_input":"2021-08-03T12:50:36.692616Z","iopub.status.idle":"2021-08-03T12:50:36.696828Z","shell.execute_reply.started":"2021-08-03T12:50:36.692585Z","shell.execute_reply":"2021-08-03T12:50:36.695581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_generator = train_data_generator.flow_from_dataframe(dataframe=train_images,\n            directory=\"./train/\",\n            x_col=\"path\",\n            y_col=\"label\",\n            subset=\"validation\",\n            batch_size=batch_size,\n            shuffle=True,\n            target_size=(32,32),\n            class_mode=\"categorical\")","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:50:41.341316Z","iopub.execute_input":"2021-08-03T12:50:41.341691Z","iopub.status.idle":"2021-08-03T12:50:41.775793Z","shell.execute_reply.started":"2021-08-03T12:50:41.341654Z","shell.execute_reply":"2021-08-03T12:50:41.774713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size = len(train_generator.filenames)\nvalidation_size = len(validation_generator.filenames)\nprint('validation_size:\\t',validation_size)\nprint('train_size:\\t\\t',train_size)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:50:47.761378Z","iopub.execute_input":"2021-08-03T12:50:47.761740Z","iopub.status.idle":"2021-08-03T12:50:47.767483Z","shell.execute_reply.started":"2021-08-03T12:50:47.761711Z","shell.execute_reply":"2021-08-03T12:50:47.766485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = 0    \nfig = plt.figure(figsize = (16,10))\nfor item in train_images.values[:20]:\n    index += 1\n    plt.subplot(5, 5, index)\n    test_path = item[2]\n    test_image = load_img(test_path, target_size=(32,32))\n    plt.imshow(test_image)\n    plt.colorbar()\n    plt.grid(False)\n    plt.axis(\"off\")\n    plt.title(class_names[int(item[1])])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:50:53.261523Z","iopub.execute_input":"2021-08-03T12:50:53.261899Z","iopub.status.idle":"2021-08-03T12:50:55.880563Z","shell.execute_reply.started":"2021-08-03T12:50:53.261865Z","shell.execute_reply":"2021-08-03T12:50:55.879781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keras.backend.clear_session()\n\nmodel = keras.models.Sequential()\nmodel.add(keras.layers.Conv2D(64, (3, 3), activation='relu', input_shape=(32, 32,  3)))\n\nmodel.add(keras.layers.BatchNormalization())\nmodel.add(keras.layers.Conv2D(64, (2, 2), activation='relu',padding='same'))\nmodel.add(keras.layers.MaxPooling2D(1, 1))\nmodel.add(keras.layers.Dropout(0.1))\n\nmodel.add(keras.layers.BatchNormalization())\nmodel.add(keras.layers.Conv2D(64, (3, 3), activation='relu',padding='same'))\nmodel.add(keras.layers.MaxPooling2D(2, 2))\nmodel.add(keras.layers.Dropout(0.2))\n\nmodel.add(keras.layers.BatchNormalization())\nmodel.add(keras.layers.Conv2D(64, (2, 2), activation='relu',padding='same'))\nmodel.add(keras.layers.MaxPooling2D(1, 1))\nmodel.add(keras.layers.Dropout(0.1))\n\nmodel.add(keras.layers.Flatten())\nmodel.add(keras.layers.Dense(64, activation='relu'))\nmodel.add(keras.layers.BatchNormalization())\nmodel.add(keras.layers.Dropout(0.2))\n\nmodel.add(keras.layers.Dense(10, activation=\"softmax\"))\n\nmodel.compile(loss=keras.losses.CategoricalCrossentropy(from_logits=True), \n          optimizer=keras.optimizers.RMSprop(lr=0.001, decay = 1e-3, momentum = 0.3),\n          metrics=['accuracy'])\n    \nmodel.input ","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:51:01.184257Z","iopub.execute_input":"2021-08-03T12:51:01.184857Z","iopub.status.idle":"2021-08-03T12:51:01.726270Z","shell.execute_reply.started":"2021-08-03T12:51:01.184809Z","shell.execute_reply":"2021-08-03T12:51:01.725010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nhistory = model.fit(train_generator, \n                    steps_per_epoch=(train_size//batch_size),\n                    epochs= 5,\n                    validation_data=validation_generator,\n                   validation_steps=(validation_size//batch_size)\n                   )","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:51:10.542108Z","iopub.execute_input":"2021-08-03T12:51:10.542603Z","iopub.status.idle":"2021-08-03T13:15:08.428766Z","shell.execute_reply.started":"2021-08-03T12:51:10.542568Z","shell.execute_reply":"2021-08-03T13:15:08.427416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_labels.head(2)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T13:15:16.110603Z","iopub.execute_input":"2021-08-03T13:15:16.111009Z","iopub.status.idle":"2021-08-03T13:15:16.124211Z","shell.execute_reply.started":"2021-08-03T13:15:16.110973Z","shell.execute_reply":"2021-08-03T13:15:16.122883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if os.path.exists(\"./test\"):\n    shutil.rmtree(\"./test\")\nif os.path.exists(\"./train\"):\n    shutil.rmtree(\"./train\")\nif not os.path.exists(\"./data\"):\n    os.mkdir(\"./data\")\n\nshutil.unpack_archive('/kaggle/input/cifar-10/test.7z', '/kaggle/working/data')","metadata":{"execution":{"iopub.status.busy":"2021-08-03T13:15:18.413664Z","iopub.execute_input":"2021-08-03T13:15:18.414198Z","iopub.status.idle":"2021-08-03T13:35:20.116511Z","shell.execute_reply.started":"2021-08-03T13:15:18.414143Z","shell.execute_reply":"2021-08-03T13:35:20.115243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir = os.listdir(\"./data/test\");\ntest_dir_len = len(test_dir)\nprint('min:\\t',min(test_dir))\nprint('max:\\t',max(test_dir))\nprint(\".\\\\test:\\t\",test_dir_len)\nprint(\"files:\\t\\t\",test_dir[:3])","metadata":{"execution":{"iopub.status.busy":"2021-08-03T13:35:31.699248Z","iopub.execute_input":"2021-08-03T13:35:31.699921Z","iopub.status.idle":"2021-08-03T13:35:31.904830Z","shell.execute_reply.started":"2021-08-03T13:35:31.699863Z","shell.execute_reply":"2021-08-03T13:35:31.903436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_generator = ImageDataGenerator(rescale=1./255.)\ntest_generator = test_data_generator.flow_from_directory(directory='/kaggle/working/data',\n            batch_size=batch_size,\n            shuffle=False,color_mode='rgb',\n            target_size=(32,32),\n            class_mode=None)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T13:35:36.528718Z","iopub.execute_input":"2021-08-03T13:35:36.529117Z","iopub.status.idle":"2021-08-03T13:35:44.125733Z","shell.execute_reply.started":"2021-08-03T13:35:36.529083Z","shell.execute_reply":"2021-08-03T13:35:44.124745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_test = model.predict_generator(test_generator)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T13:35:46.948489Z","iopub.execute_input":"2021-08-03T13:35:46.949240Z","iopub.status.idle":"2021-08-03T13:42:38.128697Z","shell.execute_reply.started":"2021-08-03T13:35:46.949198Z","shell.execute_reply":"2021-08-03T13:42:38.127498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_generator = np.argmax(predict_test, axis=1)\nprint(class_names)\npredict_generator[:2],[class_names[int(i)] for i in predict_generator[:2]]","metadata":{"execution":{"iopub.status.busy":"2021-08-03T13:42:53.729452Z","iopub.execute_input":"2021-08-03T13:42:53.729803Z","iopub.status.idle":"2021-08-03T13:42:53.750467Z","shell.execute_reply.started":"2021-08-03T13:42:53.729773Z","shell.execute_reply":"2021-08-03T13:42:53.749363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(columns = ['id','label'],dtype=str)\nsubmission[\"label\"] = [class_names[int(i)] for i in predict_generator]\nsubmission[\"id\"] = [ (''.join(filter(str.isdigit, name ))) for name in test_generator.filenames]\nsubmission.head(101)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T13:42:59.760451Z","iopub.execute_input":"2021-08-03T13:42:59.760803Z","iopub.status.idle":"2021-08-03T13:43:00.567335Z","shell.execute_reply.started":"2021-08-03T13:42:59.760773Z","shell.execute_reply":"2021-08-03T13:43:00.566219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" submission.values[50:100]","metadata":{"execution":{"iopub.status.busy":"2021-08-03T13:43:07.529574Z","iopub.execute_input":"2021-08-03T13:43:07.529970Z","iopub.status.idle":"2021-08-03T13:43:07.538510Z","shell.execute_reply.started":"2021-08-03T13:43:07.529938Z","shell.execute_reply":"2021-08-03T13:43:07.537094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = 0    \nfig = plt.figure(figsize = (16,10))\nfor item in submission.values[50:70]:\n    index += 1\n    plt.subplot(5, 5, index)\n    test_path = '/kaggle/working/data/test/'+item[0]+'.png'\n    test_image = load_img(test_path, target_size=(32,32))\n    plt.imshow(test_image)\n    plt.colorbar()\n    plt.grid(False)\n    plt.axis(\"off\")\n    plt.title(item[1])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-03T13:43:14.249704Z","iopub.execute_input":"2021-08-03T13:43:14.250060Z","iopub.status.idle":"2021-08-03T13:43:16.593270Z","shell.execute_reply.started":"2021-08-03T13:43:14.250030Z","shell.execute_reply":"2021-08-03T13:43:16.592000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T13:43:23.859303Z","iopub.execute_input":"2021-08-03T13:43:23.859773Z","iopub.status.idle":"2021-08-03T13:43:24.482593Z","shell.execute_reply.started":"2021-08-03T13:43:23.859740Z","shell.execute_reply":"2021-08-03T13:43:24.481535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" shutil.rmtree(\"./data\")","metadata":{"execution":{"iopub.status.busy":"2021-08-03T13:43:27.958469Z","iopub.execute_input":"2021-08-03T13:43:27.958826Z","iopub.status.idle":"2021-08-03T13:43:43.139656Z","shell.execute_reply.started":"2021-08-03T13:43:27.958795Z","shell.execute_reply":"2021-08-03T13:43:43.138638Z"},"trusted":true},"execution_count":null,"outputs":[]}]}