{"cells":[{"metadata":{},"cell_type":"markdown","source":"![](https://1.bp.blogspot.com/-EQNRBJuBXJo/XMthch8bdWI/AAAAAAAAEG0/oHVw1fxfiXoMfsSn_rNQPR-fyRqM_N4CwCEwYBhgL/s1600/image1.png)\n## What you need to know about this competition?\nIn this competition, you are asked to take test images and recognize which landmarks (if any) are depicted in them. The training set is available in the `train/ folder`, with corresponding landmark labels in train.csv. The test set images are listed in the `test/ folder`. **Each image has a unique id**. Since there are a large number of images, each image is placed within three subfolders according to the first three characters of the image id (i.e. image abcdef.jpg is placed in a/b/c/abcdef.jpg).\n\n**This is a synchronous rerun code competition.** The provided test set is a representative set of files to demonstrate the format of the private test set. When you submit your notebook, Kaggle will rerun your code on the private dataset. Additionally, this competition also has two unique characteristics:\n\n* **To facilitate recognition-by-retrieval approaches, the private training set contains only a 100k subset of the total public training set.** This 100k subset contains all of the training set images associated with the landmarks in the private test set. You may still attach the full training set as an external data set if you wish.\n* **Submissions are given 12 hours to run, as compared to the site-wide session limit of 9 hours. While your commit must still finish in the 9 hour limit in order to be eligible to submit, the rerun may take the full 12 hours.**\n\n### Loading Libraries","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom os import listdir\nfrom glob import glob\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Let's have a look at the given datasets. ","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_data = pd.read_csv('../input/landmark-recognition-2020/train.csv')\nsubmission = pd.read_csv(\"../input/landmark-recognition-2020/sample_submission.csv\")\nprint('Training dataframe shape: ', train_data.shape)\nprint('Total number of training images', train_data.shape[0])\nprint('Total number of test images', len(glob('../input/landmark-recognition-2020/test/*/*/*/*.jpg')))\ntrain_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Let's have a look at the sample submission file. \nsubmission.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Let's have a look if there is any null values in the training data. ","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data['landmark_id'].value_counts().hist()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# missing data in training data \ntotal = train_data.isnull().sum().sort_values(ascending = False)\nprint(total)\npercent = (train_data.isnull().sum()/train_data.isnull().count()).sort_values(ascending = False)\nmissing_train_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])\nmissing_train_data.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now Let's have a look at the **most frequent landmarks** in the dataset. ","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"most_frequent = 10\n# Occurance of landmark_id in decreasing order(Top categories)\ntemp = pd.DataFrame(train_data.landmark_id.value_counts().head(most_frequent)).copy()\ntemp.reset_index(inplace=True)\ntemp.columns = ['landmark_id','count']\ntemp.sort_values(by='count', ascending=False, inplace = True)\ntemp.set_index('landmark_id', inplace = True)\ntemp.plot.bar()\nmost_ids = temp.index.copy()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Here the most frequent landmark is with **landmark_id = 138982** and it has **6272** samples in it. Let's have a look at the comparative frequency plots of the dataset. ","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"Now Let's have a look at the **least frequent landmarks** in the dataset. ","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"least_frequent = 10\n# Occurance of landmark_id in decreasing order(Top categories)\ntemp = pd.DataFrame(train_data.landmark_id.value_counts().tail(least_frequent)).copy()\ntemp.reset_index(inplace=True)\ntemp.columns = ['landmark_id','count']\ntemp.sort_values(by='count', ascending=False, inplace = True)\ntemp.set_index('landmark_id', inplace = True)\nprint(temp)\ntemp.plot.bar()\nleast_ids = temp.index.copy()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Therefore, it is evident that, each of the class has at least two data samples in them and no class is empty. ","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data[train_data['landmark_id'] == 110417].reset_index()['id']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"## Now let's have a look at some of the training images\n### Most Frequent Location ( landmark_id = 138982)","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"\ndef view_images(ID=110417):\n    import PIL\n    from PIL import Image\n#     ID = 110417\n#     Generating the filepaths \n    image_file_names = train_data[train_data['landmark_id'] == ID].reset_index()['id']\n#     print(image_file_names)\n    image_paths = []\n    for image_name in image_file_names:\n        sub_folder = image_name[0] + '/'+ image_name[1] + '/' +  image_name[2] + '/'\n        image_paths.append('../input/landmark-recognition-2020/train/' + sub_folder + image_name + '.jpg')\n#     print(image_paths)\n\n\n\n    grid_size = 4\n\n    rows = grid_size\n    cols = grid_size\n    fig = plt.figure(figsize=(grid_size*3+2, grid_size*3))\n    for i in range(1, rows*cols+1):\n        if(i>len(image_paths)):\n            break\n        fig.add_subplot(rows, cols, i)\n        plt.imshow(Image.open(image_paths[i-1]))\n        plt.title('ID =' + str(ID), fontsize=16)\n        plt.axis(False)\n        fig.add_subplot\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## View the Images Interactively\n### Most frequent images\n* The top 10 location_ids with most number of samples are listed below and you can have a look at them by selecting the maximum number images. You can select the location id from the dropdown menu and then you can select the IDs and view thm separately. \n* You might have to wait a bit more time to let image generate, plot and view. ","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"from ipywidgets import interact\ninteract(view_images, ID = most_ids)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## View the Images Interactively\n### Most frequent images\n* The top 10 location_ids with least number of samples are listed below and you can have a look at them by selecting the maximum number images. You can select the location id from the dropdown menu and then you can select the IDs and view thm separately. \n* You might have to wait a bit more time to let image generate, plot and view. ","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"interact(view_images, ID = least_ids)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Images of class_id = 110417 (One of the least frequent)","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"view_images(least_ids[0])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Images of class_id = 59905  (One of the least frequent)","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"view_images(least_ids[1])\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Images of class_id = 4171  (One of the least frequent)","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"view_images(least_ids[2])\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Images of class_id = 110417  (One of the least frequent)","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"view_images(least_ids[3])\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Images of class_id = 110417","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"view_images(least_ids[0])\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Images of class_id = 195143  (One of the least frequent)","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"view_images(least_ids[4])\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Images of class_id = 180503  (One of the least frequent)","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"view_images(least_ids[5])\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Now Let's have a look at the most frequent images in the class.\n### Images of class_id = 138982  (One of the most frequent)\n* Here in each of these categories, there might be a huge number of images but it is quite evident that most of the image in one categories are kind of similar. \n* For example, for the image `landmark_id=138982` has most of the image which are paintings of the wall where the details of the paintings are written usually at the bottom. ","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"view_images(most_ids[0])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Images of class_id = 126637  (One of the most frequent)\n* Visually it might seem that the images are from industrial area or location","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"view_images(most_ids[1])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Images of class_id = 20409  (One of the most frequent)\n* Visually it is evident that most of the images are from ancient brick work or some kind of terracota. ","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"view_images(most_ids[2])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Images of class_id = 83144  (One of the most frequent)\n* Visually it is evident that these are images of single hut or similar buildings. ","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"view_images(most_ids[3])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"view_images(most_ids[4])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Observations:\n### * There is massive class imbalance in there\n### * There are some noise in lot of images\n### * Images are of different shapes. If we convert all of them at the same size, it will create problem because size will be distorted. \n### * Need some intelligent system of cropping the images. ","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}