{"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-09-23T17:17:35.408772Z","iopub.execute_input":"2022-09-23T17:17:35.409205Z","iopub.status.idle":"2022-09-23T17:17:35.440943Z","shell.execute_reply.started":"2022-09-23T17:17:35.409100Z","shell.execute_reply":"2022-09-23T17:17:35.439794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!apt install libarchive-dev\n!pip install libarchive","metadata":{"execution":{"iopub.status.busy":"2022-09-23T17:17:35.443162Z","iopub.execute_input":"2022-09-23T17:17:35.443874Z","iopub.status.idle":"2022-09-23T17:17:55.975642Z","shell.execute_reply.started":"2022-09-23T17:17:35.443837Z","shell.execute_reply":"2022-09-23T17:17:55.974412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import libarchive.public","metadata":{"execution":{"iopub.status.busy":"2022-09-23T17:17:55.978049Z","iopub.execute_input":"2022-09-23T17:17:55.978754Z","iopub.status.idle":"2022-09-23T17:17:56.041201Z","shell.execute_reply.started":"2022-09-23T17:17:55.978711Z","shell.execute_reply":"2022-09-23T17:17:56.039944Z"},"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 % 10000 == 0: print(cnt)","metadata":{"execution":{"iopub.status.busy":"2022-09-23T17:17:56.043019Z","iopub.execute_input":"2022-09-23T17:17:56.043407Z","iopub.status.idle":"2022-09-23T17:18:08.383664Z","shell.execute_reply.started":"2022-09-23T17:17:56.043370Z","shell.execute_reply":"2022-09-23T17:18:08.382597Z"},"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 % 10000 == 0: print(cnt)","metadata":{"execution":{"iopub.status.busy":"2022-09-23T17:18:08.386352Z","iopub.execute_input":"2022-09-23T17:18:08.387101Z","iopub.status.idle":"2022-09-23T17:19:21.333483Z","shell.execute_reply.started":"2022-09-23T17:18:08.387071Z","shell.execute_reply":"2022-09-23T17:19:21.332447Z"},"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":{"execution":{"iopub.status.busy":"2022-09-23T17:19:21.335046Z","iopub.execute_input":"2022-09-23T17:19:21.335430Z","iopub.status.idle":"2022-09-23T17:19:27.671688Z","shell.execute_reply.started":"2022-09-23T17:19:21.335392Z","shell.execute_reply":"2022-09-23T17:19:27.670634Z"},"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":{"execution":{"iopub.status.busy":"2022-09-23T17:19:27.673234Z","iopub.execute_input":"2022-09-23T17:19:27.673870Z","iopub.status.idle":"2022-09-23T17:19:27.713465Z","shell.execute_reply.started":"2022-09-23T17:19:27.673831Z","shell.execute_reply":"2022-09-23T17:19:27.712593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras_preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2022-09-23T17:19:27.715123Z","iopub.execute_input":"2022-09-23T17:19:27.715550Z","iopub.status.idle":"2022-09-23T17:19:27.720026Z","shell.execute_reply.started":"2022-09-23T17:19:27.715513Z","shell.execute_reply":"2022-09-23T17:19:27.719097Z"},"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":{"execution":{"iopub.status.busy":"2022-09-23T17:19:27.721408Z","iopub.execute_input":"2022-09-23T17:19:27.722389Z","iopub.status.idle":"2022-09-23T17:19:27.729674Z","shell.execute_reply.started":"2022-09-23T17:19:27.722353Z","shell.execute_reply":"2022-09-23T17:19:27.728462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['path'] = df['id']","metadata":{"execution":{"iopub.status.busy":"2022-09-23T17:19:27.731112Z","iopub.execute_input":"2022-09-23T17:19:27.731956Z","iopub.status.idle":"2022-09-23T17:19:27.748207Z","shell.execute_reply.started":"2022-09-23T17:19:27.731921Z","shell.execute_reply":"2022-09-23T17:19:27.747158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['path'] = df['path'].apply(append_ext)","metadata":{"execution":{"iopub.status.busy":"2022-09-23T17:19:27.754537Z","iopub.execute_input":"2022-09-23T17:19:27.754794Z","iopub.status.idle":"2022-09-23T17:19:27.784249Z","shell.execute_reply.started":"2022-09-23T17:19:27.754771Z","shell.execute_reply":"2022-09-23T17:19:27.783417Z"},"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":{"execution":{"iopub.status.busy":"2022-09-23T17:19:27.785444Z","iopub.execute_input":"2022-09-23T17:19:27.785861Z","iopub.status.idle":"2022-09-23T17:19:27.793228Z","shell.execute_reply.started":"2022-09-23T17:19:27.785826Z","shell.execute_reply":"2022-09-23T17:19:27.792324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bs=64","metadata":{"execution":{"iopub.status.busy":"2022-09-23T17:19:27.794779Z","iopub.execute_input":"2022-09-23T17:19:27.795131Z","iopub.status.idle":"2022-09-23T17:19:27.801970Z","shell.execute_reply.started":"2022-09-23T17:19:27.795097Z","shell.execute_reply":"2022-09-23T17:19:27.801033Z"},"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":{"execution":{"iopub.status.busy":"2022-09-23T17:19:27.803257Z","iopub.execute_input":"2022-09-23T17:19:27.803813Z","iopub.status.idle":"2022-09-23T17:19:28.558169Z","shell.execute_reply.started":"2022-09-23T17:19:27.803779Z","shell.execute_reply":"2022-09-23T17:19:28.557193Z"},"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":{"execution":{"iopub.status.busy":"2022-09-23T17:19:28.559560Z","iopub.execute_input":"2022-09-23T17:19:28.560002Z","iopub.status.idle":"2022-09-23T17:19:31.883553Z","shell.execute_reply.started":"2022-09-23T17:19:28.559965Z","shell.execute_reply":"2022-09-23T17:19:31.882598Z"},"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":{"execution":{"iopub.status.busy":"2022-09-23T17:19:31.884854Z","iopub.execute_input":"2022-09-23T17:19:31.885875Z","iopub.status.idle":"2022-09-23T17:19:31.900357Z","shell.execute_reply.started":"2022-09-23T17:19:31.885836Z","shell.execute_reply":"2022-09-23T17:19:31.899216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(\n    train_generator, \n    epochs=150,\n    steps_per_epoch=np.ceil(40000/bs) \n)","metadata":{"execution":{"iopub.status.busy":"2022-09-23T18:30:55.623352Z","iopub.execute_input":"2022-09-23T18:30:55.623660Z","iopub.status.idle":"2022-09-23T20:49:06.460580Z","shell.execute_reply.started":"2022-09-23T18:30:55.623632Z","shell.execute_reply":"2022-09-23T20:49:06.459429Z"},"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":{"execution":{"iopub.status.busy":"2022-09-23T20:49:33.303122Z","iopub.execute_input":"2022-09-23T20:49:33.303616Z","iopub.status.idle":"2022-09-23T20:49:45.740496Z","shell.execute_reply.started":"2022-09-23T20:49:33.303575Z","shell.execute_reply":"2022-09-23T20:49:45.739432Z"},"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":{"execution":{"iopub.status.busy":"2022-09-23T20:49:59.034236Z","iopub.execute_input":"2022-09-23T20:49:59.034605Z","iopub.status.idle":"2022-09-23T20:49:59.458776Z","shell.execute_reply.started":"2022-09-23T20:49:59.034572Z","shell.execute_reply":"2022-09-23T20:49:59.457770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.DataFrame(test_path, columns=['path'])","metadata":{"execution":{"iopub.status.busy":"2022-09-23T20:50:03.005434Z","iopub.execute_input":"2022-09-23T20:50:03.005823Z","iopub.status.idle":"2022-09-23T20:50:03.020611Z","shell.execute_reply.started":"2022-09-23T20:50:03.005791Z","shell.execute_reply":"2022-09-23T20:50:03.019512Z"},"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":{"execution":{"iopub.status.busy":"2022-09-23T20:50:05.474522Z","iopub.execute_input":"2022-09-23T20:50:05.475185Z","iopub.status.idle":"2022-09-23T20:50:08.040208Z","shell.execute_reply.started":"2022-09-23T20:50:05.475123Z","shell.execute_reply":"2022-09-23T20:50:08.039256Z"},"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":{"execution":{"iopub.status.busy":"2022-09-23T20:50:11.656306Z","iopub.execute_input":"2022-09-23T20:50:11.656970Z","iopub.status.idle":"2022-09-23T20:52:33.829252Z","shell.execute_reply.started":"2022-09-23T20:50:11.656934Z","shell.execute_reply":"2022-09-23T20:52:33.827969Z"},"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":{"execution":{"iopub.status.busy":"2022-09-23T20:52:34.030922Z","iopub.execute_input":"2022-09-23T20:52:34.032415Z","iopub.status.idle":"2022-09-23T20:52:34.120422Z","shell.execute_reply.started":"2022-09-23T20:52:34.032377Z","shell.execute_reply":"2022-09-23T20:52:34.119513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames=test_generator.filenames\nresults=pd.DataFrame({\"id\":filenames,\n                      \"label\":predictions})","metadata":{"execution":{"iopub.status.busy":"2022-09-23T20:52:39.126869Z","iopub.execute_input":"2022-09-23T20:52:39.127374Z","iopub.status.idle":"2022-09-23T20:52:39.215204Z","shell.execute_reply.started":"2022-09-23T20:52:39.127326Z","shell.execute_reply":"2022-09-23T20:52:39.214105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results['id'] = results['id'].map(lambda x: str(x)[:-4])","metadata":{"execution":{"iopub.status.busy":"2022-09-23T20:52:42.774211Z","iopub.execute_input":"2022-09-23T20:52:42.774567Z","iopub.status.idle":"2022-09-23T20:52:42.898289Z","shell.execute_reply.started":"2022-09-23T20:52:42.774536Z","shell.execute_reply":"2022-09-23T20:52:42.897283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results['id'] = results['id'].astype(int)\nresults = results.sort_values('id')","metadata":{"execution":{"iopub.status.busy":"2022-09-23T20:52:45.696830Z","iopub.execute_input":"2022-09-23T20:52:45.697209Z","iopub.status.idle":"2022-09-23T20:52:45.794077Z","shell.execute_reply.started":"2022-09-23T20:52:45.697174Z","shell.execute_reply":"2022-09-23T20:52:45.793195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-23T20:52:50.166347Z","iopub.execute_input":"2022-09-23T20:52:50.166925Z","iopub.status.idle":"2022-09-23T20:52:50.184546Z","shell.execute_reply.started":"2022-09-23T20:52:50.166889Z","shell.execute_reply":"2022-09-23T20:52:50.183585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = results.reset_index(drop=True)\nresults.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-23T20:52:54.556691Z","iopub.execute_input":"2022-09-23T20:52:54.557054Z","iopub.status.idle":"2022-09-23T20:52:54.571069Z","shell.execute_reply.started":"2022-09-23T20:52:54.557022Z","shell.execute_reply":"2022-09-23T20:52:54.570100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results.to_csv(\"Submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-23T20:52:59.374952Z","iopub.execute_input":"2022-09-23T20:52:59.375314Z","iopub.status.idle":"2022-09-23T20:52:59.668927Z","shell.execute_reply.started":"2022-09-23T20:52:59.375283Z","shell.execute_reply":"2022-09-23T20:52:59.667986Z"},"trusted":true},"execution_count":null,"outputs":[]}]}