{"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-21T20:27:04.393875Z","iopub.execute_input":"2022-04-21T20:27:04.394727Z","iopub.status.idle":"2022-04-21T20:27:04.399381Z","shell.execute_reply.started":"2022-04-21T20:27:04.394660Z","shell.execute_reply":"2022-04-21T20:27:04.398494Z"},"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-21T20:27:04.422678Z","iopub.execute_input":"2022-04-21T20:27:04.422976Z","iopub.status.idle":"2022-04-21T20:27:09.432101Z","shell.execute_reply.started":"2022-04-21T20:27:04.422949Z","shell.execute_reply":"2022-04-21T20:27:09.431370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.vgg16 import VGG16","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:27:09.434433Z","iopub.execute_input":"2022-04-21T20:27:09.434623Z","iopub.status.idle":"2022-04-21T20:27:09.444068Z","shell.execute_reply.started":"2022-04-21T20:27:09.434599Z","shell.execute_reply":"2022-04-21T20:27:09.443345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.vgg19 import VGG19","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:27:09.446526Z","iopub.execute_input":"2022-04-21T20:27:09.446839Z","iopub.status.idle":"2022-04-21T20:27:09.456887Z","shell.execute_reply.started":"2022-04-21T20:27:09.446806Z","shell.execute_reply":"2022-04-21T20:27:09.456218Z"},"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-21T20:27:09.459114Z","iopub.execute_input":"2022-04-21T20:27:09.459960Z","iopub.status.idle":"2022-04-21T20:27:09.465285Z","shell.execute_reply.started":"2022-04-21T20:27:09.459922Z","shell.execute_reply":"2022-04-21T20:27:09.464575Z"},"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-21T20:27:09.466747Z","iopub.execute_input":"2022-04-21T20:27:09.467261Z","iopub.status.idle":"2022-04-21T20:27:09.473485Z","shell.execute_reply.started":"2022-04-21T20:27:09.467225Z","shell.execute_reply":"2022-04-21T20:27:09.472809Z"},"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-21T20:27:09.474850Z","iopub.execute_input":"2022-04-21T20:27:09.475519Z","iopub.status.idle":"2022-04-21T20:27:09.484285Z","shell.execute_reply.started":"2022-04-21T20:27:09.475424Z","shell.execute_reply":"2022-04-21T20:27:09.483610Z"},"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-21T20:27:09.486274Z","iopub.execute_input":"2022-04-21T20:27:09.487688Z","iopub.status.idle":"2022-04-21T20:27:10.782999Z","shell.execute_reply.started":"2022-04-21T20:27:09.487656Z","shell.execute_reply":"2022-04-21T20:27:10.782277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindf.shape","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-21T20:27:10.784297Z","iopub.execute_input":"2022-04-21T20:27:10.784533Z","iopub.status.idle":"2022-04-21T20:27:10.792754Z","shell.execute_reply.started":"2022-04-21T20:27:10.784500Z","shell.execute_reply":"2022-04-21T20:27:10.791938Z"},"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-21T20:27:10.793870Z","iopub.execute_input":"2022-04-21T20:27:10.794127Z","iopub.status.idle":"2022-04-21T20:27:10.821099Z","shell.execute_reply.started":"2022-04-21T20:27:10.794091Z","shell.execute_reply":"2022-04-21T20:27:10.820542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"landmark_unique[0:50]","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:27:10.823347Z","iopub.execute_input":"2022-04-21T20:27:10.823536Z","iopub.status.idle":"2022-04-21T20:27:10.828925Z","shell.execute_reply.started":"2022-04-21T20:27:10.823513Z","shell.execute_reply":"2022-04-21T20:27:10.828301Z"},"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-21T20:27:10.830201Z","iopub.execute_input":"2022-04-21T20:27:10.830610Z","iopub.status.idle":"2022-04-21T20:27:10.949317Z","shell.execute_reply.started":"2022-04-21T20:27:10.830574Z","shell.execute_reply":"2022-04-21T20:27:10.948648Z"},"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-21T20:27:10.951639Z","iopub.execute_input":"2022-04-21T20:27:10.951941Z","iopub.status.idle":"2022-04-21T20:27:28.690441Z","shell.execute_reply.started":"2022-04-21T20:27:10.951907Z","shell.execute_reply":"2022-04-21T20:27:28.689709Z"},"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-21T20:27:28.691855Z","iopub.execute_input":"2022-04-21T20:27:28.692114Z","iopub.status.idle":"2022-04-21T20:27:28.697589Z","shell.execute_reply.started":"2022-04-21T20:27:28.692078Z","shell.execute_reply":"2022-04-21T20:27:28.696911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(temp_labels)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:27:28.698983Z","iopub.execute_input":"2022-04-21T20:27:28.699550Z","iopub.status.idle":"2022-04-21T20:27:28.706754Z","shell.execute_reply.started":"2022-04-21T20:27:28.699512Z","shell.execute_reply":"2022-04-21T20:27:28.705896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-21T20:27:28.707983Z","iopub.execute_input":"2022-04-21T20:27:28.708264Z","iopub.status.idle":"2022-04-21T20:27:28.713576Z","shell.execute_reply.started":"2022-04-21T20:27:28.708228Z","shell.execute_reply":"2022-04-21T20:27:28.712758Z"},"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-21T20:27:28.714675Z","iopub.execute_input":"2022-04-21T20:27:28.715371Z","iopub.status.idle":"2022-04-21T20:27:30.156741Z","shell.execute_reply.started":"2022-04-21T20:27:28.715335Z","shell.execute_reply":"2022-04-21T20:27:30.155996Z"},"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-21T20:27:30.158032Z","iopub.execute_input":"2022-04-21T20:27:30.158299Z","iopub.status.idle":"2022-04-21T20:27:31.491207Z","shell.execute_reply.started":"2022-04-21T20:27:30.158264Z","shell.execute_reply":"2022-04-21T20:27:31.490512Z"},"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-21T20:27:31.492204Z","iopub.execute_input":"2022-04-21T20:27:31.492534Z","iopub.status.idle":"2022-04-21T20:27:31.501912Z","shell.execute_reply.started":"2022-04-21T20:27:31.492503Z","shell.execute_reply":"2022-04-21T20:27:31.501140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels_data)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:27:31.503273Z","iopub.execute_input":"2022-04-21T20:27:31.503728Z","iopub.status.idle":"2022-04-21T20:27:31.509692Z","shell.execute_reply.started":"2022-04-21T20:27:31.503678Z","shell.execute_reply":"2022-04-21T20:27:31.508948Z"},"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":"2022-04-21T20:27:31.511057Z","iopub.execute_input":"2022-04-21T20:27:31.511487Z","iopub.status.idle":"2022-04-21T20:27:31.518894Z","shell.execute_reply.started":"2022-04-21T20:27:31.511454Z","shell.execute_reply":"2022-04-21T20:27:31.518142Z"},"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":"2022-04-21T20:27:31.520321Z","iopub.execute_input":"2022-04-21T20:27:31.520794Z","iopub.status.idle":"2022-04-21T20:27:31.531366Z","shell.execute_reply.started":"2022-04-21T20:27:31.520759Z","shell.execute_reply":"2022-04-21T20:27:31.530546Z"},"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-21T20:27:31.532801Z","iopub.execute_input":"2022-04-21T20:27:31.533266Z","iopub.status.idle":"2022-04-21T20:27:31.593924Z","shell.execute_reply.started":"2022-04-21T20:27:31.533229Z","shell.execute_reply":"2022-04-21T20:27:31.593166Z"},"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-21T20:27:31.595379Z","iopub.execute_input":"2022-04-21T20:27:31.595641Z","iopub.status.idle":"2022-04-21T20:27:31.606054Z","shell.execute_reply.started":"2022-04-21T20:27:31.595604Z","shell.execute_reply":"2022-04-21T20:27:31.605212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels_data[0:6])","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:27:31.607282Z","iopub.execute_input":"2022-04-21T20:27:31.608102Z","iopub.status.idle":"2022-04-21T20:27:31.613383Z","shell.execute_reply.started":"2022-04-21T20:27:31.608055Z","shell.execute_reply":"2022-04-21T20:27:31.612581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(a_pd)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:27:31.614931Z","iopub.execute_input":"2022-04-21T20:27:31.615517Z","iopub.status.idle":"2022-04-21T20:27:31.621769Z","shell.execute_reply.started":"2022-04-21T20:27:31.615482Z","shell.execute_reply":"2022-04-21T20:27:31.621022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_data=a_pd","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:27:31.623314Z","iopub.execute_input":"2022-04-21T20:27:31.623926Z","iopub.status.idle":"2022-04-21T20:27:31.627317Z","shell.execute_reply.started":"2022-04-21T20:27:31.623889Z","shell.execute_reply":"2022-04-21T20:27:31.626559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels_data[0:5])","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:27:31.628728Z","iopub.execute_input":"2022-04-21T20:27:31.629192Z","iopub.status.idle":"2022-04-21T20:27:31.639970Z","shell.execute_reply.started":"2022-04-21T20:27:31.629157Z","shell.execute_reply":"2022-04-21T20:27:31.639315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:27:31.645923Z","iopub.execute_input":"2022-04-21T20:27:31.646109Z","iopub.status.idle":"2022-04-21T20:27:31.651504Z","shell.execute_reply.started":"2022-04-21T20:27:31.646081Z","shell.execute_reply":"2022-04-21T20:27:31.650570Z"},"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-21T20:27:31.653115Z","iopub.execute_input":"2022-04-21T20:27:31.653373Z","iopub.status.idle":"2022-04-21T20:27:31.724547Z","shell.execute_reply.started":"2022-04-21T20:27:31.653339Z","shell.execute_reply":"2022-04-21T20:27:31.723659Z"},"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-21T20:27:31.726097Z","iopub.execute_input":"2022-04-21T20:27:31.726358Z","iopub.status.idle":"2022-04-21T20:27:31.733479Z","shell.execute_reply.started":"2022-04-21T20:27:31.726323Z","shell.execute_reply":"2022-04-21T20:27:31.732577Z"},"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-21T20:27:31.734959Z","iopub.execute_input":"2022-04-21T20:27:31.735554Z","iopub.status.idle":"2022-04-21T20:27:31.774630Z","shell.execute_reply.started":"2022-04-21T20:27:31.735516Z","shell.execute_reply":"2022-04-21T20:27:31.773890Z"},"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-21T20:27:31.776109Z","iopub.execute_input":"2022-04-21T20:27:31.776577Z","iopub.status.idle":"2022-04-21T20:27:31.791866Z","shell.execute_reply.started":"2022-04-21T20:27:31.776537Z","shell.execute_reply":"2022-04-21T20:27:31.791166Z"},"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-21T20:27:31.793191Z","iopub.execute_input":"2022-04-21T20:27:31.793604Z","iopub.status.idle":"2022-04-21T20:27:31.800797Z","shell.execute_reply.started":"2022-04-21T20:27:31.793567Z","shell.execute_reply":"2022-04-21T20:27:31.800139Z"},"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-21T20:27:31.802423Z","iopub.execute_input":"2022-04-21T20:27:31.803821Z","iopub.status.idle":"2022-04-21T20:27:31.812721Z","shell.execute_reply.started":"2022-04-21T20:27:31.803782Z","shell.execute_reply":"2022-04-21T20:27:31.812037Z"},"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-21T20:27:31.815788Z","iopub.execute_input":"2022-04-21T20:27:31.816192Z","iopub.status.idle":"2022-04-21T20:27:31.825964Z","shell.execute_reply.started":"2022-04-21T20:27:31.816164Z","shell.execute_reply":"2022-04-21T20:27:31.825248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg_model19_a = getVGG19Model(lastFourTrainable=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:27:31.828719Z","iopub.execute_input":"2022-04-21T20:27:31.829221Z","iopub.status.idle":"2022-04-21T20:27:43.516740Z","shell.execute_reply.started":"2022-04-21T20:27:31.829179Z","shell.execute_reply":"2022-04-21T20:27:43.515926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg_model19_b = getVGG19Model(lastFourTrainable=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:27:43.518634Z","iopub.execute_input":"2022-04-21T20:27:43.519211Z","iopub.status.idle":"2022-04-21T20:27:45.448769Z","shell.execute_reply.started":"2022-04-21T20:27:43.519164Z","shell.execute_reply":"2022-04-21T20:27:45.448086Z"},"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-21T20:27:45.451643Z","iopub.execute_input":"2022-04-21T20:27:45.451865Z","iopub.status.idle":"2022-04-21T20:27:49.136522Z","shell.execute_reply.started":"2022-04-21T20:27:45.451838Z","shell.execute_reply":"2022-04-21T20:27:49.135816Z"},"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-21T20:27:49.137754Z","iopub.execute_input":"2022-04-21T20:27:49.138062Z","iopub.status.idle":"2022-04-21T20:27:49.143666Z","shell.execute_reply.started":"2022-04-21T20:27:49.138024Z","shell.execute_reply":"2022-04-21T20:27:49.142989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:27:49.144838Z","iopub.execute_input":"2022-04-21T20:27:49.145069Z","iopub.status.idle":"2022-04-21T20:27:49.506980Z","shell.execute_reply.started":"2022-04-21T20:27:49.145036Z","shell.execute_reply":"2022-04-21T20:27:49.506225Z"},"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-21T20:27:49.508608Z","iopub.execute_input":"2022-04-21T20:27:49.508877Z","iopub.status.idle":"2022-04-21T20:27:49.516366Z","shell.execute_reply.started":"2022-04-21T20:27:49.508843Z","shell.execute_reply":"2022-04-21T20:27:49.515593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:27:49.517890Z","iopub.execute_input":"2022-04-21T20:27:49.518296Z","iopub.status.idle":"2022-04-21T20:27:49.523668Z","shell.execute_reply.started":"2022-04-21T20:27:49.518262Z","shell.execute_reply":"2022-04-21T20:27:49.522981Z"},"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-21T20:43:29.976342Z","iopub.execute_input":"2022-04-21T20:43:29.976618Z","iopub.status.idle":"2022-04-21T20:43:30.250905Z","shell.execute_reply.started":"2022-04-21T20:43:29.976588Z","shell.execute_reply":"2022-04-21T20:43:30.250130Z"},"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-21T20:27:49.797309Z","iopub.execute_input":"2022-04-21T20:27:49.797551Z","iopub.status.idle":"2022-04-21T20:27:49.915420Z","shell.execute_reply.started":"2022-04-21T20:27:49.797518Z","shell.execute_reply":"2022-04-21T20:27:49.914587Z"},"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-21T20:27:49.916813Z","iopub.execute_input":"2022-04-21T20:27:49.917074Z","iopub.status.idle":"2022-04-21T20:29:15.373462Z","shell.execute_reply.started":"2022-04-21T20:27:49.917038Z","shell.execute_reply":"2022-04-21T20:29:15.372603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_hist(history1,'Resnet-50')","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:29:15.378982Z","iopub.execute_input":"2022-04-21T20:29:15.379224Z","iopub.status.idle":"2022-04-21T20:29:15.656684Z","shell.execute_reply.started":"2022-04-21T20:29:15.379195Z","shell.execute_reply":"2022-04-21T20:29:15.656021Z"},"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-21T20:29:15.657758Z","iopub.execute_input":"2022-04-21T20:29:15.658473Z","iopub.status.idle":"2022-04-21T20:29:17.779198Z","shell.execute_reply.started":"2022-04-21T20:29:15.658435Z","shell.execute_reply":"2022-04-21T20:29:17.778133Z"},"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-21T20:29:17.780602Z","iopub.execute_input":"2022-04-21T20:29:17.780939Z","iopub.status.idle":"2022-04-21T20:29:19.211975Z","shell.execute_reply.started":"2022-04-21T20:29:17.780897Z","shell.execute_reply":"2022-04-21T20:29:19.211270Z"},"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-21T20:29:19.214649Z","iopub.execute_input":"2022-04-21T20:29:19.214872Z","iopub.status.idle":"2022-04-21T20:30:08.759424Z","shell.execute_reply.started":"2022-04-21T20:29:19.214845Z","shell.execute_reply":"2022-04-21T20:30:08.758752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_hist(history,'Resnet-50-b')","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:30:08.763785Z","iopub.execute_input":"2022-04-21T20:30:08.763985Z","iopub.status.idle":"2022-04-21T20:30:09.021905Z","shell.execute_reply.started":"2022-04-21T20:30:08.763960Z","shell.execute_reply":"2022-04-21T20:30:09.021241Z"},"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-21T20:30:09.023104Z","iopub.execute_input":"2022-04-21T20:30:09.023422Z","iopub.status.idle":"2022-04-21T20:30:12.334217Z","shell.execute_reply.started":"2022-04-21T20:30:09.023383Z","shell.execute_reply":"2022-04-21T20:30:12.333386Z"},"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-21T20:30:12.341010Z","iopub.execute_input":"2022-04-21T20:30:12.341214Z","iopub.status.idle":"2022-04-21T20:30:12.608245Z","shell.execute_reply.started":"2022-04-21T20:30:12.341188Z","shell.execute_reply":"2022-04-21T20:30:12.607503Z"},"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-21T20:30:12.609605Z","iopub.execute_input":"2022-04-21T20:30:12.609860Z","iopub.status.idle":"2022-04-21T20:30:12.727208Z","shell.execute_reply.started":"2022-04-21T20:30:12.609825Z","shell.execute_reply":"2022-04-21T20:30:12.726465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgga=getVGG16Model(lastFourTrainable=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:30:12.728670Z","iopub.execute_input":"2022-04-21T20:30:12.728948Z","iopub.status.idle":"2022-04-21T20:30:19.252284Z","shell.execute_reply.started":"2022-04-21T20:30:12.728912Z","shell.execute_reply":"2022-04-21T20:30:19.251590Z"},"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-21T20:30:19.253475Z","iopub.execute_input":"2022-04-21T20:30:19.253745Z","iopub.status.idle":"2022-04-21T20:31:18.018425Z","shell.execute_reply.started":"2022-04-21T20:30:19.253707Z","shell.execute_reply":"2022-04-21T20:31:18.017729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_hist(history,'VGG-16')","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:31:18.024623Z","iopub.execute_input":"2022-04-21T20:31:18.024843Z","iopub.status.idle":"2022-04-21T20:31:18.285323Z","shell.execute_reply.started":"2022-04-21T20:31:18.024817Z","shell.execute_reply":"2022-04-21T20:31:18.284591Z"},"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-21T20:31:18.286555Z","iopub.execute_input":"2022-04-21T20:31:18.286804Z","iopub.status.idle":"2022-04-21T20:31:24.128534Z","shell.execute_reply.started":"2022-04-21T20:31:18.286769Z","shell.execute_reply":"2022-04-21T20:31:24.127771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vggb=getVGG16Model(lastFourTrainable=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:31:24.129977Z","iopub.execute_input":"2022-04-21T20:31:24.130300Z","iopub.status.idle":"2022-04-21T20:31:26.019210Z","shell.execute_reply.started":"2022-04-21T20:31:24.130260Z","shell.execute_reply":"2022-04-21T20:31:26.018522Z"},"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-21T20:31:26.020604Z","iopub.execute_input":"2022-04-21T20:31:26.020867Z","iopub.status.idle":"2022-04-21T20:32:49.531913Z","shell.execute_reply.started":"2022-04-21T20:31:26.020831Z","shell.execute_reply":"2022-04-21T20:32:49.530970Z"},"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-21T20:32:49.536881Z","iopub.execute_input":"2022-04-21T20:32:49.537121Z","iopub.status.idle":"2022-04-21T20:32:49.847364Z","shell.execute_reply.started":"2022-04-21T20:32:49.537091Z","shell.execute_reply":"2022-04-21T20:32:49.846563Z"},"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-21T20:32:49.852132Z","iopub.execute_input":"2022-04-21T20:32:49.852337Z","iopub.status.idle":"2022-04-21T20:32:51.366951Z","shell.execute_reply.started":"2022-04-21T20:32:49.852311Z","shell.execute_reply":"2022-04-21T20:32:51.365434Z"},"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-21T20:32:51.369470Z","iopub.execute_input":"2022-04-21T20:32:51.370612Z","iopub.status.idle":"2022-04-21T20:32:52.244237Z","shell.execute_reply.started":"2022-04-21T20:32:51.370567Z","shell.execute_reply":"2022-04-21T20:32:52.243483Z"},"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-21T20:32:52.245330Z","iopub.execute_input":"2022-04-21T20:32:52.247108Z","iopub.status.idle":"2022-04-21T20:32:52.519827Z","shell.execute_reply.started":"2022-04-21T20:32:52.247065Z","shell.execute_reply":"2022-04-21T20:32:52.518978Z"},"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-21T20:32:52.521126Z","iopub.execute_input":"2022-04-21T20:32:52.521823Z","iopub.status.idle":"2022-04-21T20:33:56.196876Z","shell.execute_reply.started":"2022-04-21T20:32:52.521777Z","shell.execute_reply":"2022-04-21T20:33:56.196055Z"},"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-21T20:33:56.201651Z","iopub.execute_input":"2022-04-21T20:33:56.201884Z","iopub.status.idle":"2022-04-21T20:33:57.855099Z","shell.execute_reply.started":"2022-04-21T20:33:56.201857Z","shell.execute_reply":"2022-04-21T20:33:57.854359Z"},"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-21T20:33:57.856686Z","iopub.execute_input":"2022-04-21T20:33:57.857089Z","iopub.status.idle":"2022-04-21T20:33:58.142658Z","shell.execute_reply.started":"2022-04-21T20:33:57.857024Z","shell.execute_reply":"2022-04-21T20:33:58.141978Z"},"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-21T20:33:58.143913Z","iopub.execute_input":"2022-04-21T20:33:58.144304Z","iopub.status.idle":"2022-04-21T20:35:09.292818Z","shell.execute_reply.started":"2022-04-21T20:33:58.144265Z","shell.execute_reply":"2022-04-21T20:35:09.292114Z"},"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-21T20:35:09.297253Z","iopub.execute_input":"2022-04-21T20:35:09.297453Z","iopub.status.idle":"2022-04-21T20:35:09.561153Z","shell.execute_reply.started":"2022-04-21T20:35:09.297428Z","shell.execute_reply":"2022-04-21T20:35:09.560503Z"},"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-21T20:35:09.562312Z","iopub.execute_input":"2022-04-21T20:35:09.563021Z","iopub.status.idle":"2022-04-21T20:35:11.126946Z","shell.execute_reply.started":"2022-04-21T20:35:09.562982Z","shell.execute_reply":"2022-04-21T20:35:11.126238Z"},"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-21T20:35:11.128333Z","iopub.execute_input":"2022-04-21T20:35:11.128663Z","iopub.status.idle":"2022-04-21T20:35:11.132755Z","shell.execute_reply.started":"2022-04-21T20:35:11.128624Z","shell.execute_reply":"2022-04-21T20:35:11.132044Z"},"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-21T20:35:11.134197Z","iopub.execute_input":"2022-04-21T20:35:11.134730Z","iopub.status.idle":"2022-04-21T20:35:11.144718Z","shell.execute_reply.started":"2022-04-21T20:35:11.134680Z","shell.execute_reply":"2022-04-21T20:35:11.143857Z"},"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-21T20:35:11.146059Z","iopub.execute_input":"2022-04-21T20:35:11.146302Z","iopub.status.idle":"2022-04-21T20:35:14.408089Z","shell.execute_reply.started":"2022-04-21T20:35:11.146268Z","shell.execute_reply":"2022-04-21T20:35:14.407379Z"},"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-21T20:35:14.409309Z","iopub.execute_input":"2022-04-21T20:35:14.409625Z","iopub.status.idle":"2022-04-21T20:35:15.067333Z","shell.execute_reply.started":"2022-04-21T20:35:14.409586Z","shell.execute_reply":"2022-04-21T20:35:15.066581Z"},"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-21T20:35:15.068794Z","iopub.execute_input":"2022-04-21T20:35:15.069040Z","iopub.status.idle":"2022-04-21T20:35:15.437134Z","shell.execute_reply.started":"2022-04-21T20:35:15.069005Z","shell.execute_reply":"2022-04-21T20:35:15.436367Z"},"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-21T20:35:15.438414Z","iopub.execute_input":"2022-04-21T20:35:15.438666Z","iopub.status.idle":"2022-04-21T20:36:07.435174Z","shell.execute_reply.started":"2022-04-21T20:35:15.438631Z","shell.execute_reply":"2022-04-21T20:36:07.434476Z"},"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-21T20:36:07.439964Z","iopub.execute_input":"2022-04-21T20:36:07.440174Z","iopub.status.idle":"2022-04-21T20:36:10.892473Z","shell.execute_reply.started":"2022-04-21T20:36:07.440147Z","shell.execute_reply":"2022-04-21T20:36:10.891182Z"},"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-21T20:55:38.303710Z","iopub.execute_input":"2022-04-21T20:55:38.304198Z","iopub.status.idle":"2022-04-21T20:55:38.307964Z","shell.execute_reply.started":"2022-04-21T20:55:38.304158Z","shell.execute_reply":"2022-04-21T20:55:38.307207Z"},"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":"2022-04-21T20:55:39.935536Z","iopub.execute_input":"2022-04-21T20:55:39.936086Z","iopub.status.idle":"2022-04-21T20:55:39.945444Z","shell.execute_reply.started":"2022-04-21T20:55:39.936040Z","shell.execute_reply":"2022-04-21T20:55:39.944724Z"},"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":"2022-04-21T20:55:57.007815Z","iopub.execute_input":"2022-04-21T20:55:57.008075Z","iopub.status.idle":"2022-04-21T20:55:59.501614Z","shell.execute_reply.started":"2022-04-21T20:55:57.008039Z","shell.execute_reply":"2022-04-21T20:55:59.500875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_vects.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:56:02.288046Z","iopub.execute_input":"2022-04-21T20:56:02.288296Z","iopub.status.idle":"2022-04-21T20:56:02.296881Z","shell.execute_reply.started":"2022-04-21T20:56:02.288267Z","shell.execute_reply":"2022-04-21T20:56:02.296190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier as KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:56:06.920051Z","iopub.execute_input":"2022-04-21T20:56:06.920326Z","iopub.status.idle":"2022-04-21T20:56:06.924432Z","shell.execute_reply.started":"2022-04-21T20:56:06.920298Z","shell.execute_reply":"2022-04-21T20:56:06.923718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import GridSearchCV","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:56:11.677088Z","iopub.execute_input":"2022-04-21T20:56:11.677390Z","iopub.status.idle":"2022-04-21T20:56:11.681481Z","shell.execute_reply.started":"2022-04-21T20:56:11.677353Z","shell.execute_reply":"2022-04-21T20:56:11.680822Z"},"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-21T20:56:16.327156Z","iopub.execute_input":"2022-04-21T20:56:16.327465Z","iopub.status.idle":"2022-04-21T20:56:16.334323Z","shell.execute_reply.started":"2022-04-21T20:56:16.327430Z","shell.execute_reply":"2022-04-21T20:56:16.333496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_integers.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:56:22.182384Z","iopub.execute_input":"2022-04-21T20:56:22.182987Z","iopub.status.idle":"2022-04-21T20:56:22.188270Z","shell.execute_reply.started":"2022-04-21T20:56:22.182945Z","shell.execute_reply":"2022-04-21T20:56:22.187509Z"},"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-21T20:56:25.063902Z","iopub.execute_input":"2022-04-21T20:56:25.064160Z","iopub.status.idle":"2022-04-21T20:56:25.072324Z","shell.execute_reply.started":"2022-04-21T20:56:25.064129Z","shell.execute_reply":"2022-04-21T20:56:25.071424Z"},"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":"2022-04-21T20:56:40.688267Z","iopub.execute_input":"2022-04-21T20:56:40.688951Z","iopub.status.idle":"2022-04-21T20:56:42.577499Z","shell.execute_reply.started":"2022-04-21T20:56:40.688913Z","shell.execute_reply":"2022-04-21T20:56:42.576748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_vects.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:56:43.355765Z","iopub.execute_input":"2022-04-21T20:56:43.356312Z","iopub.status.idle":"2022-04-21T20:56:43.361635Z","shell.execute_reply.started":"2022-04-21T20:56:43.356271Z","shell.execute_reply":"2022-04-21T20:56:43.360888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:56:45.371054Z","iopub.execute_input":"2022-04-21T20:56:45.371311Z","iopub.status.idle":"2022-04-21T20:56:45.376471Z","shell.execute_reply.started":"2022-04-21T20:56:45.371283Z","shell.execute_reply":"2022-04-21T20:56:45.375380Z"},"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-21T20:56:50.700829Z","iopub.execute_input":"2022-04-21T20:56:50.701420Z","iopub.status.idle":"2022-04-21T20:56:50.705581Z","shell.execute_reply.started":"2022-04-21T20:56:50.701378Z","shell.execute_reply":"2022-04-21T20:56:50.704483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_label_integers.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:56:52.921533Z","iopub.execute_input":"2022-04-21T20:56:52.922377Z","iopub.status.idle":"2022-04-21T20:56:52.928183Z","shell.execute_reply.started":"2022-04-21T20:56:52.922325Z","shell.execute_reply":"2022-04-21T20:56:52.927370Z"},"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-21T20:56:56.735457Z","iopub.execute_input":"2022-04-21T20:56:56.735741Z","iopub.status.idle":"2022-04-21T20:56:56.780046Z","shell.execute_reply.started":"2022-04-21T20:56:56.735690Z","shell.execute_reply":"2022-04-21T20:56:56.779270Z"},"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-21T20:57:03.610215Z","iopub.execute_input":"2022-04-21T20:57:03.610778Z","iopub.status.idle":"2022-04-21T20:57:03.618156Z","shell.execute_reply.started":"2022-04-21T20:57:03.610738Z","shell.execute_reply":"2022-04-21T20:57:03.617464Z"},"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-21T20:57:06.989081Z","iopub.execute_input":"2022-04-21T20:57:06.989470Z","iopub.status.idle":"2022-04-21T20:57:07.016556Z","shell.execute_reply.started":"2022-04-21T20:57:06.989435Z","shell.execute_reply":"2022-04-21T20:57:07.015614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:57:08.556379Z","iopub.execute_input":"2022-04-21T20:57:08.556986Z","iopub.status.idle":"2022-04-21T20:57:08.562530Z","shell.execute_reply.started":"2022-04-21T20:57:08.556942Z","shell.execute_reply":"2022-04-21T20:57:08.561764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.max(indices)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:57:10.139330Z","iopub.execute_input":"2022-04-21T20:57:10.139875Z","iopub.status.idle":"2022-04-21T20:57:10.145107Z","shell.execute_reply.started":"2022-04-21T20:57:10.139837Z","shell.execute_reply":"2022-04-21T20:57:10.144384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.min(indices)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:57:12.075052Z","iopub.execute_input":"2022-04-21T20:57:12.075804Z","iopub.status.idle":"2022-04-21T20:57:12.082213Z","shell.execute_reply.started":"2022-04-21T20:57:12.075751Z","shell.execute_reply":"2022-04-21T20:57:12.081242Z"},"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-21T20:57:15.056626Z","iopub.execute_input":"2022-04-21T20:57:15.057228Z","iopub.status.idle":"2022-04-21T20:57:15.065558Z","shell.execute_reply.started":"2022-04-21T20:57:15.057191Z","shell.execute_reply":"2022-04-21T20:57:15.064839Z"},"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-21T20:57:18.874031Z","iopub.execute_input":"2022-04-21T20:57:18.874277Z","iopub.status.idle":"2022-04-21T20:57:23.296224Z","shell.execute_reply.started":"2022-04-21T20:57:18.874249Z","shell.execute_reply":"2022-04-21T20:57:23.295558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ypred=knn7.predict(test_vects)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:57:28.830455Z","iopub.execute_input":"2022-04-21T20:57:28.830727Z","iopub.status.idle":"2022-04-21T20:57:28.871491Z","shell.execute_reply.started":"2022-04-21T20:57:28.830678Z","shell.execute_reply":"2022-04-21T20:57:28.870668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import metrics \nprint(metrics.classification_report(test_label_integers,ypred))","metadata":{"execution":{"iopub.status.busy":"2022-04-21T20:57:30.487217Z","iopub.execute_input":"2022-04-21T20:57:30.487871Z","iopub.status.idle":"2022-04-21T20:57:30.501724Z","shell.execute_reply.started":"2022-04-21T20:57:30.487832Z","shell.execute_reply":"2022-04-21T20:57:30.501026Z"},"trusted":true},"execution_count":null,"outputs":[]}]}