{"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":"markdown","source":"The notebook is focused on \"***Landmark Recognition using MobileNetV2***\". The code begins by setting up the necessary libraries required for data manipulation, image processing, and building a deep learning model.\n\n**MobileNetV2 architecture**, a lightweight deep learning model known for its efficiency in mobile vision applications, recognizes landmarks from images.","metadata":{"execution":{"iopub.status.busy":"2023-08-21T12:02:29.517148Z","iopub.execute_input":"2023-08-21T12:02:29.517474Z","iopub.status.idle":"2023-08-21T12:02:47.031807Z","shell.execute_reply.started":"2023-08-21T12:02:29.517445Z","shell.execute_reply":"2023-08-21T12:02:47.030533Z"}}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport cv2\nimport tensorflow as tf\nfrom tensorflow.keras.applications import MobileNetV2\nfrom tensorflow.keras.layers import Dense, Dropout\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Loading the data**","metadata":{}},{"cell_type":"code","source":"traindf = pd.read_csv(\"../input/landmark-recognition-2021/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-08-21T12:03:01.233149Z","iopub.execute_input":"2023-08-21T12:03:01.233892Z","iopub.status.idle":"2023-08-21T12:03:02.734411Z","shell.execute_reply.started":"2023-08-21T12:03:01.233835Z","shell.execute_reply":"2023-08-21T12:03:02.733345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindf.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-21T12:57:53.306740Z","iopub.execute_input":"2023-08-21T12:57:53.307471Z","iopub.status.idle":"2023-08-21T12:57:53.317554Z","shell.execute_reply.started":"2023-08-21T12:57:53.307438Z","shell.execute_reply":"2023-08-21T12:57:53.316526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Extracting Limited(50) Unique Landmarks and Corresponding Image IDs**","metadata":{}},{"cell_type":"code","source":"landmark_unique = traindf['landmark_id'].unique()[0:50]\nimage_ids = []\nlabels = []\ntemp_labels = []\n\nfor i, id_ in enumerate(landmark_unique):\n    for iid in traindf['id'][traindf['landmark_id'] == id_]:\n        image_ids.append(iid)\n        labels.append(id_)\n        temp_labels.append(i)","metadata":{"execution":{"iopub.status.busy":"2023-08-21T12:03:06.513592Z","iopub.execute_input":"2023-08-21T12:03:06.515119Z","iopub.status.idle":"2023-08-21T12:03:06.689475Z","shell.execute_reply.started":"2023-08-21T12:03:06.515078Z","shell.execute_reply":"2023-08-21T12:03:06.688331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Image Loading and Resizing for Landmark Recognition Dataset**","metadata":{}},{"cell_type":"code","source":"mainpath = '../input/landmark-recognition-2021/train'\nimages_pixels = []\n\nfor iid in image_ids:\n    first_dir = os.path.join(mainpath, iid[0])\n    second_dir = os.path.join(first_dir, iid[1])\n    third_dir = os.path.join(second_dir, iid[2])\n    finalpath = os.path.join(third_dir, iid + '.jpg')\n    \n    img_pix = cv2.imread(finalpath, 1)\n    images_pixels.append(cv2.resize(img_pix, (100, 100)))","metadata":{"execution":{"iopub.status.busy":"2023-08-21T12:03:09.840633Z","iopub.execute_input":"2023-08-21T12:03:09.841313Z","iopub.status.idle":"2023-08-21T12:03:29.950805Z","shell.execute_reply.started":"2023-08-21T12:03:09.841274Z","shell.execute_reply":"2023-08-21T12:03:29.949779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_data = np.array(images_pixels) / 255.0\nY_data = to_categorical(temp_labels, num_classes=50)","metadata":{"execution":{"iopub.status.busy":"2023-08-21T12:03:37.730742Z","iopub.execute_input":"2023-08-21T12:03:37.731454Z","iopub.status.idle":"2023-08-21T12:03:37.843187Z","shell.execute_reply.started":"2023-08-21T12:03:37.731420Z","shell.execute_reply":"2023-08-21T12:03:37.842096Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2023-08-21T12:03:42.857931Z","iopub.execute_input":"2023-08-21T12:03:42.858281Z","iopub.status.idle":"2023-08-21T12:03:42.947528Z","shell.execute_reply.started":"2023-08-21T12:03:42.858252Z","shell.execute_reply":"2023-08-21T12:03:42.946479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Displaying a Subset of Training Images with Labels**","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.figure(figsize=(12, 8))\nfor i in range(16):\n    plt.subplot(4, 4, i + 1)\n    plt.imshow(X_train[i])\n    plt.title(f\"Label: {np.argmax(Y_train[i])}\")\n    plt.axis('off')\n    \nplt.tight_layout()\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**MobileNetV2 Model Initialization**","metadata":{}},{"cell_type":"code","source":"base_model = MobileNetV2(input_shape=(100,100,3), include_top=False, weights='imagenet', pooling='avg')\nbase_model.trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-08-21T12:03:46.040970Z","iopub.execute_input":"2023-08-21T12:03:46.041549Z","iopub.status.idle":"2023-08-21T12:03:53.954489Z","shell.execute_reply.started":"2023-08-21T12:03:46.041518Z","shell.execute_reply":"2023-08-21T12:03:53.953490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Adding Custom Layers**","metadata":{}},{"cell_type":"code","source":"x = base_model.output\nx = Dropout(0.25)(x)\nx = Dense(128, activation='relu')(x)\npredictions = Dense(50, activation='softmax')(x)","metadata":{"execution":{"iopub.status.busy":"2023-08-21T12:04:22.555708Z","iopub.execute_input":"2023-08-21T12:04:22.556174Z","iopub.status.idle":"2023-08-21T12:04:22.589556Z","shell.execute_reply.started":"2023-08-21T12:04:22.556140Z","shell.execute_reply":"2023-08-21T12:04:22.588543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Compiling the Landmark Recognition Model**","metadata":{}},{"cell_type":"code","source":"model = Model(inputs=base_model.input, outputs=predictions)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-21T12:04:28.755929Z","iopub.execute_input":"2023-08-21T12:04:28.756298Z","iopub.status.idle":"2023-08-21T12:04:28.777655Z","shell.execute_reply.started":"2023-08-21T12:04:28.756268Z","shell.execute_reply":"2023-08-21T12:04:28.776691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = Adam(learning_rate=0.001)\nmodel.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['acc'])\n","metadata":{"execution":{"iopub.status.busy":"2023-08-21T12:04:53.704105Z","iopub.execute_input":"2023-08-21T12:04:53.704501Z","iopub.status.idle":"2023-08-21T12:04:53.731658Z","shell.execute_reply.started":"2023-08-21T12:04:53.704457Z","shell.execute_reply":"2023-08-21T12:04:53.730621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Image Data Augmentation Configuration**","metadata":{}},{"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\")\n","metadata":{"execution":{"iopub.status.busy":"2023-08-21T12:04:59.882195Z","iopub.execute_input":"2023-08-21T12:04:59.882602Z","iopub.status.idle":"2023-08-21T12:04:59.888513Z","shell.execute_reply.started":"2023-08-21T12:04:59.882573Z","shell.execute_reply":"2023-08-21T12:04:59.887166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Training the Landmark Recognition Model with Data Augmentation**","metadata":{}},{"cell_type":"code","source":"history = model.fit(datagen.flow(X_train, Y_train, batch_size=32),\n                    validation_data=(X_val, Y_val),\n                    epochs=30)","metadata":{"execution":{"iopub.status.busy":"2023-08-21T12:07:27.579628Z","iopub.execute_input":"2023-08-21T12:07:27.580009Z","iopub.status.idle":"2023-08-21T12:09:41.158246Z","shell.execute_reply.started":"2023-08-21T12:07:27.579978Z","shell.execute_reply":"2023-08-21T12:09:41.157099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Model Visualization**","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Plotting training and validation accuracy\nplt.figure(figsize=(12, 4))\nplt.subplot(1, 2, 1)\nplt.plot(history.history['acc'], label='Training Accuracy')\nplt.plot(history.history['val_acc'], label='Validation Accuracy')\nplt.legend()\nplt.title('Training and Validation Accuracy')\n","metadata":{"execution":{"iopub.status.busy":"2023-08-21T12:17:35.049812Z","iopub.execute_input":"2023-08-21T12:17:35.050212Z","iopub.status.idle":"2023-08-21T12:17:35.520293Z","shell.execute_reply.started":"2023-08-21T12:17:35.050182Z","shell.execute_reply":"2023-08-21T12:17:35.518819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting training and validation loss\nplt.subplot(1, 2, 2)\nplt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.legend()\nplt.title('Training and Validation Loss')\n","metadata":{"execution":{"iopub.status.busy":"2023-08-21T12:18:10.755522Z","iopub.execute_input":"2023-08-21T12:18:10.755922Z","iopub.status.idle":"2023-08-21T12:18:11.115902Z","shell.execute_reply.started":"2023-08-21T12:18:10.755889Z","shell.execute_reply":"2023-08-21T12:18:11.114826Z"},"trusted":true},"execution_count":null,"outputs":[]}]}