{"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":"2022-05-05T08:21:51.942642Z","iopub.execute_input":"2022-05-05T08:21:51.943156Z","iopub.status.idle":"2022-05-05T08:21:51.974059Z","shell.execute_reply.started":"2022-05-05T08:21:51.943020Z","shell.execute_reply":"2022-05-05T08:21:51.973287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/cifar-10/trainLabels.csv')","metadata":{"execution":{"iopub.status.busy":"2022-05-05T08:21:51.975925Z","iopub.execute_input":"2022-05-05T08:21:51.976452Z","iopub.status.idle":"2022-05-05T08:21:52.008365Z","shell.execute_reply.started":"2022-05-05T08:21:51.976414Z","shell.execute_reply":"2022-05-05T08:21:52.007635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2022-05-05T08:21:52.010930Z","iopub.execute_input":"2022-05-05T08:21:52.011445Z","iopub.status.idle":"2022-05-05T08:22:18.526772Z","shell.execute_reply.started":"2022-05-05T08:21:52.011407Z","shell.execute_reply":"2022-05-05T08:22:18.525881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import py7zr\n\narchive = py7zr.SevenZipFile('../input/cifar-10/train.7z', mode='r')\narchive.extractall(path='../temp')","metadata":{"execution":{"iopub.status.busy":"2022-05-05T08:22:18.529313Z","iopub.execute_input":"2022-05-05T08:22:18.530523Z","iopub.status.idle":"2022-05-05T08:23:06.959250Z","shell.execute_reply.started":"2022-05-05T08:22:18.530482Z","shell.execute_reply":"2022-05-05T08:23:06.958480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.image as img","metadata":{"execution":{"iopub.status.busy":"2022-05-05T08:23:06.960263Z","iopub.execute_input":"2022-05-05T08:23:06.960493Z","iopub.status.idle":"2022-05-05T08:23:06.970503Z","shell.execute_reply.started":"2022-05-05T08:23:06.960462Z","shell.execute_reply":"2022-05-05T08:23:06.969922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ../temp","metadata":{"execution":{"iopub.status.busy":"2022-05-05T08:23:06.971477Z","iopub.execute_input":"2022-05-05T08:23:06.971855Z","iopub.status.idle":"2022-05-05T08:23:07.077871Z","shell.execute_reply.started":"2022-05-05T08:23:06.971819Z","shell.execute_reply":"2022-05-05T08:23:07.077058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%ls","metadata":{"execution":{"iopub.status.busy":"2022-05-05T08:23:07.079344Z","iopub.execute_input":"2022-05-05T08:23:07.079680Z","iopub.status.idle":"2022-05-05T08:23:07.752971Z","shell.execute_reply.started":"2022-05-05T08:23:07.079646Z","shell.execute_reply":"2022-05-05T08:23:07.752132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd train","metadata":{"execution":{"iopub.status.busy":"2022-05-05T08:23:07.755839Z","iopub.execute_input":"2022-05-05T08:23:07.756057Z","iopub.status.idle":"2022-05-05T08:23:07.762416Z","shell.execute_reply.started":"2022-05-05T08:23:07.756031Z","shell.execute_reply":"2022-05-05T08:23:07.761374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%pwd","metadata":{"execution":{"iopub.status.busy":"2022-05-05T08:23:07.764134Z","iopub.execute_input":"2022-05-05T08:23:07.764667Z","iopub.status.idle":"2022-05-05T08:23:07.774050Z","shell.execute_reply.started":"2022-05-05T08:23:07.764631Z","shell.execute_reply":"2022-05-05T08:23:07.773399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = img.imread('1.png')\n  \n# show image\nplt.imshow(im)","metadata":{"execution":{"iopub.status.busy":"2022-05-05T08:23:07.777773Z","iopub.execute_input":"2022-05-05T08:23:07.778211Z","iopub.status.idle":"2022-05-05T08:23:07.993447Z","shell.execute_reply.started":"2022-05-05T08:23:07.778183Z","shell.execute_reply":"2022-05-05T08:23:07.992780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = df['label'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-05-05T08:23:07.994442Z","iopub.execute_input":"2022-05-05T08:23:07.994790Z","iopub.status.idle":"2022-05-05T08:23:08.008588Z","shell.execute_reply.started":"2022-05-05T08:23:07.994757Z","shell.execute_reply":"2022-05-05T08:23:08.007870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_path = '/kaggle/temp/train'\nval_images_path = '/kaggle/temp/validation'\n# test_images_path = '/kaggle/temp/test'","metadata":{"execution":{"iopub.status.busy":"2022-05-05T08:23:08.009849Z","iopub.execute_input":"2022-05-05T08:23:08.010127Z","iopub.status.idle":"2022-05-05T08:23:08.016600Z","shell.execute_reply.started":"2022-05-05T08:23:08.010061Z","shell.execute_reply":"2022-05-05T08:23:08.015884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir(val_images_path)\nfor c in classes:\n    train_path=os.path.join(train_images_path,c)\n    if not os.path.exists(train_path):\n        os.mkdir(train_path)\n    validation_path=os.path.join(val_images_path,c)\n    if not os.path.exists(validation_path):\n        os.mkdir(validation_path)\n\n\nfor ind in df.index:\n    x=str(df['id'][ind])+\".png\"\n    path1=os.path.join(train_images_path,x)  #currently stored\n    \n    path2=os.path.join(train_images_path,df['label'][ind],x)\n    path3=os.path.join(val_images_path,df['label'][ind],x)\n    \n    r=np.random.random()\n    if r<=0.8:\n        os.replace(path1,path2)\n    else:\n        os.replace(path1,path3)","metadata":{"execution":{"iopub.status.busy":"2022-05-05T08:23:08.017820Z","iopub.execute_input":"2022-05-05T08:23:08.018953Z","iopub.status.idle":"2022-05-05T08:23:11.512444Z","shell.execute_reply.started":"2022-05-05T08:23:08.018923Z","shell.execute_reply":"2022-05-05T08:23:11.511309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd frog","metadata":{"execution":{"iopub.status.busy":"2022-05-05T08:23:11.513740Z","iopub.execute_input":"2022-05-05T08:23:11.514117Z","iopub.status.idle":"2022-05-05T08:23:11.530722Z","shell.execute_reply.started":"2022-05-05T08:23:11.514081Z","shell.execute_reply":"2022-05-05T08:23:11.529877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ls\nim.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-05T08:23:11.531679Z","iopub.execute_input":"2022-05-05T08:23:11.532083Z","iopub.status.idle":"2022-05-05T08:23:12.118612Z","shell.execute_reply.started":"2022-05-05T08:23:11.532033Z","shell.execute_reply":"2022-05-05T08:23:12.117993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ..","metadata":{"execution":{"iopub.status.busy":"2022-05-05T08:23:12.119992Z","iopub.execute_input":"2022-05-05T08:23:12.120429Z","iopub.status.idle":"2022-05-05T08:23:12.156668Z","shell.execute_reply.started":"2022-05-05T08:23:12.120394Z","shell.execute_reply":"2022-05-05T08:23:12.155897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ..","metadata":{"execution":{"iopub.status.busy":"2022-05-05T08:23:12.157738Z","iopub.execute_input":"2022-05-05T08:23:12.158276Z","iopub.status.idle":"2022-05-05T08:23:12.171600Z","shell.execute_reply.started":"2022-05-05T08:23:12.158240Z","shell.execute_reply":"2022-05-05T08:23:12.170705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\ntrain_datagen = ImageDataGenerator(featurewise_center=False,\n                                  samplewise_center=False,\n                                  featurewise_std_normalization=False,\n                                  samplewise_std_normalization=False,\n                                  zca_whitening=False,\n#                                   rotation_range=10,\n#                                   zoom_range=0.1,\n#                                   width_shift_range=0.1,\n#                                   height_shift_range=0.1,\n                                  horizontal_flip=False,\n                                  vertical_flip=False\n                                  )\n\ntrain_generator = train_datagen.flow_from_directory(\n    directory=r\"./train/\",\n    target_size=(224, 224),\n    color_mode=\"rgb\",\n    batch_size=32,\n    class_mode=\"categorical\",\n    shuffle=True,\n    seed=42\n)\n\nval_datagen = ImageDataGenerator(featurewise_center=False,\n                                  samplewise_center=False,\n                                  featurewise_std_normalization=False,\n                                  samplewise_std_normalization=False,\n                                  zca_whitening=False,\n#                                   rotation_range=10,\n#                                   zoom_range=0.1,\n#                                   width_shift_range=0.1,\n#                                   height_shift_range=0.1,\n                                  horizontal_flip=False,\n                                  vertical_flip=False\n                                  )\n\nval_generator = val_datagen.flow_from_directory(\n    directory=r\"./validation/\",\n    target_size=(224, 224),\n    color_mode=\"rgb\",\n    batch_size=32,\n    class_mode=\"categorical\",\n    shuffle=True,\n    seed=42\n)","metadata":{"execution":{"iopub.status.busy":"2022-05-05T08:23:12.173074Z","iopub.execute_input":"2022-05-05T08:23:12.173349Z","iopub.status.idle":"2022-05-05T08:23:20.989427Z","shell.execute_reply.started":"2022-05-05T08:23:12.173307Z","shell.execute_reply":"2022-05-05T08:23:20.988608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import Sequential\nfrom keras.utils.np_utils import to_categorical\nfrom tensorflow.keras.layers import Conv2D, MaxPool2D , Dense,Activation, Flatten,Dropout,BatchNormalization","metadata":{"execution":{"iopub.status.busy":"2022-05-05T08:23:20.990885Z","iopub.execute_input":"2022-05-05T08:23:20.991393Z","iopub.status.idle":"2022-05-05T08:23:21.002035Z","shell.execute_reply.started":"2022-05-05T08:23:20.991354Z","shell.execute_reply":"2022-05-05T08:23:21.001384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom keras.callbacks import ReduceLROnPlateau\n\nreduceLROnPlateau = ReduceLROnPlateau(monitor='val_accuracy',\n                                     patience=3,\n                                     verbose=1,\n                                     factor=0.1,\n                                     min_lr=0.00001)\nearly_stopping_callback = keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=20, restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2022-05-05T08:23:21.005663Z","iopub.execute_input":"2022-05-05T08:23:21.007673Z","iopub.status.idle":"2022-05-05T08:23:21.031459Z","shell.execute_reply.started":"2022-05-05T08:23:21.007629Z","shell.execute_reply":"2022-05-05T08:23:21.030711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def my_model():\n    model=Sequential()\n    model.add( Conv2D(filters=32, kernel_size=(3,3), strides=(1,1), padding='valid', activation=None, use_bias=False, input_shape=(224,224,3)) )\n    model.add(BatchNormalization())\n    model.add(Activation('relu'))\n\n    model.add( Conv2D(filters=64, kernel_size=(3,3), strides=(1,1), padding='valid', activation=None, use_bias=False) )\n    model.add(BatchNormalization())\n    model.add(Activation('relu'))\n\n    model.add( Conv2D(filters=128, kernel_size=(3,3), strides=(1,1), padding='valid', activation=None, use_bias=False) )\n    model.add(BatchNormalization())\n    model.add(Activation('relu'))\n\n\n    model.add(Flatten())\n\n    model.add(Dense(units=10))\n    model.add(BatchNormalization())\n    model.add(Activation('softmax'))\n    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n    return model\n\n","metadata":{"execution":{"iopub.status.busy":"2022-05-05T08:23:21.033195Z","iopub.execute_input":"2022-05-05T08:23:21.033736Z","iopub.status.idle":"2022-05-05T08:23:21.046938Z","shell.execute_reply.started":"2022-05-05T08:23:21.033701Z","shell.execute_reply":"2022-05-05T08:23:21.046230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=my_model()","metadata":{"execution":{"iopub.status.busy":"2022-05-05T08:23:21.048542Z","iopub.execute_input":"2022-05-05T08:23:21.049151Z","iopub.status.idle":"2022-05-05T08:23:23.483884Z","shell.execute_reply.started":"2022-05-05T08:23:21.049113Z","shell.execute_reply":"2022-05-05T08:23:23.483091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(model,show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2022-05-05T08:23:23.485173Z","iopub.execute_input":"2022-05-05T08:23:23.485569Z","iopub.status.idle":"2022-05-05T08:23:24.324579Z","shell.execute_reply.started":"2022-05-05T08:23:23.485531Z","shell.execute_reply":"2022-05-05T08:23:24.323751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_generator, \n                      epochs=10,  \n                      callbacks=[early_stopping_callback, reduceLROnPlateau],\n                      workers=8, \n                      use_multiprocessing=True, \n                      verbose=1, \n                      validation_data=val_generator)","metadata":{"execution":{"iopub.status.busy":"2022-05-05T08:23:24.325873Z","iopub.execute_input":"2022-05-05T08:23:24.327240Z","iopub.status.idle":"2022-05-05T09:11:57.289881Z","shell.execute_reply.started":"2022-05-05T08:23:24.327201Z","shell.execute_reply":"2022-05-05T09:11:57.288830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ..","metadata":{"execution":{"iopub.status.busy":"2022-05-05T09:11:57.294410Z","iopub.execute_input":"2022-05-05T09:11:57.297471Z","iopub.status.idle":"2022-05-05T09:11:57.324799Z","shell.execute_reply.started":"2022-05-05T09:11:57.297428Z","shell.execute_reply":"2022-05-05T09:11:57.321480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"archive = py7zr.SevenZipFile('../kaggle/input/cifar-10/test.7z', mode='r')\narchive.extractall(path='../temp')","metadata":{"execution":{"iopub.status.busy":"2022-05-05T09:32:08.916054Z","iopub.execute_input":"2022-05-05T09:32:08.916643Z","iopub.status.idle":"2022-05-05T09:47:42.052768Z","shell.execute_reply.started":"2022-05-05T09:32:08.916603Z","shell.execute_reply":"2022-05-05T09:47:42.052001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls","metadata":{"execution":{"iopub.status.busy":"2022-05-05T09:47:42.067317Z","iopub.execute_input":"2022-05-05T09:47:42.067793Z","iopub.status.idle":"2022-05-05T09:47:43.196819Z","shell.execute_reply.started":"2022-05-05T09:47:42.067756Z","shell.execute_reply":"2022-05-05T09:47:43.195756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd temp","metadata":{"execution":{"iopub.status.busy":"2022-05-05T09:47:42.054371Z","iopub.execute_input":"2022-05-05T09:47:42.054593Z","iopub.status.idle":"2022-05-05T09:47:42.065753Z","shell.execute_reply.started":"2022-05-05T09:47:42.054561Z","shell.execute_reply":"2022-05-05T09:47:42.064918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(featurewise_center=False,\n                                  samplewise_center=False,\n                                  featurewise_std_normalization=False,\n                                  samplewise_std_normalization=False,\n                                  zca_whitening=False,\n#                                   rotation_range=10,\n#                                   zoom_range=0.1,\n#                                   width_shift_range=0.1,\n#                                   height_shift_range=0.1,\n                                  horizontal_flip=False,\n                                  vertical_flip=False\n                                  )\n\ntest_generator = test_datagen.flow_from_directory(\n    directory=r\"./test/\",\n    target_size=(224, 224),\n    color_mode=\"rgb\",\n    batch_size=32,\n    class_mode=\"categorical\",\n    shuffle=True,\n    seed=42\n)","metadata":{"execution":{"iopub.status.busy":"2022-05-05T09:27:38.193715Z","iopub.execute_input":"2022-05-05T09:27:38.196632Z","iopub.status.idle":"2022-05-05T09:27:38.678804Z","shell.execute_reply.started":"2022-05-05T09:27:38.196592Z","shell.execute_reply":"2022-05-05T09:27:38.677251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = model.predict(X_test_reshaped)","metadata":{"execution":{"iopub.status.busy":"2022-05-05T09:27:38.679781Z","iopub.status.idle":"2022-05-05T09:27:38.681464Z","shell.execute_reply.started":"2022-05-05T09:27:38.681218Z","shell.execute_reply":"2022-05-05T09:27:38.681247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_label_list = []\nfor i in label:\n    final_label_list.append(np.argmax(i))","metadata":{"execution":{"iopub.status.busy":"2022-05-05T09:27:38.685461Z","iopub.status.idle":"2022-05-05T09:27:38.686886Z","shell.execute_reply.started":"2022-05-05T09:27:38.686644Z","shell.execute_reply":"2022-05-05T09:27:38.686671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data= {\n    'ImageId':list(range(1,28001)),\n    'Label':final_label_list\n}\n\ndf = pd.DataFrame(data).set_index('ImageId')\ndf","metadata":{"execution":{"iopub.status.busy":"2022-05-05T09:27:38.689309Z","iopub.status.idle":"2022-05-05T09:27:38.689954Z","shell.execute_reply.started":"2022-05-05T09:27:38.689711Z","shell.execute_reply":"2022-05-05T09:27:38.689736Z"},"trusted":true},"execution_count":null,"outputs":[]}]}