{"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":"2021-05-29T10:16:56.264965Z","iopub.execute_input":"2021-05-29T10:16:56.26541Z","iopub.status.idle":"2021-05-29T10:17:25.665132Z","shell.execute_reply.started":"2021-05-29T10:16:56.265318Z","shell.execute_reply":"2021-05-29T10:17:25.663039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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","metadata":{"execution":{"iopub.status.busy":"2021-05-29T10:17:25.6707Z","iopub.execute_input":"2021-05-29T10:17:25.671045Z","iopub.status.idle":"2021-05-29T10:17:31.745233Z","shell.execute_reply.started":"2021-05-29T10:17:25.671016Z","shell.execute_reply":"2021-05-29T10:17:31.744097Z"},"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":{"execution":{"iopub.status.busy":"2021-05-29T10:17:31.747378Z","iopub.execute_input":"2021-05-29T10:17:31.747815Z","iopub.status.idle":"2021-05-29T10:17:31.753732Z","shell.execute_reply.started":"2021-05-29T10:17:31.74777Z","shell.execute_reply":"2021-05-29T10:17:31.752718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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)","metadata":{"execution":{"iopub.status.busy":"2021-05-29T10:17:31.75579Z","iopub.execute_input":"2021-05-29T10:17:31.756278Z","iopub.status.idle":"2021-05-29T10:17:32.835874Z","shell.execute_reply.started":"2021-05-29T10:17:31.756235Z","shell.execute_reply":"2021-05-29T10:17:32.834891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindf.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-29T10:17:32.837022Z","iopub.execute_input":"2021-05-29T10:17:32.837293Z","iopub.status.idle":"2021-05-29T10:17:32.856961Z","shell.execute_reply.started":"2021-05-29T10:17:32.837265Z","shell.execute_reply":"2021-05-29T10:17:32.855977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testdf.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-29T10:17:32.858302Z","iopub.execute_input":"2021-05-29T10:17:32.85873Z","iopub.status.idle":"2021-05-29T10:17:32.886976Z","shell.execute_reply.started":"2021-05-29T10:17:32.858696Z","shell.execute_reply":"2021-05-29T10:17:32.885786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"src_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'))","metadata":{"execution":{"iopub.status.busy":"2021-05-29T10:17:32.888397Z","iopub.execute_input":"2021-05-29T10:17:32.888801Z","iopub.status.idle":"2021-05-29T10:17:34.023304Z","shell.execute_reply.started":"2021-05-29T10:17:32.888767Z","shell.execute_reply":"2021-05-29T10:17:34.022066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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                                  )","metadata":{"execution":{"iopub.status.busy":"2021-05-29T10:17:34.026624Z","iopub.execute_input":"2021-05-29T10:17:34.027078Z","iopub.status.idle":"2021-05-29T10:17:34.034563Z","shell.execute_reply.started":"2021-05-29T10:17:34.027034Z","shell.execute_reply":"2021-05-29T10:17:34.033407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 32\nimage_size=(299,299)","metadata":{"execution":{"iopub.status.busy":"2021-05-29T10:17:34.036444Z","iopub.execute_input":"2021-05-29T10:17:34.036768Z","iopub.status.idle":"2021-05-29T10:17:34.052572Z","shell.execute_reply.started":"2021-05-29T10:17:34.036738Z","shell.execute_reply":"2021-05-29T10:17:34.051475Z"},"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\" )","metadata":{"execution":{"iopub.status.busy":"2021-05-29T10:17:34.05411Z","iopub.execute_input":"2021-05-29T10:17:34.054395Z","iopub.status.idle":"2021-05-29T10:17:37.324419Z","shell.execute_reply.started":"2021-05-29T10:17:34.054368Z","shell.execute_reply":"2021-05-29T10:17:37.323686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x,y = next(train_generator)\nprint(type(x))\nprint(x.shape)\nprint(y.shape)","metadata":{"execution":{"iopub.status.busy":"2021-05-29T10:17:37.325432Z","iopub.execute_input":"2021-05-29T10:17:37.325823Z","iopub.status.idle":"2021-05-29T10:17:37.654617Z","shell.execute_reply.started":"2021-05-29T10:17:37.325793Z","shell.execute_reply":"2021-05-29T10:17:37.653953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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')","metadata":{"execution":{"iopub.status.busy":"2021-05-29T10:17:37.655748Z","iopub.execute_input":"2021-05-29T10:17:37.656247Z","iopub.status.idle":"2021-05-29T10:17:37.665038Z","shell.execute_reply.started":"2021-05-29T10:17:37.6562Z","shell.execute_reply":"2021-05-29T10:17:37.66406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nshow_grid(x,4,8,figsize=(25,25))","metadata":{"execution":{"iopub.status.busy":"2021-05-29T10:17:37.666535Z","iopub.execute_input":"2021-05-29T10:17:37.666876Z","iopub.status.idle":"2021-05-29T10:17:41.848657Z","shell.execute_reply.started":"2021-05-29T10:17:37.666823Z","shell.execute_reply":"2021-05-29T10:17:41.847716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Validation data\nvalid_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\")","metadata":{"execution":{"iopub.status.busy":"2021-05-29T10:17:41.8498Z","iopub.execute_input":"2021-05-29T10:17:41.850088Z","iopub.status.idle":"2021-05-29T10:17:45.230598Z","shell.execute_reply.started":"2021-05-29T10:17:41.850061Z","shell.execute_reply":"2021-05-29T10:17:45.22986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test data\ntest_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":{"execution":{"iopub.status.busy":"2021-05-29T10:17:45.231759Z","iopub.execute_input":"2021-05-29T10:17:45.232241Z","iopub.status.idle":"2021-05-29T10:17:48.580147Z","shell.execute_reply.started":"2021-05-29T10:17:45.232209Z","shell.execute_reply":"2021-05-29T10:17:48.579013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Pretrained model InceptionV3\nshape=[299,299,3]\n\npretrained_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    ])","metadata":{"execution":{"iopub.status.busy":"2021-05-29T10:17:48.581353Z","iopub.execute_input":"2021-05-29T10:17:48.581624Z","iopub.status.idle":"2021-05-29T10:17:52.339882Z","shell.execute_reply.started":"2021-05-29T10:17:48.581597Z","shell.execute_reply":"2021-05-29T10:17:52.338904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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()","metadata":{"execution":{"iopub.status.busy":"2021-05-29T10:17:52.341437Z","iopub.execute_input":"2021-05-29T10:17:52.341985Z","iopub.status.idle":"2021-05-29T10:17:52.38399Z","shell.execute_reply.started":"2021-05-29T10:17:52.34194Z","shell.execute_reply":"2021-05-29T10:17:52.383004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Early stopping\n#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)","metadata":{"execution":{"iopub.status.busy":"2021-05-29T10:17:52.385241Z","iopub.execute_input":"2021-05-29T10:17:52.385543Z","iopub.status.idle":"2021-05-29T10:17:52.389087Z","shell.execute_reply.started":"2021-05-29T10:17:52.385514Z","shell.execute_reply":"2021-05-29T10:17:52.388449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fitting model\nSTEP_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],)","metadata":{"execution":{"iopub.status.busy":"2021-05-29T10:17:52.390286Z","iopub.execute_input":"2021-05-29T10:17:52.390692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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.'])\n    \ndisplay_training_curves(\n    history.history['loss'],\n    history.history['val_loss'],\n    'LOSS',\n    211,\n    )\n\ndisplay_training_curves(\n    history.history['accuracy'],\n    history.history['val_accuracy'],\n    'ACCURACY',\n    211,\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = model.evaluate(valid_generator,batch_size=32)\nprint(\"Accuracy: {:.2f}%\".format(score[1] * 100)) \nprint(\"Loss: \",score[0])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import f1_score\nY_pred = model.predict(valid_generator)\ny_pred = np.argmax(Y_pred, axis=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f_score = f1_score(valid_generator.classes,y_pred,average='macro')\nprint('F1 score:',f_score)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#now predecting test imgs\npred=model.predict(test_generator)\ndf_submission = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')\ndf_submission.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"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)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission['id'] = id_list\ndf_submission.iloc[:,1:] = pred\ndf_submission.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prediction Score for selected breeds\nbreeds=['id','beagle','chihuahua','doberman','french_bulldog', 'golden_retriever', 'malamute','pug','saint_bernard','scottish_deerhound','tibetan_mastiff']\nselected_breeds = df_submission.loc[:,breeds]\nprint('Breeds and prediction score')\nselected_breeds.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_sub = df_submission.set_index('id')\nfinal_sub.to_csv('Submission.csv')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}