{"cells":[{"metadata":{},"cell_type":"markdown","source":"# 1. Introduction","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"## 1.a Data Description","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"In 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).","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"## 1.b Evaluation Metrics","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"Submissions are evaluated using Global Average Precision (GAP) at k, where k=1. This metric is also known as micro Average Precision (μ\n\nAP), as per [1,2]. It works as follows:\n\nFor each test image, you will predict one landmark label and a corresponding confidence score. The evaluation treats each prediction as an individual data point in a long list of predictions (sorted in descending order by confidence scores), and computes the Average Precision based on this list.\n\nIf a submission has N\n\npredictions (label/confidence pairs) sorted in descending order by their confidence scores, then the Global Average Precision is computed as:\n\nGAP=(1/M)∑P(i)rel(i)\n\nwhere:\n\n* N is the total number of predictions returned by the system, across all queries\n* M is the total number of queries with at least one landmark from the training set visible in it (note that some queries may not depict landmarks)\n* P(i) is the precision at rank i\n* rel(i) denotes the relevance of prediciton i: it’s 1 if the i-th prediction is correct, and 0 otherwise\n\nFor each id in the test set, you can predict at most one landmark and its corresponding confidence score. Some images contain no landmarks. You may decide not to predict any result for a given query, by submitting an empty prediction. ","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"## Importing Libraries","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nimport plotly.express as px\n\nimport glob\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport cv2","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Reading files","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train= pd.read_csv(\"../input/landmark-recognition-2020/train.csv\")\ndf_submission = pd.read_csv(\"../input/landmark-recognition-2020/sample_submission.csv\")\nTRAIN_PATH = \"../input/landmark-recognition-2020/train\"\nTEST_PATH = \"../input/landmark-recognition-2020/test\"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Looking at the dataset","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_list = glob.glob('../input/landmark-recognition-2020/train/*/*/*/*')\ntest_list = glob.glob('../input/landmark-recognition-2020/test/*/*/*/*')\n\nprint( 'Images in Train Folder:', len(train_list))\nprint( 'Images in Test Folder:', len(test_list))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"df_train.shape[0], df_train.id.nunique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.landmark_id.nunique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_image_counts = df_train.groupby(\"landmark_id\").agg(images = (\"id\",\"nunique\")).reset_index()\ndf_image_counts.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"px.box(df_image_counts, x= \"images\",width=1000, height=300)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"What we have:\n* We have ~1.6M images in train data with ~81 k landmarks (classes) \n* We have a minimum of 2 images per landmark and a exptionally high 6272 images of a landmark. The upper threshold of the boxplot is 42 meaning most of images have less than or equal to 42 images per landmark (Though it would be interesting to see which is the landmark with 6272 images 😛)","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def plot_images(image_list,rows,cols,title):\n    fig,ax = plt.subplots(rows,cols,figsize = (25,5*rows))\n    ax = ax.flatten()\n    for i, image_id in enumerate(image_list):\n        image = cv2.imread(TRAIN_PATH+'/{}/{}/{}/{}.jpg'.format(image_id[0],image_id[1],image_id[2],image_id))\n        image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)\n        ax[i].imshow(image)\n        ax[i].set_axis_off()\n        ax[i].set_title(image_id)\n    plt.suptitle(title)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_images(df_train.loc[df_train.landmark_id==df_image_counts[df_image_counts.images == 6272][\"landmark_id\"].values[0],\"id\"].values[:10],2,5,\"Images of Landmark - 138982 (10 out of 6k images of this landmark)\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* This seems to be the default class meaning all the images that donot have a class or a landmark in it are put into this class","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_images(df_train.loc[df_train.landmark_id==df_image_counts[df_image_counts.images == 2231][\"landmark_id\"].values[0],\"id\"].values[:10],2,5,\"Images of Landmark with second highest number of images (10 out of 2 k images of this landmark)\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_images(df_train.loc[df_train.landmark_id==df_image_counts[df_image_counts.images == 10][\"landmark_id\"].values[0],\"id\"].values[:10],2,5,\"Images of Landmark:\" + str(df_image_counts[df_image_counts.images == 10][\"landmark_id\"].values[0]))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Please Upvote if you found this helpful :)","execution_count":null}],"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}