{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport random\nimport cv2\n\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n\nimport tensorflow as tf\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.python.keras.preprocessing.image import ImageDataGenerator","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-09-17T16:54:14.798170Z","iopub.execute_input":"2021-09-17T16:54:14.798492Z","iopub.status.idle":"2021-09-17T16:54:20.142821Z","shell.execute_reply.started":"2021-09-17T16:54:14.798413Z","shell.execute_reply":"2021-09-17T16:54:20.141996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindf = pd.read_csv(\"../input/landmark-recognition-2021/train.csv\")\ntraindf.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-17T16:54:20.144222Z","iopub.execute_input":"2021-09-17T16:54:20.144539Z","iopub.status.idle":"2021-09-17T16:54:21.703073Z","shell.execute_reply.started":"2021-09-17T16:54:20.144504Z","shell.execute_reply":"2021-09-17T16:54:21.702303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindf.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-17T16:54:21.704979Z","iopub.execute_input":"2021-09-17T16:54:21.705359Z","iopub.status.idle":"2021-09-17T16:54:21.710874Z","shell.execute_reply.started":"2021-09-17T16:54:21.705319Z","shell.execute_reply":"2021-09-17T16:54:21.710006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Total unique label ids:\", len(traindf['landmark_id'].unique()))","metadata":{"execution":{"iopub.status.busy":"2021-09-17T16:54:21.712541Z","iopub.execute_input":"2021-09-17T16:54:21.713152Z","iopub.status.idle":"2021-09-17T16:54:21.742384Z","shell.execute_reply.started":"2021-09-17T16:54:21.713115Z","shell.execute_reply":"2021-09-17T16:54:21.741397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"landmark_unique = traindf['landmark_id'].unique()\nlen(landmark_unique)","metadata":{"execution":{"iopub.status.busy":"2021-09-17T16:54:21.743705Z","iopub.execute_input":"2021-09-17T16:54:21.744040Z","iopub.status.idle":"2021-09-17T16:54:21.766633Z","shell.execute_reply.started":"2021-09-17T16:54:21.744005Z","shell.execute_reply":"2021-09-17T16:54:21.765641Z"},"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":"2021-09-17T16:54:21.767974Z","iopub.execute_input":"2021-09-17T16:54:21.768340Z","iopub.status.idle":"2021-09-17T16:54:21.904809Z","shell.execute_reply.started":"2021-09-17T16:54:21.768306Z","shell.execute_reply":"2021-09-17T16:54:21.903995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"landmark_unique[0:50]","metadata":{"execution":{"iopub.status.busy":"2021-09-17T16:54:21.906074Z","iopub.execute_input":"2021-09-17T16:54:21.906430Z","iopub.status.idle":"2021-09-17T16:54:21.914658Z","shell.execute_reply.started":"2021-09-17T16:54:21.906394Z","shell.execute_reply":"2021-09-17T16:54:21.913606Z"},"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, (100,100)))\n    \n    image_path.append(finalpath)","metadata":{"execution":{"iopub.status.busy":"2021-09-17T16:54:21.917939Z","iopub.execute_input":"2021-09-17T16:54:21.918490Z","iopub.status.idle":"2021-09-17T16:54:48.011488Z","shell.execute_reply.started":"2021-09-17T16:54:21.918454Z","shell.execute_reply":"2021-09-17T16:54:48.010609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Images: ', len(image_path))\nprint('Image labels: ', len(labels))","metadata":{"execution":{"iopub.status.busy":"2021-09-17T16:54:48.013316Z","iopub.execute_input":"2021-09-17T16:54:48.013578Z","iopub.status.idle":"2021-09-17T16:54:48.018132Z","shell.execute_reply.started":"2021-09-17T16:54:48.013554Z","shell.execute_reply":"2021-09-17T16:54:48.017322Z"},"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 = mpimg.imread(img_path)\n    plt.imshow(img)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-17T16:54:48.019205Z","iopub.execute_input":"2021-09-17T16:54:48.019583Z","iopub.status.idle":"2021-09-17T16:54:49.681943Z","shell.execute_reply.started":"2021-09-17T16:54:48.019548Z","shell.execute_reply":"2021-09-17T16:54:49.681041Z"},"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 = mpimg.imread(img_path)\n    plt.imshow(img)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-17T16:54:49.683062Z","iopub.execute_input":"2021-09-17T16:54:49.683377Z","iopub.status.idle":"2021-09-17T16:54:50.863953Z","shell.execute_reply.started":"2021-09-17T16:54:49.683346Z","shell.execute_reply":"2021-09-17T16:54:50.863039Z"},"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":{"execution":{"iopub.status.busy":"2021-09-17T16:54:50.865309Z","iopub.execute_input":"2021-09-17T16:54:50.865624Z","iopub.status.idle":"2021-09-17T16:54:50.874791Z","shell.execute_reply.started":"2021-09-17T16:54:50.865591Z","shell.execute_reply":"2021-09-17T16:54:50.873593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_data = np.array(train_data) / 255\nY_data =  to_categorical(labels_data, num_classes = 50) ","metadata":{"execution":{"iopub.status.busy":"2021-09-17T16:54:50.876210Z","iopub.execute_input":"2021-09-17T16:54:50.876517Z","iopub.status.idle":"2021-09-17T16:54:50.988849Z","shell.execute_reply.started":"2021-09-17T16:54:50.876487Z","shell.execute_reply":"2021-09-17T16:54:50.987944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_data[0]","metadata":{"execution":{"iopub.status.busy":"2021-09-17T16:54:50.990327Z","iopub.execute_input":"2021-09-17T16:54:50.990733Z","iopub.status.idle":"2021-09-17T16:54:50.997158Z","shell.execute_reply.started":"2021-09-17T16:54:50.990696Z","shell.execute_reply":"2021-09-17T16:54:50.996116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_data[0]","metadata":{"execution":{"iopub.status.busy":"2021-09-17T16:54:50.998794Z","iopub.execute_input":"2021-09-17T16:54:50.999622Z","iopub.status.idle":"2021-09-17T16:54:51.008068Z","shell.execute_reply.started":"2021-09-17T16:54:50.999521Z","shell.execute_reply":"2021-09-17T16:54:51.007007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, Y_train, Y_val = train_test_split(X_data, Y_data, test_size = 0.3, random_state=101)\n\nprint(\"X train data : \", len(X_train))\nprint(\"X label data : \", len(X_val))\nprint(\"Y test data : \", len(Y_train))\nprint(\"Y label data : \", len(Y_val))","metadata":{"execution":{"iopub.status.busy":"2021-09-17T16:54:51.009534Z","iopub.execute_input":"2021-09-17T16:54:51.009907Z","iopub.status.idle":"2021-09-17T16:54:51.102334Z","shell.execute_reply.started":"2021-09-17T16:54:51.009872Z","shell.execute_reply":"2021-09-17T16:54:51.101247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(horizontal_flip=False,\n                             vertical_flip=False,\n                             rotation_range=0,\n                             zoom_range=0.2,\n                             width_shift_range=0,\n                             height_shift_range=0,\n                             shear_range=0,\n                             fill_mode=\"nearest\")","metadata":{"execution":{"iopub.status.busy":"2021-09-17T16:54:51.103692Z","iopub.execute_input":"2021-09-17T16:54:51.104230Z","iopub.status.idle":"2021-09-17T16:54:51.108984Z","shell.execute_reply.started":"2021-09-17T16:54:51.104187Z","shell.execute_reply":"2021-09-17T16:54:51.108125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pretrained_model = tf.keras.applications.DenseNet201(input_shape=(100,100,3),\n                                                      include_top=False,\n                                                      weights='imagenet',\n                                                      pooling='avg')\npretrained_model.trainable = False","metadata":{"execution":{"iopub.status.busy":"2021-09-17T16:54:51.110334Z","iopub.execute_input":"2021-09-17T16:54:51.110877Z","iopub.status.idle":"2021-09-17T16:54:57.921223Z","shell.execute_reply.started":"2021-09-17T16:54:51.110838Z","shell.execute_reply":"2021-09-17T16:54:57.920364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = pretrained_model.input\ndrop_layer = tf.keras.layers.Dropout(0.25)(pretrained_model.output)\nx_layer = tf.keras.layers.Dense(512, activation='relu')(drop_layer)\nx_layer1 = tf.keras.layers.Dense(128, activation='relu')(x_layer)\ndrop_layer1 = tf.keras.layers.Dropout(0.20)(x_layer1)\noutputs = tf.keras.layers.Dense(50, activation='softmax')(drop_layer1)\n\n\nmodel = tf.keras.Model(inputs=inputs, outputs=outputs)","metadata":{"execution":{"iopub.status.busy":"2021-09-17T16:54:57.922582Z","iopub.execute_input":"2021-09-17T16:54:57.922927Z","iopub.status.idle":"2021-09-17T16:54:57.991768Z","shell.execute_reply.started":"2021-09-17T16:54:57.922891Z","shell.execute_reply":"2021-09-17T16:54:57.991007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-17T16:54:57.992884Z","iopub.execute_input":"2021-09-17T16:54:57.993219Z","iopub.status.idle":"2021-09-17T16:54:57.999323Z","shell.execute_reply.started":"2021-09-17T16:54:57.993182Z","shell.execute_reply":"2021-09-17T16:54:57.998588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)\nmodel.compile(optimizer=optimizer,loss='categorical_crossentropy',metrics=['acc'])\nhistory = model.fit(datagen.flow(X_train,Y_train,batch_size=32),validation_data=(X_val,Y_val),epochs=30)","metadata":{"execution":{"iopub.status.busy":"2021-09-17T16:54:58.000892Z","iopub.execute_input":"2021-09-17T16:54:58.001299Z","iopub.status.idle":"2021-09-17T16:56:37.091735Z","shell.execute_reply.started":"2021-09-17T16:54:58.001248Z","shell.execute_reply":"2021-09-17T16:56:37.090812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nacc = history.history['acc']\nval_acc = history.history['val_acc']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'r', label='Training accuracy')\nplt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.legend(loc=0)\nplt.figure()\n\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-17T16:56:37.093464Z","iopub.execute_input":"2021-09-17T16:56:37.093820Z","iopub.status.idle":"2021-09-17T16:56:37.274152Z","shell.execute_reply.started":"2021-09-17T16:56:37.093783Z","shell.execute_reply":"2021-09-17T16:56:37.273334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(epochs, loss, 'r', label='Training loss')\nplt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend(loc=0)\nplt.figure()\n\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-17T16:56:37.277357Z","iopub.execute_input":"2021-09-17T16:56:37.277610Z","iopub.status.idle":"2021-09-17T16:56:37.434202Z","shell.execute_reply.started":"2021-09-17T16:56:37.277585Z","shell.execute_reply":"2021-09-17T16:56:37.433207Z"},"trusted":true},"execution_count":null,"outputs":[]}]}