{"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":"%%time\nimport tensorflow as tf\nimport pandas as pd\nimport numpy as np \nfrom keras_preprocessing.image import ImageDataGenerator\nfrom mpl_toolkits.axes_grid1 import ImageGrid\nimport itertools \nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"papermill":{"duration":5.207411,"end_time":"2021-03-16T10:35:58.886428","exception":false,"start_time":"2021-03-16T10:35:53.679017","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:53:15.137279Z","iopub.execute_input":"2021-06-13T08:53:15.137659Z","iopub.status.idle":"2021-06-13T08:53:15.145302Z","shell.execute_reply.started":"2021-06-13T08:53:15.137627Z","shell.execute_reply":"2021-06-13T08:53:15.144283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntrain_dir = '../input/dog-breed-identification/train'\ntest_dir ='../input/dog-breed-identification/test'","metadata":{"papermill":{"duration":0.084354,"end_time":"2021-03-16T10:35:59.040784","exception":false,"start_time":"2021-03-16T10:35:58.95643","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:53:15.193820Z","iopub.execute_input":"2021-06-13T08:53:15.194135Z","iopub.status.idle":"2021-06-13T08:53:15.199881Z","shell.execute_reply.started":"2021-06-13T08:53:15.194107Z","shell.execute_reply":"2021-06-13T08:53:15.198794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def append_ext(fn):\n    return fn+\".jpg\"","metadata":{"execution":{"iopub.status.busy":"2021-06-13T08:53:15.240174Z","iopub.execute_input":"2021-06-13T08:53:15.240490Z","iopub.status.idle":"2021-06-13T08:53:15.246816Z","shell.execute_reply.started":"2021-06-13T08:53:15.240463Z","shell.execute_reply":"2021-06-13T08:53:15.245761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n# def 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)\n\n","metadata":{"papermill":{"duration":0.726751,"end_time":"2021-03-16T10:35:59.838551","exception":false,"start_time":"2021-03-16T10:35:59.1118","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:53:15.278292Z","iopub.execute_input":"2021-06-13T08:53:15.278586Z","iopub.status.idle":"2021-06-13T08:53:15.752508Z","shell.execute_reply.started":"2021-06-13T08:53:15.278557Z","shell.execute_reply":"2021-06-13T08:53:15.751373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindf[\"id\"] = traindf[\"id\"].apply(append_ext)\ntestdf[\"id\"] = testdf[\"id\"].apply(append_ext)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T08:53:15.757456Z","iopub.execute_input":"2021-06-13T08:53:15.759709Z","iopub.status.idle":"2021-06-13T08:53:15.782478Z","shell.execute_reply.started":"2021-06-13T08:53:15.759665Z","shell.execute_reply":"2021-06-13T08:53:15.781426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"included_breed = ['beagle', 'chihuahua', 'doberman',\n'french_bulldog', 'golden_retriever', 'malamute', 'pug', 'saint_bernard', 'scottish_deerhound',\n'tibetan_mastiff']\nfor index,breed in traindf.iterrows():\n    if breed[1] not in included_breed:\n        traindf= traindf.drop([index])\ntraindf.shape\n        ","metadata":{"execution":{"iopub.status.busy":"2021-06-13T08:53:15.787969Z","iopub.execute_input":"2021-06-13T08:53:15.790639Z","iopub.status.idle":"2021-06-13T08:53:21.602713Z","shell.execute_reply.started":"2021-06-13T08:53:15.790590Z","shell.execute_reply":"2021-06-13T08:53:21.601983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training Labels","metadata":{"papermill":{"duration":0.068505,"end_time":"2021-03-16T10:35:59.976152","exception":false,"start_time":"2021-03-16T10:35:59.907647","status":"completed"},"tags":[]}},{"cell_type":"code","source":"traindf.head()","metadata":{"papermill":{"duration":0.091166,"end_time":"2021-03-16T10:36:00.136753","exception":false,"start_time":"2021-03-16T10:36:00.045587","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:53:21.604010Z","iopub.execute_input":"2021-06-13T08:53:21.604403Z","iopub.status.idle":"2021-06-13T08:53:21.613929Z","shell.execute_reply.started":"2021-06-13T08:53:21.604371Z","shell.execute_reply":"2021-06-13T08:53:21.612979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test id or Sample Submission","metadata":{"papermill":{"duration":0.06905,"end_time":"2021-03-16T10:36:00.274221","exception":false,"start_time":"2021-03-16T10:36:00.205171","status":"completed"},"tags":[]}},{"cell_type":"code","source":"testdf.head()","metadata":{"papermill":{"duration":0.100591,"end_time":"2021-03-16T10:36:00.441971","exception":false,"start_time":"2021-03-16T10:36:00.34138","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:53:21.617617Z","iopub.execute_input":"2021-06-13T08:53:21.618279Z","iopub.status.idle":"2021-06-13T08:53:21.649692Z","shell.execute_reply.started":"2021-06-13T08:53:21.618223Z","shell.execute_reply":"2021-06-13T08:53:21.648641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training Images","metadata":{"papermill":{"duration":0.127859,"end_time":"2021-03-16T10:36:00.685749","exception":false,"start_time":"2021-03-16T10:36:00.55789","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import os \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'))\n    plt.axis('off')","metadata":{"papermill":{"duration":1.188808,"end_time":"2021-03-16T10:36:01.996333","exception":false,"start_time":"2021-03-16T10:36:00.807525","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:53:21.653839Z","iopub.execute_input":"2021-06-13T08:53:21.654445Z","iopub.status.idle":"2021-06-13T08:53:22.195743Z","shell.execute_reply.started":"2021-06-13T08:53:21.654403Z","shell.execute_reply":"2021-06-13T08:53:22.194897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data preprocessing","metadata":{"_kg_hide-output":true,"papermill":{"duration":0.075855,"end_time":"2021-03-16T10:36:02.14835","exception":false,"start_time":"2021-03-16T10:36:02.072495","status":"completed"},"tags":[]}},{"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.1\n                                  )","metadata":{"papermill":{"duration":0.085989,"end_time":"2021-03-16T10:36:02.309474","exception":false,"start_time":"2021-03-16T10:36:02.223485","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:53:22.198265Z","iopub.execute_input":"2021-06-13T08:53:22.198845Z","iopub.status.idle":"2021-06-13T08:53:22.205768Z","shell.execute_reply.started":"2021-06-13T08:53:22.198798Z","shell.execute_reply":"2021-06-13T08:53:22.204605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 32","metadata":{"papermill":{"duration":0.083649,"end_time":"2021-03-16T10:36:02.467716","exception":false,"start_time":"2021-03-16T10:36:02.384067","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:53:22.207284Z","iopub.execute_input":"2021-06-13T08:53:22.207858Z","iopub.status.idle":"2021-06-13T08:53:22.216428Z","shell.execute_reply.started":"2021-06-13T08:53:22.207812Z","shell.execute_reply":"2021-06-13T08:53:22.215650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size=(224,224)","metadata":{"papermill":{"duration":0.081857,"end_time":"2021-03-16T10:36:02.624017","exception":false,"start_time":"2021-03-16T10:36:02.54216","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:53:22.218159Z","iopub.execute_input":"2021-06-13T08:53:22.218832Z","iopub.status.idle":"2021-06-13T08:53:22.228102Z","shell.execute_reply.started":"2021-06-13T08:53:22.218789Z","shell.execute_reply":"2021-06-13T08:53:22.226721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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)","metadata":{"papermill":{"duration":3.515719,"end_time":"2021-03-16T10:36:06.214598","exception":false,"start_time":"2021-03-16T10:36:02.698879","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:53:22.229863Z","iopub.execute_input":"2021-06-13T08:53:22.230326Z","iopub.status.idle":"2021-06-13T08:53:22.502497Z","shell.execute_reply.started":"2021-06-13T08:53:22.230263Z","shell.execute_reply":"2021-06-13T08:53:22.501475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plotting Augmented images","metadata":{"papermill":{"duration":0.074604,"end_time":"2021-03-16T10:36:06.365676","exception":false,"start_time":"2021-03-16T10:36:06.291072","status":"completed"},"tags":[]}},{"cell_type":"code","source":"x,y = next(train_generator)","metadata":{"papermill":{"duration":0.331865,"end_time":"2021-03-16T10:36:06.772658","exception":false,"start_time":"2021-03-16T10:36:06.440793","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:53:22.503846Z","iopub.execute_input":"2021-06-13T08:53:22.504378Z","iopub.status.idle":"2021-06-13T08:53:22.979621Z","shell.execute_reply.started":"2021-06-13T08:53:22.504338Z","shell.execute_reply":"2021-06-13T08:53:22.978664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(type(x))\nprint(x.shape)\nprint(y.shape)","metadata":{"papermill":{"duration":0.083869,"end_time":"2021-03-16T10:36:06.931543","exception":false,"start_time":"2021-03-16T10:36:06.847674","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:53:22.982081Z","iopub.execute_input":"2021-06-13T08:53:22.982629Z","iopub.status.idle":"2021-06-13T08:53:22.988642Z","shell.execute_reply.started":"2021-06-13T08:53:22.982588Z","shell.execute_reply":"2021-06-13T08:53:22.987541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_grid(image_list,nrows,ncols,figsize=(10,10),showaxis='off'):\n    \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        ","metadata":{"papermill":{"duration":0.090092,"end_time":"2021-03-16T10:36:07.096839","exception":false,"start_time":"2021-03-16T10:36:07.006747","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:53:22.989988Z","iopub.execute_input":"2021-06-13T08:53:22.990379Z","iopub.status.idle":"2021-06-13T08:53:22.999184Z","shell.execute_reply.started":"2021-06-13T08:53:22.990330Z","shell.execute_reply":"2021-06-13T08:53:22.998040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nshow_grid(x,8,3,figsize=(25,25))\n","metadata":{"papermill":{"duration":3.526836,"end_time":"2021-03-16T10:36:10.69815","exception":false,"start_time":"2021-03-16T10:36:07.171314","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:53:23.000968Z","iopub.execute_input":"2021-06-13T08:53:23.001519Z","iopub.status.idle":"2021-06-13T08:53:26.362476Z","shell.execute_reply.started":"2021-06-13T08:53:23.001464Z","shell.execute_reply":"2021-06-13T08:53:26.360900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Validation Data","metadata":{"papermill":{"duration":0.118688,"end_time":"2021-03-16T10:36:10.944053","exception":false,"start_time":"2021-03-16T10:36:10.825365","status":"completed"},"tags":[]}},{"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","metadata":{"papermill":{"duration":3.133173,"end_time":"2021-03-16T10:36:14.19651","exception":false,"start_time":"2021-03-16T10:36:11.063337","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:53:26.364636Z","iopub.execute_input":"2021-06-13T08:53:26.365011Z","iopub.status.idle":"2021-06-13T08:53:26.402321Z","shell.execute_reply.started":"2021-06-13T08:53:26.364971Z","shell.execute_reply":"2021-06-13T08:53:26.400340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test Data","metadata":{"papermill":{"duration":0.118623,"end_time":"2021-03-16T10:36:14.436363","exception":false,"start_time":"2021-03-16T10:36:14.31774","status":"completed"},"tags":[]}},{"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\")","metadata":{"papermill":{"duration":3.898941,"end_time":"2021-03-16T10:36:18.453182","exception":false,"start_time":"2021-03-16T10:36:14.554241","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:53:26.404628Z","iopub.execute_input":"2021-06-13T08:53:26.405030Z","iopub.status.idle":"2021-06-13T08:53:29.470352Z","shell.execute_reply.started":"2021-06-13T08:53:26.404993Z","shell.execute_reply":"2021-06-13T08:53:29.469326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Pretrained model NASNetLarge","metadata":{"papermill":{"duration":0.120292,"end_time":"2021-03-16T10:36:18.693727","exception":false,"start_time":"2021-03-16T10:36:18.573435","status":"completed"},"tags":[]}},{"cell_type":"code","source":"shape=(224,224,3)","metadata":{"papermill":{"duration":0.128561,"end_time":"2021-03-16T10:36:18.942533","exception":false,"start_time":"2021-03-16T10:36:18.813972","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:53:29.471799Z","iopub.execute_input":"2021-06-13T08:53:29.472175Z","iopub.status.idle":"2021-06-13T08:53:29.478871Z","shell.execute_reply.started":"2021-06-13T08:53:29.472134Z","shell.execute_reply":"2021-06-13T08:53:29.478001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = len(included_breed)\nclasses","metadata":{"execution":{"iopub.status.busy":"2021-06-13T08:53:29.480135Z","iopub.execute_input":"2021-06-13T08:53:29.480698Z","iopub.status.idle":"2021-06-13T08:53:29.494212Z","shell.execute_reply.started":"2021-06-13T08:53:29.480661Z","shell.execute_reply":"2021-06-13T08:53:29.493405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pretrained_model = tf.keras.applications.ResNet50V2(\n        weights='imagenet',\n        include_top=False ,\n        input_shape=shape\n    )\n# pretrained_model.trainable = False\n    \nmodel = tf.keras.Sequential([ \n        pretrained_model,  \n        tf.keras.layers.Flatten(),\n#         tf.keras.layers.GlobalAveragePooling2D(),\n        tf.keras.layers.Dense(2048, activation='relu'),\n        tf.keras.layers.Dropout(0.5),\n#         tf.keras.layers.Dense(512, activation='relu'),\n#         tf.keras.layers.Dropout(0.5),\n#         tf.keras.layers.Dense(128, activation='relu'),\n#         tf.keras.layers.Dropout(0.4),\n        tf.keras.layers.Dense(256, activation='relu'),\n        tf.keras.layers.Dropout(0.5),\n        tf.keras.layers.Dense(64, activation='relu'),\n        tf.keras.layers.Dropout(0.3),\n    \n        tf.keras.layers.Dense(10, activation='softmax')\n    ])","metadata":{"papermill":{"duration":6.127009,"end_time":"2021-03-16T10:36:25.188793","exception":false,"start_time":"2021-03-16T10:36:19.061784","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:53:29.496992Z","iopub.execute_input":"2021-06-13T08:53:29.497330Z","iopub.status.idle":"2021-06-13T08:53:31.207950Z","shell.execute_reply.started":"2021-06-13T08:53:29.497300Z","shell.execute_reply":"2021-06-13T08:53:31.207132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import backend as K\n\ndef recall_m(y_true, y_pred):\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n    recall = true_positives / (possible_positives + K.epsilon())\n    return recall\n\ndef precision_m(y_true, y_pred):\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n    precision = true_positives / (predicted_positives + K.epsilon())\n    return precision\n\ndef f1_m(y_true, y_pred): \n    precision = precision_m(y_true, y_pred)\n    recall = recall_m(y_true, y_pred)\n    return 2*((precision*recall)/(precision+recall+K.epsilon()))\n\n# compile the model\n\n# fit the model\n# history = model.fit(Xtrain, ytrain, validation_split=0.3, epochs=10, verbose=0)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T08:53:31.209351Z","iopub.execute_input":"2021-06-13T08:53:31.209702Z","iopub.status.idle":"2021-06-13T08:53:31.219373Z","shell.execute_reply.started":"2021-06-13T08:53:31.209666Z","shell.execute_reply":"2021-06-13T08:53:31.217724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#opt = tf.keras.optimizers.Adam(learning_rate=0.001)\nopt=tf.keras.optimizers.Adam(lr=1e-4)\nmodel.compile(optimizer= opt , loss='categorical_crossentropy', metrics=['acc',f1_m,precision_m, recall_m])\n\n# model.compile(optimizer = opt ,\n#               loss=\"categorical_crossentropy\",\n#               metrics=[\"accuracy\"])\nmodel.summary()","metadata":{"papermill":{"duration":0.17769,"end_time":"2021-03-16T10:36:25.501594","exception":false,"start_time":"2021-03-16T10:36:25.323904","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:53:31.220954Z","iopub.execute_input":"2021-06-13T08:53:31.221443Z","iopub.status.idle":"2021-06-13T08:53:31.263018Z","shell.execute_reply.started":"2021-06-13T08:53:31.221399Z","shell.execute_reply":"2021-06-13T08:53:31.261750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Early stopping","metadata":{"papermill":{"duration":0.140371,"end_time":"2021-03-16T10:36:25.777072","exception":false,"start_time":"2021-03-16T10:36:25.636701","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# reduce = tf.keras.callbacks.ReduceLROnPlateau( monitor='val_loss',factor=0.2,patience=5, min_lr=0.001 )\n\n# early = tf.keras.callbacks.EarlyStopping( patience=2,\n#                                           min_delta=0.001,\n#                                           restore_best_weights=True)","metadata":{"papermill":{"duration":0.149269,"end_time":"2021-03-16T10:36:26.063886","exception":false,"start_time":"2021-03-16T10:36:25.914617","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:53:31.265292Z","iopub.execute_input":"2021-06-13T08:53:31.265651Z","iopub.status.idle":"2021-06-13T08:53:31.270934Z","shell.execute_reply.started":"2021-06-13T08:53:31.265602Z","shell.execute_reply":"2021-06-13T08:53:31.269954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n# Fitting the Model:","metadata":{"papermill":{"duration":0.124982,"end_time":"2021-03-16T10:36:26.319586","exception":false,"start_time":"2021-03-16T10:36:26.194604","status":"completed"},"tags":[]}},{"cell_type":"code","source":"STEP_SIZE_TRAIN = train_generator.n//train_generator.batch_size\nSTEP_SIZE_VALID = valid_generator.n//valid_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=10,\n#                     callbacks=[early],\n                   )","metadata":{"papermill":{"duration":1245.64345,"end_time":"2021-03-16T10:57:12.090137","exception":false,"start_time":"2021-03-16T10:36:26.446687","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:53:31.273073Z","iopub.execute_input":"2021-06-13T08:53:31.273488Z","iopub.status.idle":"2021-06-13T08:56:10.061865Z","shell.execute_reply.started":"2021-06-13T08:53:31.273425Z","shell.execute_reply":"2021-06-13T08:56:10.061080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loss and Accuracy Curves","metadata":{"papermill":{"duration":1.560335,"end_time":"2021-03-16T10:57:15.213278","exception":false,"start_time":"2021-03-16T10:57:13.652943","status":"completed"},"tags":[]}},{"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_xlabel('epoch')\n    ax.legend(['train', 'valid.'])","metadata":{"papermill":{"duration":1.574586,"end_time":"2021-03-16T10:57:18.372678","exception":false,"start_time":"2021-03-16T10:57:16.798092","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:56:10.063517Z","iopub.execute_input":"2021-06-13T08:56:10.063912Z","iopub.status.idle":"2021-06-13T08:56:10.071614Z","shell.execute_reply.started":"2021-06-13T08:56:10.063871Z","shell.execute_reply":"2021-06-13T08:56:10.070573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_training_curves(\n    history.history['loss'],\n    history.history['val_loss'],\n    'LOSS',\n    211,\n)\n\ndisplay_training_curves(\n    history.history['acc'],\n    history.history['val_acc'],\n    'ACCURACY',\n    212,\n)\n","metadata":{"papermill":{"duration":2.086662,"end_time":"2021-03-16T10:57:22.135604","exception":false,"start_time":"2021-03-16T10:57:20.048942","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:56:10.073636Z","iopub.execute_input":"2021-06-13T08:56:10.074332Z","iopub.status.idle":"2021-06-13T08:56:10.496333Z","shell.execute_reply.started":"2021-06-13T08:56:10.074175Z","shell.execute_reply":"2021-06-13T08:56:10.495407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Accuracy","metadata":{"papermill":{"duration":1.566686,"end_time":"2021-03-16T10:57:29.120074","exception":false,"start_time":"2021-03-16T10:57:27.553388","status":"completed"},"tags":[]}},{"cell_type":"code","source":"loss, accuracy, f1_score, precision, recall = model.evaluate(valid_generator,batch_size=32)\n# score = model.evaluate(valid_generator,batch_size=32)\n# print(\"Accuracy: {:.2f}%\".format(score[1] * 100)) \n# print(\"Loss: \",score[0])\n\nprint(\"Loss:\", loss)\nprint(\"Accuracy:\", accuracy)\nprint(\"F1 Score:\", f1_score)\n\n","metadata":{"papermill":{"duration":3.696096,"end_time":"2021-03-16T10:57:34.451049","exception":false,"start_time":"2021-03-16T10:57:30.754953","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:56:10.497660Z","iopub.execute_input":"2021-06-13T08:56:10.498196Z","iopub.status.idle":"2021-06-13T08:56:12.740529Z","shell.execute_reply.started":"2021-06-13T08:56:10.498152Z","shell.execute_reply":"2021-06-13T08:56:12.739034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"DogClassificationByResNet2.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-06-13T08:57:42.301700Z","iopub.execute_input":"2021-06-13T08:57:42.302057Z","iopub.status.idle":"2021-06-13T08:57:50.840984Z","shell.execute_reply.started":"2021-06-13T08:57:42.302018Z","shell.execute_reply":"2021-06-13T08:57:50.839016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predicting Test Images","metadata":{"papermill":{"duration":1.62017,"end_time":"2021-03-16T10:57:53.608834","exception":false,"start_time":"2021-03-16T10:57:51.988664","status":"completed"},"tags":[]}},{"cell_type":"code","source":"%%time\npred=model.predict(test_generator)","metadata":{"papermill":{"duration":90.347441,"end_time":"2021-03-16T10:59:25.80864","exception":false,"start_time":"2021-03-16T10:57:55.461199","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:56:21.907224Z","iopub.execute_input":"2021-06-13T08:56:21.910222Z","iopub.status.idle":"2021-06-13T08:57:07.769945Z","shell.execute_reply.started":"2021-06-13T08:56:21.910167Z","shell.execute_reply":"2021-06-13T08:57:07.768134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv', usecols= included_breed+['id'])\ndf_submission.head()\n","metadata":{"papermill":{"duration":1.885996,"end_time":"2021-03-16T10:59:29.261066","exception":false,"start_time":"2021-03-16T10:59:27.37507","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:57:07.771654Z","iopub.execute_input":"2021-06-13T08:57:07.772008Z","iopub.status.idle":"2021-06-13T08:57:07.960711Z","shell.execute_reply.started":"2021-06-13T08:57:07.771969Z","shell.execute_reply":"2021-06-13T08:57:07.959644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission.iloc[:,1:] = pred\ndf_submission.head()","metadata":{"papermill":{"duration":2.016893,"end_time":"2021-03-16T10:59:36.863961","exception":false,"start_time":"2021-03-16T10:59:34.847068","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-13T08:57:07.962646Z","iopub.execute_input":"2021-06-13T08:57:07.963079Z","iopub.status.idle":"2021-06-13T08:57:08.015320Z","shell.execute_reply.started":"2021-06-13T08:57:07.963036Z","shell.execute_reply":"2021-06-13T08:57:08.014293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"%%time\ndf_submission.to_csv('Submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-13T08:57:08.016857Z","iopub.execute_input":"2021-06-13T08:57:08.017290Z","iopub.status.idle":"2021-06-13T08:57:08.314575Z","shell.execute_reply.started":"2021-06-13T08:57:08.017222Z","shell.execute_reply":"2021-06-13T08:57:08.313595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}