{"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":"**Introduction**\n\n* This kernel is a detailed guide for transfer learning on Dog Breeds problem, it's all about learning a new technique, evaluate it using only Kaggle training set without cheating.\n\n* The aim of this kernel is to show you how to use pre-trained CNN models as feature extractors, which one of the most effective transfer learning techniques.\n\n* A reasonable question comes to your mind, 'Wait, why do we have to use this technique, why don't we just use regular transfer learning ?', if you try to do so, you will figure out that the problem is pretty hard for a single model to handle (you would get higher loss and less accuracy).\n\n* It's even hard for humankind to distinguish between 120 dog breeds!, single poor CNN would struggle.\n\n**Explanation**\n\n* Take look at general CNN architecture for image classification in two main parts, “feature extractor” that based on conv-layers, and “classifier” which usually based on fully connected layers:\n\n![image.png](attachment:image.png)\n\n* Simply, feature extractor could be created as follow > (Feature Extractor = Pretrained Model - Late Fully Connected Layers)\n\n* For example, InceptionV3 feature extractor (without last FC layer) outputs 2048 vector for each image sample, each value represent a certain feature of dog image (Coded in numerical values of course), like Dog color?, How big is his head?, Shape of the eyes?, length of the tale?, Size? .. etc\n\n* Hence, more \"different\" feature extractors mean more features to be used to determine which breed does this dog belong.\n\n* So our strategy goes as the following,\n  1. Create 4 feature extractor using different pre-trained CNN models\n  2. Extract features from raw data and stacks the features together.\n  3. Use a simple DNN with one dense layer and a heavy dropout layer to figure out patterns in the feature extracted from the data.\n     \n     \n* The code is simple, concise and fully-commented. Feel free to ask for help / more info / more explanation in the comments.\n\n* Finally if this kernel helps you somehow, kindly don't forget to leave a little upvote.\n\n* ENJOY.\n","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.010221Z","iopub.execute_input":"2022-06-30T14:09:00.010598Z","iopub.status.idle":"2022-06-30T14:09:00.048697Z","shell.execute_reply.started":"2022-06-30T14:09:00.010544Z","shell.execute_reply":"2022-06-30T14:09:00.046656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os, cv2, random, time, shutil, csv\nimport tensorflow as tf\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom tqdm import tqdm\nnp.random.seed(42)\n%matplotlib inline \nimport json\nimport os\nimport cv2\nimport keras\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Model\nfrom keras.layers import BatchNormalization, Dense, GlobalAveragePooling2D, Lambda, Dropout, InputLayer, Input\nfrom keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing.image import load_img","metadata":{"execution":{"iopub.status.busy":"2022-07-10T13:43:31.257951Z","iopub.execute_input":"2022-07-10T13:43:31.258291Z","iopub.status.idle":"2022-07-10T13:43:39.551787Z","shell.execute_reply.started":"2022-07-10T13:43:31.258228Z","shell.execute_reply":"2022-07-10T13:43:39.550825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Data Paths\ntrain_dir = '/kaggle/input/dog-breed-identification/train/'\ntest_dir = '/kaggle/input/dog-breed-identification/test/'\n#Count/Print train and test samples.\n","metadata":{"execution":{"iopub.status.busy":"2022-07-10T13:43:43.63352Z","iopub.execute_input":"2022-07-10T13:43:43.633877Z","iopub.status.idle":"2022-07-10T13:43:43.63856Z","shell.execute_reply.started":"2022-07-10T13:43:43.633816Z","shell.execute_reply":"2022-07-10T13:43:43.63757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Read train labels.\nlabels_dataframe = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\n#Read sample_submission file to be modified by pridected labels.\n# sample_df = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')\n#Incpect labels_dataframe.\nlabels_dataframe.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T13:43:52.543392Z","iopub.execute_input":"2022-07-10T13:43:52.543894Z","iopub.status.idle":"2022-07-10T13:43:52.568885Z","shell.execute_reply.started":"2022-07-10T13:43:52.543658Z","shell.execute_reply":"2022-07-10T13:43:52.567855Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Create list of alphabetically sorted labels.\ndog_breeds = sorted(list(set(labels_dataframe['breed'])))\nn_classes = len(dog_breeds)\nprint(n_classes)\ndog_breeds[:10]","metadata":{"execution":{"iopub.status.busy":"2022-07-10T13:44:06.384899Z","iopub.execute_input":"2022-07-10T13:44:06.385222Z","iopub.status.idle":"2022-07-10T13:44:06.39796Z","shell.execute_reply.started":"2022-07-10T13:44:06.38517Z","shell.execute_reply":"2022-07-10T13:44:06.396782Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# with open('/kaggle/working/dog_breeds.json', 'w') as fp:\n#     json.dump(dog_breeds,fp)","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.058276Z","iopub.status.idle":"2022-06-30T14:09:00.059156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_to_num = dict(zip(dog_breeds, range(n_classes)))\nclass_to_num\n","metadata":{"execution":{"iopub.status.busy":"2022-07-10T13:44:24.920846Z","iopub.execute_input":"2022-07-10T13:44:24.921361Z","iopub.status.idle":"2022-07-10T13:44:24.932241Z","shell.execute_reply.started":"2022-07-10T13:44:24.921286Z","shell.execute_reply":"2022-07-10T13:44:24.931604Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_to_num['toy_poodle']","metadata":{"execution":{"iopub.status.busy":"2022-07-10T13:44:31.108891Z","iopub.execute_input":"2022-07-10T13:44:31.109455Z","iopub.status.idle":"2022-07-10T13:44:31.114612Z","shell.execute_reply.started":"2022-07-10T13:44:31.109405Z","shell.execute_reply":"2022-07-10T13:44:31.113915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# with open('/kaggle/working/class_to_num.json', 'w') as fp:\n#     json.dump(class_to_num,fp)","metadata":{"execution":{"iopub.status.busy":"2022-07-03T15:20:12.794843Z","iopub.execute_input":"2022-07-03T15:20:12.795187Z","iopub.status.idle":"2022-07-03T15:20:12.8023Z","shell.execute_reply.started":"2022-07-03T15:20:12.795133Z","shell.execute_reply":"2022-07-03T15:20:12.801242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dataframe['file_name'] = labels_dataframe['id'].apply(lambda x:train_dir+f\"{x}.jpg\")","metadata":{"execution":{"iopub.status.busy":"2022-07-10T13:49:22.901532Z","iopub.execute_input":"2022-07-10T13:49:22.901887Z","iopub.status.idle":"2022-07-10T13:49:22.916759Z","shell.execute_reply.started":"2022-07-10T13:49:22.901819Z","shell.execute_reply":"2022-07-10T13:49:22.915934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dataframe.head(5)['file_name'][0]","metadata":{"execution":{"iopub.status.busy":"2022-07-10T13:49:29.398932Z","iopub.execute_input":"2022-07-10T13:49:29.399458Z","iopub.status.idle":"2022-07-10T13:49:29.408485Z","shell.execute_reply.started":"2022-07-10T13:49:29.39941Z","shell.execute_reply":"2022-07-10T13:49:29.407697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dataframe.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T13:49:36.827014Z","iopub.execute_input":"2022-07-10T13:49:36.827351Z","iopub.status.idle":"2022-07-10T13:49:36.839242Z","shell.execute_reply.started":"2022-07-10T13:49:36.827293Z","shell.execute_reply":"2022-07-10T13:49:36.838359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"codes = []\nfor index, data in labels_dataframe.iterrows():\n    breed = data['breed']\n    codes.append(class_to_num[breed])","metadata":{"execution":{"iopub.status.busy":"2022-07-10T13:49:46.935552Z","iopub.execute_input":"2022-07-10T13:49:46.935879Z","iopub.status.idle":"2022-07-10T13:49:48.172302Z","shell.execute_reply.started":"2022-07-10T13:49:46.935826Z","shell.execute_reply":"2022-07-10T13:49:48.171543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-07-10T13:49:51.002725Z","iopub.execute_input":"2022-07-10T13:49:51.003296Z","iopub.status.idle":"2022-07-10T13:49:51.031754Z","shell.execute_reply.started":"2022-07-10T13:49:51.003246Z","shell.execute_reply":"2022-07-10T13:49:51.030868Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-07-10T13:51:29.394093Z","iopub.execute_input":"2022-07-10T13:51:29.394397Z","iopub.status.idle":"2022-07-10T13:51:29.401842Z","shell.execute_reply.started":"2022-07-10T13:51:29.394351Z","shell.execute_reply":"2022-07-10T13:51:29.400527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-07-10T13:51:35.481259Z","iopub.execute_input":"2022-07-10T13:51:35.482036Z","iopub.status.idle":"2022-07-10T13:51:35.489477Z","shell.execute_reply.started":"2022-07-10T13:51:35.481961Z","shell.execute_reply":"2022-07-10T13:51:35.488391Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Model","metadata":{}},{"cell_type":"code","source":"# from keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input as resnet_preprocess\n# from keras.applications.inception_v3 import InceptionV3, preprocess_input as inception_preprocess\n# from keras.applications.xception import Xception, preprocess_input as xception_preprocess\n# from keras.applications.nasnet import NASNetLarge, preprocess_input as nasnet_preprocess\n# from keras.layers.merge import concatenate\n\n# input_shape = (331,331,3)\n# input_layer = Input(shape=input_shape)\n\n\n# #first extractor inception_resnet\n# preprocessor_resnet = Lambda(resnet_preprocess)(input_layer)\n# inception_resnet = InceptionResNetV2(weights = 'imagenet',\n#                                      include_top = False,input_shape = input_shape,pooling ='avg')(preprocessor_resnet)\n\n# # second extractor InceptionV3\n# preprocessor_inception = Lambda(inception_preprocess)(input_layer)\n# inception_v3 = InceptionV3(weights = 'imagenet',\n#                                      include_top = False,input_shape = input_shape,pooling ='avg')(preprocessor_inception)\n# # Third extractor Xception\n# preprocessor_xception = Lambda(xception_preprocess)(input_layer)\n# xception = Xception(weights = 'imagenet',\n#                                      include_top = False,input_shape = input_shape,pooling ='avg')(preprocessor_xception)\n\n# # fourth extractor nasnet large\n# preprocessor_nasnet = Lambda(nasnet_preprocess)(input_layer)\n# nasnet = NASNetLarge(weights = 'imagenet',\n#                                      include_top = False,input_shape = input_shape,pooling ='avg')(preprocessor_nasnet)\n\n\n# merge = concatenate([inception_v3, xception,nasnet,inception_resnet])\n# model = Model(inputs = input_layer, output = merge)\n\n\n# from keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input as resnet_preprocess\n# from keras.applications.inception_v3 import InceptionV3, preprocess_input as inception_preprocess\nfrom keras.applications.xception import Xception, preprocess_input as xception_preprocess\nfrom keras.applications.nasnet import NASNetMobile, preprocess_input as nasnet_preprocess\nfrom keras.applications.densenet import DenseNet121, preprocess_input as densenet_preprocess\n# from keras.applications.mobilenet_v2 import MobileNetV2, preprocess_input as mobilenet_preprocess\nfrom keras.applications.resnet_v2 import ResNet50V2, preprocess_input as resnet_v2_preprocess\nfrom keras.layers.merge import concatenate\nfrom keras.layers import Input,Lambda\nfrom keras.models import Model\n\ninput_shape = (224,224,3)\ninput_layer = Input(shape=input_shape)\n\n\n#first extractor resnet50v2\npreprocessor_resnet = Lambda(resnet_v2_preprocess)(input_layer)\nresnet = ResNet50V2(weights = 'imagenet',\n                                     include_top = False,input_shape = input_shape,pooling ='avg')(preprocessor_resnet)\n\n# second extractor densenet\npreprocessor_inception = Lambda(densenet_preprocess)(input_layer)\ndensenet = DenseNet121(weights = 'imagenet',\n                                     include_top = False,input_shape = input_shape,pooling ='avg')(preprocessor_inception)\n# # Third extractor nasnet\npreprocessor_xception = Lambda(nasnet_preprocess)(input_layer)\nnasnet = NASNetMobile(weights = 'imagenet',\n                                     include_top = False,input_shape = input_shape,pooling ='avg')(preprocessor_xception)\n# Third extractor Xception\n# preprocessor_xception = Lambda(xception_preprocess)(input_layer)\n# xception = Xception(weights = 'imagenet',\n#                                      include_top = False,input_shape = input_shape,pooling ='avg')(preprocessor_xception)\n# # # # fourth extractor nasnet large\n# preprocessor_nasnet = Lambda(nasnet_preprocess)(input_layer)\n# nasnet = NASNetLarge(weights = 'imagenet',\n#                                      include_top = False,input_shape = input_shape,pooling ='avg')(preprocessor_nasnet)\n\n\nmerge = concatenate([resnet,nasnet,densenet])\nmodel = Model(inputs = input_layer, outputs = merge)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T13:52:27.575929Z","iopub.execute_input":"2022-07-10T13:52:27.576467Z","iopub.status.idle":"2022-07-10T13:52:50.318645Z","shell.execute_reply.started":"2022-07-10T13:52:27.576421Z","shell.execute_reply":"2022-07-10T13:52:50.31698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('/kaggle/working/feature_extractor.h5')","metadata":{"execution":{"iopub.status.busy":"2022-07-03T15:27:46.843188Z","iopub.execute_input":"2022-07-03T15:27:46.843573Z","iopub.status.idle":"2022-07-03T15:27:46.872035Z","shell.execute_reply.started":"2022-07-03T15:27:46.843512Z","shell.execute_reply":"2022-07-03T15:27:46.870422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"v= model.output.shape[1:]\nprint(v)","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.203617Z","iopub.status.idle":"2022-06-30T14:09:00.204175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(v)","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.205174Z","iopub.status.idle":"2022-06-30T14:09:00.205702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Feature extraction of data not labels","metadata":{}},{"cell_type":"code","source":"# for feature_extraction dataframe must have to contain file_name and  breed columns\ndef feature_extractor(df):\n    img_size = (224,224,3)\n    data_size = len(df)\n    batch_size = 20\n    X = np.zeros([data_size,4128], dtype=np.uint8)\n#     y = np.zeros([data_size,120], dtype=np.uint8)\n    datagen = ImageDataGenerator()\n    generator = datagen.flow_from_dataframe(df,\n    x_col = 'file_name', class_mode = None, \n    batch_size=20, shuffle = False,target_size = (img_size[:2]),color_mode = 'rgb')\n    i = 0\n    for input_batch in tqdm(generator):\n        input_batch = model.predict(input_batch)\n        X[i * batch_size : (i + 1) * batch_size] = input_batch\n        i += 1\n        if i * batch_size >= data_size:\n           break\n    return X\n        \n    ","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.206742Z","iopub.status.idle":"2022-06-30T14:09:00.20728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = feature_extractor(labels_dataframe)","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.2083Z","iopub.status.idle":"2022-06-30T14:09:00.208741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dataframe","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.20994Z","iopub.status.idle":"2022-06-30T14:09:00.210507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.211524Z","iopub.status.idle":"2022-06-30T14:09:00.212068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping,ModelCheckpoint, ReduceLROnPlateau\n#Prepare call backs\nEarlyStop_callback = keras.callbacks.EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)\ncheckpoint = ModelCheckpoint('/kaggle/working/checkpoing',\n                             monitor = 'val_loss',mode = 'min',save_best_only= True)\nlr = ReduceLROnPlateau(monitor = 'val_loss',factor = 0.5,patience = 3,min_lr = 0.00001)\nmy_callback=[EarlyStop_callback,checkpoint]","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.213103Z","iopub.status.idle":"2022-06-30T14:09:00.213636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dnn = keras.models.Sequential([\n    InputLayer(X.shape[1:]),\n    Dropout(0.7),\n    Dense(n_classes, activation='softmax')\n])\n\ndnn.compile(optimizer='adam',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n\n#Train simple DNN on extracted features.\nh = dnn.fit(X , y,\n            batch_size=128,\n            epochs=60,\n            validation_split=0.1 ,\n           callbacks = my_callback)","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.214649Z","iopub.status.idle":"2022-06-30T14:09:00.215174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plot the Result ","metadata":{}},{"cell_type":"code","source":"fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 12))\n\nax1.plot(h.history['loss'],color = 'b',label = 'loss')\nax1.plot(h.history['val_loss'],color = 'r',label = 'val_loss')\nax1.set_xticks(np.arange(1, 60, 1))\nax1.set_yticks(np.arange(0, 1, 0.1))\nax1.legend(['loss','val_loss'],shadow = True)\n\n\nax2.plot(h.history['accuracy'],color = 'green',label = 'accuracy')\nax2.plot(h.history['val_accuracy'],color = 'red',label = 'val_accuracy')\nax2.legend(['accuracy','val_accuracy'],shadow = True)\n# ax2.set_xticks(np.arange(1, 60, 1))\n# ax2.set_yticks(np.arange(0, 60, 0.1))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.216107Z","iopub.status.idle":"2022-06-30T14:09:00.216606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Saving and loading the model ","metadata":{}},{"cell_type":"code","source":"\nfrom keras.models import load_model\ndnn.save('/kaggle/working/dogbreed.h5')\n\n# saved_model = load_model('/kaggle/working/dogbreed.h5')\n","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.217524Z","iopub.status.idle":"2022-06-30T14:09:00.218043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prediction of the Test Data","metadata":{}},{"cell_type":"code","source":"\ntest_data = []\nfor pic in os.listdir(test_dir):\n    test_data.append(test_dir+pic)","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.218957Z","iopub.status.idle":"2022-06-30T14:09:00.219447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.220299Z","iopub.status.idle":"2022-06-30T14:09:00.220731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataframe = pd.DataFrame({'file_name':test_data})","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.22155Z","iopub.status.idle":"2022-06-30T14:09:00.222013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataframe","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.222833Z","iopub.status.idle":"2022-06-30T14:09:00.223269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_features = feature_extractor(test_dataframe)","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.224082Z","iopub.status.idle":"2022-06-30T14:09:00.224498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = dnn.predict(test_features)","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.225314Z","iopub.status.idle":"2022-06-30T14:09:00.225746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.226551Z","iopub.status.idle":"2022-06-30T14:09:00.22701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_key(val): \n        for key, value in class_to_num.items(): \n         if val == value: \n             return key \n            \npred_codes = np.argmax(y_pred, axis = 1)\npred_codes\n    ","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.328946Z","iopub.execute_input":"2022-06-30T14:09:00.32956Z","iopub.status.idle":"2022-06-30T14:09:00.342359Z","shell.execute_reply.started":"2022-06-30T14:09:00.329505Z","shell.execute_reply":"2022-06-30T14:09:00.341184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = []\nfor i in pred_codes:\n    predictions.append(get_key(i))","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.343467Z","iopub.status.idle":"2022-06-30T14:09:00.344278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.345482Z","iopub.status.idle":"2022-06-30T14:09:00.345968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataframe['breed'] = predictions","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.347128Z","iopub.status.idle":"2022-06-30T14:09:00.347662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataframe","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.348955Z","iopub.status.idle":"2022-06-30T14:09:00.349513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Drawing some images ","metadata":{}},{"cell_type":"code","source":"\nplt.figure(figsize=(6,6))\n\nfor index , data in test_dataframe[:10].iterrows():\n    img = data['file_name']\n    label = data['breed']\n    img = cv2.imread(img)\n#     plt.subplot(2,5, index+1)\n    plt.imshow(img)\n    plt.xlabel(label,fontsize = (15))\n    plt.tight_layout()\n    plt.show()\n    \n","metadata":{"execution":{"iopub.status.busy":"2022-06-30T14:09:00.350743Z","iopub.status.idle":"2022-06-30T14:09:00.351259Z"},"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":[]}]}