{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2021-03-16T09:31:44.516142Z","iopub.status.busy":"2021-03-16T09:31:44.515598Z","iopub.status.idle":"2021-03-16T09:32:40.136749Z","shell.execute_reply":"2021-03-16T09:32:40.135872Z"},"papermill":{"duration":55.655726,"end_time":"2021-03-16T09:32:40.137023","exception":false,"start_time":"2021-03-16T09:31:44.481297","status":"completed"},"tags":[],"trusted":false},"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","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:32:40.490131Z","iopub.status.busy":"2021-03-16T09:32:40.489383Z","iopub.status.idle":"2021-03-16T09:32:45.890476Z","shell.execute_reply":"2021-03-16T09:32:45.889565Z"},"papermill":{"duration":5.560328,"end_time":"2021-03-16T09:32:45.890609","exception":false,"start_time":"2021-03-16T09:32:40.330281","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"%%time\nimport tensorflow as tf\nfrom keras_preprocessing.image import ImageDataGenerator\nfrom mpl_toolkits.axes_grid1 import ImageGrid\nimport itertools \nimport re\nimport matplotlib.pyplot as plt\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:32:46.048368Z","iopub.status.busy":"2021-03-16T09:32:46.047380Z","iopub.status.idle":"2021-03-16T09:32:46.051763Z","shell.execute_reply":"2021-03-16T09:32:46.052184Z"},"papermill":{"duration":0.088283,"end_time":"2021-03-16T09:32:46.052359","exception":false,"start_time":"2021-03-16T09:32:45.964076","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"%%time\ntrain_dir = '../input/dog-breed-identification/train'\ntest_dir ='../input/dog-breed-identification/test'","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:32:46.214602Z","iopub.status.busy":"2021-03-16T09:32:46.213934Z","iopub.status.idle":"2021-03-16T09:32:47.425650Z","shell.execute_reply":"2021-03-16T09:32:47.426234Z"},"papermill":{"duration":1.293039,"end_time":"2021-03-16T09:32:47.426459","exception":false,"start_time":"2021-03-16T09:32:46.133420","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"%%time\ndef append_ext(fn):\n    return fn+\".jpg\"\ntraindf = pd.read_csv('../input/dog-breed-identification/labels.csv',dtype=str)\ntestdf = pd.read_csv('../input/dog-breed-identification/sample_submission.csv',dtype=str)\ntraindf[\"id\"] = traindf[\"id\"].apply(append_ext)\ntestdf[\"id\"] = testdf[\"id\"].apply(append_ext)\n","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.067346,"end_time":"2021-03-16T09:32:47.562213","exception":false,"start_time":"2021-03-16T09:32:47.494867","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Training Labels"},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:32:47.711392Z","iopub.status.busy":"2021-03-16T09:32:47.710469Z","iopub.status.idle":"2021-03-16T09:32:47.718394Z","shell.execute_reply":"2021-03-16T09:32:47.718797Z"},"papermill":{"duration":0.089556,"end_time":"2021-03-16T09:32:47.718931","exception":false,"start_time":"2021-03-16T09:32:47.629375","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"traindf.head()","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.069751,"end_time":"2021-03-16T09:32:47.862674","exception":false,"start_time":"2021-03-16T09:32:47.792923","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Test id or Sample Submission"},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:32:48.037479Z","iopub.status.busy":"2021-03-16T09:32:48.036777Z","iopub.status.idle":"2021-03-16T09:32:48.044179Z","shell.execute_reply":"2021-03-16T09:32:48.043696Z"},"papermill":{"duration":0.108123,"end_time":"2021-03-16T09:32:48.044359","exception":false,"start_time":"2021-03-16T09:32:47.936236","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"testdf.head()","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.078375,"end_time":"2021-03-16T09:32:48.199375","exception":false,"start_time":"2021-03-16T09:32:48.121000","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Training Images"},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:32:48.359535Z","iopub.status.busy":"2021-03-16T09:32:48.358933Z","iopub.status.idle":"2021-03-16T09:32:49.295165Z","shell.execute_reply":"2021-03-16T09:32:49.295703Z"},"papermill":{"duration":1.019005,"end_time":"2021-03-16T09:32:49.295870","exception":false,"start_time":"2021-03-16T09:32:48.276865","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"\nsrc_path = \"../input/dog-breed-identification/train\"\nsub_class = os.listdir(src_path)\n\nfig = plt.figure(figsize=(10,5))\nfor e in range(len(sub_class[:8])):\n    plt.subplot(2,4,e+1)\n    img = plt.imread(os.path.join(src_path,sub_class[e]))\n    plt.imshow(img, cmap=plt.get_cmap('gray'))","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"papermill":{"duration":0.077916,"end_time":"2021-03-16T09:32:49.449639","exception":false,"start_time":"2021-03-16T09:32:49.371723","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Data preprocessing"},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:32:49.608091Z","iopub.status.busy":"2021-03-16T09:32:49.607256Z","iopub.status.idle":"2021-03-16T09:32:49.610896Z","shell.execute_reply":"2021-03-16T09:32:49.611551Z"},"papermill":{"duration":0.086104,"end_time":"2021-03-16T09:32:49.611740","exception":false,"start_time":"2021-03-16T09:32:49.525636","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"%%time\ntrain_datagen=ImageDataGenerator( rescale=1./255.,\n                                  #rotation_range = 20,\n                                  #brightness_range=[0.2,1.0],\n                                  #width_shift_range = 0.2,\n                                  #height_shift_range = 0.2,\n                                  #shear_range = 0.2,\n                                  #zoom_range = [0.7,1],\n                                  horizontal_flip = True,\n                                  #Setting validation split to 2% \n                                  validation_split=0.02 \n                                  )","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:32:49.771541Z","iopub.status.busy":"2021-03-16T09:32:49.770839Z","iopub.status.idle":"2021-03-16T09:32:49.773874Z","shell.execute_reply":"2021-03-16T09:32:49.773415Z"},"papermill":{"duration":0.085727,"end_time":"2021-03-16T09:32:49.773989","exception":false,"start_time":"2021-03-16T09:32:49.688262","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"BATCH_SIZE = 32","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:32:49.933861Z","iopub.status.busy":"2021-03-16T09:32:49.933030Z","iopub.status.idle":"2021-03-16T09:32:49.935818Z","shell.execute_reply":"2021-03-16T09:32:49.935304Z"},"papermill":{"duration":0.082779,"end_time":"2021-03-16T09:32:49.935933","exception":false,"start_time":"2021-03-16T09:32:49.853154","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"image_size=(299,299)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:32:50.117602Z","iopub.status.busy":"2021-03-16T09:32:50.114489Z","iopub.status.idle":"2021-03-16T09:33:00.889846Z","shell.execute_reply":"2021-03-16T09:33:00.889418Z"},"papermill":{"duration":10.880008,"end_time":"2021-03-16T09:33:00.889975","exception":false,"start_time":"2021-03-16T09:32:50.009967","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"train_generator=train_datagen.flow_from_dataframe(\ndataframe=traindf,\ndirectory=train_dir,\nx_col=\"id\",\ny_col=\"breed\",\nsubset=\"training\",\nbatch_size=BATCH_SIZE,\nseed=42,\nshuffle=True,\nclass_mode=\"categorical\",\ntarget_size=image_size,\ncolor_mode=\"rgb\" \n)","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.072348,"end_time":"2021-03-16T09:33:01.035402","exception":false,"start_time":"2021-03-16T09:33:00.963054","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Plotting Augmented images"},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:33:01.190288Z","iopub.status.busy":"2021-03-16T09:33:01.189613Z","iopub.status.idle":"2021-03-16T09:33:01.669970Z","shell.execute_reply":"2021-03-16T09:33:01.668835Z"},"papermill":{"duration":0.560374,"end_time":"2021-03-16T09:33:01.670112","exception":false,"start_time":"2021-03-16T09:33:01.109738","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"x,y = next(train_generator)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:33:01.821085Z","iopub.status.busy":"2021-03-16T09:33:01.820374Z","iopub.status.idle":"2021-03-16T09:33:01.824036Z","shell.execute_reply":"2021-03-16T09:33:01.824601Z"},"papermill":{"duration":0.081411,"end_time":"2021-03-16T09:33:01.824789","exception":false,"start_time":"2021-03-16T09:33:01.743378","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"print(type(x))\nprint(x.shape)\nprint(y.shape)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:33:01.979135Z","iopub.status.busy":"2021-03-16T09:33:01.978369Z","iopub.status.idle":"2021-03-16T09:33:01.981528Z","shell.execute_reply":"2021-03-16T09:33:01.981007Z"},"papermill":{"duration":0.084192,"end_time":"2021-03-16T09:33:01.981645","exception":false,"start_time":"2021-03-16T09:33:01.897453","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"def show_grid(image_list,nrows,ncols,figsize=(10,10),showaxis='off'):\n    if type(image_list) is not list:\n        if(image_list.shape[-1]==1):\n            image_list = [image_list[i,:,:,0] for i in range(image_list.shape[0])]\n        elif(image_list.shape[-1]==3):\n            image_list = [image_list[i,:,:,:] for i in range(image_list.shape[0])]\n    fig = plt.figure(None, figsize,frameon=False)\n    grid = ImageGrid(fig, 111,  # similar to subplot(111)\n                     nrows_ncols=(nrows, ncols),  # creates 2x2 grid of axes\n                     axes_pad=0.3,  # pad between axes in inch.\n                     share_all=True,\n                     )\n    for i in range(nrows*ncols):\n        ax = grid[i]\n        ax.imshow(image_list[i],cmap='Greys_r')  # The AxesGrid object work as a list of axes.\n        ax.axis('off')\n        ","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:33:02.150820Z","iopub.status.busy":"2021-03-16T09:33:02.149976Z","iopub.status.idle":"2021-03-16T09:33:05.537074Z","shell.execute_reply":"2021-03-16T09:33:05.537533Z"},"papermill":{"duration":3.483365,"end_time":"2021-03-16T09:33:05.537693","exception":false,"start_time":"2021-03-16T09:33:02.054328","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"%%time\nshow_grid(x,4,8,figsize=(25,25))\n","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.11594,"end_time":"2021-03-16T09:33:05.768226","exception":false,"start_time":"2021-03-16T09:33:05.652286","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Validation Data"},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:33:06.008650Z","iopub.status.busy":"2021-03-16T09:33:06.007554Z","iopub.status.idle":"2021-03-16T09:33:15.378458Z","shell.execute_reply":"2021-03-16T09:33:15.378875Z"},"papermill":{"duration":9.495002,"end_time":"2021-03-16T09:33:15.379030","exception":false,"start_time":"2021-03-16T09:33:05.884028","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"valid_generator=train_datagen.flow_from_dataframe(\ndataframe=traindf,\ndirectory=train_dir,\nx_col=\"id\",\ny_col=\"breed\",\nsubset=\"validation\",\nbatch_size=BATCH_SIZE,\nseed=42,\nshuffle=True,\nclass_mode=\"categorical\",\ntarget_size=image_size,\ncolor_mode=\"rgb\")\n","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.119729,"end_time":"2021-03-16T09:33:15.614947","exception":false,"start_time":"2021-03-16T09:33:15.495218","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Test Data"},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:33:15.859500Z","iopub.status.busy":"2021-03-16T09:33:15.858594Z","iopub.status.idle":"2021-03-16T09:33:27.308753Z","shell.execute_reply":"2021-03-16T09:33:27.309672Z"},"papermill":{"duration":11.578977,"end_time":"2021-03-16T09:33:27.309902","exception":false,"start_time":"2021-03-16T09:33:15.730925","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"test_datagen=ImageDataGenerator(rescale=1./255.)\n\ntest_generator=test_datagen.flow_from_dataframe(\ndataframe=testdf,\ndirectory=test_dir,\nx_col=\"id\",\ny_col=None,\nbatch_size=BATCH_SIZE,\nseed=42,\nshuffle=False,\nclass_mode=None,\ntarget_size=image_size,\ncolor_mode=\"rgb\")","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.193908,"end_time":"2021-03-16T09:33:27.700007","exception":false,"start_time":"2021-03-16T09:33:27.506099","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Pretrained model InceptionV3"},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:33:28.145628Z","iopub.status.busy":"2021-03-16T09:33:28.144586Z","iopub.status.idle":"2021-03-16T09:33:28.146478Z","shell.execute_reply":"2021-03-16T09:33:28.147083Z"},"papermill":{"duration":0.250457,"end_time":"2021-03-16T09:33:28.150355","exception":false,"start_time":"2021-03-16T09:33:27.899898","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"shape=[299,299,3]","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:33:28.629641Z","iopub.status.busy":"2021-03-16T09:33:28.628625Z","iopub.status.idle":"2021-03-16T09:33:35.236072Z","shell.execute_reply":"2021-03-16T09:33:35.234748Z"},"papermill":{"duration":6.840066,"end_time":"2021-03-16T09:33:35.236219","exception":false,"start_time":"2021-03-16T09:33:28.396153","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"pretrained_model = tf.keras.applications.InceptionV3(\n        weights='imagenet',\n        include_top=False ,\n        input_shape=shape\n    )\npretrained_model.trainable = False\n    \nmodel = tf.keras.Sequential([ \n        pretrained_model,   \n        tf.keras.layers.GlobalAveragePooling2D(),\n        #tf.keras.layers.Dense(256, activation='relu'),\n        #tf.keras.layers.Dropout(0.5),\n        tf.keras.layers.Dense(120, activation='softmax')\n    ])","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:33:35.490015Z","iopub.status.busy":"2021-03-16T09:33:35.489119Z","iopub.status.idle":"2021-03-16T09:33:35.521586Z","shell.execute_reply":"2021-03-16T09:33:35.522120Z"},"papermill":{"duration":0.163249,"end_time":"2021-03-16T09:33:35.522328","exception":false,"start_time":"2021-03-16T09:33:35.359079","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"#opt = tf.keras.optimizers.Adam(learning_rate=0.001)\nopt=tf.keras.optimizers.SGD(lr=1e-3, momentum=0.9)\nmodel.compile(optimizer = opt ,\n              loss=\"categorical_crossentropy\",\n              metrics=[\"accuracy\"])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.124455,"end_time":"2021-03-16T09:33:35.769389","exception":false,"start_time":"2021-03-16T09:33:35.644934","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Early stopping"},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:33:36.029374Z","iopub.status.busy":"2021-03-16T09:33:36.027498Z","iopub.status.idle":"2021-03-16T09:33:36.029928Z","shell.execute_reply":"2021-03-16T09:33:36.030328Z"},"papermill":{"duration":0.135362,"end_time":"2021-03-16T09:33:36.030480","exception":false,"start_time":"2021-03-16T09:33:35.895118","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"#reduce = tf.keras.callbacks.ReduceLROnPlateau( monitor='val_loss',factor=0.2,patience=5, min_lr=0.001 )\n\nearly = tf.keras.callbacks.EarlyStopping( patience=2,\n                                          min_delta=0.001,\n                                          restore_best_weights=True)","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.127027,"end_time":"2021-03-16T09:33:36.282308","exception":false,"start_time":"2021-03-16T09:33:36.155281","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Fitting the Model:"},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:33:36.541568Z","iopub.status.busy":"2021-03-16T09:33:36.540990Z","iopub.status.idle":"2021-03-16T09:56:27.697239Z","shell.execute_reply":"2021-03-16T09:56:27.696126Z"},"papermill":{"duration":1371.290808,"end_time":"2021-03-16T09:56:27.697405","exception":false,"start_time":"2021-03-16T09:33:36.406597","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"STEP_SIZE_TRAIN = train_generator.n//train_generator.batch_size\nSTEP_SIZE_VALID = valid_generator.n//valid_generator.batch_size\nSTEP_SIZE_TEST = test_generator.n//test_generator.batch_size\nhistory = model.fit(train_generator,\n                    steps_per_epoch=STEP_SIZE_TRAIN,\n                    validation_data=valid_generator,\n                    validation_steps=STEP_SIZE_VALID,\n                    epochs=25,\n                    callbacks=[early],)","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":1.585122,"end_time":"2021-03-16T09:56:30.832249","exception":false,"start_time":"2021-03-16T09:56:29.247127","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Loss and Accuracy Curves"},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:56:33.990695Z","iopub.status.busy":"2021-03-16T09:56:33.989988Z","iopub.status.idle":"2021-03-16T09:56:33.993889Z","shell.execute_reply":"2021-03-16T09:56:33.993438Z"},"papermill":{"duration":1.611987,"end_time":"2021-03-16T09:56:33.994009","exception":false,"start_time":"2021-03-16T09:56:32.382022","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"def display_training_curves(training, validation, title, subplot):\n    if subplot%10==1: # set up the subplots on the first call\n        plt.subplots(figsize=(10,10), facecolor='#F0F0F0')\n        plt.tight_layout()\n    ax = plt.subplot(subplot)\n    ax.set_facecolor('#F8F8F8')\n    ax.plot(training)\n    ax.plot(validation)\n    ax.set_title('MODEL '+ title)\n    ax.set_ylabel(title)\n    #ax.set_ylim(0.28,1.05)\n    ax.set_xlabel('epoch')\n    ax.legend(['train', 'valid.'])","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:56:37.424691Z","iopub.status.busy":"2021-03-16T09:56:37.423902Z","iopub.status.idle":"2021-03-16T09:56:37.635699Z","shell.execute_reply":"2021-03-16T09:56:37.636112Z"},"papermill":{"duration":1.833766,"end_time":"2021-03-16T09:56:37.636276","exception":false,"start_time":"2021-03-16T09:56:35.802510","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"display_training_curves(\n    history.history['loss'],\n    history.history['val_loss'],\n    'LOSS',\n    211,\n)\n","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:56:40.756615Z","iopub.status.busy":"2021-03-16T09:56:40.755397Z","iopub.status.idle":"2021-03-16T09:56:40.938974Z","shell.execute_reply":"2021-03-16T09:56:40.939405Z"},"papermill":{"duration":1.750921,"end_time":"2021-03-16T09:56:40.939555","exception":false,"start_time":"2021-03-16T09:56:39.188634","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"display_training_curves(\n    history.history['accuracy'],\n    history.history['val_accuracy'],\n    'ACCURACY',\n    211,\n)\n","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":1.586237,"end_time":"2021-03-16T09:56:44.073088","exception":false,"start_time":"2021-03-16T09:56:42.486851","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Accuracy"},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:56:47.600513Z","iopub.status.busy":"2021-03-16T09:56:47.599920Z","iopub.status.idle":"2021-03-16T09:56:49.657515Z","shell.execute_reply":"2021-03-16T09:56:49.658312Z"},"papermill":{"duration":3.768052,"end_time":"2021-03-16T09:56:49.658514","exception":false,"start_time":"2021-03-16T09:56:45.890462","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"score = model.evaluate(valid_generator,batch_size=32)\nprint(\"Accuracy: {:.2f}%\".format(score[1] * 100)) \nprint(\"Loss: \",score[0])\n\n","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:56:52.819617Z","iopub.status.busy":"2021-03-16T09:56:52.818886Z","iopub.status.idle":"2021-03-16T09:56:53.416935Z","shell.execute_reply":"2021-03-16T09:56:53.416436Z"},"papermill":{"duration":2.167603,"end_time":"2021-03-16T09:56:53.417060","exception":false,"start_time":"2021-03-16T09:56:51.249457","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"from sklearn.metrics import f1_score","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:56:57.295491Z","iopub.status.busy":"2021-03-16T09:56:57.294918Z","iopub.status.idle":"2021-03-16T09:57:00.133526Z","shell.execute_reply":"2021-03-16T09:57:00.133018Z"},"papermill":{"duration":4.679627,"end_time":"2021-03-16T09:57:00.133669","exception":false,"start_time":"2021-03-16T09:56:55.454042","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"Y_pred = model.predict(valid_generator)\ny_pred = np.argmax(Y_pred, axis=1)","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":1.544542,"end_time":"2021-03-16T09:57:03.225581","exception":false,"start_time":"2021-03-16T09:57:01.681039","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# F1 Score"},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:57:06.404837Z","iopub.status.busy":"2021-03-16T09:57:06.404069Z","iopub.status.idle":"2021-03-16T09:57:06.411833Z","shell.execute_reply":"2021-03-16T09:57:06.411317Z"},"papermill":{"duration":1.608589,"end_time":"2021-03-16T09:57:06.411948","exception":false,"start_time":"2021-03-16T09:57:04.803359","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"f_score = f1_score(valid_generator.classes,y_pred,average='macro')\nprint('F1 score:',f_score)\n","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":1.564165,"end_time":"2021-03-16T09:57:09.828712","exception":false,"start_time":"2021-03-16T09:57:08.264547","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Predicting Test Images"},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:57:13.006026Z","iopub.status.busy":"2021-03-16T09:57:13.005425Z","iopub.status.idle":"2021-03-16T09:59:12.366289Z","shell.execute_reply":"2021-03-16T09:59:12.365468Z"},"papermill":{"duration":120.933496,"end_time":"2021-03-16T09:59:12.366436","exception":false,"start_time":"2021-03-16T09:57:11.432940","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"pred=model.predict(test_generator)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:59:15.480177Z","iopub.status.busy":"2021-03-16T09:59:15.479433Z","iopub.status.idle":"2021-03-16T09:59:15.775086Z","shell.execute_reply":"2021-03-16T09:59:15.775521Z"},"papermill":{"duration":1.86035,"end_time":"2021-03-16T09:59:15.775674","exception":false,"start_time":"2021-03-16T09:59:13.915324","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"df_submission = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')\ndf_submission.head()","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:59:18.963551Z","iopub.status.busy":"2021-03-16T09:59:18.953403Z","iopub.status.idle":"2021-03-16T09:59:18.975987Z","shell.execute_reply":"2021-03-16T09:59:18.975560Z"},"papermill":{"duration":1.603427,"end_time":"2021-03-16T09:59:18.976113","exception":false,"start_time":"2021-03-16T09:59:17.372686","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"file_list = test_generator.filenames\nid_list = []\nfor name in file_list:\n    m = re.sub('test/', '', name)\n    m = re.sub('.jpg', '', m)\n    id_list.append(m)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:59:22.453616Z","iopub.status.busy":"2021-03-16T09:59:22.452771Z","iopub.status.idle":"2021-03-16T09:59:22.931289Z","shell.execute_reply":"2021-03-16T09:59:22.931705Z"},"papermill":{"duration":2.086982,"end_time":"2021-03-16T09:59:22.931854","exception":false,"start_time":"2021-03-16T09:59:20.844872","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"df_submission['id'] = id_list\ndf_submission.iloc[:,1:] = pred\ndf_submission.head()","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":1.600498,"end_time":"2021-03-16T09:59:26.079334","exception":false,"start_time":"2021-03-16T09:59:24.478836","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Prediction Score for selected breeds"},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:59:29.240393Z","iopub.status.busy":"2021-03-16T09:59:29.239547Z","iopub.status.idle":"2021-03-16T09:59:29.241866Z","shell.execute_reply":"2021-03-16T09:59:29.242340Z"},"papermill":{"duration":1.601664,"end_time":"2021-03-16T09:59:29.242494","exception":false,"start_time":"2021-03-16T09:59:27.640830","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"breeds=['id','beagle','chihuahua','doberman','french_bulldog', 'golden_retriever', 'malamute','pug','saint_bernard','scottish_deerhound','tibetan_mastiff']","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:59:32.606295Z","iopub.status.busy":"2021-03-16T09:59:32.605411Z","iopub.status.idle":"2021-03-16T09:59:32.607564Z","shell.execute_reply":"2021-03-16T09:59:32.608042Z"},"papermill":{"duration":1.56359,"end_time":"2021-03-16T09:59:32.608185","exception":false,"start_time":"2021-03-16T09:59:31.044595","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"selected_breeds = df_submission.loc[:,breeds]","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:59:35.769374Z","iopub.status.busy":"2021-03-16T09:59:35.768396Z","iopub.status.idle":"2021-03-16T09:59:35.772949Z","shell.execute_reply":"2021-03-16T09:59:35.773362Z"},"papermill":{"duration":1.618625,"end_time":"2021-03-16T09:59:35.773505","exception":false,"start_time":"2021-03-16T09:59:34.154880","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"print('Breeds and prediction score')\nselected_breeds.head()","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":1.565125,"end_time":"2021-03-16T09:59:38.920720","exception":false,"start_time":"2021-03-16T09:59:37.355595","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Final Submission to Competition"},{"metadata":{"execution":{"iopub.execute_input":"2021-03-16T09:59:42.319720Z","iopub.status.busy":"2021-03-16T09:59:42.318909Z","iopub.status.idle":"2021-03-16T09:59:45.672000Z","shell.execute_reply":"2021-03-16T09:59:45.673089Z"},"papermill":{"duration":5.155401,"end_time":"2021-03-16T09:59:45.673316","exception":false,"start_time":"2021-03-16T09:59:40.517915","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"final_sub = df_submission.set_index('id')\nfinal_sub.to_csv('Submission.csv')","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}