{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport os\nimport gc\n\nfrom sklearn.model_selection import train_test_split\n\n\nimport tensorflow as tf\nfrom tqdm.autonotebook import tqdm\n\nimport numpy as np #\nimport pandas as pd \n\nfrom keras import Sequential\nfrom keras.callbacks import EarlyStopping\nfrom tensorflow.keras.optimizers import Adam,SGD\nfrom keras.callbacks import ReduceLROnPlateau\nfrom keras.layers import Flatten,Dense,BatchNormalization,Activation,Dropout\nfrom keras.layers import Lambda, Input, GlobalAveragePooling2D,BatchNormalization\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.models import Model\nfrom keras.preprocessing.image import load_img","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-28T04:35:04.960603Z","iopub.execute_input":"2022-04-28T04:35:04.961117Z","iopub.status.idle":"2022-04-28T04:35:11.352172Z","shell.execute_reply.started":"2022-04-28T04:35:04.961025Z","shell.execute_reply":"2022-04-28T04:35:11.351370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv('../input/dog-breed-identification/labels.csv')\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:35:11.354802Z","iopub.execute_input":"2022-04-28T04:35:11.355219Z","iopub.status.idle":"2022-04-28T04:35:11.395688Z","shell.execute_reply.started":"2022-04-28T04:35:11.355173Z","shell.execute_reply":"2022-04-28T04:35:11.395029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.describe()","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:35:11.397008Z","iopub.execute_input":"2022-04-28T04:35:11.397258Z","iopub.status.idle":"2022-04-28T04:35:11.424147Z","shell.execute_reply.started":"2022-04-28T04:35:11.397225Z","shell.execute_reply":"2022-04-28T04:35:11.423513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def barw(ax): \n    \n    for p in ax.patches:\n        val = p.get_width() \n        x = p.get_x()+ p.get_width()\n        y = p.get_y() + p.get_height()/2\n        ax.annotate(round(val,2),(x,y))\n\nplt.figure(figsize = (15,30))\nax0 =sns.countplot(y=labels['breed'],order=labels['breed'].value_counts().index)\nbarw(ax0)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:35:11.426150Z","iopub.execute_input":"2022-04-28T04:35:11.426588Z","iopub.status.idle":"2022-04-28T04:35:13.644994Z","shell.execute_reply.started":"2022-04-28T04:35:11.426551Z","shell.execute_reply":"2022-04-28T04:35:13.644284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nif len(os.listdir('../input/dog-breed-identification/train')) == len(labels['id']):\n    print('equal images and labels')\nelse:\n    print('number of images and labels are not equal')","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:35:13.645954Z","iopub.execute_input":"2022-04-28T04:35:13.646213Z","iopub.status.idle":"2022-04-28T04:35:13.949779Z","shell.execute_reply.started":"2022-04-28T04:35:13.646178Z","shell.execute_reply":"2022-04-28T04:35:13.948889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = sorted(list(set(labels['breed'])))\nn_classes = len(classes)\nprint('Total breeds {}'.format(n_classes))\nclass_to_num = dict(zip(classes, range(n_classes)))\nclass_to_num","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:35:13.950975Z","iopub.execute_input":"2022-04-28T04:35:13.951603Z","iopub.status.idle":"2022-04-28T04:35:13.964699Z","shell.execute_reply.started":"2022-04-28T04:35:13.951564Z","shell.execute_reply":"2022-04-28T04:35:13.963935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape = (331,331,3)\n\n\ndef images_to_array(directory, label_dataframe, target_size = input_shape):\n    \n    image_labels = label_dataframe['breed']\n    images = np.zeros([len(label_dataframe), target_size[0], target_size[1], target_size[2]],dtype=np.uint8) #as we have huge data and limited ram memory. uint8 takes less memory\n    y = np.zeros([len(label_dataframe),1],dtype = np.uint8)\n    \n    for ix, image_name in enumerate(tqdm(label_dataframe['id'].values)):\n        img_dir = os.path.join(directory, image_name + '.jpg')\n        img = load_img(img_dir, target_size = target_size)\n#         img = np.expand_dims(img, axis=0)\n#         img = processed_image_resnet(img)\n#         img = img/255\n        images[ix]=img\n#         images[ix] = img_to_array(img)\n        del img\n        \n        dog_breed = image_labels[ix]\n        y[ix] = class_to_num[dog_breed]\n    y = to_categorical(y)\n    \n    return images,y","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:35:13.966119Z","iopub.execute_input":"2022-04-28T04:35:13.966575Z","iopub.status.idle":"2022-04-28T04:35:13.976109Z","shell.execute_reply.started":"2022-04-28T04:35:13.966538Z","shell.execute_reply":"2022-04-28T04:35:13.975166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X,y = images_to_array('../input/dog-breed-identification/train', labels[:])","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:35:13.977294Z","iopub.execute_input":"2022-04-28T04:35:13.977846Z","iopub.status.idle":"2022-04-28T04:36:45.870932Z","shell.execute_reply.started":"2022-04-28T04:35:13.977811Z","shell.execute_reply":"2022-04-28T04:36:45.870246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:36:45.872376Z","iopub.execute_input":"2022-04-28T04:36:45.872849Z","iopub.status.idle":"2022-04-28T04:36:46.038248Z","shell.execute_reply.started":"2022-04-28T04:36:45.872811Z","shell.execute_reply":"2022-04-28T04:36:46.037513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n=25\n\nplt.figure(figsize=(20,20))\n\nfor i in range(n):\n    ax = plt.subplot(5, 5, i+1)\n    plt.title(classes[np.where(y[i] ==1)[0][0]])\n    plt.imshow(X[i].astype('int32')) ","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:36:46.042265Z","iopub.execute_input":"2022-04-28T04:36:46.042618Z","iopub.status.idle":"2022-04-28T04:36:50.123005Z","shell.execute_reply.started":"2022-04-28T04:36:46.042581Z","shell.execute_reply":"2022-04-28T04:36:50.122347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lrr= ReduceLROnPlateau(monitor='val_acc', factor=.01, patience=3, min_lr=1e-5,verbose = 1)\n\nEarlyStop = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:36:50.124042Z","iopub.execute_input":"2022-04-28T04:36:50.125592Z","iopub.status.idle":"2022-04-28T04:36:50.129960Z","shell.execute_reply.started":"2022-04-28T04:36:50.125551Z","shell.execute_reply":"2022-04-28T04:36:50.129256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size= 128\nepochs=50\nlearn_rate=.001\nsgd=SGD(lr=learn_rate,momentum=.9,nesterov=False)\nadam=Adam(lr=learn_rate, beta_1=0.9, beta_2=0.999, epsilon=None,  amsgrad=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:36:50.131218Z","iopub.execute_input":"2022-04-28T04:36:50.131684Z","iopub.status.idle":"2022-04-28T04:36:50.149828Z","shell.execute_reply.started":"2022-04-28T04:36:50.131650Z","shell.execute_reply":"2022-04-28T04:36:50.149018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_size = (331,331,3)\n\ndef get_features(model_name, model_preprocessor, input_size, data):\n\n    input_layer = Input(input_size)\n    preprocessor = Lambda(model_preprocessor)(input_layer)\n    base_model = model_name(weights='imagenet', include_top=False,\n                            input_shape=input_size)(preprocessor)\n    avg = GlobalAveragePooling2D()(base_model)\n    feature_extractor = Model(inputs = input_layer, outputs = avg)\n    feature_maps = feature_extractor.predict(data, verbose=1)\n    print('Feature maps shape: ', feature_maps.shape)\n    return feature_maps","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:36:50.151234Z","iopub.execute_input":"2022-04-28T04:36:50.151471Z","iopub.status.idle":"2022-04-28T04:36:50.158063Z","shell.execute_reply.started":"2022-04-28T04:36:50.151429Z","shell.execute_reply":"2022-04-28T04:36:50.157275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.inception_v3 import InceptionV3, preprocess_input\ninception_preprocessor = preprocess_input\ninception_features = get_features(InceptionV3,\n                                  inception_preprocessor,\n                                  img_size, X)","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:36:50.159430Z","iopub.execute_input":"2022-04-28T04:36:50.159970Z","iopub.status.idle":"2022-04-28T04:37:43.617999Z","shell.execute_reply.started":"2022-04-28T04:36:50.159936Z","shell.execute_reply":"2022-04-28T04:37:43.617316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:37:43.621838Z","iopub.execute_input":"2022-04-28T04:37:43.626898Z","iopub.status.idle":"2022-04-28T04:37:44.036901Z","shell.execute_reply.started":"2022-04-28T04:37:43.626857Z","shell.execute_reply":"2022-04-28T04:37:44.036066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_features = np.concatenate([inception_features,], axis=-1)\nprint('Final feature maps shape', final_features.shape)","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:37:44.038818Z","iopub.execute_input":"2022-04-28T04:37:44.039138Z","iopub.status.idle":"2022-04-28T04:37:44.077728Z","shell.execute_reply.started":"2022-04-28T04:37:44.039101Z","shell.execute_reply":"2022-04-28T04:37:44.077101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Dropout(0.7,input_shape=(final_features.shape[1],)))\nmodel.add(Dense(n_classes,activation= 'softmax'))\n\nmodel.compile(optimizer=adam,\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n\n#Training the model. \nhistory = model.fit(final_features, y,\n            batch_size=batch_size,\n            epochs=epochs,\n            validation_split=0.2,\n            callbacks=[lrr,EarlyStop])","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:37:44.079077Z","iopub.execute_input":"2022-04-28T04:37:44.079346Z","iopub.status.idle":"2022-04-28T04:37:54.250436Z","shell.execute_reply.started":"2022-04-28T04:37:44.079312Z","shell.execute_reply":"2022-04-28T04:37:54.249764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del inception_features\ndel final_features\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:37:54.251837Z","iopub.execute_input":"2022-04-28T04:37:54.252085Z","iopub.status.idle":"2022-04-28T04:37:54.561027Z","shell.execute_reply.started":"2022-04-28T04:37:54.252051Z","shell.execute_reply":"2022-04-28T04:37:54.560121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"def images_to_array_test(test_path, img_size = (331,331,3)):\n    test_filenames = [test_path + fname for fname in os.listdir(test_path)]\n\n    data_size = len(test_filenames)\n    images = np.zeros([data_size, img_size[0], img_size[1], 3], dtype=np.uint8)\n    \n    \n    for ix,img_dir in enumerate(tqdm(test_filenames)):\n        #img_dir = os.path.join(test_path, img_dir + '.jpg')\n        img = load_img(img_dir, target_size = img_size)\n#         img = np.expand_dims(img, axis=0)\n#         img = processed_image_resnet(img)\n#         img = img/255\n        images[ix]=img\n#         images[ix] = img_to_array(img)\n        del img\n    print('Ouptut Data Size: ', images.shape)\n    return images","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:37:54.562882Z","iopub.execute_input":"2022-04-28T04:37:54.563486Z","iopub.status.idle":"2022-04-28T04:37:54.574313Z","shell.execute_reply.started":"2022-04-28T04:37:54.563434Z","shell.execute_reply":"2022-04-28T04:37:54.573331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = images_to_array_test('../input/dog-breed-identification/test/', img_size)","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:37:54.576779Z","iopub.execute_input":"2022-04-28T04:37:54.577581Z","iopub.status.idle":"2022-04-28T04:39:32.176106Z","shell.execute_reply.started":"2022-04-28T04:37:54.577513Z","shell.execute_reply":"2022-04-28T04:39:32.175341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extact_features(data):\n    inception_features = get_features(InceptionV3, inception_preprocessor, img_size, data)\n    final_features = np.concatenate([inception_features],axis=-1)\n    \n    print('Final feature maps shape', final_features.shape)\n\n    del inception_features\n    gc.collect()\n    \n    \n    return final_features\n\ntest_features = extact_features(test_data)","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:39:32.177522Z","iopub.execute_input":"2022-04-28T04:39:32.177972Z","iopub.status.idle":"2022-04-28T04:40:08.166102Z","shell.execute_reply.started":"2022-04-28T04:39:32.177931Z","shell.execute_reply":"2022-04-28T04:40:08.165357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del test_data\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:40:08.176073Z","iopub.execute_input":"2022-04-28T04:40:08.177346Z","iopub.status.idle":"2022-04-28T04:40:08.502722Z","shell.execute_reply.started":"2022-04-28T04:40:08.177310Z","shell.execute_reply":"2022-04-28T04:40:08.501766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import display\nfrom PIL import Image\n\ndef predict_dog(link):\n    basewidth = 300\n    img = Image.open(link)\n    wpercent = (basewidth/float(img.size[0]))\n    hsize = int((float(img.size[1])*float(wpercent)))\n    img = img.resize((basewidth,hsize), Image.ANTIALIAS)\n    display(img)\n    img_g = load_img(link,target_size = img_size)\n    img_g = np.expand_dims(img_g, axis=0)\n    img_g.shape\n    test_features = extact_features(img_g)\n    predg = model.predict(test_features)\n    print(f\"Predicted label: {classes[np.argmax(predg[0])]}\")\n    print(f\"Probability of prediction): {round(np.max(predg[0])) * 100} %\")","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:40:08.504130Z","iopub.execute_input":"2022-04-28T04:40:08.504514Z","iopub.status.idle":"2022-04-28T04:40:08.513252Z","shell.execute_reply.started":"2022-04-28T04:40:08.504474Z","shell.execute_reply":"2022-04-28T04:40:08.512630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_dog(\"../input/custom-test/greatdane.jpg\")","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:40:08.515494Z","iopub.execute_input":"2022-04-28T04:40:08.515960Z","iopub.status.idle":"2022-04-28T04:40:12.722630Z","shell.execute_reply.started":"2022-04-28T04:40:08.515924Z","shell.execute_reply":"2022-04-28T04:40:12.721907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_dog('../input/custom-test/german_shepherd.jpeg')","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:40:12.723885Z","iopub.execute_input":"2022-04-28T04:40:12.724123Z","iopub.status.idle":"2022-04-28T04:40:16.484911Z","shell.execute_reply.started":"2022-04-28T04:40:12.724088Z","shell.execute_reply":"2022-04-28T04:40:16.484142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_dog('../input/custom-test/unknown_dog1.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:40:16.486272Z","iopub.execute_input":"2022-04-28T04:40:16.486541Z","iopub.status.idle":"2022-04-28T04:40:20.552528Z","shell.execute_reply.started":"2022-04-28T04:40:16.486504Z","shell.execute_reply":"2022-04-28T04:40:20.551102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#VIT Dogs Prediction","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:40:20.553980Z","iopub.execute_input":"2022-04-28T04:40:20.554258Z","iopub.status.idle":"2022-04-28T04:40:20.557818Z","shell.execute_reply.started":"2022-04-28T04:40:20.554218Z","shell.execute_reply":"2022-04-28T04:40:20.556951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_dog('../input/custom-test/vit_dog1.jpeg')","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:40:20.561536Z","iopub.execute_input":"2022-04-28T04:40:20.561939Z","iopub.status.idle":"2022-04-28T04:40:24.463389Z","shell.execute_reply.started":"2022-04-28T04:40:20.561904Z","shell.execute_reply":"2022-04-28T04:40:24.462629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_dog('../input/custom-test/vit_dog1_v2.jpeg')","metadata":{"execution":{"iopub.status.busy":"2022-04-28T04:40:24.464814Z","iopub.execute_input":"2022-04-28T04:40:24.465076Z","iopub.status.idle":"2022-04-28T04:40:28.575345Z","shell.execute_reply.started":"2022-04-28T04:40:24.465039Z","shell.execute_reply":"2022-04-28T04:40:28.574549Z"},"trusted":true},"execution_count":null,"outputs":[]}]}