{"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\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","editable":false,"execution":{"iopub.status.busy":"2022-04-21T09:32:41.948159Z","iopub.execute_input":"2022-04-21T09:32:41.948676Z","iopub.status.idle":"2022-04-21T09:32:41.953102Z","shell.execute_reply.started":"2022-04-21T09:32:41.948618Z","shell.execute_reply":"2022-04-21T09:32:41.952415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import np_utils\nfrom tensorflow.keras.models import Model, Sequential, load_model\nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Reshape, Dropout\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications.resnet50 import ResNet50\nfrom sklearn.metrics import confusion_matrix, classification_report\nimport itertools\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-21T09:32:50.474323Z","iopub.execute_input":"2022-04-21T09:32:50.474817Z","iopub.status.idle":"2022-04-21T09:32:58.961939Z","shell.execute_reply.started":"2022-04-21T09:32:50.474761Z","shell.execute_reply":"2022-04-21T09:32:58.960891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.vgg16 import VGG16","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:58:25.062025Z","iopub.execute_input":"2022-04-21T08:58:25.062873Z","iopub.status.idle":"2022-04-21T08:58:25.068161Z","shell.execute_reply.started":"2022-04-21T08:58:25.062829Z","shell.execute_reply":"2022-04-21T08:58:25.067388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.vgg19 import VGG19","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:21:45.429400Z","iopub.execute_input":"2022-04-21T08:21:45.430061Z","iopub.status.idle":"2022-04-21T08:21:45.433995Z","shell.execute_reply.started":"2022-04-21T08:21:45.430021Z","shell.execute_reply":"2022-04-21T08:21:45.433288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-21T09:32:58.963911Z","iopub.execute_input":"2022-04-21T09:32:58.964262Z","iopub.status.idle":"2022-04-21T09:32:58.970368Z","shell.execute_reply.started":"2022-04-21T09:32:58.964217Z","shell.execute_reply":"2022-04-21T09:32:58.969727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Constants\nimg_rows = 224\nimg_cols = 224\ninput_shape = (img_rows,img_cols,3)\nepochs = 10\nbatch_size = 64\n","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-21T09:33:05.400374Z","iopub.execute_input":"2022-04-21T09:33:05.400851Z","iopub.status.idle":"2022-04-21T09:33:05.404727Z","shell.execute_reply.started":"2022-04-21T09:33:05.400800Z","shell.execute_reply":"2022-04-21T09:33:05.404026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get ResNet-50 Model\ndef getResNet50Model(lastFourTrainable=False):\n    resnet_model = ResNet50(weights='imagenet', input_shape=input_shape, include_top=True)\n    # Make all layers non-trainable\n    for layer in resnet_model.layers[:]:\n        layer.trainable = False\n    # Add fully connected layer which have 1024 neuron to ResNet-50 model\n    output = resnet_model.get_layer('avg_pool').output\n    output = Flatten(name='new_flatten')(output)\n    output = Dense(units=1024, activation='relu', name='new_fc')(output)\n    predictions = Dense(units=50, activation='softmax')(output)\n    resnet_model = Model(resnet_model.input, predictions)\n    # Make last 4 layers trainable if lastFourTrainable == True\n    if lastFourTrainable == True:\n        resnet_model.get_layer('conv5_block3_2_bn').trainable = True\n        resnet_model.get_layer('conv5_block3_3_conv').trainable = True\n        resnet_model.get_layer('conv5_block3_3_bn').trainable = True\n        resnet_model.get_layer('new_fc').trainable = True\n    # Compile ResNet-50 model\n    resnet_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n    resnet_model.summary()\n    return resnet_model","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-21T09:33:07.306593Z","iopub.execute_input":"2022-04-21T09:33:07.307436Z","iopub.status.idle":"2022-04-21T09:33:07.317276Z","shell.execute_reply.started":"2022-04-21T09:33:07.307394Z","shell.execute_reply":"2022-04-21T09:33:07.316276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindf = pd.read_csv(\"../input/landmark-recognition-2021/train.csv\")\ntraindf.head()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-21T09:33:14.976322Z","iopub.execute_input":"2022-04-21T09:33:14.976598Z","iopub.status.idle":"2022-04-21T09:33:16.690077Z","shell.execute_reply.started":"2022-04-21T09:33:14.976570Z","shell.execute_reply":"2022-04-21T09:33:16.689142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindf.shape","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-21T09:33:18.442305Z","iopub.execute_input":"2022-04-21T09:33:18.442591Z","iopub.status.idle":"2022-04-21T09:33:18.448822Z","shell.execute_reply.started":"2022-04-21T09:33:18.442562Z","shell.execute_reply":"2022-04-21T09:33:18.447781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"landmark_unique = traindf['landmark_id'].unique()\nlen(landmark_unique)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-21T09:33:25.087524Z","iopub.execute_input":"2022-04-21T09:33:25.087804Z","iopub.status.idle":"2022-04-21T09:33:25.121762Z","shell.execute_reply.started":"2022-04-21T09:33:25.087775Z","shell.execute_reply":"2022-04-21T09:33:25.121120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"landmark_unique[0:50]","metadata":{"execution":{"iopub.status.busy":"2022-04-21T09:33:27.403606Z","iopub.execute_input":"2022-04-21T09:33:27.404199Z","iopub.status.idle":"2022-04-21T09:33:27.409845Z","shell.execute_reply.started":"2022-04-21T09:33:27.404144Z","shell.execute_reply":"2022-04-21T09:33:27.409265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_ids = []\nlabels = []\ntemp_labels = []\ni=0\nfor id_ in landmark_unique[0:50]:\n    for iid in traindf['id'][traindf['landmark_id'] == id_]:\n        image_ids.append(iid)\n        labels.append(id_)\n        temp_labels.append(i)\n    i = i+1\nlen(image_ids)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T09:33:34.815446Z","iopub.execute_input":"2022-04-21T09:33:34.815903Z","iopub.status.idle":"2022-04-21T09:33:34.936904Z","shell.execute_reply.started":"2022-04-21T09:33:34.815863Z","shell.execute_reply":"2022-04-21T09:33:34.935930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mainpath = '../input/landmark-recognition-2021/train'\nimage_path = []\nimages_pixels = []\n\nfor i in range(0,len(image_ids)):\n    first_dir = os.path.join(mainpath,image_ids[i][0])\n    second_dir = os.path.join(first_dir,image_ids[i][1])\n    third_dir = os.path.join(second_dir,image_ids[i][2])\n    finalpath = os.path.join(third_dir,image_ids[i]+'.jpg')\n    \n    img_pix = cv2.imread(finalpath,1)\n    images_pixels.append(cv2.resize(img_pix, (224,224)))\n    \n    image_path.append(finalpath)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-21T09:33:41.653351Z","iopub.execute_input":"2022-04-21T09:33:41.653661Z","iopub.status.idle":"2022-04-21T09:34:02.397202Z","shell.execute_reply.started":"2022-04-21T09:33:41.653630Z","shell.execute_reply":"2022-04-21T09:34:02.396159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Images: ', len(image_path))\nprint('Image labels: ', len(labels))","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-21T09:34:02.399819Z","iopub.execute_input":"2022-04-21T09:34:02.400185Z","iopub.status.idle":"2022-04-21T09:34:02.405846Z","shell.execute_reply.started":"2022-04-21T09:34:02.400137Z","shell.execute_reply":"2022-04-21T09:34:02.405162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(temp_labels)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T09:34:06.624660Z","iopub.execute_input":"2022-04-21T09:34:06.625252Z","iopub.status.idle":"2022-04-21T09:34:06.631325Z","shell.execute_reply.started":"2022-04-21T09:34:06.625203Z","shell.execute_reply":"2022-04-21T09:34:06.630425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-21T09:34:12.656236Z","iopub.execute_input":"2022-04-21T09:34:12.656703Z","iopub.status.idle":"2022-04-21T09:34:12.662975Z","shell.execute_reply.started":"2022-04-21T09:34:12.656665Z","shell.execute_reply":"2022-04-21T09:34:12.661876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.gcf()\nfig.set_size_inches(16, 16)\n\nnext_pix_ = image_path\n\nfor i, img_path in enumerate(next_pix_[0:16]):\n    \n    sp = plt.subplot(5, 4, i + 1)\n    sp.axis('Off')\n\n    img = cv2.imread(img_path)\n    plt.imshow(img)\n\nplt.show()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-21T08:23:43.483266Z","iopub.execute_input":"2022-04-21T08:23:43.483525Z","iopub.status.idle":"2022-04-21T08:23:44.956400Z","shell.execute_reply.started":"2022-04-21T08:23:43.483494Z","shell.execute_reply":"2022-04-21T08:23:44.955127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.gcf()\nfig.set_size_inches(16, 16)\n\nnext_pix = image_path\nrandom.shuffle(next_pix)\n\nfor i, img_path in enumerate(next_pix[0:12]):\n    \n    sp = plt.subplot(4, 4, i + 1)\n    sp.axis('Off')\n\n    img = cv2.imread(img_path)\n    plt.imshow(img)\n\nplt.show()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-21T07:21:03.806707Z","iopub.execute_input":"2022-04-21T07:21:03.806967Z","iopub.status.idle":"2022-04-21T07:21:05.142046Z","shell.execute_reply.started":"2022-04-21T07:21:03.806937Z","shell.execute_reply":"2022-04-21T07:21:05.141360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shuf = list(zip(images_pixels,temp_labels))\nrandom.shuffle(shuf)\n\ntrain_data, labels_data = zip(*shuf)\nprint('Images: ', len(train_data))\nprint('Image labels: ', len(labels_data))","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-21T09:34:23.365292Z","iopub.execute_input":"2022-04-21T09:34:23.365567Z","iopub.status.idle":"2022-04-21T09:34:23.375127Z","shell.execute_reply.started":"2022-04-21T09:34:23.365538Z","shell.execute_reply":"2022-04-21T09:34:23.374154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels_data)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T09:34:25.583956Z","iopub.execute_input":"2022-04-21T09:34:25.584632Z","iopub.status.idle":"2022-04-21T09:34:25.591076Z","shell.execute_reply.started":"2022-04-21T09:34:25.584587Z","shell.execute_reply":"2022-04-21T09:34:25.590064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''# Function for plotting the Confusion Matrix\ndef plotConfusionMatrix(cm, class_names, normalize=True, title='Confusion matrix', cmap=plt.cm.Blues):\n    plt.figure(figsize=(10,10))\n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks = np.arange(len(class_names))\n    plt.xticks(tick_marks, class_names, rotation=45)\n    plt.yticks(tick_marks, class_names)\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n        cm = np.around(cm, decimals=2)\n        cm[np.isnan(cm)] = 0.0\n        print(\"Normalized confusion matrix\")\n    else:\n        print('Confusion matrix, without normalization')\n\n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, cm[i, j],\n                horizontalalignment=\"center\",\n                color=\"white\" if cm[i, j] > thresh else \"black\")\n\n    plt.tight_layout()\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')\n    plt.show()\n    '''","metadata":{"execution":{"iopub.status.busy":"2022-04-19T15:37:22.206098Z","iopub.execute_input":"2022-04-19T15:37:22.206408Z","iopub.status.idle":"2022-04-19T15:37:22.215064Z","shell.execute_reply.started":"2022-04-19T15:37:22.206372Z","shell.execute_reply":"2022-04-19T15:37:22.21417Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\n# Function for train the given model and get Confusion Matrix\ndef trainModelAndGetConfusionMatrix(model,train_generator,valid_generator,test_generator,epochs,batch_size):\n    # Fit the model\n    model.fit_generator(train_generator,\n                      epochs=epochs,\n                      steps_per_epoch=len(train_generator) // batch_size,\n                      validation_data=valid_generator,\n                      validation_steps=len(valid_generator) // batch_size)\n    # Evaluate the model\n    loss_and_metrics = model.evaluate_generator(test_generator, steps=len(test_generator) // batch_size+1)\n    print(\"Test Loss: \", loss_and_metrics[0])\n    print(\"Test Accuracy: \", loss_and_metrics[1])\n    test_generator.reset()\n    # Get Confusion Matrix and plot it\n    Y_pred = model.predict_generator(test_generator, steps=len(test_generator) // batch_size+1)\n    y_pred = np.argmax(Y_pred, axis=1)\n    class_names = []\n    for key in train_generator.class_indices:\n        class_names.append(key)\n    cr = classification_report(test_generator.classes, y_pred, target_names=class_names)\n    print(\"Classification Report\\n\" + cr) # Print classification report\n    cm = confusion_matrix(test_generator.classes, y_pred)\n    plotConfusionMatrix(cm, class_names, normalize=False, title='Confusion Matrix')\n    plotConfusionMatrix(cm, class_names, normalize=True, title='Confusion Matrix')\n    return model\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-04-19T15:37:14.862224Z","iopub.execute_input":"2022-04-19T15:37:14.862921Z","iopub.status.idle":"2022-04-19T15:37:14.873339Z","shell.execute_reply.started":"2022-04-19T15:37:14.862872Z","shell.execute_reply":"2022-04-19T15:37:14.872702Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = np.array(train_data) #/ 255\n","metadata":{"execution":{"iopub.status.busy":"2022-04-21T09:34:30.681530Z","iopub.execute_input":"2022-04-21T09:34:30.681830Z","iopub.status.idle":"2022-04-21T09:34:30.760742Z","shell.execute_reply.started":"2022-04-21T09:34:30.681802Z","shell.execute_reply":"2022-04-21T09:34:30.760083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = np.array(labels_data)\na_pd = pd.get_dummies(a).astype('float32').values ","metadata":{"execution":{"iopub.status.busy":"2022-04-21T09:34:33.273293Z","iopub.execute_input":"2022-04-21T09:34:33.273576Z","iopub.status.idle":"2022-04-21T09:34:33.284909Z","shell.execute_reply.started":"2022-04-21T09:34:33.273542Z","shell.execute_reply":"2022-04-21T09:34:33.284185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels_data[0:6])","metadata":{"execution":{"iopub.status.busy":"2022-04-21T09:34:42.616014Z","iopub.execute_input":"2022-04-21T09:34:42.616905Z","iopub.status.idle":"2022-04-21T09:34:42.621756Z","shell.execute_reply.started":"2022-04-21T09:34:42.616858Z","shell.execute_reply":"2022-04-21T09:34:42.620899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(a_pd)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T09:34:47.070841Z","iopub.execute_input":"2022-04-21T09:34:47.071146Z","iopub.status.idle":"2022-04-21T09:34:47.076955Z","shell.execute_reply.started":"2022-04-21T09:34:47.071114Z","shell.execute_reply":"2022-04-21T09:34:47.076145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_data=a_pd","metadata":{"execution":{"iopub.status.busy":"2022-04-21T09:35:00.971808Z","iopub.execute_input":"2022-04-21T09:35:00.972156Z","iopub.status.idle":"2022-04-21T09:35:00.976544Z","shell.execute_reply.started":"2022-04-21T09:35:00.972118Z","shell.execute_reply":"2022-04-21T09:35:00.975338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels_data[0:5])","metadata":{"execution":{"iopub.status.busy":"2022-04-21T09:35:03.255866Z","iopub.execute_input":"2022-04-21T09:35:03.256746Z","iopub.status.idle":"2022-04-21T09:35:03.265638Z","shell.execute_reply.started":"2022-04-21T09:35:03.256698Z","shell.execute_reply":"2022-04-21T09:35:03.264614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-21T09:35:11.999375Z","iopub.execute_input":"2022-04-21T09:35:11.999682Z","iopub.status.idle":"2022-04-21T09:35:12.006329Z","shell.execute_reply.started":"2022-04-21T09:35:11.999649Z","shell.execute_reply":"2022-04-21T09:35:12.005430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_test, Y_train, Y_test = train_test_split(train_data, labels_data, test_size = 0.3, random_state=101)\n\nprint(\"X train data : \", len(X_train))\nprint(\"X label data : \", len(X_test))\nprint(\"Y test data : \", len(Y_train))\nprint(\"Y label data : \", len(Y_test))","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-21T09:35:17.536512Z","iopub.execute_input":"2022-04-21T09:35:17.536802Z","iopub.status.idle":"2022-04-21T09:35:17.633360Z","shell.execute_reply.started":"2022-04-21T09:35:17.536773Z","shell.execute_reply":"2022-04-21T09:35:17.632414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_sample=X_train.shape[0]\n#20% of the enteries has to be in validation set\nvalidation_freq=int(num_sample*0.2)\n#Generating random sample of indices equal to validation_freq\nvalidationlist = random.sample(range(0, num_sample), validation_freq)\ntraininglist=list(set(range(0,num_sample))-set(validationlist))\nprint(\"No interesection between validationlist and traininglist:\",set(traininglist).intersection(validationlist))","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-21T09:35:23.187286Z","iopub.execute_input":"2022-04-21T09:35:23.187564Z","iopub.status.idle":"2022-04-21T09:35:23.194916Z","shell.execute_reply.started":"2022-04-21T09:35:23.187530Z","shell.execute_reply":"2022-04-21T09:35:23.194163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x_final=[]\ntrain_y_final=[]\nfor val in traininglist:\n    train_x_final.append(X_train[val])\n    train_y_final.append(Y_train[val])\ntrain_x_final=np.array(train_x_final)\ntrain_y_final=np.array(train_y_final)\nprint(\"Training data shape\",train_x_final.shape)\nprint(\"Training label shape\",train_y_final.shape)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-21T09:35:33.587236Z","iopub.execute_input":"2022-04-21T09:35:33.587776Z","iopub.status.idle":"2022-04-21T09:35:33.636487Z","shell.execute_reply.started":"2022-04-21T09:35:33.587738Z","shell.execute_reply":"2022-04-21T09:35:33.635767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_x_final=[]\nvalidation_y_final=[]\nfor val in validationlist:\n    validation_x_final.append(X_train[val])\n    validation_y_final.append(Y_train[val])\nvalidation_x_final=np.array(validation_x_final)\nvalidation_y_final=np.array(validation_y_final)\nprint(\"Validation data shape\",validation_x_final.shape)\nprint(\"Validation label shape\",validation_y_final.shape)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-21T09:35:39.334335Z","iopub.execute_input":"2022-04-21T09:35:39.334766Z","iopub.status.idle":"2022-04-21T09:35:39.352131Z","shell.execute_reply.started":"2022-04-21T09:35:39.334734Z","shell.execute_reply":"2022-04-21T09:35:39.350823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot the training results\ndef plot_hist(history,title):\n    plt.subplot(121)\n    plt.plot(history.history['accuracy'])\n    plt.plot(history.history['val_accuracy'])\n    plt.title(str(title)+' accuracy')\n    plt.ylabel('Accuracy')\n    plt.xlabel('Epoch')\n    plt.legend(['Train', 'Test'], loc='upper left')\n\n    # Plot training & validation loss values\n    plt.subplot(122)\n    plt.plot(history.history['loss'])\n    plt.plot(history.history['val_loss'])\n    plt.title(str(title)+' loss')\n    plt.ylabel('Loss')\n    plt.xlabel('Epoch')\n    plt.legend(['Train', 'Test'], loc='upper left')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T09:35:41.747987Z","iopub.execute_input":"2022-04-21T09:35:41.748286Z","iopub.status.idle":"2022-04-21T09:35:41.756241Z","shell.execute_reply.started":"2022-04-21T09:35:41.748253Z","shell.execute_reply":"2022-04-21T09:35:41.755249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get VGG-16 Model\ndef getVGG16Model(lastFourTrainable=False):\n    vgg_model = VGG16(weights='imagenet', input_shape=input_shape, include_top=True)\n    # Make all layers untrainable\n    for layer in vgg_model.layers[:]:\n        layer.trainable = False\n    # Add fully connected layer which have 1024 neuron to VGG-16 model\n    output = vgg_model.get_layer('fc2').output\n    output = Flatten(name='new_flatten')(output)\n    output = Dense(units=1024, activation='relu', name='new_fc')(output)\n    output = Dense(units=50, activation='softmax')(output)\n    vgg_model = Model(vgg_model.input, output)\n    # Make last 4 layers trainable if lastFourTrainable == True\n    if lastFourTrainable == True:\n        vgg_model.get_layer('block5_conv3').trainable = True\n        vgg_model.get_layer('fc1').trainable = True\n        vgg_model.get_layer('fc2').trainable = True\n        vgg_model.get_layer('new_fc').trainable = True\n    # Compile VGG-16 model\n    vgg_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n    vgg_model.summary()\n\n    return vgg_model","metadata":{"execution":{"iopub.status.busy":"2022-04-21T09:01:06.344579Z","iopub.execute_input":"2022-04-21T09:01:06.345409Z","iopub.status.idle":"2022-04-21T09:01:06.353766Z","shell.execute_reply.started":"2022-04-21T09:01:06.345353Z","shell.execute_reply":"2022-04-21T09:01:06.352933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get VGG-19 Model\ndef getVGG19Model(lastFourTrainable=False):\n    vgg_model_19 = VGG19(weights='imagenet', input_shape=input_shape, include_top=True)\n    # Make all layers untrainable\n    for layer in vgg_model_19.layers[:]:\n        layer.trainable = False\n    # Add fully connected layer which have 1024 neuron to VGG-16 model\n    output = vgg_model_19.get_layer('fc2').output\n    output = Flatten(name='new_flatten')(output)\n    output = Dense(units=1024, activation='relu', name='new_fc')(output)\n    output = Dense(units=50, activation='softmax')(output)\n    vgg_model_19 = Model(vgg_model_19.input, output)\n    # Make last 4 layers trainable if lastFourTrainable == True\n    if lastFourTrainable == True:\n        vgg_model_19.get_layer('block5_conv3').trainable = True\n        vgg_model_19.get_layer('fc1').trainable = True\n        vgg_model_19.get_layer('fc2').trainable = True\n        vgg_model_19.get_layer('new_fc').trainable = True\n    # Compile VGG-16 model\n    vgg_model_19.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n    vgg_model_19.summary()\n\n    return vgg_model_19","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:26:06.095236Z","iopub.execute_input":"2022-04-21T08:26:06.095496Z","iopub.status.idle":"2022-04-21T08:26:06.103130Z","shell.execute_reply.started":"2022-04-21T08:26:06.095466Z","shell.execute_reply":"2022-04-21T08:26:06.102053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg_model19_a = getVGG19Model(lastFourTrainable=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:26:14.297182Z","iopub.execute_input":"2022-04-21T08:26:14.297500Z","iopub.status.idle":"2022-04-21T08:26:20.302931Z","shell.execute_reply.started":"2022-04-21T08:26:14.297439Z","shell.execute_reply":"2022-04-21T08:26:20.302220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg_model19_b = getVGG19Model(lastFourTrainable=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:26:25.561385Z","iopub.execute_input":"2022-04-21T08:26:25.561857Z","iopub.status.idle":"2022-04-21T08:26:27.793786Z","shell.execute_reply.started":"2022-04-21T08:26:25.561818Z","shell.execute_reply":"2022-04-21T08:26:27.792618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get ResNet-50 Model with lastFourTrainable=False\nresnet_model_a = getResNet50Model(lastFourTrainable=False)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-21T09:36:00.705177Z","iopub.execute_input":"2022-04-21T09:36:00.706028Z","iopub.status.idle":"2022-04-21T09:36:03.467003Z","shell.execute_reply.started":"2022-04-21T09:36:00.705960Z","shell.execute_reply":"2022-04-21T09:36:03.465979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def score_train(model,test_x,test_y):\n    # Score trained model.\n    train_scores = model.evaluate(test_x, test_y, verbose=1)\n    print('Test loss:', train_scores[0])\n    print('Test accuracy:', train_scores[1])","metadata":{"execution":{"iopub.status.busy":"2022-04-21T09:36:13.629665Z","iopub.execute_input":"2022-04-21T09:36:13.629994Z","iopub.status.idle":"2022-04-21T09:36:13.635989Z","shell.execute_reply.started":"2022-04-21T09:36:13.629959Z","shell.execute_reply":"2022-04-21T09:36:13.635033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam","metadata":{"execution":{"iopub.status.busy":"2022-04-21T09:36:16.198533Z","iopub.execute_input":"2022-04-21T09:36:16.198810Z","iopub.status.idle":"2022-04-21T09:36:16.203484Z","shell.execute_reply.started":"2022-04-21T09:36:16.198782Z","shell.execute_reply":"2022-04-21T09:36:16.202630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(validation_x_final.shape)\nprint(validation_y_final.shape)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T09:01:41.063301Z","iopub.execute_input":"2022-04-21T09:01:41.064016Z","iopub.status.idle":"2022-04-21T09:01:41.069126Z","shell.execute_reply.started":"2022-04-21T09:01:41.063977Z","shell.execute_reply":"2022-04-21T09:01:41.068348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow","metadata":{"execution":{"iopub.status.busy":"2022-04-21T09:36:48.198546Z","iopub.execute_input":"2022-04-21T09:36:48.199027Z","iopub.status.idle":"2022-04-21T09:36:48.203505Z","shell.execute_reply.started":"2022-04-21T09:36:48.198991Z","shell.execute_reply":"2022-04-21T09:36:48.202618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x_final_resnet=tensorflow.keras.applications.resnet.preprocess_input(train_x_final)\nvalidation_x_final_resnet=tensorflow.keras.applications.resnet.preprocess_input(validation_x_final)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T09:36:54.338594Z","iopub.execute_input":"2022-04-21T09:36:54.338911Z","iopub.status.idle":"2022-04-21T09:36:54.689915Z","shell.execute_reply.started":"2022-04-21T09:36:54.338877Z","shell.execute_reply":"2022-04-21T09:36:54.688941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_x_final_resnet=tensorflow.keras.applications.resnet.preprocess_input(X_test)\n","metadata":{"execution":{"iopub.status.busy":"2022-04-21T09:36:59.449216Z","iopub.execute_input":"2022-04-21T09:36:59.449524Z","iopub.status.idle":"2022-04-21T09:36:59.599812Z","shell.execute_reply.started":"2022-04-21T09:36:59.449490Z","shell.execute_reply":"2022-04-21T09:36:59.598941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"RESNET-50-a","metadata":{}},{"cell_type":"code","source":"# Train ResNet-50 Model \n#resnet_model_a.compile(optimizer=Adam(learning_rate=0.001),loss='categorical_crossentropy',metrics=['accuracy'])\nhistory1 = resnet_model_a.fit(train_x_final_resnet, train_y_final, epochs=30,validation_data=(validation_x_final_resnet, validation_y_final))\n#resnet_model_a = trainModelAndGetConfusionMatrix(resnet_model_a,train_x_final,validation_x_final,X_test,10,64)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T09:37:06.272859Z","iopub.execute_input":"2022-04-21T09:37:06.273184Z","iopub.status.idle":"2022-04-21T10:05:01.724851Z","shell.execute_reply.started":"2022-04-21T09:37:06.273148Z","shell.execute_reply":"2022-04-21T10:05:01.723718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_hist(history1,'Resnet-50')","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:05:20.979466Z","iopub.execute_input":"2022-04-21T10:05:20.980033Z","iopub.status.idle":"2022-04-21T10:05:21.327142Z","shell.execute_reply.started":"2022-04-21T10:05:20.979994Z","shell.execute_reply":"2022-04-21T10:05:21.326071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score_train(resnet_model_a,test_x_final_resnet,Y_test)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:05:30.012156Z","iopub.execute_input":"2022-04-21T10:05:30.012491Z","iopub.status.idle":"2022-04-21T10:05:56.069691Z","shell.execute_reply.started":"2022-04-21T10:05:30.012459Z","shell.execute_reply":"2022-04-21T10:05:56.068146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Resnet-50-b","metadata":{}},{"cell_type":"code","source":"resnet_model_b = getResNet50Model(lastFourTrainable=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:29:34.860262Z","iopub.execute_input":"2022-04-21T08:29:34.860531Z","iopub.status.idle":"2022-04-21T08:29:36.607943Z","shell.execute_reply.started":"2022-04-21T08:29:34.860500Z","shell.execute_reply":"2022-04-21T08:29:36.607233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train ResNet-50 Model \n#resnet_model_a.compile(optimizer=Adam(learning_rate=0.001),loss='categorical_crossentropy',metrics=['accuracy'])\nhistory = resnet_model_b.fit(train_x_final_resnet, train_y_final, epochs=30,validation_data=(validation_x_final_resnet, validation_y_final))\n#resnet_model_a = trainModelAndGetConfusionMatrix(resnet_model_a,train_x_final,validation_x_final,X_test,10,64)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:30:16.739963Z","iopub.execute_input":"2022-04-21T08:30:16.740219Z","iopub.status.idle":"2022-04-21T08:31:03.767443Z","shell.execute_reply.started":"2022-04-21T08:30:16.740191Z","shell.execute_reply":"2022-04-21T08:31:03.766729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_hist(history,'Resnet-50-b')","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:31:13.618742Z","iopub.execute_input":"2022-04-21T08:31:13.618998Z","iopub.status.idle":"2022-04-21T08:31:13.904171Z","shell.execute_reply.started":"2022-04-21T08:31:13.618969Z","shell.execute_reply":"2022-04-21T08:31:13.903417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score_train(resnet_model_b,test_x_final_resnet,Y_test)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:31:34.096240Z","iopub.execute_input":"2022-04-21T08:31:34.096506Z","iopub.status.idle":"2022-04-21T08:31:36.688640Z","shell.execute_reply.started":"2022-04-21T08:31:34.096477Z","shell.execute_reply":"2022-04-21T08:31:36.687850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"VGG-16-a","metadata":{}},{"cell_type":"code","source":"train_x_final_vgg16=tensorflow.keras.applications.vgg16.preprocess_input(train_x_final)\nvalidation_x_final_vgg16=tensorflow.keras.applications.vgg16.preprocess_input(validation_x_final)\n","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:31:59.492868Z","iopub.execute_input":"2022-04-21T08:31:59.493123Z","iopub.status.idle":"2022-04-21T08:31:59.762740Z","shell.execute_reply.started":"2022-04-21T08:31:59.493095Z","shell.execute_reply":"2022-04-21T08:31:59.761960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_x_final_vgg16=tensorflow.keras.applications.vgg16.preprocess_input(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:32:05.181265Z","iopub.execute_input":"2022-04-21T08:32:05.181528Z","iopub.status.idle":"2022-04-21T08:32:05.300864Z","shell.execute_reply.started":"2022-04-21T08:32:05.181496Z","shell.execute_reply":"2022-04-21T08:32:05.300075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgga=getVGG16Model(lastFourTrainable=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:32:12.128768Z","iopub.execute_input":"2022-04-21T08:32:12.129301Z","iopub.status.idle":"2022-04-21T08:32:15.474653Z","shell.execute_reply.started":"2022-04-21T08:32:12.129261Z","shell.execute_reply":"2022-04-21T08:32:15.473936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = vgga.fit(train_x_final_vgg16, train_y_final,validation_data=(validation_x_final_vgg16,validation_y_final), epochs=30)\n","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:33:14.027264Z","iopub.execute_input":"2022-04-21T08:33:14.027519Z","iopub.status.idle":"2022-04-21T08:34:37.062933Z","shell.execute_reply.started":"2022-04-21T08:33:14.027489Z","shell.execute_reply":"2022-04-21T08:34:37.061950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_hist(history,'VGG-16')","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:34:37.351318Z","iopub.execute_input":"2022-04-21T08:34:37.351647Z","iopub.status.idle":"2022-04-21T08:34:37.651874Z","shell.execute_reply.started":"2022-04-21T08:34:37.351608Z","shell.execute_reply":"2022-04-21T08:34:37.650968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score_train(vgga,test_x_final_vgg16,Y_test)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:34:44.842938Z","iopub.execute_input":"2022-04-21T08:34:44.843192Z","iopub.status.idle":"2022-04-21T08:34:48.543806Z","shell.execute_reply.started":"2022-04-21T08:34:44.843163Z","shell.execute_reply":"2022-04-21T08:34:48.543110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vggb=getVGG16Model(lastFourTrainable=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:34:53.979496Z","iopub.execute_input":"2022-04-21T08:34:53.980052Z","iopub.status.idle":"2022-04-21T08:34:56.025785Z","shell.execute_reply.started":"2022-04-21T08:34:53.980011Z","shell.execute_reply":"2022-04-21T08:34:56.025094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_vgg16_b = vggb.fit(train_x_final_vgg16, train_y_final,validation_data=(validation_x_final_vgg16,validation_y_final), epochs=30)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:36:35.666859Z","iopub.execute_input":"2022-04-21T08:36:35.667416Z","iopub.status.idle":"2022-04-21T08:37:58.511642Z","shell.execute_reply.started":"2022-04-21T08:36:35.667377Z","shell.execute_reply":"2022-04-21T08:37:58.510841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_hist(history_vgg16_b,'Vgg-16-b')","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:37:58.833637Z","iopub.execute_input":"2022-04-21T08:37:58.833971Z","iopub.status.idle":"2022-04-21T08:37:59.120063Z","shell.execute_reply.started":"2022-04-21T08:37:58.833928Z","shell.execute_reply":"2022-04-21T08:37:59.119406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score_train(vggb,test_x_final_vgg16,Y_test)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:38:09.571036Z","iopub.execute_input":"2022-04-21T08:38:09.571306Z","iopub.status.idle":"2022-04-21T08:38:10.838188Z","shell.execute_reply.started":"2022-04-21T08:38:09.571278Z","shell.execute_reply":"2022-04-21T08:38:10.837434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"VGG-19-a","metadata":{}},{"cell_type":"code","source":"train_x_final_vgg19=tensorflow.keras.applications.vgg19.preprocess_input(train_x_final)\nvalidation_x_final_vgg19=tensorflow.keras.applications.vgg19.preprocess_input(validation_x_final)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:38:17.966922Z","iopub.execute_input":"2022-04-21T08:38:17.967408Z","iopub.status.idle":"2022-04-21T08:38:18.252048Z","shell.execute_reply.started":"2022-04-21T08:38:17.967363Z","shell.execute_reply":"2022-04-21T08:38:18.251269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_x_final_vgg19=tensorflow.keras.applications.vgg19.preprocess_input(X_test)\n","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:38:20.769281Z","iopub.execute_input":"2022-04-21T08:38:20.769535Z","iopub.status.idle":"2022-04-21T08:38:20.889616Z","shell.execute_reply.started":"2022-04-21T08:38:20.769506Z","shell.execute_reply":"2022-04-21T08:38:20.888845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_vgg19_a = vgg_model19_a.fit(train_x_final_vgg19, train_y_final,validation_data=(validation_x_final_vgg19,validation_y_final), epochs=30)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:38:32.851026Z","iopub.execute_input":"2022-04-21T08:38:32.851284Z","iopub.status.idle":"2022-04-21T08:39:56.494299Z","shell.execute_reply.started":"2022-04-21T08:38:32.851254Z","shell.execute_reply":"2022-04-21T08:39:56.493524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score_train(vgg_model19_a,test_x_final_vgg19,Y_test)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:40:03.954027Z","iopub.execute_input":"2022-04-21T08:40:03.954651Z","iopub.status.idle":"2022-04-21T08:40:05.617623Z","shell.execute_reply.started":"2022-04-21T08:40:03.954606Z","shell.execute_reply":"2022-04-21T08:40:05.608128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_hist(history_vgg19_a,'vgg-19-a')","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:40:09.380887Z","iopub.execute_input":"2022-04-21T08:40:09.381155Z","iopub.status.idle":"2022-04-21T08:40:09.660021Z","shell.execute_reply.started":"2022-04-21T08:40:09.381124Z","shell.execute_reply":"2022-04-21T08:40:09.659357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"VGG-19-b","metadata":{}},{"cell_type":"code","source":"history_vgg19_b = vgg_model19_b.fit(train_x_final_vgg19, train_y_final,validation_data=(validation_x_final_vgg19,validation_y_final), epochs=30)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:40:31.339934Z","iopub.execute_input":"2022-04-21T08:40:31.340191Z","iopub.status.idle":"2022-04-21T08:41:43.208349Z","shell.execute_reply.started":"2022-04-21T08:40:31.340160Z","shell.execute_reply":"2022-04-21T08:41:43.207607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_hist(history_vgg19_b,'VGG-19-b')","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:41:56.927690Z","iopub.execute_input":"2022-04-21T08:41:56.928255Z","iopub.status.idle":"2022-04-21T08:41:57.188249Z","shell.execute_reply.started":"2022-04-21T08:41:56.928220Z","shell.execute_reply":"2022-04-21T08:41:57.187583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score_train(vgg_model19_b,test_x_final_vgg19,Y_test)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:42:03.586383Z","iopub.execute_input":"2022-04-21T08:42:03.586655Z","iopub.status.idle":"2022-04-21T08:42:05.294992Z","shell.execute_reply.started":"2022-04-21T08:42:03.586624Z","shell.execute_reply":"2022-04-21T08:42:05.294233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"DenseNet","metadata":{}},{"cell_type":"code","source":"from keras.applications.densenet import DenseNet121","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:42:12.679262Z","iopub.execute_input":"2022-04-21T08:42:12.680147Z","iopub.status.idle":"2022-04-21T08:42:12.685240Z","shell.execute_reply.started":"2022-04-21T08:42:12.680107Z","shell.execute_reply":"2022-04-21T08:42:12.682834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get DenseNet-121 Model\ndef getDenseNet121Model(lastFourTrainable=False):\n    densenet_model = DenseNet121(weights='imagenet', input_shape=input_shape, include_top=True)\n    # Make all layers non-trainable\n    for layer in densenet_model.layers[:]:\n        layer.trainable = False\n    # Add fully connected layer which have 1024 neuron to ResNet-50 model\n    output = densenet_model.get_layer('avg_pool').output\n    output = Flatten(name='new_flatten')(output)\n    output = Dense(units=1024, activation='relu', name='new_fc')(output)\n    predictions = Dense(units=50, activation='softmax')(output)\n    densenet_model = Model(densenet_model.input, predictions)\n    # Make last 4 layers trainable if lastFourTrainable == True\n    if lastFourTrainable == True:\n        densenet_model.get_layer('conv5_block3_2_bn').trainable = True\n        densenet_model.get_layer('conv5_block3_3_conv').trainable = True\n        densenet_model.get_layer('conv5_block3_3_bn').trainable = True\n        densenet_model.get_layer('new_fc').trainable = True\n    # Compile ResNet-50 model\n    densenet_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n    densenet_model.summary()\n    return densenet_model","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:42:16.121270Z","iopub.execute_input":"2022-04-21T08:42:16.122112Z","iopub.status.idle":"2022-04-21T08:42:16.131096Z","shell.execute_reply.started":"2022-04-21T08:42:16.122061Z","shell.execute_reply":"2022-04-21T08:42:16.130248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get DenseNet-121 Model with lastFourTrainable=False\ndensenet_model_a = getDenseNet121Model(lastFourTrainable=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:42:27.743176Z","iopub.execute_input":"2022-04-21T08:42:27.743454Z","iopub.status.idle":"2022-04-21T08:42:30.762332Z","shell.execute_reply.started":"2022-04-21T08:42:27.743406Z","shell.execute_reply":"2022-04-21T08:42:30.761604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x_final_densenet=tensorflow.keras.applications.densenet.preprocess_input(train_x_final)\nvalidation_x_final_densenet=tensorflow.keras.applications.densenet.preprocess_input(validation_x_final)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:42:48.372051Z","iopub.execute_input":"2022-04-21T08:42:48.372313Z","iopub.status.idle":"2022-04-21T08:42:49.042678Z","shell.execute_reply.started":"2022-04-21T08:42:48.372283Z","shell.execute_reply":"2022-04-21T08:42:49.041619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_x_final_densenet=tensorflow.keras.applications.densenet.preprocess_input(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:43:16.935308Z","iopub.execute_input":"2022-04-21T08:43:16.935595Z","iopub.status.idle":"2022-04-21T08:43:17.321627Z","shell.execute_reply.started":"2022-04-21T08:43:16.935544Z","shell.execute_reply":"2022-04-21T08:43:17.320851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train DENSETNET-121 Model \n#resnet_model_a.compile(optimizer=Adam(learning_rate=0.001),loss='categorical_crossentropy',metrics=['accuracy'])\nhistory_densenet_model_a = densenet_model_a.fit(train_x_final_densenet, train_y_final, epochs=30,validation_data=(validation_x_final_densenet, validation_y_final))\n#resnet_model_a = trainModelAndGetConfusionMatrix(resnet_model_a,train_x_final,validation_x_final,X_test,10,64)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:43:52.847688Z","iopub.execute_input":"2022-04-21T08:43:52.848243Z","iopub.status.idle":"2022-04-21T08:44:41.058991Z","shell.execute_reply.started":"2022-04-21T08:43:52.848203Z","shell.execute_reply":"2022-04-21T08:44:41.058215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_hist(history_densenet_model_a,'Densenet')\nscore_train(densenet_model_a,test_x_final_densenet,Y_test)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T08:45:01.839889Z","iopub.execute_input":"2022-04-21T08:45:01.840155Z","iopub.status.idle":"2022-04-21T08:45:05.355660Z","shell.execute_reply.started":"2022-04-21T08:45:01.840124Z","shell.execute_reply":"2022-04-21T08:45:05.354878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## KNN- Image Retrieval","metadata":{}},{"cell_type":"markdown","source":"Using resnet-50-a for feature extraction and KNN","metadata":{}},{"cell_type":"code","source":"from keras.models import Model","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:06:07.050816Z","iopub.execute_input":"2022-04-21T10:06:07.051284Z","iopub.status.idle":"2022-04-21T10:06:07.055595Z","shell.execute_reply.started":"2022-04-21T10:06:07.051252Z","shell.execute_reply":"2022-04-21T10:06:07.054300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_model_resnet= Model(inputs=resnet_model_a.input, outputs=resnet_model_a.get_layer('new_fc').output)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:06:12.998088Z","iopub.execute_input":"2022-04-21T10:06:12.998536Z","iopub.status.idle":"2022-04-21T10:06:13.017028Z","shell.execute_reply.started":"2022-04-21T10:06:12.998504Z","shell.execute_reply":"2022-04-21T10:06:13.016140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_vects=feature_model_resnet.predict(train_x_final_resnet)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:06:15.914265Z","iopub.execute_input":"2022-04-21T10:06:15.914669Z","iopub.status.idle":"2022-04-21T10:07:02.377488Z","shell.execute_reply.started":"2022-04-21T10:06:15.914640Z","shell.execute_reply":"2022-04-21T10:07:02.376616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_vects.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:07:02.379382Z","iopub.execute_input":"2022-04-21T10:07:02.380177Z","iopub.status.idle":"2022-04-21T10:07:02.387784Z","shell.execute_reply.started":"2022-04-21T10:07:02.380128Z","shell.execute_reply":"2022-04-21T10:07:02.386788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier as KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:08:04.204889Z","iopub.execute_input":"2022-04-21T10:08:04.205160Z","iopub.status.idle":"2022-04-21T10:08:04.356056Z","shell.execute_reply.started":"2022-04-21T10:08:04.205132Z","shell.execute_reply":"2022-04-21T10:08:04.355270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import GridSearchCV","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:08:07.198023Z","iopub.execute_input":"2022-04-21T10:08:07.198778Z","iopub.status.idle":"2022-04-21T10:08:07.203454Z","shell.execute_reply.started":"2022-04-21T10:08:07.198723Z","shell.execute_reply":"2022-04-21T10:08:07.202545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_integers=np.argmax(train_y_final, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:08:14.883603Z","iopub.execute_input":"2022-04-21T10:08:14.884111Z","iopub.status.idle":"2022-04-21T10:08:14.888910Z","shell.execute_reply.started":"2022-04-21T10:08:14.884080Z","shell.execute_reply":"2022-04-21T10:08:14.888054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_integers.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:10:38.454316Z","iopub.execute_input":"2022-04-21T10:10:38.454602Z","iopub.status.idle":"2022-04-21T10:10:38.461038Z","shell.execute_reply.started":"2022-04-21T10:10:38.454573Z","shell.execute_reply":"2022-04-21T10:10:38.460434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier()\nknn.fit(feature_vects,labels_integers)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:30:22.156929Z","iopub.execute_input":"2022-04-21T10:30:22.157396Z","iopub.status.idle":"2022-04-21T10:30:22.164503Z","shell.execute_reply.started":"2022-04-21T10:30:22.157351Z","shell.execute_reply":"2022-04-21T10:30:22.163726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_vects=feature_model_resnet.predict(test_x_final_resnet)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:20:32.097049Z","iopub.execute_input":"2022-04-21T10:20:32.097839Z","iopub.status.idle":"2022-04-21T10:20:55.940129Z","shell.execute_reply.started":"2022-04-21T10:20:32.097793Z","shell.execute_reply":"2022-04-21T10:20:55.939335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_vects.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:21:03.303219Z","iopub.execute_input":"2022-04-21T10:21:03.303520Z","iopub.status.idle":"2022-04-21T10:21:03.309287Z","shell.execute_reply.started":"2022-04-21T10:21:03.303485Z","shell.execute_reply":"2022-04-21T10:21:03.308338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:21:05.607715Z","iopub.execute_input":"2022-04-21T10:21:05.607986Z","iopub.status.idle":"2022-04-21T10:21:05.614856Z","shell.execute_reply.started":"2022-04-21T10:21:05.607960Z","shell.execute_reply":"2022-04-21T10:21:05.613641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_label_integers=np.argmax(Y_test, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:21:16.481689Z","iopub.execute_input":"2022-04-21T10:21:16.482281Z","iopub.status.idle":"2022-04-21T10:21:16.486870Z","shell.execute_reply.started":"2022-04-21T10:21:16.482232Z","shell.execute_reply":"2022-04-21T10:21:16.486016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_label_integers.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:21:19.174335Z","iopub.execute_input":"2022-04-21T10:21:19.174981Z","iopub.status.idle":"2022-04-21T10:21:19.181640Z","shell.execute_reply.started":"2022-04-21T10:21:19.174928Z","shell.execute_reply":"2022-04-21T10:21:19.180704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(knn.score(test_vects,test_label_integers))\n#ypred=knn.predict(fin_test_img)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:30:36.965434Z","iopub.execute_input":"2022-04-21T10:30:36.965875Z","iopub.status.idle":"2022-04-21T10:30:37.015121Z","shell.execute_reply.started":"2022-04-21T10:30:36.965843Z","shell.execute_reply":"2022-04-21T10:30:37.014228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"RETREIVAL of FIRST 7 IMAGES","metadata":{}},{"cell_type":"code","source":"knn7 = KNeighborsClassifier(n_neighbors = 7)\nknn7.fit(feature_vects,labels_integers)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:31:35.118253Z","iopub.execute_input":"2022-04-21T10:31:35.118566Z","iopub.status.idle":"2022-04-21T10:31:35.126815Z","shell.execute_reply.started":"2022-04-21T10:31:35.118535Z","shell.execute_reply":"2022-04-21T10:31:35.126134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices=knn7.kneighbors(test_vects, return_distance=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:35:47.827783Z","iopub.execute_input":"2022-04-21T10:35:47.828108Z","iopub.status.idle":"2022-04-21T10:35:47.853885Z","shell.execute_reply.started":"2022-04-21T10:35:47.828065Z","shell.execute_reply":"2022-04-21T10:35:47.852825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:36:24.996529Z","iopub.execute_input":"2022-04-21T10:36:24.996809Z","iopub.status.idle":"2022-04-21T10:36:25.002784Z","shell.execute_reply.started":"2022-04-21T10:36:24.996781Z","shell.execute_reply":"2022-04-21T10:36:25.002016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.max(indices)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:37:51.820022Z","iopub.execute_input":"2022-04-21T10:37:51.820343Z","iopub.status.idle":"2022-04-21T10:37:51.827364Z","shell.execute_reply.started":"2022-04-21T10:37:51.820308Z","shell.execute_reply":"2022-04-21T10:37:51.826506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.min(indices)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:38:08.264219Z","iopub.execute_input":"2022-04-21T10:38:08.264513Z","iopub.status.idle":"2022-04-21T10:38:08.271051Z","shell.execute_reply.started":"2022-04-21T10:38:08.264480Z","shell.execute_reply":"2022-04-21T10:38:08.269941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_neighbors(orig, neighbors):\n    f, axarr = plt.subplots(4, 2)\n    for i, ax in enumerate(axarr.flatten()):\n        if i == 0:\n            ax.set_title(\"Query image\")\n            ax.imshow(orig)\n        else:\n            ax.set_title(f\"Neighbor {i}\")\n            ax.imshow(train_x_final[neighbors[i-1]])\n        ax.set_yticklabels([])\n        ax.set_xticklabels([])\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:51:28.178451Z","iopub.execute_input":"2022-04-21T10:51:28.178780Z","iopub.status.idle":"2022-04-21T10:51:28.186101Z","shell.execute_reply.started":"2022-04-21T10:51:28.178743Z","shell.execute_reply":"2022-04-21T10:51:28.185240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices=knn7.kneighbors(test_vects, return_distance=False)\nfor i in range(0,5):\n    show_neighbors(X_test[i],indices[i])\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2022-04-21T10:51:30.725094Z","iopub.execute_input":"2022-04-21T10:51:30.725416Z","iopub.status.idle":"2022-04-21T10:51:34.913766Z","shell.execute_reply.started":"2022-04-21T10:51:30.725377Z","shell.execute_reply":"2022-04-21T10:51:34.912709Z"},"trusted":true},"execution_count":null,"outputs":[]}]}