{"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":"2022-07-12T05:12:51.525254Z","iopub.execute_input":"2022-07-12T05:12:51.525656Z","iopub.status.idle":"2022-07-12T05:12:51.533050Z","shell.execute_reply.started":"2022-07-12T05:12:51.525626Z","shell.execute_reply":"2022-07-12T05:12:51.531007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras \nprint(keras.__version__)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T05:12:51.546172Z","iopub.execute_input":"2022-07-12T05:12:51.546494Z","iopub.status.idle":"2022-07-12T05:12:57.592184Z","shell.execute_reply.started":"2022-07-12T05:12:51.546464Z","shell.execute_reply":"2022-07-12T05:12:57.590945Z"},"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 tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing.image import load_img","metadata":{"execution":{"iopub.status.busy":"2022-07-12T05:12:57.594446Z","iopub.execute_input":"2022-07-12T05:12:57.595053Z","iopub.status.idle":"2022-07-12T05:12:58.771803Z","shell.execute_reply.started":"2022-07-12T05:12:57.595014Z","shell.execute_reply":"2022-07-12T05:12:58.770523Z"},"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":"2022-07-12T05:12:58.773289Z","iopub.execute_input":"2022-07-12T05:12:58.774362Z","iopub.status.idle":"2022-07-12T05:12:58.785735Z","shell.execute_reply.started":"2022-07-12T05:12:58.774329Z","shell.execute_reply":"2022-07-12T05:12:58.783867Z"},"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":"2022-07-12T05:12:58.790346Z","iopub.execute_input":"2022-07-12T05:12:58.791112Z","iopub.status.idle":"2022-07-12T05:12:59.364885Z","shell.execute_reply.started":"2022-07-12T05:12:58.791080Z","shell.execute_reply":"2022-07-12T05:12:59.363096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dataframe.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-12T05:12:59.366954Z","iopub.execute_input":"2022-07-12T05:12:59.367524Z","iopub.status.idle":"2022-07-12T05:12:59.377459Z","shell.execute_reply.started":"2022-07-12T05:12:59.367464Z","shell.execute_reply":"2022-07-12T05:12:59.376148Z"},"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":"2022-07-12T05:12:59.379106Z","iopub.execute_input":"2022-07-12T05:12:59.380277Z","iopub.status.idle":"2022-07-12T05:12:59.906238Z","shell.execute_reply.started":"2022-07-12T05:12:59.380234Z","shell.execute_reply":"2022-07-12T05:12:59.904850Z"},"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":"2022-07-12T05:12:59.907839Z","iopub.execute_input":"2022-07-12T05:12:59.908331Z","iopub.status.idle":"2022-07-12T05:12:59.920805Z","shell.execute_reply.started":"2022-07-12T05:12:59.908288Z","shell.execute_reply":"2022-07-12T05:12:59.919443Z"},"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":"2022-07-12T05:12:59.923052Z","iopub.execute_input":"2022-07-12T05:12:59.923858Z","iopub.status.idle":"2022-07-12T05:12:59.942074Z","shell.execute_reply.started":"2022-07-12T05:12:59.923816Z","shell.execute_reply":"2022-07-12T05:12:59.940826Z"},"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":"2022-07-12T05:12:59.944316Z","iopub.execute_input":"2022-07-12T05:12:59.945093Z","iopub.status.idle":"2022-07-12T05:12:59.961137Z","shell.execute_reply.started":"2022-07-12T05:12:59.945052Z","shell.execute_reply":"2022-07-12T05:12:59.959113Z"},"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":"2022-07-12T05:12:59.967102Z","iopub.execute_input":"2022-07-12T05:12:59.968042Z","iopub.status.idle":"2022-07-12T05:13:00.006500Z","shell.execute_reply.started":"2022-07-12T05:12:59.968000Z","shell.execute_reply":"2022-07-12T05:13:00.005120Z"},"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":"2022-07-12T05:13:00.008363Z","iopub.execute_input":"2022-07-12T05:13:00.008930Z","iopub.status.idle":"2022-07-12T05:13:00.307251Z","shell.execute_reply.started":"2022-07-12T05:13:00.008890Z","shell.execute_reply":"2022-07-12T05:13:00.306051Z"},"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-12T05:13:00.308587Z","iopub.execute_input":"2022-07-12T05:13:00.309094Z","iopub.status.idle":"2022-07-12T05:13:00.326622Z","shell.execute_reply.started":"2022-07-12T05:13:00.309033Z","shell.execute_reply":"2022-07-12T05:13:00.325365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dataframe.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T05:13:00.328900Z","iopub.execute_input":"2022-07-12T05:13:00.330084Z","iopub.status.idle":"2022-07-12T05:13:00.344081Z","shell.execute_reply.started":"2022-07-12T05:13:00.330040Z","shell.execute_reply":"2022-07-12T05:13:00.342663Z"},"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":"2022-07-12T05:13:00.347407Z","iopub.execute_input":"2022-07-12T05:13:00.348020Z","iopub.status.idle":"2022-07-12T05:13:00.363435Z","shell.execute_reply.started":"2022-07-12T05:13:00.347944Z","shell.execute_reply":"2022-07-12T05:13:00.362161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dataframe.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T05:13:00.365637Z","iopub.execute_input":"2022-07-12T05:13:00.366413Z","iopub.status.idle":"2022-07-12T05:13:00.382883Z","shell.execute_reply.started":"2022-07-12T05:13:00.366368Z","shell.execute_reply":"2022-07-12T05:13:00.381537Z"},"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":"2022-07-12T05:13:00.385026Z","iopub.execute_input":"2022-07-12T05:13:00.385611Z","iopub.status.idle":"2022-07-12T05:13:00.396337Z","shell.execute_reply.started":"2022-07-12T05:13:00.385567Z","shell.execute_reply":"2022-07-12T05:13:00.395114Z"},"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":"2022-07-12T05:13:00.397829Z","iopub.execute_input":"2022-07-12T05:13:00.398147Z","iopub.status.idle":"2022-07-12T05:13:13.187278Z","shell.execute_reply.started":"2022-07-12T05:13:00.398106Z","shell.execute_reply":"2022-07-12T05:13:13.185827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T05:13:13.189149Z","iopub.execute_input":"2022-07-12T05:13:13.190387Z","iopub.status.idle":"2022-07-12T05:13:13.239877Z","shell.execute_reply.started":"2022-07-12T05:13:13.190342Z","shell.execute_reply":"2022-07-12T05:13:13.238387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('feature_extractor.h5')","metadata":{"execution":{"iopub.status.busy":"2022-07-12T05:13:13.242184Z","iopub.execute_input":"2022-07-12T05:13:13.242672Z","iopub.status.idle":"2022-07-12T05:13:14.634046Z","shell.execute_reply.started":"2022-07-12T05:13:13.242627Z","shell.execute_reply":"2022-07-12T05:13:14.632671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loaded_model = keras.models.load_model('./feature_extractor.h5')","metadata":{"execution":{"iopub.status.busy":"2022-07-12T05:13:14.639781Z","iopub.execute_input":"2022-07-12T05:13:14.643052Z","iopub.status.idle":"2022-07-12T05:13:21.102149Z","shell.execute_reply.started":"2022-07-12T05:13:14.643003Z","shell.execute_reply":"2022-07-12T05:13:21.100793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import plot_model\nplot_model(model, show_shapes = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T05:13:21.104195Z","iopub.execute_input":"2022-07-12T05:13:21.104672Z","iopub.status.idle":"2022-07-12T05:13:22.211829Z","shell.execute_reply.started":"2022-07-12T05:13:21.104641Z","shell.execute_reply":"2022-07-12T05:13:22.210331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.output.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-12T05:13:22.213868Z","iopub.execute_input":"2022-07-12T05:13:22.214800Z","iopub.status.idle":"2022-07-12T05:13:22.224507Z","shell.execute_reply.started":"2022-07-12T05:13:22.214749Z","shell.execute_reply":"2022-07-12T05:13:22.223052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(model.trainable_weights)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T05:13:22.226421Z","iopub.execute_input":"2022-07-12T05:13:22.227632Z","iopub.status.idle":"2022-07-12T05:13:22.247252Z","shell.execute_reply.started":"2022-07-12T05:13:22.227585Z","shell.execute_reply":"2022-07-12T05:13:22.245503Z"},"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":"2022-07-12T05:13:22.250022Z","iopub.execute_input":"2022-07-12T05:13:22.251434Z","iopub.status.idle":"2022-07-12T05:13:22.262479Z","shell.execute_reply.started":"2022-07-12T05:13:22.251388Z","shell.execute_reply":"2022-07-12T05:13:22.261151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = feature_extractor(labels_dataframe)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-12T05:13:22.265869Z","iopub.execute_input":"2022-07-12T05:13:22.266889Z","iopub.status.idle":"2022-07-12T05:17:15.797511Z","shell.execute_reply.started":"2022-07-12T05:13:22.266843Z","shell.execute_reply":"2022-07-12T05:17:15.796247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-12T05:17:15.799736Z","iopub.execute_input":"2022-07-12T05:17:15.800151Z","iopub.status.idle":"2022-07-12T05:17:15.813042Z","shell.execute_reply.started":"2022-07-12T05:17:15.800107Z","shell.execute_reply":"2022-07-12T05:17:15.811674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X\n","metadata":{"execution":{"iopub.status.busy":"2022-07-12T05:17:15.816883Z","iopub.execute_input":"2022-07-12T05:17:15.818211Z","iopub.status.idle":"2022-07-12T05:17:15.833934Z","shell.execute_reply.started":"2022-07-12T05:17:15.818168Z","shell.execute_reply":"2022-07-12T05:17:15.832627Z"},"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":"2022-07-12T05:17:15.855740Z","iopub.execute_input":"2022-07-12T05:17:15.856524Z","iopub.status.idle":"2022-07-12T05:17:15.869481Z","shell.execute_reply.started":"2022-07-12T05:17:15.856482Z","shell.execute_reply":"2022-07-12T05:17:15.868046Z"},"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":"2022-07-12T05:17:15.889152Z","iopub.execute_input":"2022-07-12T05:17:15.889952Z","iopub.status.idle":"2022-07-12T05:17:36.701596Z","shell.execute_reply.started":"2022-07-12T05:17:15.889882Z","shell.execute_reply":"2022-07-12T05:17:36.700051Z"},"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-07-12T05:17:36.703271Z","iopub.execute_input":"2022-07-12T05:17:36.704068Z","iopub.status.idle":"2022-07-12T05:17:37.622413Z","shell.execute_reply.started":"2022-07-12T05:17:36.704020Z","shell.execute_reply":"2022-07-12T05:17:37.621031Z"},"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":"2022-07-12T05:17:37.626060Z","iopub.execute_input":"2022-07-12T05:17:37.626370Z","iopub.status.idle":"2022-07-12T05:17:37.654424Z","shell.execute_reply.started":"2022-07-12T05:17:37.626341Z","shell.execute_reply":"2022-07-12T05:17:37.653247Z"},"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":"2022-07-12T05:17:37.657723Z","iopub.execute_input":"2022-07-12T05:17:37.658540Z","iopub.status.idle":"2022-07-12T05:17:37.666255Z","shell.execute_reply.started":"2022-07-12T05:17:37.658505Z","shell.execute_reply":"2022-07-12T05:17:37.664565Z"},"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":"2022-07-12T05:17:37.668411Z","iopub.execute_input":"2022-07-12T05:17:37.669151Z","iopub.status.idle":"2022-07-12T05:17:37.691845Z","shell.execute_reply.started":"2022-07-12T05:17:37.669105Z","shell.execute_reply":"2022-07-12T05:17:37.690713Z"},"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":"2022-07-12T05:17:37.693520Z","iopub.execute_input":"2022-07-12T05:17:37.694160Z","iopub.status.idle":"2022-07-12T05:17:37.703235Z","shell.execute_reply.started":"2022-07-12T05:17:37.694114Z","shell.execute_reply":"2022-07-12T05:17:37.701670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_features = feature_extractor(test_dataframe)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-12T05:17:37.705155Z","iopub.execute_input":"2022-07-12T05:17:37.705925Z","iopub.status.idle":"2022-07-12T05:21:20.560748Z","shell.execute_reply.started":"2022-07-12T05:17:37.705880Z","shell.execute_reply":"2022-07-12T05:21:20.558293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = dnn.predict(test_features)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T05:21:20.562947Z","iopub.execute_input":"2022-07-12T05:21:20.563384Z","iopub.status.idle":"2022-07-12T05:21:21.264021Z","shell.execute_reply.started":"2022-07-12T05:21:20.563339Z","shell.execute_reply":"2022-07-12T05:21:21.262659Z"},"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":"2022-07-12T05:21:21.266355Z","iopub.execute_input":"2022-07-12T05:21:21.267274Z","iopub.status.idle":"2022-07-12T05:21:21.475821Z","shell.execute_reply.started":"2022-07-12T05:21:21.267227Z","shell.execute_reply":"2022-07-12T05:21:21.474497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataframe['breed'] = predictions","metadata":{"execution":{"iopub.status.busy":"2022-07-12T05:21:21.477286Z","iopub.execute_input":"2022-07-12T05:21:21.477706Z","iopub.status.idle":"2022-07-12T05:21:21.486523Z","shell.execute_reply.started":"2022-07-12T05:21:21.477664Z","shell.execute_reply":"2022-07-12T05:21:21.485197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.set_printoptions(suppress=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T05:21:21.488160Z","iopub.execute_input":"2022-07-12T05:21:21.489122Z","iopub.status.idle":"2022-07-12T05:21:21.499970Z","shell.execute_reply.started":"2022-07-12T05:21:21.489076Z","shell.execute_reply":"2022-07-12T05:21:21.498490Z"},"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":"2022-07-12T05:21:21.501167Z","iopub.execute_input":"2022-07-12T05:21:21.502424Z","iopub.status.idle":"2022-07-12T05:21:21.515376Z","shell.execute_reply.started":"2022-07-12T05:21:21.502395Z","shell.execute_reply":"2022-07-12T05:21:21.513893Z"},"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":"2022-07-12T05:21:21.517275Z","iopub.execute_input":"2022-07-12T05:21:21.518183Z","iopub.status.idle":"2022-07-12T05:21:21.530941Z","shell.execute_reply.started":"2022-07-12T05:21:21.518111Z","shell.execute_reply":"2022-07-12T05:21:21.529692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.set_index('id')","metadata":{"execution":{"iopub.status.busy":"2022-07-12T05:21:21.532861Z","iopub.execute_input":"2022-07-12T05:21:21.534150Z","iopub.status.idle":"2022-07-12T05:21:21.575660Z","shell.execute_reply.started":"2022-07-12T05:21:21.534106Z","shell.execute_reply":"2022-07-12T05:21:21.574384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('sumbmission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T05:21:21.577119Z","iopub.execute_input":"2022-07-12T05:21:21.578301Z","iopub.status.idle":"2022-07-12T05:21:23.218080Z","shell.execute_reply.started":"2022-07-12T05:21:21.578258Z","shell.execute_reply":"2022-07-12T05:21:23.216869Z"},"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":"2022-07-12T05:21:23.219934Z","iopub.execute_input":"2022-07-12T05:21:23.220783Z","iopub.status.idle":"2022-07-12T05:21:26.748299Z","shell.execute_reply.started":"2022-07-12T05:21:23.220740Z","shell.execute_reply":"2022-07-12T05:21:26.747000Z"},"trusted":true},"execution_count":null,"outputs":[]}]}