{"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":"!apt install libarchive-dev\n!pip install libarchive","metadata":{"id":"Ey555X8m3Z_j","outputId":"d7488e3d-040f-4e6b-b297-971fe026b92b","execution":{"iopub.status.busy":"2022-09-02T07:36:09.139592Z","iopub.execute_input":"2022-09-02T07:36:09.140457Z","iopub.status.idle":"2022-09-02T07:36:28.721768Z","shell.execute_reply.started":"2022-09-02T07:36:09.139938Z","shell.execute_reply":"2022-09-02T07:36:28.720611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import libarchive.public","metadata":{"id":"RKomRawb3aCr","execution":{"iopub.status.busy":"2022-09-02T07:36:28.725277Z","iopub.execute_input":"2022-09-02T07:36:28.725896Z","iopub.status.idle":"2022-09-02T07:36:28.784051Z","shell.execute_reply.started":"2022-09-02T07:36:28.725862Z","shell.execute_reply":"2022-09-02T07:36:28.782936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnt = 0\n\nfor entry in libarchive.public.file_pour('../input/cifar-10/train.7z'):\n    cnt += 1\n    if cnt % 1000 == 0: print(cnt)","metadata":{"execution":{"iopub.status.busy":"2022-09-02T07:36:28.785863Z","iopub.execute_input":"2022-09-02T07:36:28.786214Z","iopub.status.idle":"2022-09-02T07:36:40.823726Z","shell.execute_reply.started":"2022-09-02T07:36:28.786176Z","shell.execute_reply":"2022-09-02T07:36:40.822426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnt = 0\nfor entry in libarchive.public.file_pour('../input/cifar-10/test.7z'):\n    cnt += 1\n    if cnt % 1000 == 0: print(cnt)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-02T07:36:40.825438Z","iopub.execute_input":"2022-09-02T07:36:40.825804Z","iopub.status.idle":"2022-09-02T07:37:53.891753Z","shell.execute_reply.started":"2022-09-02T07:36:40.825768Z","shell.execute_reply":"2022-09-02T07:37:53.889693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\nfrom tensorflow.keras.datasets import cifar10\nfrom tensorflow.keras.layers import Input, Conv2D, Dense, Dropout, MaxPool2D, Flatten, BatchNormalization\nfrom tensorflow.keras import Model, Sequential\nfrom tensorflow.keras.optimizers import Adam\n\nfrom tensorflow.keras.optimizers.schedules import ExponentialDecay\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import ModelCheckpoint\nfrom keras.preprocessing import image","metadata":{"id":"pXcNJiym5Vn9","execution":{"iopub.status.busy":"2022-09-02T07:37:53.895979Z","iopub.execute_input":"2022-09-02T07:37:53.896626Z","iopub.status.idle":"2022-09-02T07:37:59.347298Z","shell.execute_reply.started":"2022-09-02T07:37:53.896587Z","shell.execute_reply":"2022-09-02T07:37:59.346292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ndf = pd.read_csv('../input/cifar-10/trainLabels.csv')\n#train labels contain the labels of training data and id of image/labels","metadata":{"id":"uS0JTvmm5dc8","execution":{"iopub.status.busy":"2022-09-02T07:37:59.348555Z","iopub.execute_input":"2022-09-02T07:37:59.349222Z","iopub.status.idle":"2022-09-02T07:37:59.380317Z","shell.execute_reply.started":"2022-09-02T07:37:59.349191Z","shell.execute_reply":"2022-09-02T07:37:59.379409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras_preprocessing.image import ImageDataGenerator","metadata":{"id":"F7Tk4i6GAKxL","execution":{"iopub.status.busy":"2022-09-02T07:37:59.381963Z","iopub.execute_input":"2022-09-02T07:37:59.382348Z","iopub.status.idle":"2022-09-02T07:37:59.387478Z","shell.execute_reply.started":"2022-09-02T07:37:59.382311Z","shell.execute_reply":"2022-09-02T07:37:59.386230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def append_ext(fn):\n    return \"/kaggle/working/train/\"+str(fn)+'.png'\n\n    # we have just id's like 1, 2,.... we adding the extension of image to make the imgae full name and path","metadata":{"id":"7KcaQq5PAMrN","execution":{"iopub.status.busy":"2022-09-02T07:37:59.389286Z","iopub.execute_input":"2022-09-02T07:37:59.389982Z","iopub.status.idle":"2022-09-02T07:37:59.410752Z","shell.execute_reply.started":"2022-09-02T07:37:59.389939Z","shell.execute_reply":"2022-09-02T07:37:59.408741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['path'] = df['id']","metadata":{"id":"lar4ZVOHM0pP","execution":{"iopub.status.busy":"2022-09-02T07:37:59.412342Z","iopub.execute_input":"2022-09-02T07:37:59.413344Z","iopub.status.idle":"2022-09-02T07:37:59.441506Z","shell.execute_reply.started":"2022-09-02T07:37:59.413303Z","shell.execute_reply":"2022-09-02T07:37:59.439067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['path'] = df['path'].apply(append_ext)","metadata":{"id":"7cQLNi4WA2xl","execution":{"iopub.status.busy":"2022-09-02T07:37:59.442637Z","iopub.execute_input":"2022-09-02T07:37:59.442891Z","iopub.status.idle":"2022-09-02T07:37:59.501322Z","shell.execute_reply.started":"2022-09-02T07:37:59.442867Z","shell.execute_reply":"2022-09-02T07:37:59.500486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen=ImageDataGenerator(rotation_range=15,width_shift_range=4,height_shift_range=4, validation_split=0.2, rescale=1./255.)","metadata":{"id":"KtbmGFCGBOC4","execution":{"iopub.status.busy":"2022-09-02T07:37:59.502724Z","iopub.execute_input":"2022-09-02T07:37:59.503055Z","iopub.status.idle":"2022-09-02T07:37:59.508272Z","shell.execute_reply.started":"2022-09-02T07:37:59.503022Z","shell.execute_reply":"2022-09-02T07:37:59.506682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bs=128","metadata":{"id":"aURpc9zvcdaB","execution":{"iopub.status.busy":"2022-09-02T07:37:59.510197Z","iopub.execute_input":"2022-09-02T07:37:59.510949Z","iopub.status.idle":"2022-09-02T07:37:59.517340Z","shell.execute_reply.started":"2022-09-02T07:37:59.510849Z","shell.execute_reply":"2022-09-02T07:37:59.516333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator=datagen.flow_from_dataframe(\ndataframe=df,\nx_col=\"path\",\ny_col=\"label\",\nbatch_size=bs,\nseed=42,\nsubset = 'training',\nshuffle=True,\nclass_mode=\"categorical\",\ntarget_size =  (32,32))\nvalid_generator=datagen.flow_from_dataframe(\ndataframe=df,\nx_col=\"path\",\ny_col=\"label\",\nbatch_size=bs,\nseed=42,\nsubset = 'validation',\nshuffle=True,\nclass_mode=\"categorical\",\ntarget_size =  (32,32))","metadata":{"id":"b0N5mXy5Bn6E","outputId":"dd4f53c1-4aed-4351-84c4-b7b22e16779b","execution":{"iopub.status.busy":"2022-09-02T07:37:59.518953Z","iopub.execute_input":"2022-09-02T07:37:59.519410Z","iopub.status.idle":"2022-09-02T07:38:00.446333Z","shell.execute_reply.started":"2022-09-02T07:37:59.519375Z","shell.execute_reply":"2022-09-02T07:38:00.445308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add( Conv2D(filters=64, kernel_size=(3,3), activation='relu', padding='same', input_shape=(32, 32, 3)))\nmodel.add( BatchNormalization())\nmodel.add( Conv2D(filters=64, kernel_size=(3,3), activation='relu', padding='same'))\nmodel.add( BatchNormalization())\nmodel.add( Conv2D(filters=64, kernel_size=5, strides=2, activation='relu', padding='same'))\nmodel.add( BatchNormalization())\nmodel.add( Dropout(0.2))\n\nmodel.add( Conv2D(filters=128, kernel_size=(3,3), activation='relu', padding='same'))\nmodel.add( BatchNormalization())\nmodel.add( Conv2D(filters=128, kernel_size=(3,3), activation='relu', padding='same'))\nmodel.add( BatchNormalization())\nmodel.add( Conv2D(filters=128, kernel_size=5, strides=2, activation='relu', padding='same'))\nmodel.add( BatchNormalization())\nmodel.add( Dropout(0.3))\n\nmodel.add( Conv2D(filters=256, kernel_size=(3,3), activation='relu', padding='same'))\nmodel.add( BatchNormalization())\nmodel.add( Conv2D(filters=256, kernel_size=(3,3), activation='relu', padding='same'))\nmodel.add( BatchNormalization())\nmodel.add( Conv2D(filters=256, kernel_size=5, strides=2, activation='relu', padding='same'))\nmodel.add( BatchNormalization())\nmodel.add( Dropout(0.4))\n\nmodel.add( Flatten())\nmodel.add( Dense(512, activation='relu'))\nmodel.add( BatchNormalization())\nmodel.add( Dropout(0.5))\nmodel.add( Dense(10, activation='softmax'))","metadata":{"id":"JRtU5hJ_uKRe","execution":{"iopub.status.busy":"2022-09-02T07:38:00.454281Z","iopub.execute_input":"2022-09-02T07:38:00.459675Z","iopub.status.idle":"2022-09-02T07:38:03.942386Z","shell.execute_reply.started":"2022-09-02T07:38:00.459621Z","shell.execute_reply":"2022-09-02T07:38:03.941459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LEARN_RATE = 0.003\nfrom tensorflow.keras.optimizers import Adam\nmodel.compile(optimizer = Adam(lr = LEARN_RATE), loss = 'categorical_crossentropy',\n                           metrics = ['categorical_accuracy'])","metadata":{"id":"JDdBJ9H2OEBw","execution":{"iopub.status.busy":"2022-09-02T07:38:03.944018Z","iopub.execute_input":"2022-09-02T07:38:03.944417Z","iopub.status.idle":"2022-09-02T07:38:03.960304Z","shell.execute_reply.started":"2022-09-02T07:38:03.944378Z","shell.execute_reply":"2022-09-02T07:38:03.959255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(\n    train_generator, \n    epochs=20,\n    steps_per_epoch=np.ceil(40000/bs) \n)","metadata":{"id":"II7ZJXGgHaPW","outputId":"a66a54ea-211b-41cc-d938-8fb40ca0f245","execution":{"iopub.status.busy":"2022-09-02T07:38:03.961623Z","iopub.execute_input":"2022-09-02T07:38:03.962584Z","iopub.status.idle":"2022-09-02T07:51:08.837196Z","shell.execute_reply.started":"2022-09-02T07:38:03.962549Z","shell.execute_reply":"2022-09-02T07:51:08.836142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# New Section","metadata":{"id":"rwF9XXkxN_04"}},{"cell_type":"code","source":"# cnt =0\n# for entry in libarchive.public.file_pour('/content/sampleSubmission.csv.zip'):\n#     cnt += 1\n#     if cnt % 1000 == 0: print(cnt)","metadata":{"id":"id83tzU0Thq4","execution":{"iopub.status.busy":"2022-09-02T07:51:08.838877Z","iopub.execute_input":"2022-09-02T07:51:08.839246Z","iopub.status.idle":"2022-09-02T07:51:08.845433Z","shell.execute_reply.started":"2022-09-02T07:51:08.839210Z","shell.execute_reply":"2022-09-02T07:51:08.844160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = model.evaluate_generator(generator=valid_generator,\nsteps=np.ceil(10000/bs))\nprint(score)","metadata":{"id":"ghfD2WR41qQk","outputId":"8ca0dea9-8829-49f3-e86a-57ddefc4a335","execution":{"iopub.status.busy":"2022-09-02T07:51:08.846763Z","iopub.execute_input":"2022-09-02T07:51:08.847706Z","iopub.status.idle":"2022-09-02T07:51:18.752543Z","shell.execute_reply.started":"2022-09-02T07:51:08.847658Z","shell.execute_reply":"2022-09-02T07:51:18.751426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os \ntest_path =[]\ndef load_images(folder_name):\n  for file in os.listdir(folder_name):\n    base = os.path.basename(file)\n    test_path.append(base)\n    \nfolder_name = '/kaggle/working/test/'\nload_images(folder_name)","metadata":{"id":"TCYth9WqxjX-","execution":{"iopub.status.busy":"2022-09-02T07:51:18.754283Z","iopub.execute_input":"2022-09-02T07:51:18.755268Z","iopub.status.idle":"2022-09-02T07:51:19.173054Z","shell.execute_reply.started":"2022-09-02T07:51:18.755230Z","shell.execute_reply":"2022-09-02T07:51:19.171915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.DataFrame(test_path, columns=['path'])","metadata":{"id":"vTfZNjIPyHUB","execution":{"iopub.status.busy":"2022-09-02T07:51:19.175439Z","iopub.execute_input":"2022-09-02T07:51:19.175826Z","iopub.status.idle":"2022-09-02T07:51:19.194462Z","shell.execute_reply.started":"2022-09-02T07:51:19.175785Z","shell.execute_reply":"2022-09-02T07:51:19.193323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"id":"_mKPflLhyfWw","outputId":"d85a1575-5590-414b-c6f1-0f225c1e7120","execution":{"iopub.status.busy":"2022-09-02T07:51:19.195968Z","iopub.execute_input":"2022-09-02T07:51:19.198221Z","iopub.status.idle":"2022-09-02T07:51:19.221225Z","shell.execute_reply.started":"2022-09-02T07:51:19.197918Z","shell.execute_reply":"2022-09-02T07:51:19.220264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testgen = ImageDataGenerator(rescale=(1./255.))\ntest_generator=testgen.flow_from_dataframe(\ndataframe=test_df,\ndirectory=\"/kaggle/working/test\",\nx_col=\"path\",\ny_col = None,\nclass_mode = None,\nbatch_size=bs,\nseed=42,\nshuffle=False,\ntarget_size =  (32,32))","metadata":{"id":"pqGpAcGP8Evz","outputId":"5a2720c0-ced5-44eb-b6b9-fe27d930ada8","execution":{"iopub.status.busy":"2022-09-02T07:51:19.222812Z","iopub.execute_input":"2022-09-02T07:51:19.223505Z","iopub.status.idle":"2022-09-02T07:51:21.750895Z","shell.execute_reply.started":"2022-09-02T07:51:19.223467Z","shell.execute_reply":"2022-09-02T07:51:21.749859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.load_weights(weight_path)","metadata":{"id":"35yDvNLLzJre","execution":{"iopub.status.busy":"2022-09-02T07:51:21.752536Z","iopub.execute_input":"2022-09-02T07:51:21.752930Z","iopub.status.idle":"2022-09-02T07:51:21.758839Z","shell.execute_reply.started":"2022-09-02T07:51:21.752892Z","shell.execute_reply":"2022-09-02T07:51:21.757768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator.reset()\npred=model.predict_generator(test_generator,\nsteps=np.ceil(len(test_path)/bs),\nverbose=1)","metadata":{"id":"3KYweXU5-qR-","outputId":"e83a53b4-a820-425b-e059-9a030df7c1f7","execution":{"iopub.status.busy":"2022-09-02T07:51:21.760154Z","iopub.execute_input":"2022-09-02T07:51:21.761064Z","iopub.status.idle":"2022-09-02T07:53:02.594527Z","shell.execute_reply.started":"2022-09-02T07:51:21.761026Z","shell.execute_reply":"2022-09-02T07:53:02.593290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_class_indices=np.argmax(pred,axis=1)\nlabels = (train_generator.class_indices)\nlabels = dict((v,k) for k,v in labels.items())\npredictions = [labels[k] for k in predicted_class_indices]","metadata":{"id":"SXq6HgSz_bvC","execution":{"iopub.status.busy":"2022-09-02T07:53:02.597912Z","iopub.execute_input":"2022-09-02T07:53:02.598905Z","iopub.status.idle":"2022-09-02T07:53:02.685272Z","shell.execute_reply.started":"2022-09-02T07:53:02.598864Z","shell.execute_reply":"2022-09-02T07:53:02.684393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames=test_generator.filenames\nresults=pd.DataFrame({\"id\":filenames,\n                      \"label\":predictions})","metadata":{"id":"gpmE8M_FAGO8","execution":{"iopub.status.busy":"2022-09-02T07:53:02.687450Z","iopub.execute_input":"2022-09-02T07:53:02.688081Z","iopub.status.idle":"2022-09-02T07:53:02.722196Z","shell.execute_reply.started":"2022-09-02T07:53:02.688046Z","shell.execute_reply":"2022-09-02T07:53:02.721306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results['id'] = results['id'].map(lambda x: str(x)[:-4])","metadata":{"id":"uhIDvNrPMiv8","execution":{"iopub.status.busy":"2022-09-02T07:53:02.724335Z","iopub.execute_input":"2022-09-02T07:53:02.725040Z","iopub.status.idle":"2022-09-02T07:53:02.834073Z","shell.execute_reply.started":"2022-09-02T07:53:02.725002Z","shell.execute_reply":"2022-09-02T07:53:02.833097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results['id'] = results['id'].astype(int)\nresults = results.sort_values('id')","metadata":{"id":"vnx67GQzPE7z","execution":{"iopub.status.busy":"2022-09-02T07:53:02.835754Z","iopub.execute_input":"2022-09-02T07:53:02.836145Z","iopub.status.idle":"2022-09-02T07:53:02.928502Z","shell.execute_reply.started":"2022-09-02T07:53:02.836109Z","shell.execute_reply":"2022-09-02T07:53:02.927406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results.head()","metadata":{"id":"83ZQTPuaWO0m","outputId":"8c176029-cb8c-416f-9548-d2fff14de942","execution":{"iopub.status.busy":"2022-09-02T07:53:02.930049Z","iopub.execute_input":"2022-09-02T07:53:02.930454Z","iopub.status.idle":"2022-09-02T07:53:02.940009Z","shell.execute_reply.started":"2022-09-02T07:53:02.930417Z","shell.execute_reply":"2022-09-02T07:53:02.939069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = results.reset_index(drop=True)\nresults.head()\n","metadata":{"id":"kXEz6jpxN2sj","outputId":"943d7d4e-f169-4302-f957-20b0e2050b95","execution":{"iopub.status.busy":"2022-09-02T07:53:02.941459Z","iopub.execute_input":"2022-09-02T07:53:02.942086Z","iopub.status.idle":"2022-09-02T07:53:02.955588Z","shell.execute_reply.started":"2022-09-02T07:53:02.942048Z","shell.execute_reply":"2022-09-02T07:53:02.954313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results.to_csv(\"Submission.csv\", index=False)\n","metadata":{"id":"h05NF0tOSyiS","execution":{"iopub.status.busy":"2022-09-02T07:53:02.957061Z","iopub.execute_input":"2022-09-02T07:53:02.957956Z","iopub.status.idle":"2022-09-02T07:53:03.309593Z","shell.execute_reply.started":"2022-09-02T07:53:02.957921Z","shell.execute_reply":"2022-09-02T07:53:03.308588Z"},"trusted":true},"execution_count":null,"outputs":[]}]}