{"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-27T16:11:52.536821Z","iopub.execute_input":"2022-04-27T16:11:52.537083Z","iopub.status.idle":"2022-04-27T16:11:58.473392Z","shell.execute_reply.started":"2022-04-27T16:11:52.537054Z","shell.execute_reply":"2022-04-27T16:11:58.472672Z"},"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-27T16:12:02.334702Z","iopub.execute_input":"2022-04-27T16:12:02.335176Z","iopub.status.idle":"2022-04-27T16:12:02.388717Z","shell.execute_reply.started":"2022-04-27T16:12:02.335137Z","shell.execute_reply":"2022-04-27T16:12:02.388073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.describe()","metadata":{"execution":{"iopub.status.busy":"2022-04-27T16:12:26.368643Z","iopub.execute_input":"2022-04-27T16:12:26.369533Z","iopub.status.idle":"2022-04-27T16:12:26.399592Z","shell.execute_reply.started":"2022-04-27T16:12:26.369494Z","shell.execute_reply":"2022-04-27T16:12:26.398800Z"},"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-27T16:12:39.770586Z","iopub.execute_input":"2022-04-27T16:12:39.771202Z","iopub.status.idle":"2022-04-27T16:12:41.958186Z","shell.execute_reply.started":"2022-04-27T16:12:39.771162Z","shell.execute_reply":"2022-04-27T16:12:41.957455Z"},"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-27T16:13:11.778083Z","iopub.execute_input":"2022-04-27T16:13:11.778349Z","iopub.status.idle":"2022-04-27T16:13:12.138130Z","shell.execute_reply.started":"2022-04-27T16:13:11.778317Z","shell.execute_reply":"2022-04-27T16:13:12.137291Z"},"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-27T16:13:27.735121Z","iopub.execute_input":"2022-04-27T16:13:27.735430Z","iopub.status.idle":"2022-04-27T16:13:27.750755Z","shell.execute_reply.started":"2022-04-27T16:13:27.735396Z","shell.execute_reply":"2022-04-27T16:13:27.749048Z"},"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-27T16:14:25.571469Z","iopub.execute_input":"2022-04-27T16:14:25.572095Z","iopub.status.idle":"2022-04-27T16:14:25.579306Z","shell.execute_reply.started":"2022-04-27T16:14:25.572057Z","shell.execute_reply":"2022-04-27T16:14:25.578669Z"},"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-27T16:15:17.732263Z","iopub.execute_input":"2022-04-27T16:15:17.732523Z","iopub.status.idle":"2022-04-27T16:16:37.337240Z","shell.execute_reply.started":"2022-04-27T16:15:17.732493Z","shell.execute_reply":"2022-04-27T16:16:37.336505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-04-27T16:16:37.338858Z","iopub.execute_input":"2022-04-27T16:16:37.339544Z","iopub.status.idle":"2022-04-27T16:16:37.507221Z","shell.execute_reply.started":"2022-04-27T16:16:37.339492Z","shell.execute_reply":"2022-04-27T16:16:37.506308Z"},"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-27T16:17:16.583382Z","iopub.execute_input":"2022-04-27T16:17:16.583832Z","iopub.status.idle":"2022-04-27T16:17:16.590501Z","shell.execute_reply.started":"2022-04-27T16:17:16.583788Z","shell.execute_reply":"2022-04-27T16:17:16.589885Z"},"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-27T16:17:19.437896Z","iopub.execute_input":"2022-04-27T16:17:19.438162Z","iopub.status.idle":"2022-04-27T16:17:19.452535Z","shell.execute_reply.started":"2022-04-27T16:17:19.438130Z","shell.execute_reply":"2022-04-27T16:17:19.451136Z"},"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-27T16:17:23.972387Z","iopub.execute_input":"2022-04-27T16:17:23.972782Z","iopub.status.idle":"2022-04-27T16:17:23.980864Z","shell.execute_reply.started":"2022-04-27T16:17:23.972746Z","shell.execute_reply":"2022-04-27T16:17:23.978528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.xception import Xception, preprocess_input\nxception_preprocessor = preprocess_input\nxception_features = get_features(Xception,\n                                 xception_preprocessor,\n                                 img_size, X)","metadata":{"execution":{"iopub.status.busy":"2022-04-27T16:17:30.542834Z","iopub.execute_input":"2022-04-27T16:17:30.543156Z","iopub.status.idle":"2022-04-27T16:19:04.026247Z","shell.execute_reply.started":"2022-04-27T16:17:30.543114Z","shell.execute_reply":"2022-04-27T16:19:04.025443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-04-27T16:19:37.605197Z","iopub.execute_input":"2022-04-27T16:19:37.605455Z","iopub.status.idle":"2022-04-27T16:19:37.940652Z","shell.execute_reply.started":"2022-04-27T16:19:37.605426Z","shell.execute_reply":"2022-04-27T16:19:37.939807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_features = np.concatenate([xception_features,], axis=-1)\nprint('Final feature maps shape', final_features.shape)","metadata":{"execution":{"iopub.status.busy":"2022-04-27T16:20:10.071585Z","iopub.execute_input":"2022-04-27T16:20:10.071863Z","iopub.status.idle":"2022-04-27T16:20:10.112024Z","shell.execute_reply.started":"2022-04-27T16:20:10.071832Z","shell.execute_reply":"2022-04-27T16:20:10.111296Z"},"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-27T16:20:15.711676Z","iopub.execute_input":"2022-04-27T16:20:15.711945Z","iopub.status.idle":"2022-04-27T16:20:23.336560Z","shell.execute_reply.started":"2022-04-27T16:20:15.711915Z","shell.execute_reply":"2022-04-27T16:20:23.335879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del xception_features\ndel final_features\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-04-27T16:21:00.440860Z","iopub.execute_input":"2022-04-27T16:21:00.441283Z","iopub.status.idle":"2022-04-27T16:21:00.658127Z","shell.execute_reply.started":"2022-04-27T16:21:00.441243Z","shell.execute_reply":"2022-04-27T16:21:00.657183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-27T16:21:03.925471Z","iopub.execute_input":"2022-04-27T16:21:03.925983Z","iopub.status.idle":"2022-04-27T16:21:03.934127Z","shell.execute_reply.started":"2022-04-27T16:21:03.925944Z","shell.execute_reply":"2022-04-27T16:21:03.933264Z"},"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-27T16:21:10.935513Z","iopub.execute_input":"2022-04-27T16:21:10.936356Z","iopub.status.idle":"2022-04-27T16:22:53.679231Z","shell.execute_reply.started":"2022-04-27T16:21:10.936301Z","shell.execute_reply":"2022-04-27T16:22:53.678538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extact_features(data):\n    xception_features = get_features(Xception, xception_preprocessor, img_size, data)\n    final_features = np.concatenate([xception_features],axis=-1)\n    \n    print('Final feature maps shape', final_features.shape)\n\n    del xception_features\n    gc.collect()\n    \n    \n    return final_features\n\ntest_features = extact_features(test_data)","metadata":{"execution":{"iopub.status.busy":"2022-04-27T16:24:47.606370Z","iopub.execute_input":"2022-04-27T16:24:47.606685Z","iopub.status.idle":"2022-04-27T16:25:44.552132Z","shell.execute_reply.started":"2022-04-27T16:24:47.606649Z","shell.execute_reply":"2022-04-27T16:25:44.551328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del test_data\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-04-27T16:25:49.592973Z","iopub.execute_input":"2022-04-27T16:25:49.593669Z","iopub.status.idle":"2022-04-27T16:25:49.886821Z","shell.execute_reply.started":"2022-04-27T16:25:49.593633Z","shell.execute_reply":"2022-04-27T16:25:49.885783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(test_features)","metadata":{"execution":{"iopub.status.busy":"2022-04-27T16:35:10.310041Z","iopub.execute_input":"2022-04-27T16:35:10.310323Z","iopub.status.idle":"2022-04-27T16:35:10.802471Z","shell.execute_reply.started":"2022-04-27T16:35:10.310294Z","shell.execute_reply":"2022-04-27T16:35:10.801730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"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-27T16:32:48.287236Z","iopub.execute_input":"2022-04-27T16:32:48.287524Z","iopub.status.idle":"2022-04-27T16:32:48.294537Z","shell.execute_reply.started":"2022-04-27T16:32:48.287492Z","shell.execute_reply":"2022-04-27T16:32:48.293662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_dog(\"../input/custom-test/greatdane.jpg\")","metadata":{"execution":{"iopub.status.busy":"2022-04-27T16:36:36.003742Z","iopub.execute_input":"2022-04-27T16:36:36.004239Z","iopub.status.idle":"2022-04-27T16:36:38.315371Z","shell.execute_reply.started":"2022-04-27T16:36:36.004199Z","shell.execute_reply":"2022-04-27T16:36:38.314669Z"},"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-27T16:33:26.687517Z","iopub.execute_input":"2022-04-27T16:33:26.688060Z","iopub.status.idle":"2022-04-27T16:33:29.235918Z","shell.execute_reply.started":"2022-04-27T16:33:26.688021Z","shell.execute_reply":"2022-04-27T16:33:29.235183Z"},"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-27T16:33:54.735047Z","iopub.execute_input":"2022-04-27T16:33:54.735294Z","iopub.status.idle":"2022-04-27T16:33:57.205400Z","shell.execute_reply.started":"2022-04-27T16:33:54.735258Z","shell.execute_reply":"2022-04-27T16:33:57.204655Z"},"trusted":true},"execution_count":null,"outputs":[]}]}