{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":29762,"databundleVersionId":2541532,"sourceType":"competition"}],"dockerImageVersionId":30177,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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":"2024-02-27T12:58:30.405613Z","iopub.execute_input":"2024-02-27T12:58:30.406233Z","iopub.status.idle":"2024-02-27T12:58:30.433868Z","shell.execute_reply.started":"2024-02-27T12:58:30.406129Z","shell.execute_reply":"2024-02-27T12:58:30.433283Z"},"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":"2024-02-27T12:58:30.435122Z","iopub.execute_input":"2024-02-27T12:58:30.435344Z","iopub.status.idle":"2024-02-27T12:58:36.950043Z","shell.execute_reply.started":"2024-02-27T12:58:30.435315Z","shell.execute_reply":"2024-02-27T12:58:36.949394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.vgg16 import VGG16","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:58:36.951231Z","iopub.execute_input":"2024-02-27T12:58:36.951532Z","iopub.status.idle":"2024-02-27T12:58:36.955876Z","shell.execute_reply.started":"2024-02-27T12:58:36.951495Z","shell.execute_reply":"2024-02-27T12:58:36.955151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.vgg19 import VGG19","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:58:36.957468Z","iopub.execute_input":"2024-02-27T12:58:36.957696Z","iopub.status.idle":"2024-02-27T12:58:36.976192Z","shell.execute_reply.started":"2024-02-27T12:58:36.957656Z","shell.execute_reply":"2024-02-27T12:58:36.975385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical","metadata":{"editable":false,"execution":{"iopub.status.busy":"2024-02-27T12:58:36.977050Z","iopub.execute_input":"2024-02-27T12:58:36.977272Z","iopub.status.idle":"2024-02-27T12:58:36.987108Z","shell.execute_reply.started":"2024-02-27T12:58:36.977245Z","shell.execute_reply":"2024-02-27T12:58:36.986386Z"},"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":"2024-02-27T12:58:36.988118Z","iopub.execute_input":"2024-02-27T12:58:36.988409Z","iopub.status.idle":"2024-02-27T12:58:36.996853Z","shell.execute_reply.started":"2024-02-27T12:58:36.988372Z","shell.execute_reply":"2024-02-27T12:58:36.996232Z"},"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":"2024-02-27T12:58:36.998821Z","iopub.execute_input":"2024-02-27T12:58:36.999035Z","iopub.status.idle":"2024-02-27T12:58:37.007862Z","shell.execute_reply.started":"2024-02-27T12:58:36.999010Z","shell.execute_reply":"2024-02-27T12:58:37.007131Z"},"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":"2024-02-27T12:58:37.008695Z","iopub.execute_input":"2024-02-27T12:58:37.008885Z","iopub.status.idle":"2024-02-27T12:58:38.474898Z","shell.execute_reply.started":"2024-02-27T12:58:37.008861Z","shell.execute_reply":"2024-02-27T12:58:38.474119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindf.shape","metadata":{"editable":false,"execution":{"iopub.status.busy":"2024-02-27T12:58:38.476146Z","iopub.execute_input":"2024-02-27T12:58:38.476524Z","iopub.status.idle":"2024-02-27T12:58:38.482165Z","shell.execute_reply.started":"2024-02-27T12:58:38.476483Z","shell.execute_reply":"2024-02-27T12:58:38.481401Z"},"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":"2024-02-27T12:58:38.484804Z","iopub.execute_input":"2024-02-27T12:58:38.485025Z","iopub.status.idle":"2024-02-27T12:58:38.516755Z","shell.execute_reply.started":"2024-02-27T12:58:38.484999Z","shell.execute_reply":"2024-02-27T12:58:38.516059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"landmark_unique[0:50]","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:58:38.517604Z","iopub.execute_input":"2024-02-27T12:58:38.517785Z","iopub.status.idle":"2024-02-27T12:58:38.523029Z","shell.execute_reply.started":"2024-02-27T12:58:38.517762Z","shell.execute_reply":"2024-02-27T12:58:38.522395Z"},"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":"2024-02-27T12:58:38.523990Z","iopub.execute_input":"2024-02-27T12:58:38.524202Z","iopub.status.idle":"2024-02-27T12:58:38.618039Z","shell.execute_reply.started":"2024-02-27T12:58:38.524176Z","shell.execute_reply":"2024-02-27T12:58:38.617308Z"},"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":"2024-02-27T12:58:38.618988Z","iopub.execute_input":"2024-02-27T12:58:38.619204Z","iopub.status.idle":"2024-02-27T12:58:54.154643Z","shell.execute_reply.started":"2024-02-27T12:58:38.619177Z","shell.execute_reply":"2024-02-27T12:58:54.153855Z"},"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":"2024-02-27T12:58:54.155876Z","iopub.execute_input":"2024-02-27T12:58:54.156265Z","iopub.status.idle":"2024-02-27T12:58:54.162515Z","shell.execute_reply.started":"2024-02-27T12:58:54.156221Z","shell.execute_reply":"2024-02-27T12:58:54.161590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(temp_labels)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:58:54.164324Z","iopub.execute_input":"2024-02-27T12:58:54.164674Z","iopub.status.idle":"2024-02-27T12:58:54.176580Z","shell.execute_reply.started":"2024-02-27T12:58:54.164638Z","shell.execute_reply":"2024-02-27T12:58:54.175674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random","metadata":{"editable":false,"execution":{"iopub.status.busy":"2024-02-27T12:58:54.177819Z","iopub.execute_input":"2024-02-27T12:58:54.178077Z","iopub.status.idle":"2024-02-27T12:58:54.188063Z","shell.execute_reply.started":"2024-02-27T12:58:54.178044Z","shell.execute_reply":"2024-02-27T12:58:54.187277Z"},"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":"2024-02-27T12:58:54.190980Z","iopub.execute_input":"2024-02-27T12:58:54.191194Z","iopub.status.idle":"2024-02-27T12:58:55.801382Z","shell.execute_reply.started":"2024-02-27T12:58:54.191168Z","shell.execute_reply":"2024-02-27T12:58:55.800463Z"},"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":"2024-02-27T12:58:55.802444Z","iopub.execute_input":"2024-02-27T12:58:55.802670Z","iopub.status.idle":"2024-02-27T12:58:57.183304Z","shell.execute_reply.started":"2024-02-27T12:58:55.802634Z","shell.execute_reply":"2024-02-27T12:58:57.182555Z"},"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":"2024-02-27T12:58:57.184447Z","iopub.execute_input":"2024-02-27T12:58:57.184696Z","iopub.status.idle":"2024-02-27T12:58:57.194131Z","shell.execute_reply.started":"2024-02-27T12:58:57.184657Z","shell.execute_reply":"2024-02-27T12:58:57.193367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels_data)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:58:57.195244Z","iopub.execute_input":"2024-02-27T12:58:57.195508Z","iopub.status.idle":"2024-02-27T12:58:57.206261Z","shell.execute_reply.started":"2024-02-27T12:58:57.195476Z","shell.execute_reply":"2024-02-27T12:58:57.205510Z"},"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":{"editable":false,"execution":{"iopub.status.busy":"2024-02-27T12:58:57.207272Z","iopub.execute_input":"2024-02-27T12:58:57.207552Z","iopub.status.idle":"2024-02-27T12:58:57.218938Z","shell.execute_reply.started":"2024-02-27T12:58:57.207524Z","shell.execute_reply":"2024-02-27T12:58:57.218205Z"},"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":{"editable":false,"execution":{"iopub.status.busy":"2024-02-27T12:58:57.219958Z","iopub.execute_input":"2024-02-27T12:58:57.220180Z","iopub.status.idle":"2024-02-27T12:58:57.235177Z","shell.execute_reply.started":"2024-02-27T12:58:57.220146Z","shell.execute_reply":"2024-02-27T12:58:57.234397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = np.array(train_data) #/ 255\n","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:58:57.236207Z","iopub.execute_input":"2024-02-27T12:58:57.236433Z","iopub.status.idle":"2024-02-27T12:58:57.299341Z","shell.execute_reply.started":"2024-02-27T12:58:57.236405Z","shell.execute_reply":"2024-02-27T12:58:57.298693Z"},"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":"2024-02-27T12:58:57.300329Z","iopub.execute_input":"2024-02-27T12:58:57.300563Z","iopub.status.idle":"2024-02-27T12:58:57.309900Z","shell.execute_reply.started":"2024-02-27T12:58:57.300535Z","shell.execute_reply":"2024-02-27T12:58:57.309186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels_data[0:6])","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:58:57.310845Z","iopub.execute_input":"2024-02-27T12:58:57.311027Z","iopub.status.idle":"2024-02-27T12:58:57.318580Z","shell.execute_reply.started":"2024-02-27T12:58:57.311004Z","shell.execute_reply":"2024-02-27T12:58:57.317865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(a_pd)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:58:57.319605Z","iopub.execute_input":"2024-02-27T12:58:57.319859Z","iopub.status.idle":"2024-02-27T12:58:57.330048Z","shell.execute_reply.started":"2024-02-27T12:58:57.319823Z","shell.execute_reply":"2024-02-27T12:58:57.329385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_data=a_pd","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:58:57.330872Z","iopub.execute_input":"2024-02-27T12:58:57.331062Z","iopub.status.idle":"2024-02-27T12:58:57.340327Z","shell.execute_reply.started":"2024-02-27T12:58:57.331038Z","shell.execute_reply":"2024-02-27T12:58:57.339635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels_data[0:5])","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:58:57.346324Z","iopub.execute_input":"2024-02-27T12:58:57.346564Z","iopub.status.idle":"2024-02-27T12:58:57.354486Z","shell.execute_reply.started":"2024-02-27T12:58:57.346534Z","shell.execute_reply":"2024-02-27T12:58:57.353669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_data.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:58:57.355519Z","iopub.execute_input":"2024-02-27T12:58:57.355733Z","iopub.status.idle":"2024-02-27T12:58:57.366322Z","shell.execute_reply.started":"2024-02-27T12:58:57.355707Z","shell.execute_reply":"2024-02-27T12:58:57.365594Z"},"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":"2024-02-27T12:58:57.367496Z","iopub.execute_input":"2024-02-27T12:58:57.367752Z","iopub.status.idle":"2024-02-27T12:58:57.439870Z","shell.execute_reply.started":"2024-02-27T12:58:57.367725Z","shell.execute_reply":"2024-02-27T12:58:57.439142Z"},"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":"2024-02-27T12:58:57.440824Z","iopub.execute_input":"2024-02-27T12:58:57.441017Z","iopub.status.idle":"2024-02-27T12:58:57.447128Z","shell.execute_reply.started":"2024-02-27T12:58:57.440992Z","shell.execute_reply":"2024-02-27T12:58:57.446401Z"},"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":"2024-02-27T12:58:57.448026Z","iopub.execute_input":"2024-02-27T12:58:57.448227Z","iopub.status.idle":"2024-02-27T12:58:57.492870Z","shell.execute_reply.started":"2024-02-27T12:58:57.448201Z","shell.execute_reply":"2024-02-27T12:58:57.492062Z"},"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":"2024-02-27T12:58:57.493938Z","iopub.execute_input":"2024-02-27T12:58:57.494163Z","iopub.status.idle":"2024-02-27T12:58:57.508877Z","shell.execute_reply.started":"2024-02-27T12:58:57.494133Z","shell.execute_reply":"2024-02-27T12:58:57.508221Z"},"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":"2024-02-27T12:58:57.509829Z","iopub.execute_input":"2024-02-27T12:58:57.510039Z","iopub.status.idle":"2024-02-27T12:58:57.517476Z","shell.execute_reply.started":"2024-02-27T12:58:57.510012Z","shell.execute_reply":"2024-02-27T12:58:57.516780Z"},"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":"2024-02-27T12:58:57.518404Z","iopub.execute_input":"2024-02-27T12:58:57.518645Z","iopub.status.idle":"2024-02-27T12:58:57.528945Z","shell.execute_reply.started":"2024-02-27T12:58:57.518618Z","shell.execute_reply":"2024-02-27T12:58:57.528031Z"},"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":"2024-02-27T12:58:57.530319Z","iopub.execute_input":"2024-02-27T12:58:57.530694Z","iopub.status.idle":"2024-02-27T12:58:57.543387Z","shell.execute_reply.started":"2024-02-27T12:58:57.530658Z","shell.execute_reply":"2024-02-27T12:58:57.542605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg_model19_a = getVGG19Model(lastFourTrainable=False)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:58:57.544410Z","iopub.execute_input":"2024-02-27T12:58:57.544614Z","iopub.status.idle":"2024-02-27T12:59:04.056164Z","shell.execute_reply.started":"2024-02-27T12:58:57.544584Z","shell.execute_reply":"2024-02-27T12:59:04.055436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg_model19_b = getVGG19Model(lastFourTrainable=True)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:59:04.057372Z","iopub.execute_input":"2024-02-27T12:59:04.057654Z","iopub.status.idle":"2024-02-27T12:59:06.043861Z","shell.execute_reply.started":"2024-02-27T12:59:04.057604Z","shell.execute_reply":"2024-02-27T12:59:06.042966Z"},"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":"2024-02-27T12:59:06.045135Z","iopub.execute_input":"2024-02-27T12:59:06.045492Z","iopub.status.idle":"2024-02-27T12:59:08.474098Z","shell.execute_reply.started":"2024-02-27T12:59:06.045452Z","shell.execute_reply":"2024-02-27T12:59:08.473285Z"},"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":"2024-02-27T12:59:08.475216Z","iopub.execute_input":"2024-02-27T12:59:08.475518Z","iopub.status.idle":"2024-02-27T12:59:08.480060Z","shell.execute_reply.started":"2024-02-27T12:59:08.475487Z","shell.execute_reply":"2024-02-27T12:59:08.479388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:59:08.481042Z","iopub.execute_input":"2024-02-27T12:59:08.481272Z","iopub.status.idle":"2024-02-27T12:59:08.491082Z","shell.execute_reply.started":"2024-02-27T12:59:08.481233Z","shell.execute_reply":"2024-02-27T12:59:08.490512Z"},"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":"2024-02-27T12:59:08.492103Z","iopub.execute_input":"2024-02-27T12:59:08.492446Z","iopub.status.idle":"2024-02-27T12:59:08.503063Z","shell.execute_reply.started":"2024-02-27T12:59:08.492407Z","shell.execute_reply":"2024-02-27T12:59:08.502387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:59:08.504084Z","iopub.execute_input":"2024-02-27T12:59:08.504311Z","iopub.status.idle":"2024-02-27T12:59:08.515376Z","shell.execute_reply.started":"2024-02-27T12:59:08.504284Z","shell.execute_reply":"2024-02-27T12:59:08.514748Z"},"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":"2024-02-27T12:59:08.516253Z","iopub.execute_input":"2024-02-27T12:59:08.516474Z","iopub.status.idle":"2024-02-27T12:59:08.794907Z","shell.execute_reply.started":"2024-02-27T12:59:08.516448Z","shell.execute_reply":"2024-02-27T12:59:08.794236Z"},"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":"2024-02-27T12:59:08.796292Z","iopub.execute_input":"2024-02-27T12:59:08.796766Z","iopub.status.idle":"2024-02-27T12:59:08.914473Z","shell.execute_reply.started":"2024-02-27T12:59:08.796723Z","shell.execute_reply":"2024-02-27T12:59:08.913794Z"},"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":"2024-02-27T12:59:08.915608Z","iopub.execute_input":"2024-02-27T12:59:08.915889Z","iopub.status.idle":"2024-02-27T13:00:02.253635Z","shell.execute_reply.started":"2024-02-27T12:59:08.915851Z","shell.execute_reply":"2024-02-27T13:00:02.252966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_hist(history1,'Resnet-50')","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:00:02.255017Z","iopub.execute_input":"2024-02-27T13:00:02.255245Z","iopub.status.idle":"2024-02-27T13:00:02.519824Z","shell.execute_reply.started":"2024-02-27T13:00:02.255217Z","shell.execute_reply":"2024-02-27T13:00:02.519079Z"},"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":"2024-02-27T13:00:02.520818Z","iopub.execute_input":"2024-02-27T13:00:02.521015Z","iopub.status.idle":"2024-02-27T13:00:05.211023Z","shell.execute_reply.started":"2024-02-27T13:00:02.520989Z","shell.execute_reply":"2024-02-27T13:00:05.210181Z"},"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":"2024-02-27T13:00:05.212213Z","iopub.execute_input":"2024-02-27T13:00:05.212426Z","iopub.status.idle":"2024-02-27T13:00:06.753149Z","shell.execute_reply.started":"2024-02-27T13:00:05.212401Z","shell.execute_reply":"2024-02-27T13:00:06.752402Z"},"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":"2024-02-27T13:00:06.754424Z","iopub.execute_input":"2024-02-27T13:00:06.754700Z","iopub.status.idle":"2024-02-27T13:01:32.268024Z","shell.execute_reply.started":"2024-02-27T13:00:06.754660Z","shell.execute_reply":"2024-02-27T13:01:32.267310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_hist(history,'Resnet-50-b')","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:01:32.269521Z","iopub.execute_input":"2024-02-27T13:01:32.269761Z","iopub.status.idle":"2024-02-27T13:01:32.568549Z","shell.execute_reply.started":"2024-02-27T13:01:32.269733Z","shell.execute_reply":"2024-02-27T13:01:32.567876Z"},"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":"2024-02-27T13:01:32.569527Z","iopub.execute_input":"2024-02-27T13:01:32.569741Z","iopub.status.idle":"2024-02-27T13:01:35.006981Z","shell.execute_reply.started":"2024-02-27T13:01:32.569712Z","shell.execute_reply":"2024-02-27T13:01:35.006224Z"},"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":"2024-02-27T13:01:35.008205Z","iopub.execute_input":"2024-02-27T13:01:35.008447Z","iopub.status.idle":"2024-02-27T13:01:35.277170Z","shell.execute_reply.started":"2024-02-27T13:01:35.008417Z","shell.execute_reply":"2024-02-27T13:01:35.276502Z"},"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":"2024-02-27T13:01:35.278482Z","iopub.execute_input":"2024-02-27T13:01:35.278710Z","iopub.status.idle":"2024-02-27T13:01:35.394917Z","shell.execute_reply.started":"2024-02-27T13:01:35.278683Z","shell.execute_reply":"2024-02-27T13:01:35.394146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgga=getVGG16Model(lastFourTrainable=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:01:35.396185Z","iopub.execute_input":"2024-02-27T13:01:35.396501Z","iopub.status.idle":"2024-02-27T13:01:39.574744Z","shell.execute_reply.started":"2024-02-27T13:01:35.396460Z","shell.execute_reply":"2024-02-27T13:01:39.574034Z"},"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":"2024-02-27T13:01:39.575903Z","iopub.execute_input":"2024-02-27T13:01:39.576132Z","iopub.status.idle":"2024-02-27T13:02:33.028027Z","shell.execute_reply.started":"2024-02-27T13:01:39.576097Z","shell.execute_reply":"2024-02-27T13:02:33.027219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_hist(history,'VGG-16')","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:02:33.030483Z","iopub.execute_input":"2024-02-27T13:02:33.033342Z","iopub.status.idle":"2024-02-27T13:02:33.332520Z","shell.execute_reply.started":"2024-02-27T13:02:33.033295Z","shell.execute_reply":"2024-02-27T13:02:33.331771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score_train(vgga,test_x_final_vgg16,Y_test)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:02:33.333710Z","iopub.execute_input":"2024-02-27T13:02:33.333926Z","iopub.status.idle":"2024-02-27T13:02:37.034190Z","shell.execute_reply.started":"2024-02-27T13:02:33.333899Z","shell.execute_reply":"2024-02-27T13:02:37.033404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vggb=getVGG16Model(lastFourTrainable=True)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:02:37.035484Z","iopub.execute_input":"2024-02-27T13:02:37.035723Z","iopub.status.idle":"2024-02-27T13:02:38.981935Z","shell.execute_reply.started":"2024-02-27T13:02:37.035694Z","shell.execute_reply":"2024-02-27T13:02:38.981088Z"},"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":"2024-02-27T13:02:38.983039Z","iopub.execute_input":"2024-02-27T13:02:38.983287Z","iopub.status.idle":"2024-02-27T13:03:36.897452Z","shell.execute_reply.started":"2024-02-27T13:02:38.983259Z","shell.execute_reply":"2024-02-27T13:03:36.896691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_hist(history_vgg16_b,'Vgg-16-b')","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:03:36.898543Z","iopub.execute_input":"2024-02-27T13:03:36.898752Z","iopub.status.idle":"2024-02-27T13:03:37.208054Z","shell.execute_reply.started":"2024-02-27T13:03:36.898725Z","shell.execute_reply":"2024-02-27T13:03:37.207379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score_train(vggb,test_x_final_vgg16,Y_test)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:03:37.209074Z","iopub.execute_input":"2024-02-27T13:03:37.209290Z","iopub.status.idle":"2024-02-27T13:03:39.221666Z","shell.execute_reply.started":"2024-02-27T13:03:37.209262Z","shell.execute_reply":"2024-02-27T13:03:39.220890Z"},"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":"2024-02-27T13:04:58.586229Z","iopub.execute_input":"2024-02-27T13:04:58.586963Z","iopub.status.idle":"2024-02-27T13:04:58.590661Z","shell.execute_reply.started":"2024-02-27T13:04:58.586908Z","shell.execute_reply":"2024-02-27T13:04:58.589925Z"},"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":"2024-02-27T13:05:00.908043Z","iopub.execute_input":"2024-02-27T13:05:00.908306Z","iopub.status.idle":"2024-02-27T13:05:00.917308Z","shell.execute_reply.started":"2024-02-27T13:05:00.908279Z","shell.execute_reply":"2024-02-27T13:05:00.916389Z"},"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":"2024-02-27T13:05:01.359330Z","iopub.execute_input":"2024-02-27T13:05:01.360267Z","iopub.status.idle":"2024-02-27T13:05:04.458396Z","shell.execute_reply.started":"2024-02-27T13:05:01.360227Z","shell.execute_reply":"2024-02-27T13:05:04.457593Z"},"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":"2024-02-27T13:05:04.463821Z","iopub.execute_input":"2024-02-27T13:05:04.464498Z","iopub.status.idle":"2024-02-27T13:05:04.934906Z","shell.execute_reply.started":"2024-02-27T13:05:04.464454Z","shell.execute_reply":"2024-02-27T13:05:04.934212Z"},"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":"2024-02-27T13:05:04.935921Z","iopub.execute_input":"2024-02-27T13:05:04.936129Z","iopub.status.idle":"2024-02-27T13:05:05.142610Z","shell.execute_reply.started":"2024-02-27T13:05:04.936096Z","shell.execute_reply":"2024-02-27T13:05:05.141943Z"},"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":"2024-02-27T13:05:05.144127Z","iopub.execute_input":"2024-02-27T13:05:05.144384Z","iopub.status.idle":"2024-02-27T13:05:55.643391Z","shell.execute_reply.started":"2024-02-27T13:05:05.144333Z","shell.execute_reply":"2024-02-27T13:05:55.642627Z"},"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":"2024-02-27T13:05:55.644862Z","iopub.execute_input":"2024-02-27T13:05:55.645142Z","iopub.status.idle":"2024-02-27T13:05:59.105079Z","shell.execute_reply.started":"2024-02-27T13:05:55.645093Z","shell.execute_reply":"2024-02-27T13:05:59.104399Z"},"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":"2024-02-27T13:05:59.106952Z","iopub.execute_input":"2024-02-27T13:05:59.107176Z","iopub.status.idle":"2024-02-27T13:05:59.110906Z","shell.execute_reply.started":"2024-02-27T13:05:59.107147Z","shell.execute_reply":"2024-02-27T13:05:59.110096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_model_vgg_model19_a= Model(inputs=vgg_model19_a.input, outputs=vgg_model19_a.get_layer('new_fc').output)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:05:59.111959Z","iopub.execute_input":"2024-02-27T13:05:59.112148Z","iopub.status.idle":"2024-02-27T13:05:59.126165Z","shell.execute_reply.started":"2024-02-27T13:05:59.112124Z","shell.execute_reply":"2024-02-27T13:05:59.125573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_vects=feature_model_vgg_model19_a.predict(train_x_final_vgg19)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:05:59.127050Z","iopub.execute_input":"2024-02-27T13:05:59.127237Z","iopub.status.idle":"2024-02-27T13:06:01.780792Z","shell.execute_reply.started":"2024-02-27T13:05:59.127214Z","shell.execute_reply":"2024-02-27T13:06:01.780042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_vects.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:06:01.782047Z","iopub.execute_input":"2024-02-27T13:06:01.782288Z","iopub.status.idle":"2024-02-27T13:06:01.787798Z","shell.execute_reply.started":"2024-02-27T13:06:01.782260Z","shell.execute_reply":"2024-02-27T13:06:01.786974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier as KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:06:01.788842Z","iopub.execute_input":"2024-02-27T13:06:01.789054Z","iopub.status.idle":"2024-02-27T13:06:01.911824Z","shell.execute_reply.started":"2024-02-27T13:06:01.789027Z","shell.execute_reply":"2024-02-27T13:06:01.911079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import GridSearchCV","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:06:01.914113Z","iopub.execute_input":"2024-02-27T13:06:01.914469Z","iopub.status.idle":"2024-02-27T13:06:01.920526Z","shell.execute_reply.started":"2024-02-27T13:06:01.914435Z","shell.execute_reply":"2024-02-27T13:06:01.919818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_integers=np.argmax(train_y_final, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:06:01.921451Z","iopub.execute_input":"2024-02-27T13:06:01.921639Z","iopub.status.idle":"2024-02-27T13:06:01.932750Z","shell.execute_reply.started":"2024-02-27T13:06:01.921615Z","shell.execute_reply":"2024-02-27T13:06:01.932088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_integers.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:06:01.933665Z","iopub.execute_input":"2024-02-27T13:06:01.933932Z","iopub.status.idle":"2024-02-27T13:06:01.947013Z","shell.execute_reply.started":"2024-02-27T13:06:01.933904Z","shell.execute_reply":"2024-02-27T13:06:01.946314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier()\nknn.fit(feature_vects,labels_integers)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:06:01.947915Z","iopub.execute_input":"2024-02-27T13:06:01.948132Z","iopub.status.idle":"2024-02-27T13:06:01.962897Z","shell.execute_reply.started":"2024-02-27T13:06:01.948092Z","shell.execute_reply":"2024-02-27T13:06:01.962148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_vects=feature_model_vgg_model19_a.predict(test_x_final_vgg19)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:06:01.963998Z","iopub.execute_input":"2024-02-27T13:06:01.964267Z","iopub.status.idle":"2024-02-27T13:06:03.453513Z","shell.execute_reply.started":"2024-02-27T13:06:01.964230Z","shell.execute_reply":"2024-02-27T13:06:03.452792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_vects.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:06:03.454646Z","iopub.execute_input":"2024-02-27T13:06:03.454842Z","iopub.status.idle":"2024-02-27T13:06:03.459993Z","shell.execute_reply.started":"2024-02-27T13:06:03.454817Z","shell.execute_reply":"2024-02-27T13:06:03.459186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_test.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:06:03.461033Z","iopub.execute_input":"2024-02-27T13:06:03.461300Z","iopub.status.idle":"2024-02-27T13:06:03.471925Z","shell.execute_reply.started":"2024-02-27T13:06:03.461270Z","shell.execute_reply":"2024-02-27T13:06:03.471171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_label_integers=np.argmax(Y_test, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:06:03.474961Z","iopub.execute_input":"2024-02-27T13:06:03.475194Z","iopub.status.idle":"2024-02-27T13:06:03.481912Z","shell.execute_reply.started":"2024-02-27T13:06:03.475165Z","shell.execute_reply":"2024-02-27T13:06:03.481301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_label_integers.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:06:03.482860Z","iopub.execute_input":"2024-02-27T13:06:03.483109Z","iopub.status.idle":"2024-02-27T13:06:03.493657Z","shell.execute_reply.started":"2024-02-27T13:06:03.483045Z","shell.execute_reply":"2024-02-27T13:06:03.492858Z"},"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":"2024-02-27T13:06:03.494598Z","iopub.execute_input":"2024-02-27T13:06:03.494821Z","iopub.status.idle":"2024-02-27T13:06:03.549829Z","shell.execute_reply.started":"2024-02-27T13:06:03.494788Z","shell.execute_reply":"2024-02-27T13:06:03.548847Z"},"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":"2024-02-27T13:06:03.551757Z","iopub.execute_input":"2024-02-27T13:06:03.552591Z","iopub.status.idle":"2024-02-27T13:06:03.562472Z","shell.execute_reply.started":"2024-02-27T13:06:03.552539Z","shell.execute_reply":"2024-02-27T13:06:03.561390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices=knn7.kneighbors(test_vects, return_distance=False)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:06:03.564471Z","iopub.execute_input":"2024-02-27T13:06:03.565263Z","iopub.status.idle":"2024-02-27T13:06:03.589263Z","shell.execute_reply.started":"2024-02-27T13:06:03.565210Z","shell.execute_reply":"2024-02-27T13:06:03.588233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:06:03.591133Z","iopub.execute_input":"2024-02-27T13:06:03.592182Z","iopub.status.idle":"2024-02-27T13:06:03.599769Z","shell.execute_reply.started":"2024-02-27T13:06:03.592126Z","shell.execute_reply":"2024-02-27T13:06:03.598888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.max(indices)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:06:03.601801Z","iopub.execute_input":"2024-02-27T13:06:03.602752Z","iopub.status.idle":"2024-02-27T13:06:03.611984Z","shell.execute_reply.started":"2024-02-27T13:06:03.602697Z","shell.execute_reply":"2024-02-27T13:06:03.610707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.min(indices)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T13:06:03.613540Z","iopub.execute_input":"2024-02-27T13:06:03.614122Z","iopub.status.idle":"2024-02-27T13:06:03.622968Z","shell.execute_reply.started":"2024-02-27T13:06:03.614070Z","shell.execute_reply":"2024-02-27T13:06:03.621930Z"},"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":"2024-02-27T13:06:03.628768Z","iopub.execute_input":"2024-02-27T13:06:03.637545Z","iopub.status.idle":"2024-02-27T13:06:03.651413Z","shell.execute_reply.started":"2024-02-27T13:06:03.637488Z","shell.execute_reply":"2024-02-27T13:06:03.650255Z"},"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":"2024-02-27T13:06:03.653229Z","iopub.execute_input":"2024-02-27T13:06:03.659114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ypred=knn7.predict(test_vects)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import metrics \nprint(metrics.classification_report(test_label_integers,ypred))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}