{"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-29T05:10:45.275272Z","iopub.execute_input":"2022-04-29T05:10:45.275537Z","iopub.status.idle":"2022-04-29T05:10:51.288937Z","shell.execute_reply.started":"2022-04-29T05:10:45.275455Z","shell.execute_reply":"2022-04-29T05:10:51.288225Z"},"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-29T05:10:51.290394Z","iopub.execute_input":"2022-04-29T05:10:51.290633Z","iopub.status.idle":"2022-04-29T05:10:51.331683Z","shell.execute_reply.started":"2022-04-29T05:10:51.290598Z","shell.execute_reply":"2022-04-29T05:10:51.330909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.describe()","metadata":{"execution":{"iopub.status.busy":"2022-04-29T05:10:51.332856Z","iopub.execute_input":"2022-04-29T05:10:51.333115Z","iopub.status.idle":"2022-04-29T05:10:51.361237Z","shell.execute_reply.started":"2022-04-29T05:10:51.333078Z","shell.execute_reply":"2022-04-29T05:10:51.360472Z"},"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-29T05:10:51.363355Z","iopub.execute_input":"2022-04-29T05:10:51.363594Z","iopub.status.idle":"2022-04-29T05:10:53.455439Z","shell.execute_reply.started":"2022-04-29T05:10:51.363561Z","shell.execute_reply":"2022-04-29T05:10:53.451113Z"},"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-29T05:10:53.456696Z","iopub.execute_input":"2022-04-29T05:10:53.457073Z","iopub.status.idle":"2022-04-29T05:10:53.682456Z","shell.execute_reply.started":"2022-04-29T05:10:53.457035Z","shell.execute_reply":"2022-04-29T05:10:53.681658Z"},"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-29T05:10:53.683756Z","iopub.execute_input":"2022-04-29T05:10:53.684021Z","iopub.status.idle":"2022-04-29T05:10:53.69706Z","shell.execute_reply.started":"2022-04-29T05:10:53.683965Z","shell.execute_reply":"2022-04-29T05:10:53.696174Z"},"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-29T05:10:53.698587Z","iopub.execute_input":"2022-04-29T05:10:53.699143Z","iopub.status.idle":"2022-04-29T05:10:53.708909Z","shell.execute_reply.started":"2022-04-29T05:10:53.699105Z","shell.execute_reply":"2022-04-29T05:10:53.708203Z"},"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-29T05:10:53.710594Z","iopub.execute_input":"2022-04-29T05:10:53.710778Z","iopub.status.idle":"2022-04-29T05:12:03.33235Z","shell.execute_reply.started":"2022-04-29T05:10:53.71075Z","shell.execute_reply":"2022-04-29T05:12:03.331641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-04-29T05:12:03.333477Z","iopub.execute_input":"2022-04-29T05:12:03.334175Z","iopub.status.idle":"2022-04-29T05:12:03.496546Z","shell.execute_reply.started":"2022-04-29T05:12:03.334136Z","shell.execute_reply":"2022-04-29T05:12:03.495853Z"},"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-29T05:12:03.499709Z","iopub.execute_input":"2022-04-29T05:12:03.500567Z","iopub.status.idle":"2022-04-29T05:12:07.447189Z","shell.execute_reply.started":"2022-04-29T05:12:03.500525Z","shell.execute_reply":"2022-04-29T05:12:07.446541Z"},"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-29T05:12:07.448236Z","iopub.execute_input":"2022-04-29T05:12:07.449137Z","iopub.status.idle":"2022-04-29T05:12:07.453488Z","shell.execute_reply.started":"2022-04-29T05:12:07.449096Z","shell.execute_reply":"2022-04-29T05:12:07.452875Z"},"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-29T05:12:07.454715Z","iopub.execute_input":"2022-04-29T05:12:07.455163Z","iopub.status.idle":"2022-04-29T05:12:07.473626Z","shell.execute_reply.started":"2022-04-29T05:12:07.455129Z","shell.execute_reply":"2022-04-29T05:12:07.472948Z"},"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-29T05:12:07.474453Z","iopub.execute_input":"2022-04-29T05:12:07.474654Z","iopub.status.idle":"2022-04-29T05:12:07.481553Z","shell.execute_reply.started":"2022-04-29T05:12:07.474627Z","shell.execute_reply":"2022-04-29T05:12:07.480863Z"},"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-29T05:12:07.482896Z","iopub.execute_input":"2022-04-29T05:12:07.483378Z","iopub.status.idle":"2022-04-29T05:13:00.721258Z","shell.execute_reply.started":"2022-04-29T05:12:07.483331Z","shell.execute_reply":"2022-04-29T05:13:00.71966Z"},"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-29T05:13:00.722779Z","iopub.execute_input":"2022-04-29T05:13:00.723063Z","iopub.status.idle":"2022-04-29T05:13:57.359918Z","shell.execute_reply.started":"2022-04-29T05:13:00.723025Z","shell.execute_reply":"2022-04-29T05:13:57.359167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-04-29T05:13:57.36134Z","iopub.execute_input":"2022-04-29T05:13:57.361606Z","iopub.status.idle":"2022-04-29T05:13:57.930147Z","shell.execute_reply.started":"2022-04-29T05:13:57.36157Z","shell.execute_reply":"2022-04-29T05:13:57.929392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_features = np.concatenate([inception_features,xception_features], axis=-1)\nprint('Final feature maps shape', final_features.shape)","metadata":{"execution":{"iopub.status.busy":"2022-04-29T05:13:57.932293Z","iopub.execute_input":"2022-04-29T05:13:57.932724Z","iopub.status.idle":"2022-04-29T05:13:57.998441Z","shell.execute_reply.started":"2022-04-29T05:13:57.932684Z","shell.execute_reply":"2022-04-29T05:13:57.997677Z"},"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-29T05:13:57.999843Z","iopub.execute_input":"2022-04-29T05:13:58.000127Z","iopub.status.idle":"2022-04-29T05:14:05.551865Z","shell.execute_reply.started":"2022-04-29T05:13:58.000089Z","shell.execute_reply":"2022-04-29T05:14:05.551187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del inception_features\ndel xception_features\ndel final_features\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-04-29T05:14:05.553217Z","iopub.execute_input":"2022-04-29T05:14:05.553458Z","iopub.status.idle":"2022-04-29T05:14:08.828525Z","shell.execute_reply.started":"2022-04-29T05:14:05.553424Z","shell.execute_reply":"2022-04-29T05:14:08.827856Z"},"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    xception_features = get_features(Xception, xception_preprocessor, img_size, data)\n    gc.collect()\n    final_features = np.concatenate([inception_features,xception_features],axis=-1)\n    \n    print('Final feature maps shape', final_features.shape)\n\n    del inception_features\n    del xception_features\n    gc.collect()\n    \n    \n    return final_features\n\n","metadata":{"execution":{"iopub.status.busy":"2022-04-29T05:14:08.829852Z","iopub.execute_input":"2022-04-29T05:14:08.830108Z","iopub.status.idle":"2022-04-29T05:14:08.837081Z","shell.execute_reply.started":"2022-04-29T05:14:08.830074Z","shell.execute_reply":"2022-04-29T05:14:08.836198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_features = extact_features(test_data)","metadata":{"execution":{"iopub.status.busy":"2022-04-29T05:14:08.838569Z","iopub.execute_input":"2022-04-29T05:14:08.838831Z","iopub.status.idle":"2022-04-29T05:14:08.850257Z","shell.execute_reply.started":"2022-04-29T05:14:08.838795Z","shell.execute_reply":"2022-04-29T05:14:08.849463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#del test_data\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-04-29T05:14:08.851904Z","iopub.execute_input":"2022-04-29T05:14:08.852244Z","iopub.status.idle":"2022-04-29T05:14:09.048928Z","shell.execute_reply.started":"2022-04-29T05:14:08.852207Z","shell.execute_reply":"2022-04-29T05:14:09.047914Z"},"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    ac=round(np.max(predg[0])) * 100\n    if (ac==0):\n        print(\"The given image is not a dog\")\n        print(f\"Probability of prediction): {round(np.max(predg[0])) * 100} %\")\n    else:\n        print(\"The given image is a dog\")\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-29T05:42:08.578771Z","iopub.execute_input":"2022-04-29T05:42:08.579039Z","iopub.status.idle":"2022-04-29T05:42:08.587431Z","shell.execute_reply.started":"2022-04-29T05:42:08.579008Z","shell.execute_reply":"2022-04-29T05:42:08.586752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_dog(\"../input/custom-test/greatdane.jpg\")","metadata":{"execution":{"iopub.status.busy":"2022-04-29T05:41:28.765894Z","iopub.execute_input":"2022-04-29T05:41:28.766173Z","iopub.status.idle":"2022-04-29T05:41:34.520447Z","shell.execute_reply.started":"2022-04-29T05:41:28.766144Z","shell.execute_reply":"2022-04-29T05:41:34.519656Z"},"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-29T05:31:49.824504Z","iopub.execute_input":"2022-04-29T05:31:49.824944Z","iopub.status.idle":"2022-04-29T05:31:55.786285Z","shell.execute_reply.started":"2022-04-29T05:31:49.824908Z","shell.execute_reply":"2022-04-29T05:31:55.785526Z"},"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-29T05:31:55.787735Z","iopub.execute_input":"2022-04-29T05:31:55.787971Z","iopub.status.idle":"2022-04-29T05:32:01.62043Z","shell.execute_reply.started":"2022-04-29T05:31:55.787938Z","shell.execute_reply":"2022-04-29T05:32:01.619659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_dog('../input/custom-test/dog3.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-04-29T05:32:01.622477Z","iopub.execute_input":"2022-04-29T05:32:01.622797Z","iopub.status.idle":"2022-04-29T05:32:08.21127Z","shell.execute_reply.started":"2022-04-29T05:32:01.622758Z","shell.execute_reply":"2022-04-29T05:32:08.210552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_dog('../input/custom-test/dog4.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-04-29T05:32:08.212703Z","iopub.execute_input":"2022-04-29T05:32:08.212945Z","iopub.status.idle":"2022-04-29T05:32:14.050026Z","shell.execute_reply.started":"2022-04-29T05:32:08.212912Z","shell.execute_reply":"2022-04-29T05:32:14.048879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_dog('../input/custom-test/house_dog1.jpeg')","metadata":{"execution":{"iopub.status.busy":"2022-04-29T05:32:14.051463Z","iopub.execute_input":"2022-04-29T05:32:14.051724Z","iopub.status.idle":"2022-04-29T05:32:20.126892Z","shell.execute_reply.started":"2022-04-29T05:32:14.05169Z","shell.execute_reply":"2022-04-29T05:32:20.125766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_dog('../input/custom-test/elephant.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-04-29T05:32:20.128545Z","iopub.execute_input":"2022-04-29T05:32:20.128802Z","iopub.status.idle":"2022-04-29T05:32:26.010471Z","shell.execute_reply.started":"2022-04-29T05:32:20.128767Z","shell.execute_reply":"2022-04-29T05:32:26.009747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_dog('../input/custom-test/zebra.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-04-29T05:32:26.011681Z","iopub.execute_input":"2022-04-29T05:32:26.011915Z","iopub.status.idle":"2022-04-29T05:32:32.117998Z","shell.execute_reply.started":"2022-04-29T05:32:26.011882Z","shell.execute_reply":"2022-04-29T05:32:32.117291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_dog('../input/custom-test/lizard.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-04-29T05:32:32.119408Z","iopub.execute_input":"2022-04-29T05:32:32.119653Z","iopub.status.idle":"2022-04-29T05:32:38.30727Z","shell.execute_reply.started":"2022-04-29T05:32:32.11962Z","shell.execute_reply":"2022-04-29T05:32:38.296261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_dog('../input/custom-test/monkey.jpg')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_dog('../input/custom-test/baby.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-04-29T05:40:08.271835Z","iopub.execute_input":"2022-04-29T05:40:08.272581Z","iopub.status.idle":"2022-04-29T05:40:14.302432Z","shell.execute_reply.started":"2022-04-29T05:40:08.272543Z","shell.execute_reply":"2022-04-29T05:40:14.301659Z"},"trusted":true},"execution_count":null,"outputs":[]}]}