{"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\n# for 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-09-12T07:00:37.459659Z","iopub.execute_input":"2021-09-12T07:00:37.460045Z","iopub.status.idle":"2021-09-12T07:00:37.469359Z","shell.execute_reply.started":"2021-09-12T07:00:37.459946Z","shell.execute_reply":"2021-09-12T07:00:37.468182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","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":{}},{"cell_type":"code","source":"import keras \nprint(keras.__version__)","metadata":{"execution":{"iopub.status.busy":"2021-09-12T07:00:52.587227Z","iopub.execute_input":"2021-09-12T07:00:52.587549Z","iopub.status.idle":"2021-09-12T07:00:57.234715Z","shell.execute_reply.started":"2021-09-12T07:00:52.587521Z","shell.execute_reply":"2021-09-12T07:00:57.233879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#### STEP-1 Loading all necessary libraries \nimport 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":"2021-09-11T13:15:07.25696Z","iopub.execute_input":"2021-09-11T13:15:07.257247Z","iopub.status.idle":"2021-09-11T13:15:12.460275Z","shell.execute_reply.started":"2021-09-11T13:15:07.257219Z","shell.execute_reply":"2021-09-11T13:15:12.459396Z"},"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.","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:15:12.462733Z","iopub.execute_input":"2021-09-11T13:15:12.4631Z","iopub.status.idle":"2021-09-11T13:15:12.467303Z","shell.execute_reply.started":"2021-09-11T13:15:12.463062Z","shell.execute_reply":"2021-09-11T13:15:12.466287Z"},"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.\nsample_df = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')\n#Incpect labels_dataframe.\nlabels_dataframe.head(5)\n# here sample df is the format of data to be submitted into kaggle \n","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:15:12.469499Z","iopub.execute_input":"2021-09-11T13:15:12.469876Z","iopub.status.idle":"2021-09-11T13:15:13.285472Z","shell.execute_reply.started":"2021-09-11T13:15:12.469838Z","shell.execute_reply":"2021-09-11T13:15:13.284579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dataframe.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:15:13.287237Z","iopub.execute_input":"2021-09-11T13:15:13.287651Z","iopub.status.idle":"2021-09-11T13:15:13.294371Z","shell.execute_reply.started":"2021-09-11T13:15:13.287612Z","shell.execute_reply":"2021-09-11T13:15:13.293414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# little reminder of working with file and folder with os \nprint(f\"number of pic in test_dir {len(os.listdir(test_dir))}\")\nprint(f\"number of pic in train dir {len(os.listdir(train_dir))}\")","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:15:13.29597Z","iopub.execute_input":"2021-09-11T13:15:13.296595Z","iopub.status.idle":"2021-09-11T13:15:14.025864Z","shell.execute_reply.started":"2021-09-11T13:15:13.296556Z","shell.execute_reply":"2021-09-11T13:15:14.025009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"so here the size of labels_dataframe and the size of train directory is the same \nand in train directory every file have unique name and in labels_dataframe we can see the label of that picture and labels_dataframe and train directory both are in ordered manned ","metadata":{}},{"cell_type":"code","source":"# lets crate the  number of unique labels we have available in our dataset \ndog_breeds = sorted(list(set(labels_dataframe.breed)))\nn_classes = len(dog_breeds)\nprint(n_classes)\nprint(dog_breeds[:10])\n\n\n# here we have 120 dog breeeds to classify ","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:15:14.027314Z","iopub.execute_input":"2021-09-11T13:15:14.027704Z","iopub.status.idle":"2021-09-11T13:15:14.038662Z","shell.execute_reply.started":"2021-09-11T13:15:14.027661Z","shell.execute_reply":"2021-09-11T13:15:14.03761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# so these dog breeds are string so we need to change them into some number for classification\n#Map each label string to an integer label.\nclass_to_num = dict(zip(dog_breeds, range(n_classes)))\nclass_to_num","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:15:14.040213Z","iopub.execute_input":"2021-09-11T13:15:14.040722Z","iopub.status.idle":"2021-09-11T13:15:14.051583Z","shell.execute_reply.started":"2021-09-11T13:15:14.040682Z","shell.execute_reply":"2021-09-11T13:15:14.050627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(train_dir+labels_dataframe.id+'.jpg')[0]\n# this is how we will access the files from our training data ","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:15:14.054539Z","iopub.execute_input":"2021-09-11T13:15:14.054886Z","iopub.status.idle":"2021-09-11T13:15:14.079386Z","shell.execute_reply.started":"2021-09-11T13:15:14.054852Z","shell.execute_reply":"2021-09-11T13:15:14.078547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv2.imread((train_dir+labels_dataframe.id+'.jpg')[0]).shape\n# size of image ","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:15:14.082258Z","iopub.execute_input":"2021-09-11T13:15:14.082522Z","iopub.status.idle":"2021-09-11T13:15:14.125701Z","shell.execute_reply.started":"2021-09-11T13:15:14.082493Z","shell.execute_reply":"2021-09-11T13:15:14.12494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(cv2.imread((train_dir+labels_dataframe.id+'.jpg')[0]))","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:15:14.128606Z","iopub.execute_input":"2021-09-11T13:15:14.128857Z","iopub.status.idle":"2021-09-11T13:15:14.328854Z","shell.execute_reply.started":"2021-09-11T13:15:14.128833Z","shell.execute_reply":"2021-09-11T13:15:14.328073Z"},"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":"2021-09-11T13:15:14.329921Z","iopub.execute_input":"2021-09-11T13:15:14.330273Z","iopub.status.idle":"2021-09-11T13:15:14.343868Z","shell.execute_reply.started":"2021-09-11T13:15:14.330231Z","shell.execute_reply":"2021-09-11T13:15:14.343106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dataframe.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:15:14.344835Z","iopub.execute_input":"2021-09-11T13:15:14.345081Z","iopub.status.idle":"2021-09-11T13:15:14.357284Z","shell.execute_reply.started":"2021-09-11T13:15:14.345056Z","shell.execute_reply":"2021-09-11T13:15:14.356125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# now we need to map our breed category according the class_to_num \nlabels_dataframe['breed'] = labels_dataframe.breed.map(class_to_num)\n","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:15:14.359919Z","iopub.execute_input":"2021-09-11T13:15:14.360164Z","iopub.status.idle":"2021-09-11T13:15:14.36603Z","shell.execute_reply.started":"2021-09-11T13:15:14.360139Z","shell.execute_reply":"2021-09-11T13:15:14.365208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dataframe.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:15:15.106938Z","iopub.execute_input":"2021-09-11T13:15:15.107257Z","iopub.status.idle":"2021-09-11T13:15:15.120746Z","shell.execute_reply.started":"2021-09-11T13:15:15.107227Z","shell.execute_reply":"2021-09-11T13:15:15.119879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = to_categorical(labels_dataframe.breed)\n# encoded our y variable to pass in our model","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:15:15.976243Z","iopub.execute_input":"2021-09-11T13:15:15.976769Z","iopub.status.idle":"2021-09-11T13:15:15.982361Z","shell.execute_reply.started":"2021-09-11T13:15:15.976603Z","shell.execute_reply":"2021-09-11T13:15:15.981212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Uptill now we have created our dataframe with file names and categories ","metadata":{}},{"cell_type":"markdown","source":"#### Lets Design model architecture for feature extraction\n","metadata":{}},{"cell_type":"code","source":"from keras.applications.resnet_v2 import ResNet50V2 , preprocess_input as resnet_preprocess\nfrom keras.applications.densenet import DenseNet121, preprocess_input as densenet_preprocess\nfrom keras.layers.merge import concatenate\n\ninput_shape = (331,331,3)\ninput_layer = Input(shape=input_shape)\n\n\n#first extractor inception_resnet\npreprocessor_resnet = Lambda(resnet_preprocess)(input_layer)\ninception_resnet = ResNet50V2(weights = 'imagenet',\n                                     include_top = False,input_shape = input_shape,pooling ='avg')(preprocessor_resnet)\n\npreprocessor_densenet = Lambda(densenet_preprocess)(input_layer)\ndensenet = DenseNet121(weights = 'imagenet',\n                                     include_top = False,input_shape = input_shape,pooling ='avg')(preprocessor_densenet)\n\n\nmerge = concatenate([inception_resnet,densenet])\nmodel = Model(inputs = input_layer, outputs = merge)","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:31:01.90419Z","iopub.execute_input":"2021-09-11T13:31:01.904541Z","iopub.status.idle":"2021-09-11T13:31:08.684938Z","shell.execute_reply.started":"2021-09-11T13:31:01.904509Z","shell.execute_reply":"2021-09-11T13:31:08.684086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:31:21.016816Z","iopub.execute_input":"2021-09-11T13:31:21.017174Z","iopub.status.idle":"2021-09-11T13:31:21.065652Z","shell.execute_reply.started":"2021-09-11T13:31:21.017142Z","shell.execute_reply":"2021-09-11T13:31:21.064604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('feature_extractor.h5')","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:31:25.577958Z","iopub.execute_input":"2021-09-11T13:31:25.578294Z","iopub.status.idle":"2021-09-11T13:31:26.527039Z","shell.execute_reply.started":"2021-09-11T13:31:25.578257Z","shell.execute_reply":"2021-09-11T13:31:26.525955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loaded_model = keras.models.load_model('./feature_extractor.h5')","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:31:27.388409Z","iopub.execute_input":"2021-09-11T13:31:27.388732Z","iopub.status.idle":"2021-09-11T13:31:32.472457Z","shell.execute_reply.started":"2021-09-11T13:31:27.388703Z","shell.execute_reply":"2021-09-11T13:31:32.471533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import plot_model\nplot_model(model, show_shapes = True)","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:31:42.075038Z","iopub.execute_input":"2021-09-11T13:31:42.075388Z","iopub.status.idle":"2021-09-11T13:31:42.236604Z","shell.execute_reply.started":"2021-09-11T13:31:42.075348Z","shell.execute_reply":"2021-09-11T13:31:42.235699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.output.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:31:44.308572Z","iopub.execute_input":"2021-09-11T13:31:44.30892Z","iopub.status.idle":"2021-09-11T13:31:44.315555Z","shell.execute_reply.started":"2021-09-11T13:31:44.308886Z","shell.execute_reply":"2021-09-11T13:31:44.31451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(model.trainable_weights)","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:31:45.114486Z","iopub.execute_input":"2021-09-11T13:31:45.114821Z","iopub.status.idle":"2021-09-11T13:31:45.1273Z","shell.execute_reply.started":"2021-09-11T13:31:45.114791Z","shell.execute_reply":"2021-09-11T13:31:45.126062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Feature Extraction by usinng pretrained models ","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 = (331,331,3)\n    data_size = len(df)\n    batch_size = 20\n    X = np.zeros([data_size,3072], dtype=np.uint8)\n#     y = np.zeros([data_size,120], dtype=np.uint8)\n    datagen = ImageDataGenerator() # here we dont need to do any image augementaion because we are prediction features \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    \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 ","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:31:55.758383Z","iopub.execute_input":"2021-09-11T13:31:55.758715Z","iopub.status.idle":"2021-09-11T13:31:55.766405Z","shell.execute_reply.started":"2021-09-11T13:31:55.758684Z","shell.execute_reply":"2021-09-11T13:31:55.765372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = feature_extractor(labels_dataframe)\n","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:31:56.697439Z","iopub.execute_input":"2021-09-11T13:31:56.697808Z","iopub.status.idle":"2021-09-11T13:35:21.716798Z","shell.execute_reply.started":"2021-09-11T13:31:56.697746Z","shell.execute_reply":"2021-09-11T13:35:21.715766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Defining some model callbacks for our training","metadata":{}},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping,ModelCheckpoint, ReduceLROnPlateau\n#Prepare call backs\nEarlyStop_callback = keras.callbacks.EarlyStopping(monitor='val_loss', patience=20, restore_best_weights=True)\ncheckpoint = ModelCheckpoint('/kaggle/working/checkpoint',\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":"2021-09-11T13:35:59.15726Z","iopub.execute_input":"2021-09-11T13:35:59.157636Z","iopub.status.idle":"2021-09-11T13:35:59.163939Z","shell.execute_reply.started":"2021-09-11T13:35:59.157598Z","shell.execute_reply":"2021-09-11T13:35:59.162917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Lets create a model for training \n","metadata":{}},{"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":"2021-09-11T13:36:00.861182Z","iopub.execute_input":"2021-09-11T13:36:00.861542Z","iopub.status.idle":"2021-09-11T13:36:18.04236Z","shell.execute_reply.started":"2021-09-11T13:36:00.86151Z","shell.execute_reply":"2021-09-11T13:36:18.041567Z"},"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":"2021-09-11T13:36:57.851587Z","iopub.execute_input":"2021-09-11T13:36:57.851938Z","iopub.status.idle":"2021-09-11T13:36:58.543715Z","shell.execute_reply.started":"2021-09-11T13:36:57.851905Z","shell.execute_reply":"2021-09-11T13:36:58.542735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# saving the model\nfrom keras.models import load_model\ndnn.save('/kaggle/working/dogbreed.h5')","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:36:59.952987Z","iopub.execute_input":"2021-09-11T13:36:59.953312Z","iopub.status.idle":"2021-09-11T13:36:59.974095Z","shell.execute_reply.started":"2021-09-11T13:36:59.953275Z","shell.execute_reply":"2021-09-11T13:36:59.97335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\nwith open('dog_breeds_category.pickle', 'wb') as handle:\n    pickle.dump(dog_breeds, handle, protocol=pickle.HIGHEST_PROTOCOL)","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:37:02.453099Z","iopub.execute_input":"2021-09-11T13:37:02.453443Z","iopub.status.idle":"2021-09-11T13:37:02.458655Z","shell.execute_reply.started":"2021-09-11T13:37:02.453409Z","shell.execute_reply":"2021-09-11T13:37:02.457622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### its time for prediction \n","metadata":{}},{"cell_type":"code","source":"test_data = []\nids = []\nfor pic in os.listdir(test_dir):\n    ids.append(pic.split('.')[0])\n    test_data.append(test_dir+pic)","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:56:34.31381Z","iopub.execute_input":"2021-09-11T13:56:34.314146Z","iopub.status.idle":"2021-09-11T13:56:34.334142Z","shell.execute_reply.started":"2021-09-11T13:56:34.314112Z","shell.execute_reply":"2021-09-11T13:56:34.333357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataframe = pd.DataFrame({'file_name':test_data})\n# we are converting into a dataframe beacause our feature extractor funtion only support dataframe","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:56:35.1941Z","iopub.execute_input":"2021-09-11T13:56:35.194433Z","iopub.status.idle":"2021-09-11T13:56:35.200898Z","shell.execute_reply.started":"2021-09-11T13:56:35.194401Z","shell.execute_reply":"2021-09-11T13:56:35.200072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_features = feature_extractor(test_dataframe)\n","metadata":{"execution":{"iopub.status.busy":"2021-09-11T13:56:35.944383Z","iopub.execute_input":"2021-09-11T13:56:35.944715Z","iopub.status.idle":"2021-09-11T14:00:35.36779Z","shell.execute_reply.started":"2021-09-11T13:56:35.944685Z","shell.execute_reply":"2021-09-11T14:00:35.366677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = dnn.predict(test_features)","metadata":{"execution":{"iopub.status.busy":"2021-09-11T14:00:35.369466Z","iopub.execute_input":"2021-09-11T14:00:35.369829Z","iopub.status.idle":"2021-09-11T14:00:35.630982Z","shell.execute_reply.started":"2021-09-11T14:00:35.369789Z","shell.execute_reply":"2021-09-11T14:00:35.630145Z"},"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 \npred_codes = np.argmax(y_pred, axis = 1)\npredictions = []\nfor i in pred_codes:\n    predictions.append(get_key(i))","metadata":{"execution":{"iopub.status.busy":"2021-09-11T14:00:35.634624Z","iopub.execute_input":"2021-09-11T14:00:35.634881Z","iopub.status.idle":"2021-09-11T14:00:35.883461Z","shell.execute_reply.started":"2021-09-11T14:00:35.634854Z","shell.execute_reply":"2021-09-11T14:00:35.882624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataframe['breed'] = predictions","metadata":{"execution":{"iopub.status.busy":"2021-09-11T14:00:35.884976Z","iopub.execute_input":"2021-09-11T14:00:35.885305Z","iopub.status.idle":"2021-09-11T14:00:35.891303Z","shell.execute_reply.started":"2021-09-11T14:00:35.885269Z","shell.execute_reply":"2021-09-11T14:00:35.890143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.set_printoptions(suppress=True)","metadata":{"execution":{"iopub.status.busy":"2021-09-11T14:00:35.892948Z","iopub.execute_input":"2021-09-11T14:00:35.893359Z","iopub.status.idle":"2021-09-11T14:00:35.90114Z","shell.execute_reply.started":"2021-09-11T14:00:35.893305Z","shell.execute_reply":"2021-09-11T14:00:35.90032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.float_format', lambda x: '%.10f' % x)","metadata":{"execution":{"iopub.status.busy":"2021-09-11T14:00:35.902508Z","iopub.execute_input":"2021-09-11T14:00:35.902916Z","iopub.status.idle":"2021-09-11T14:00:35.910521Z","shell.execute_reply.started":"2021-09-11T14:00:35.902872Z","shell.execute_reply":"2021-09-11T14:00:35.909729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### creating submission  file for the competetion","metadata":{}},{"cell_type":"code","source":"submission = pd.DataFrame(y_pred, columns = dog_breeds)\nsubmission['id'] = ids\n","metadata":{"execution":{"iopub.status.busy":"2021-09-11T14:00:35.911713Z","iopub.execute_input":"2021-09-11T14:00:35.9121Z","iopub.status.idle":"2021-09-11T14:00:35.921959Z","shell.execute_reply.started":"2021-09-11T14:00:35.912063Z","shell.execute_reply":"2021-09-11T14:00:35.921158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.set_index('id')","metadata":{"execution":{"iopub.status.busy":"2021-09-11T14:00:35.925159Z","iopub.execute_input":"2021-09-11T14:00:35.925444Z","iopub.status.idle":"2021-09-11T14:00:35.960162Z","shell.execute_reply.started":"2021-09-11T14:00:35.92542Z","shell.execute_reply":"2021-09-11T14:00:35.95928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('sumbmission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2021-09-11T14:00:35.961455Z","iopub.execute_input":"2021-09-11T14:00:35.961806Z","iopub.status.idle":"2021-09-11T14:00:38.183025Z","shell.execute_reply.started":"2021-09-11T14:00:35.96177Z","shell.execute_reply":"2021-09-11T14:00:38.182062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plotting some images ","metadata":{}},{"cell_type":"code","source":"plt.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()","metadata":{"execution":{"iopub.status.busy":"2021-09-11T14:00:38.184552Z","iopub.execute_input":"2021-09-11T14:00:38.184944Z","iopub.status.idle":"2021-09-11T14:00:40.650977Z","shell.execute_reply.started":"2021-09-11T14:00:38.184901Z","shell.execute_reply":"2021-09-11T14:00:40.650198Z"},"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":[]}]}