{"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-19T19:01:11.049409Z","iopub.execute_input":"2022-04-19T19:01:11.050151Z","iopub.status.idle":"2022-04-19T19:01:11.078512Z","shell.execute_reply.started":"2022-04-19T19:01:11.050056Z","shell.execute_reply":"2022-04-19T19:01:11.077868Z"},"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-19T19:01:51.926339Z","iopub.execute_input":"2022-04-19T19:01:51.926579Z","iopub.status.idle":"2022-04-19T19:01:51.934394Z","shell.execute_reply.started":"2022-04-19T19:01:51.926550Z","shell.execute_reply":"2022-04-19T19:01:51.933658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.vgg16 import VGG16","metadata":{"execution":{"iopub.status.busy":"2022-04-19T19:01:56.881557Z","iopub.execute_input":"2022-04-19T19:01:56.882163Z","iopub.status.idle":"2022-04-19T19:01:56.885813Z","shell.execute_reply.started":"2022-04-19T19:01:56.882124Z","shell.execute_reply":"2022-04-19T19:01:56.885066Z"},"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-19T17:57:43.502678Z","iopub.execute_input":"2022-04-19T17:57:43.503175Z","iopub.status.idle":"2022-04-19T17:57:43.507409Z","shell.execute_reply.started":"2022-04-19T17:57:43.503125Z","shell.execute_reply":"2022-04-19T17:57:43.506350Z"},"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-19T19:01:59.984838Z","iopub.execute_input":"2022-04-19T19:01:59.985383Z","iopub.status.idle":"2022-04-19T19:01:59.989751Z","shell.execute_reply.started":"2022-04-19T19:01:59.985343Z","shell.execute_reply":"2022-04-19T19:01:59.988699Z"},"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-19T19:04:29.774973Z","iopub.execute_input":"2022-04-19T19:04:29.775235Z","iopub.status.idle":"2022-04-19T19:04:29.783572Z","shell.execute_reply.started":"2022-04-19T19:04:29.775201Z","shell.execute_reply":"2022-04-19T19:04:29.782606Z"},"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-19T19:02:14.083471Z","iopub.execute_input":"2022-04-19T19:02:14.083744Z","iopub.status.idle":"2022-04-19T19:02:15.419181Z","shell.execute_reply.started":"2022-04-19T19:02:14.083711Z","shell.execute_reply":"2022-04-19T19:02:15.418482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindf.shape","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-19T17:57:56.731404Z","iopub.execute_input":"2022-04-19T17:57:56.731597Z","iopub.status.idle":"2022-04-19T17:57:56.737447Z","shell.execute_reply.started":"2022-04-19T17:57:56.731573Z","shell.execute_reply":"2022-04-19T17:57:56.736472Z"},"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-19T19:02:22.704145Z","iopub.execute_input":"2022-04-19T19:02:22.704629Z","iopub.status.idle":"2022-04-19T19:02:22.734683Z","shell.execute_reply.started":"2022-04-19T19:02:22.704592Z","shell.execute_reply":"2022-04-19T19:02:22.734057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"landmark_unique[0:50]","metadata":{"execution":{"iopub.status.busy":"2022-04-19T19:02:24.695174Z","iopub.execute_input":"2022-04-19T19:02:24.695749Z","iopub.status.idle":"2022-04-19T19:02:24.709621Z","shell.execute_reply.started":"2022-04-19T19:02:24.695683Z","shell.execute_reply":"2022-04-19T19:02:24.708628Z"},"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-19T19:02:32.375495Z","iopub.execute_input":"2022-04-19T19:02:32.375765Z","iopub.status.idle":"2022-04-19T19:02:32.491183Z","shell.execute_reply.started":"2022-04-19T19:02:32.375727Z","shell.execute_reply":"2022-04-19T19:02:32.490488Z"},"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-19T19:02:37.035264Z","iopub.execute_input":"2022-04-19T19:02:37.035515Z","iopub.status.idle":"2022-04-19T19:02:52.269499Z","shell.execute_reply.started":"2022-04-19T19:02:37.035486Z","shell.execute_reply":"2022-04-19T19:02:52.268749Z"},"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-19T19:02:52.271099Z","iopub.execute_input":"2022-04-19T19:02:52.271343Z","iopub.status.idle":"2022-04-19T19:02:52.276204Z","shell.execute_reply.started":"2022-04-19T19:02:52.271309Z","shell.execute_reply":"2022-04-19T19:02:52.275546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(temp_labels)","metadata":{"execution":{"iopub.status.busy":"2022-04-19T19:02:52.277421Z","iopub.execute_input":"2022-04-19T19:02:52.277852Z","iopub.status.idle":"2022-04-19T19:02:52.286949Z","shell.execute_reply.started":"2022-04-19T19:02:52.277817Z","shell.execute_reply":"2022-04-19T19:02:52.286253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-19T19:02:57.128780Z","iopub.execute_input":"2022-04-19T19:02:57.129030Z","iopub.status.idle":"2022-04-19T19:02:57.133286Z","shell.execute_reply.started":"2022-04-19T19:02:57.129003Z","shell.execute_reply":"2022-04-19T19:02:57.132615Z"},"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-19T17:11:44.916134Z","iopub.execute_input":"2022-04-19T17:11:44.916710Z","iopub.status.idle":"2022-04-19T17:11:46.441044Z","shell.execute_reply.started":"2022-04-19T17:11:44.916665Z","shell.execute_reply":"2022-04-19T17:11:46.440379Z"},"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-19T17:11:50.195449Z","iopub.execute_input":"2022-04-19T17:11:50.195702Z","iopub.status.idle":"2022-04-19T17:11:51.508389Z","shell.execute_reply.started":"2022-04-19T17:11:50.195673Z","shell.execute_reply":"2022-04-19T17:11:51.507077Z"},"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-19T19:03:05.045139Z","iopub.execute_input":"2022-04-19T19:03:05.045410Z","iopub.status.idle":"2022-04-19T19:03:05.053255Z","shell.execute_reply.started":"2022-04-19T19:03:05.045381Z","shell.execute_reply":"2022-04-19T19:03:05.052060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels_data)","metadata":{"execution":{"iopub.status.busy":"2022-04-19T19:03:06.773253Z","iopub.execute_input":"2022-04-19T19:03:06.773500Z","iopub.status.idle":"2022-04-19T19:03:06.778404Z","shell.execute_reply.started":"2022-04-19T19:03:06.773472Z","shell.execute_reply":"2022-04-19T19:03:06.777343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''# Function for plotting the Confusion Matrix\ndef plotConfusionMatrix(cm, class_names, normalize=True, title='Confusion matrix', cmap=plt.cm.Blues):\n    plt.figure(figsize=(10,10))\n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks = np.arange(len(class_names))\n    plt.xticks(tick_marks, class_names, rotation=45)\n    plt.yticks(tick_marks, class_names)\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n        cm = np.around(cm, decimals=2)\n        cm[np.isnan(cm)] = 0.0\n        print(\"Normalized confusion matrix\")\n    else:\n        print('Confusion matrix, without normalization')\n\n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, cm[i, j],\n                horizontalalignment=\"center\",\n                color=\"white\" if cm[i, j] > thresh else \"black\")\n\n    plt.tight_layout()\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')\n    plt.show()\n    '''","metadata":{"execution":{"iopub.status.busy":"2022-04-19T15:37:22.206098Z","iopub.execute_input":"2022-04-19T15:37:22.206408Z","iopub.status.idle":"2022-04-19T15:37:22.215064Z","shell.execute_reply.started":"2022-04-19T15:37:22.206372Z","shell.execute_reply":"2022-04-19T15:37:22.21417Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\n# Function for train the given model and get Confusion Matrix\ndef trainModelAndGetConfusionMatrix(model,train_generator,valid_generator,test_generator,epochs,batch_size):\n    # Fit the model\n    model.fit_generator(train_generator,\n                      epochs=epochs,\n                      steps_per_epoch=len(train_generator) // batch_size,\n                      validation_data=valid_generator,\n                      validation_steps=len(valid_generator) // batch_size)\n    # Evaluate the model\n    loss_and_metrics = model.evaluate_generator(test_generator, steps=len(test_generator) // batch_size+1)\n    print(\"Test Loss: \", loss_and_metrics[0])\n    print(\"Test Accuracy: \", loss_and_metrics[1])\n    test_generator.reset()\n    # Get Confusion Matrix and plot it\n    Y_pred = model.predict_generator(test_generator, steps=len(test_generator) // batch_size+1)\n    y_pred = np.argmax(Y_pred, axis=1)\n    class_names = []\n    for key in train_generator.class_indices:\n        class_names.append(key)\n    cr = classification_report(test_generator.classes, y_pred, target_names=class_names)\n    print(\"Classification Report\\n\" + cr) # Print classification report\n    cm = confusion_matrix(test_generator.classes, y_pred)\n    plotConfusionMatrix(cm, class_names, normalize=False, title='Confusion Matrix')\n    plotConfusionMatrix(cm, class_names, normalize=True, title='Confusion Matrix')\n    return model\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-04-19T15:37:14.862224Z","iopub.execute_input":"2022-04-19T15:37:14.862921Z","iopub.status.idle":"2022-04-19T15:37:14.873339Z","shell.execute_reply.started":"2022-04-19T15:37:14.862872Z","shell.execute_reply":"2022-04-19T15:37:14.872702Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelBinarizer","metadata":{"execution":{"iopub.status.busy":"2022-04-19T18:08:35.151386Z","iopub.execute_input":"2022-04-19T18:08:35.152312Z","iopub.status.idle":"2022-04-19T18:08:35.156123Z","shell.execute_reply.started":"2022-04-19T18:08:35.152269Z","shell.execute_reply":"2022-04-19T18:08:35.155357Z"},"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-19T19:03:17.414209Z","iopub.execute_input":"2022-04-19T19:03:17.414467Z","iopub.status.idle":"2022-04-19T19:03:17.472716Z","shell.execute_reply.started":"2022-04-19T19:03:17.414439Z","shell.execute_reply":"2022-04-19T19:03:17.471946Z"},"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-19T19:03:21.048075Z","iopub.execute_input":"2022-04-19T19:03:21.048776Z","iopub.status.idle":"2022-04-19T19:03:21.059857Z","shell.execute_reply.started":"2022-04-19T19:03:21.048727Z","shell.execute_reply":"2022-04-19T19:03:21.059098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels_data[0:6])","metadata":{"execution":{"iopub.status.busy":"2022-04-19T18:29:41.788031Z","iopub.execute_input":"2022-04-19T18:29:41.788670Z","iopub.status.idle":"2022-04-19T18:29:41.794440Z","shell.execute_reply.started":"2022-04-19T18:29:41.788629Z","shell.execute_reply":"2022-04-19T18:29:41.793448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(a_pd)","metadata":{"execution":{"iopub.status.busy":"2022-04-19T18:29:47.454458Z","iopub.execute_input":"2022-04-19T18:29:47.455058Z","iopub.status.idle":"2022-04-19T18:29:47.463906Z","shell.execute_reply.started":"2022-04-19T18:29:47.455003Z","shell.execute_reply":"2022-04-19T18:29:47.462975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_data=a_pd","metadata":{"execution":{"iopub.status.busy":"2022-04-19T19:03:32.339144Z","iopub.execute_input":"2022-04-19T19:03:32.339387Z","iopub.status.idle":"2022-04-19T19:03:32.343509Z","shell.execute_reply.started":"2022-04-19T19:03:32.339363Z","shell.execute_reply":"2022-04-19T19:03:32.341891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels_data[0:5])","metadata":{"execution":{"iopub.status.busy":"2022-04-19T19:03:36.857880Z","iopub.execute_input":"2022-04-19T19:03:36.858405Z","iopub.status.idle":"2022-04-19T19:03:36.864898Z","shell.execute_reply.started":"2022-04-19T19:03:36.858367Z","shell.execute_reply":"2022-04-19T19:03:36.864052Z"},"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-19T19:03:42.081603Z","iopub.execute_input":"2022-04-19T19:03:42.082288Z","iopub.status.idle":"2022-04-19T19:03:42.154221Z","shell.execute_reply.started":"2022-04-19T19:03:42.082252Z","shell.execute_reply":"2022-04-19T19:03:42.153376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(Y_train[0:5])","metadata":{"execution":{"iopub.status.busy":"2022-04-19T19:03:46.924560Z","iopub.execute_input":"2022-04-19T19:03:46.925136Z","iopub.status.idle":"2022-04-19T19:03:46.931469Z","shell.execute_reply.started":"2022-04-19T19:03:46.925096Z","shell.execute_reply":"2022-04-19T19:03:46.930755Z"},"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-19T19:03:53.527118Z","iopub.execute_input":"2022-04-19T19:03:53.527400Z","iopub.status.idle":"2022-04-19T19:03:53.534069Z","shell.execute_reply.started":"2022-04-19T19:03:53.527373Z","shell.execute_reply":"2022-04-19T19:03:53.533348Z"},"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-19T19:03:58.540149Z","iopub.execute_input":"2022-04-19T19:03:58.540680Z","iopub.status.idle":"2022-04-19T19:03:58.578781Z","shell.execute_reply.started":"2022-04-19T19:03:58.540642Z","shell.execute_reply":"2022-04-19T19:03:58.578034Z"},"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-19T19:04:03.684657Z","iopub.execute_input":"2022-04-19T19:04:03.684990Z","iopub.status.idle":"2022-04-19T19:04:03.707613Z","shell.execute_reply.started":"2022-04-19T19:04:03.684951Z","shell.execute_reply":"2022-04-19T19:04:03.706950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_y_final[0]","metadata":{"execution":{"iopub.status.busy":"2022-04-19T19:04:08.455722Z","iopub.execute_input":"2022-04-19T19:04:08.456137Z","iopub.status.idle":"2022-04-19T19:04:08.462760Z","shell.execute_reply.started":"2022-04-19T19:04:08.456105Z","shell.execute_reply":"2022-04-19T19:04:08.462073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_y_final[1]","metadata":{"execution":{"iopub.status.busy":"2022-04-19T18:49:08.332673Z","iopub.execute_input":"2022-04-19T18:49:08.333103Z","iopub.status.idle":"2022-04-19T18:49:08.339395Z","shell.execute_reply.started":"2022-04-19T18:49:08.333050Z","shell.execute_reply":"2022-04-19T18:49:08.338607Z"},"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-19T19:04:17.282653Z","iopub.execute_input":"2022-04-19T19:04:17.283210Z","iopub.status.idle":"2022-04-19T19:04:17.290899Z","shell.execute_reply.started":"2022-04-19T19:04:17.283171Z","shell.execute_reply":"2022-04-19T19:04:17.289935Z"},"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-19T19:04:37.898186Z","iopub.execute_input":"2022-04-19T19:04:37.898475Z","iopub.status.idle":"2022-04-19T19:04:42.841881Z","shell.execute_reply.started":"2022-04-19T19:04:37.898429Z","shell.execute_reply":"2022-04-19T19:04:42.841193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam","metadata":{"execution":{"iopub.status.busy":"2022-04-19T17:46:20.542514Z","iopub.execute_input":"2022-04-19T17:46:20.543149Z","iopub.status.idle":"2022-04-19T17:46:20.548625Z","shell.execute_reply.started":"2022-04-19T17:46:20.543108Z","shell.execute_reply":"2022-04-19T17:46:20.546538Z"},"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-19T18:56:11.381007Z","iopub.execute_input":"2022-04-19T18:56:11.381932Z","iopub.status.idle":"2022-04-19T18:56:11.388362Z","shell.execute_reply.started":"2022-04-19T18:56:11.381885Z","shell.execute_reply":"2022-04-19T18:56:11.387388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow","metadata":{"execution":{"iopub.status.busy":"2022-04-19T18:32:04.501715Z","iopub.execute_input":"2022-04-19T18:32:04.501986Z","iopub.status.idle":"2022-04-19T18:32:04.506428Z","shell.execute_reply.started":"2022-04-19T18:32:04.501958Z","shell.execute_reply":"2022-04-19T18:32:04.505428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x_final=tensorflow.keras.applications.resnet.preprocess_input(train_x_final)\nvalidation_x_final=tensorflow.keras.applications.resnet.preprocess_input(validation_x_final)","metadata":{"execution":{"iopub.status.busy":"2022-04-19T18:32:27.308194Z","iopub.execute_input":"2022-04-19T18:32:27.308463Z","iopub.status.idle":"2022-04-19T18:32:27.805188Z","shell.execute_reply.started":"2022-04-19T18:32:27.308435Z","shell.execute_reply":"2022-04-19T18:32:27.804337Z"},"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_a.fit(train_x_final, train_y_final, epochs=10,validation_data=(validation_x_final, 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-19T19:04:54.139374Z","iopub.execute_input":"2022-04-19T19:04:54.139895Z","iopub.status.idle":"2022-04-19T19:05:17.443398Z","shell.execute_reply.started":"2022-04-19T19:04:54.139856Z","shell.execute_reply":"2022-04-19T19:05:17.442680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgga=getVGG16Model(lastFourTrainable=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-04-19T19:05:25.737437Z","iopub.execute_input":"2022-04-19T19:05:25.737705Z","iopub.status.idle":"2022-04-19T19:05:30.039391Z","shell.execute_reply.started":"2022-04-19T19:05:25.737660Z","shell.execute_reply":"2022-04-19T19:05:30.038615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = vgga.fit(train_x_final, train_y_final,validation_data=(validation_x_final,validation_y_final), epochs=10)\n","metadata":{"execution":{"iopub.status.busy":"2022-04-19T19:05:36.079934Z","iopub.execute_input":"2022-04-19T19:05:36.080189Z","iopub.status.idle":"2022-04-19T19:05:55.407615Z","shell.execute_reply.started":"2022-04-19T19:05:36.080160Z","shell.execute_reply":"2022-04-19T19:05:55.406913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vggb=getVGG16Model(lastFourTrainable=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-19T19:10:21.074117Z","iopub.execute_input":"2022-04-19T19:10:21.074499Z","iopub.status.idle":"2022-04-19T19:10:22.906068Z","shell.execute_reply.started":"2022-04-19T19:10:21.074461Z","shell.execute_reply":"2022-04-19T19:10:22.905361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = vggb.fit(train_x_final, train_y_final,validation_data=(validation_x_final,validation_y_final), epochs=10)\n","metadata":{"execution":{"iopub.status.busy":"2022-04-19T19:11:00.698479Z","iopub.execute_input":"2022-04-19T19:11:00.698744Z","iopub.status.idle":"2022-04-19T19:11:19.548594Z","shell.execute_reply.started":"2022-04-19T19:11:00.698713Z","shell.execute_reply":"2022-04-19T19:11:19.547916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model_b = getResNet50Model(lastFourTrainable=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-19T19:12:24.177752Z","iopub.execute_input":"2022-04-19T19:12:24.178018Z","iopub.status.idle":"2022-04-19T19:12:25.655864Z","shell.execute_reply.started":"2022-04-19T19:12:24.177990Z","shell.execute_reply":"2022-04-19T19:12:25.655156Z"},"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, train_y_final, epochs=10,validation_data=(validation_x_final, 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-19T19:13:01.498486Z","iopub.execute_input":"2022-04-19T19:13:01.498811Z","iopub.status.idle":"2022-04-19T19:13:24.933498Z","shell.execute_reply.started":"2022-04-19T19:13:01.498773Z","shell.execute_reply":"2022-04-19T19:13:24.932581Z"},"trusted":true},"execution_count":null,"outputs":[]}]}