{"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":"markdown","source":"\n# Dog's Breed Identification\n\n![Dogs](https://s.wsj.net/public/resources/images/B3-EU419_201908_GR_20190822110317.jpg)\n\n* Generate dataset\n* Build a model\n* Predict","metadata":{}},{"cell_type":"markdown","source":"### Importing Necessary Libraries","metadata":{}},{"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":{"execution":{"iopub.status.busy":"2021-06-12T04:11:59.169839Z","iopub.execute_input":"2021-06-12T04:11:59.170256Z","iopub.status.idle":"2021-06-12T04:12:04.607837Z","shell.execute_reply.started":"2021-06-12T04:11:59.170166Z","shell.execute_reply":"2021-06-12T04:12:04.606928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Reading the data and adding the extension to it.**","metadata":{}},{"cell_type":"code","source":"labels = pd.read_csv(\"../input/dog-breed-identification/labels.csv\")\nsample = pd.read_csv('../input/dog-breed-identification/sample_submission.csv')\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-12T04:12:04.609265Z","iopub.execute_input":"2021-06-12T04:12:04.609595Z","iopub.status.idle":"2021-06-12T04:12:05.096925Z","shell.execute_reply.started":"2021-06-12T04:12:04.609558Z","shell.execute_reply":"2021-06-12T04:12:05.096064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def app_jpg(id):\n    return id+\".jpg\"","metadata":{"execution":{"iopub.status.busy":"2021-06-12T04:12:05.098748Z","iopub.execute_input":"2021-06-12T04:12:05.099135Z","iopub.status.idle":"2021-06-12T04:12:05.106099Z","shell.execute_reply.started":"2021-06-12T04:12:05.099096Z","shell.execute_reply":"2021-06-12T04:12:05.105262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels['id'] = labels['id'].apply(app_jpg)\nsample['id'] = sample['id'].apply(app_jpg)","metadata":{"execution":{"iopub.status.busy":"2021-06-12T04:12:05.107585Z","iopub.execute_input":"2021-06-12T04:12:05.108180Z","iopub.status.idle":"2021-06-12T04:12:05.125526Z","shell.execute_reply.started":"2021-06-12T04:12:05.108137Z","shell.execute_reply":"2021-06-12T04:12:05.124569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Data Generator**","metadata":{}},{"cell_type":"code","source":"datagen = 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-12T04:12:22.664429Z","iopub.execute_input":"2021-06-12T04:12:22.664742Z","iopub.status.idle":"2021-06-12T04:12:22.668874Z","shell.execute_reply.started":"2021-06-12T04:12:22.664713Z","shell.execute_reply":"2021-06-12T04:12:22.668048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = datagen.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 = datagen.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-12T04:12:25.044661Z","iopub.execute_input":"2021-06-12T04:12:25.045024Z","iopub.status.idle":"2021-06-12T04:12:35.677926Z","shell.execute_reply.started":"2021-06-12T04:12:25.044987Z","shell.execute_reply":"2021-06-12T04:12:35.676996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Getting class names**","metadata":{}},{"cell_type":"code","source":"a = train_ds.class_indices\nclass_names = list(a.keys())\nclass_names[:10]","metadata":{"execution":{"iopub.status.busy":"2021-06-12T04:12:44.173749Z","iopub.execute_input":"2021-06-12T04:12:44.174111Z","iopub.status.idle":"2021-06-12T04:12:44.181458Z","shell.execute_reply.started":"2021-06-12T04:12:44.174080Z","shell.execute_reply":"2021-06-12T04:12:44.180427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Getting one batch of data to visualize**","metadata":{}},{"cell_type":"code","source":"x,y = next(train_ds)\nx.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-12T04:12:45.914238Z","iopub.execute_input":"2021-06-12T04:12:45.914579Z","iopub.status.idle":"2021-06-12T04:12:46.696463Z","shell.execute_reply.started":"2021-06-12T04:12:45.914548Z","shell.execute_reply":"2021-06-12T04:12:46.695566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Function to plot images**","metadata":{}},{"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-12T04:12:49.105749Z","iopub.execute_input":"2021-06-12T04:12:49.106212Z","iopub.status.idle":"2021-06-12T04:12:49.114009Z","shell.execute_reply.started":"2021-06-12T04:12:49.106172Z","shell.execute_reply":"2021-06-12T04:12:49.113065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_images(x,y)","metadata":{"execution":{"iopub.status.busy":"2021-06-12T04:12:52.045738Z","iopub.execute_input":"2021-06-12T04:12:52.046093Z","iopub.status.idle":"2021-06-12T04:12:53.591087Z","shell.execute_reply.started":"2021-06-12T04:12:52.046063Z","shell.execute_reply":"2021-06-12T04:12:53.590267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model","metadata":{}},{"cell_type":"markdown","source":"**Getting a Pre-Trained Model**","metadata":{}},{"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-12T04:12:58.358950Z","iopub.execute_input":"2021-06-12T04:12:58.359273Z","iopub.status.idle":"2021-06-12T04:12:58.362974Z","shell.execute_reply.started":"2021-06-12T04:12:58.359244Z","shell.execute_reply":"2021-06-12T04:12:58.362148Z"},"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-12T04:13:01.058632Z","iopub.execute_input":"2021-06-12T04:13:01.058967Z","iopub.status.idle":"2021-06-12T04:13:16.000218Z","shell.execute_reply.started":"2021-06-12T04:13:01.058933Z","shell.execute_reply":"2021-06-12T04:13:15.999279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Defining our Model**","metadata":{}},{"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-12T04:13:16.001720Z","iopub.execute_input":"2021-06-12T04:13:16.002112Z","iopub.status.idle":"2021-06-12T04:13:17.855849Z","shell.execute_reply.started":"2021-06-12T04:13:16.002075Z","shell.execute_reply":"2021-06-12T04:13:17.855005Z"},"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-12T04:13:17.857644Z","iopub.execute_input":"2021-06-12T04:13:17.858025Z","iopub.status.idle":"2021-06-12T04:13:17.886158Z","shell.execute_reply.started":"2021-06-12T04:13:17.857985Z","shell.execute_reply":"2021-06-12T04:13:17.885239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Defining callbacks to preserve the best results**","metadata":{}},{"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-12T04:13:17.889663Z","iopub.execute_input":"2021-06-12T04:13:17.890005Z","iopub.status.idle":"2021-06-12T04:13:18.108120Z","shell.execute_reply.started":"2021-06-12T04:13:17.889972Z","shell.execute_reply":"2021-06-12T04:13:18.107193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Train","metadata":{}},{"cell_type":"code","source":"hist = model.fit(train_ds,epochs=25,validation_data=val_ds,callbacks=my_calls)","metadata":{"execution":{"iopub.status.busy":"2021-06-12T04:13:18.112000Z","iopub.execute_input":"2021-06-12T04:13:18.112303Z","iopub.status.idle":"2021-06-12T04:36:02.162384Z","shell.execute_reply.started":"2021-06-12T04:13:18.112275Z","shell.execute_reply":"2021-06-12T04:36:02.161508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Plotting the losses and acuuracies**","metadata":{}},{"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()\n","metadata":{"execution":{"iopub.status.busy":"2021-06-12T04:36:36.395941Z","iopub.execute_input":"2021-06-12T04:36:36.396309Z","iopub.status.idle":"2021-06-12T04:36:36.708535Z","shell.execute_reply.started":"2021-06-12T04:36:36.396273Z","shell.execute_reply":"2021-06-12T04:36:36.707525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Loading saved model**","metadata":{}},{"cell_type":"code","source":"model = load_model(\"./Model.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-06-12T04:36:44.281351Z","iopub.execute_input":"2021-06-12T04:36:44.281682Z","iopub.status.idle":"2021-06-12T04:36:51.204857Z","shell.execute_reply.started":"2021-06-12T04:36:44.281650Z","shell.execute_reply":"2021-06-12T04:36:51.203985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prediction","metadata":{}},{"cell_type":"markdown","source":"**Genrating test dataset**","metadata":{}},{"cell_type":"code","source":"testgen = Imgen(preprocessing_function=keras.applications.inception_resnet_v2.preprocess_input)","metadata":{"execution":{"iopub.status.busy":"2021-06-12T04:36:51.963935Z","iopub.execute_input":"2021-06-12T04:36:51.964269Z","iopub.status.idle":"2021-06-12T04:36:51.968445Z","shell.execute_reply.started":"2021-06-12T04:36:51.964241Z","shell.execute_reply":"2021-06-12T04:36:51.967364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds = testgen.flow_from_dataframe(\n    sample,\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-12T04:36:53.294584Z","iopub.execute_input":"2021-06-12T04:36:53.294935Z","iopub.status.idle":"2021-06-12T04:37:10.761979Z","shell.execute_reply.started":"2021-06-12T04:36:53.294898Z","shell.execute_reply":"2021-06-12T04:37:10.761051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Making Predictions**","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(test_ds,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2021-06-12T04:37:10.763488Z","iopub.execute_input":"2021-06-12T04:37:10.763853Z","iopub.status.idle":"2021-06-12T04:39:17.272444Z","shell.execute_reply.started":"2021-06-12T04:37:10.763816Z","shell.execute_reply":"2021-06-12T04:39:17.271541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- To get the most probable result","metadata":{}},{"cell_type":"code","source":"pred  = [np.argmax(i) for i in predictions]","metadata":{"execution":{"iopub.status.busy":"2021-06-12T04:39:17.276173Z","iopub.execute_input":"2021-06-12T04:39:17.276445Z","iopub.status.idle":"2021-06-12T04:39:17.318291Z","shell.execute_reply.started":"2021-06-12T04:39:17.276418Z","shell.execute_reply":"2021-06-12T04:39:17.317275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = next(test_ds)\nX.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-12T04:39:38.402083Z","iopub.execute_input":"2021-06-12T04:39:38.402408Z","iopub.status.idle":"2021-06-12T04:39:38.550229Z","shell.execute_reply.started":"2021-06-12T04:39:38.402380Z","shell.execute_reply":"2021-06-12T04:39:38.549440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Visualizing images against predictions**","metadata":{}},{"cell_type":"code","source":"def check_images(img):\n    plt.figure(figsize=[25,10])\n    for i in range(25):\n        plt.subplot(5,5,i+1)\n        plt.imshow(img[i])\n        plt.title(class_names[pred[i]])\n        plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2021-06-12T04:39:40.453730Z","iopub.execute_input":"2021-06-12T04:39:40.454086Z","iopub.status.idle":"2021-06-12T04:39:40.459844Z","shell.execute_reply.started":"2021-06-12T04:39:40.454055Z","shell.execute_reply":"2021-06-12T04:39:40.458937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"check_images(X)","metadata":{"execution":{"iopub.status.busy":"2021-06-12T04:39:42.195389Z","iopub.execute_input":"2021-06-12T04:39:42.195734Z","iopub.status.idle":"2021-06-12T04:39:44.154655Z","shell.execute_reply.started":"2021-06-12T04:39:42.195702Z","shell.execute_reply":"2021-06-12T04:39:44.153687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}