{"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":"import os\nimport numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\n\nfrom tensorflow import keras\nfrom keras.preprocessing.image import ImageDataGenerator as Imgen\n\nfrom keras.models import Sequential,load_model\nfrom keras.layers import Conv2D,MaxPooling2D,Flatten,Dense,GlobalAveragePooling2D,Dropout\n\nfrom keras.preprocessing import image\n\nimport cv2\n\nimport pickle","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-16T10:47:22.051214Z","iopub.execute_input":"2021-06-16T10:47:22.051541Z","iopub.status.idle":"2021-06-16T10:47:22.057065Z","shell.execute_reply.started":"2021-06-16T10:47:22.051506Z","shell.execute_reply":"2021-06-16T10:47:22.056039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv(\"../input/dog-breed-identification/labels.csv\")\nsample_sub = pd.read_csv(\"../input/dog-breed-identification/sample_submission.csv\")\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:09:24.932719Z","iopub.execute_input":"2021-06-16T10:09:24.933090Z","iopub.status.idle":"2021-06-16T10:09:25.397502Z","shell.execute_reply.started":"2021-06-16T10:09:24.933050Z","shell.execute_reply":"2021-06-16T10:09:25.396739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def addjpg(id):\n    return id+\".jpg\"","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:09:25.399285Z","iopub.execute_input":"2021-06-16T10:09:25.399601Z","iopub.status.idle":"2021-06-16T10:09:25.404925Z","shell.execute_reply.started":"2021-06-16T10:09:25.399565Z","shell.execute_reply":"2021-06-16T10:09:25.403072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels['id'] = labels['id'].apply(addjpg)\nsample_sub['id'] = sample_sub['id'].apply(addjpg)","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:09:25.406417Z","iopub.execute_input":"2021-06-16T10:09:25.406747Z","iopub.status.idle":"2021-06-16T10:09:25.424332Z","shell.execute_reply.started":"2021-06-16T10:09:25.406714Z","shell.execute_reply":"2021-06-16T10:09:25.423387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = Imgen(preprocessing_function=keras.applications.nasnet.preprocess_input,\n                  shear_range=0.2,\n                  zoom_range=0.2,\n                  horizontal_flip=True,\n                  validation_split=0.1\n                 )","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:09:25.425522Z","iopub.execute_input":"2021-06-16T10:09:25.425892Z","iopub.status.idle":"2021-06-16T10:09:25.434830Z","shell.execute_reply.started":"2021-06-16T10:09:25.425847Z","shell.execute_reply":"2021-06-16T10:09:25.434160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = data.flow_from_dataframe(\n    labels,\n    directory = '../input/dog-breed-identification/train', \n    x_col = 'id',\n    y_col = 'breed',\n    subset=\"training\",\n    color_mode=\"rgb\",\n    target_size = (331,331),\n    class_mode=\"categorical\",\n    batch_size=32,\n    shuffle=True,\n    seed=123,\n)\n\n\nval_ds = data.flow_from_dataframe(\n    labels,\n    directory = '../input/dog-breed-identification/train',\n    x_col = 'id',\n    y_col = 'breed',\n    subset=\"validation\",\n    color_mode=\"rgb\",\n    target_size = (331,331),\n    class_mode=\"categorical\",\n    batch_size=32,\n    shuffle=True,\n    seed=123,\n)","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:09:25.436153Z","iopub.execute_input":"2021-06-16T10:09:25.436523Z","iopub.status.idle":"2021-06-16T10:09:36.329315Z","shell.execute_reply.started":"2021-06-16T10:09:25.436486Z","shell.execute_reply":"2021-06-16T10:09:36.327910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = train_ds.class_indices\nclass_names = list(a.keys())\nclass_names[:10]","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:09:36.330618Z","iopub.execute_input":"2021-06-16T10:09:36.330977Z","iopub.status.idle":"2021-06-16T10:09:36.337016Z","shell.execute_reply.started":"2021-06-16T10:09:36.330939Z","shell.execute_reply":"2021-06-16T10:09:36.336023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x,y = next(train_ds)\nx.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:09:36.340073Z","iopub.execute_input":"2021-06-16T10:09:36.340767Z","iopub.status.idle":"2021-06-16T10:09:37.066656Z","shell.execute_reply.started":"2021-06-16T10:09:36.340729Z","shell.execute_reply":"2021-06-16T10:09:37.065711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_images(img,labels):\n    plt.figure(figsize=[15,10])\n    for i in range(25):\n        plt.subplot(5,5,i+1)\n        plt.imshow(img[i])\n        plt.title(class_names[np.argmax(labels[i])])\n        plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:09:37.068544Z","iopub.execute_input":"2021-06-16T10:09:37.068895Z","iopub.status.idle":"2021-06-16T10:09:37.074838Z","shell.execute_reply.started":"2021-06-16T10:09:37.068858Z","shell.execute_reply":"2021-06-16T10:09:37.073880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_images(x,y)","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:09:37.076309Z","iopub.execute_input":"2021-06-16T10:09:37.076675Z","iopub.status.idle":"2021-06-16T10:09:38.642629Z","shell.execute_reply.started":"2021-06-16T10:09:37.076640Z","shell.execute_reply":"2021-06-16T10:09:38.641825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.nasnet import NASNetLarge\nfrom keras.applications.inception_resnet_v2 import InceptionResNetV2","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:09:38.643709Z","iopub.execute_input":"2021-06-16T10:09:38.644078Z","iopub.status.idle":"2021-06-16T10:09:38.648722Z","shell.execute_reply.started":"2021-06-16T10:09:38.644045Z","shell.execute_reply":"2021-06-16T10:09:38.647897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = InceptionResNetV2(include_top=False,\n                     weights='imagenet',\n                     input_shape=(331,331,3)\n                     )\nbase_model.trainable = False","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:09:38.650284Z","iopub.execute_input":"2021-06-16T10:09:38.650981Z","iopub.status.idle":"2021-06-16T10:09:47.101121Z","shell.execute_reply.started":"2021-06-16T10:09:38.650946Z","shell.execute_reply":"2021-06-16T10:09:47.100272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    base_model,\n    \n    GlobalAveragePooling2D(),\n    \n    Dense(256,activation = 'relu'),\n    Dropout(0.5),\n    \n    Dense(120,activation='softmax')\n])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:10:16.231486Z","iopub.execute_input":"2021-06-16T10:10:16.231828Z","iopub.status.idle":"2021-06-16T10:10:17.765403Z","shell.execute_reply.started":"2021-06-16T10:10:16.231777Z","shell.execute_reply":"2021-06-16T10:10:17.764528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:10:30.445109Z","iopub.execute_input":"2021-06-16T10:10:30.445439Z","iopub.status.idle":"2021-06-16T10:10:30.471312Z","shell.execute_reply.started":"2021-06-16T10:10:30.445408Z","shell.execute_reply":"2021-06-16T10:10:30.470360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_calls = [keras.callbacks.EarlyStopping(monitor='val_accuracy',patience=2),\n            keras.callbacks.ModelCheckpoint(\"Model.h5\",verbose=1,save_best_only=True)]","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:10:33.043480Z","iopub.execute_input":"2021-06-16T10:10:33.043815Z","iopub.status.idle":"2021-06-16T10:10:33.049058Z","shell.execute_reply.started":"2021-06-16T10:10:33.043763Z","shell.execute_reply":"2021-06-16T10:10:33.048061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist = model.fit(train_ds,epochs=25,validation_data=val_ds,callbacks=my_calls, verbose=2, batch_size=20)","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:10:35.596160Z","iopub.execute_input":"2021-06-16T10:10:35.596499Z","iopub.status.idle":"2021-06-16T10:32:34.629873Z","shell.execute_reply.started":"2021-06-16T10:10:35.596466Z","shell.execute_reply":"2021-06-16T10:32:34.629017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,6))\n\nplt.subplot(1,2,1)\nplt.plot(hist.epoch,hist.history['accuracy'],label = 'Training')\nplt.plot(hist.epoch,hist.history['val_accuracy'],label = 'validation')\n\nplt.title(\"Accuracy\")\nplt.legend()\n\nplt.subplot(1,2,2)\nplt.plot(hist.epoch,hist.history['loss'],label = 'Training')\nplt.plot(hist.epoch,hist.history['val_loss'],label = 'validation')\n\nplt.title(\"Loss\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:45:34.207623Z","iopub.execute_input":"2021-06-16T10:45:34.208062Z","iopub.status.idle":"2021-06-16T10:45:34.511583Z","shell.execute_reply.started":"2021-06-16T10:45:34.208015Z","shell.execute_reply":"2021-06-16T10:45:34.510734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = load_model(\"./Model.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:47:26.303392Z","iopub.execute_input":"2021-06-16T10:47:26.303704Z","iopub.status.idle":"2021-06-16T10:47:33.030632Z","shell.execute_reply.started":"2021-06-16T10:47:26.303675Z","shell.execute_reply":"2021-06-16T10:47:33.029810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testgen = Imgen(preprocessing_function=keras.applications.inception_resnet_v2.preprocess_input)","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:47:33.033155Z","iopub.execute_input":"2021-06-16T10:47:33.033668Z","iopub.status.idle":"2021-06-16T10:47:33.037973Z","shell.execute_reply.started":"2021-06-16T10:47:33.033629Z","shell.execute_reply":"2021-06-16T10:47:33.037024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds = testgen.flow_from_dataframe(\n    sample_sub,\n    directory = '../input/dog-breed-identification/test',\n    x_col = 'id',\n    y_col = None,\n    target_size = (331,331),\n    class_mode= None,\n    batch_size=32,\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:47:33.039515Z","iopub.execute_input":"2021-06-16T10:47:33.039901Z","iopub.status.idle":"2021-06-16T10:47:39.861483Z","shell.execute_reply.started":"2021-06-16T10:47:33.039860Z","shell.execute_reply":"2021-06-16T10:47:39.860534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test_ds,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:47:39.862947Z","iopub.execute_input":"2021-06-16T10:47:39.863452Z","iopub.status.idle":"2021-06-16T10:49:43.475786Z","shell.execute_reply.started":"2021-06-16T10:47:39.863412Z","shell.execute_reply":"2021-06-16T10:49:43.474992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred  = [np.argmax(i) for i in predictions]","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:49:43.479113Z","iopub.execute_input":"2021-06-16T10:49:43.479372Z","iopub.status.idle":"2021-06-16T10:49:43.519504Z","shell.execute_reply.started":"2021-06-16T10:49:43.479346Z","shell.execute_reply":"2021-06-16T10:49:43.518760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:49:43.522434Z","iopub.execute_input":"2021-06-16T10:49:43.522679Z","iopub.status.idle":"2021-06-16T10:49:43.530582Z","shell.execute_reply.started":"2021-06-16T10:49:43.522655Z","shell.execute_reply":"2021-06-16T10:49:43.529755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\nfile_list = test_ds.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":{"execution":{"iopub.status.busy":"2021-06-16T10:49:43.533184Z","iopub.execute_input":"2021-06-16T10:49:43.533604Z","iopub.status.idle":"2021-06-16T10:49:43.575829Z","shell.execute_reply.started":"2021-06-16T10:49:43.533575Z","shell.execute_reply":"2021-06-16T10:49:43.575009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('../input/dog-breed-identification/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:49:43.576880Z","iopub.execute_input":"2021-06-16T10:49:43.577240Z","iopub.status.idle":"2021-06-16T10:49:43.836203Z","shell.execute_reply.started":"2021-06-16T10:49:43.577199Z","shell.execute_reply":"2021-06-16T10:49:43.835352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['id'] = id_list\nsubmission.iloc[:,1:] =predictions\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:49:43.837523Z","iopub.execute_input":"2021-06-16T10:49:43.837950Z","iopub.status.idle":"2021-06-16T10:49:44.240410Z","shell.execute_reply.started":"2021-06-16T10:49:43.837910Z","shell.execute_reply":"2021-06-16T10:49:44.239429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df = submission.set_index('id')\nfinal_df.to_csv('Submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-16T10:49:44.242593Z","iopub.execute_input":"2021-06-16T10:49:44.242958Z","iopub.status.idle":"2021-06-16T10:49:47.230563Z","shell.execute_reply.started":"2021-06-16T10:49:44.242922Z","shell.execute_reply":"2021-06-16T10:49:47.229673Z"},"trusted":true},"execution_count":null,"outputs":[]}]}