{"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":"# LOAD LIBRARIES","metadata":{}},{"cell_type":"code","source":"import os\n\n\nimport random\nimport seaborn as sns\nimport cv2\n\n# General packages\nimport pandas as pd\nimport numpy as np\nimport matplotlib\nimport matplotlib.pyplot as plt\nimport PIL\nimport IPython.display as ipd\nimport glob\nimport h5py\nimport plotly.graph_objs as go\nimport plotly.express as px\nfrom PIL import Image\nfrom tempfile import mktemp\nfrom colorama import Fore, Back, Style\n\n# Setting color palette.\nplt.rcdefaults()\nplt.style.use('dark_background')\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-15T16:31:55.642540Z","iopub.execute_input":"2021-08-15T16:31:55.643067Z","iopub.status.idle":"2021-08-15T16:31:58.826599Z","shell.execute_reply.started":"2021-08-15T16:31:55.642970Z","shell.execute_reply":"2021-08-15T16:31:58.825337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* train.csv: only contains two columns\n    * id: image id\n    * landmark_id: target landmark id","metadata":{}},{"cell_type":"code","source":"BASE_PATH = '../input/landmark-recognition-2021'\n\nTRAIN_DIR = f'{BASE_PATH}/train'\nTEST_DIR = f'{BASE_PATH}/test'\n\n\ntrain = pd.read_csv(f'{BASE_PATH}/train.csv')\nsubmission = pd.read_csv(f'{BASE_PATH}/sample_submission.csv')\ntrain.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-15T16:31:58.828512Z","iopub.execute_input":"2021-08-15T16:31:58.828948Z","iopub.status.idle":"2021-08-15T16:32:00.808413Z","shell.execute_reply.started":"2021-08-15T16:31:58.828909Z","shell.execute_reply":"2021-08-15T16:32:00.807313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"landmark=train.groupby('landmark_id').count()\nlandmark_df=landmark\nlandmark_df['frequency']=landmark_df['id']\nlandmark_df=landmark_df.drop('id',axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T16:32:00.810703Z","iopub.execute_input":"2021-08-15T16:32:00.811044Z","iopub.status.idle":"2021-08-15T16:32:01.109367Z","shell.execute_reply.started":"2021-08-15T16:32:00.811009Z","shell.execute_reply":"2021-08-15T16:32:01.108180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Unique number of landmarks in data set**\n\nthere are 81313 unique landmarks","metadata":{}},{"cell_type":"code","source":"n=train['landmark_id'].nunique()   \nsorted_df=landmark_df.sort_values(by='frequency',ascending=False).reset_index()\nprint(n)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T16:32:01.114141Z","iopub.execute_input":"2021-08-15T16:32:01.114485Z","iopub.status.idle":"2021-08-15T16:32:01.181791Z","shell.execute_reply.started":"2021-08-15T16:32:01.114452Z","shell.execute_reply":"2021-08-15T16:32:01.180513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Number of images per landmark\n\n* lets see top 50 landmarks with highest number of images in dataset","metadata":{}},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')\n\nlandmark_df=sorted_df.head(50)\nlandmark_df['landmark_id'] =  landmark_df.landmark_id.apply(lambda x: f'landmark_{x}')\n\nfig = px.bar(landmark_df, y=\"frequency\", x=\"landmark_id\",color='landmark_id', orientation='v',\n             hover_data=[\"landmark_id\", \"frequency\"],\n             height=1000,\n             title='Number of images per landmark_id (Top 50 landmark_ids)')\nfig.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2021-08-15T16:32:01.183127Z","iopub.execute_input":"2021-08-15T16:32:01.183530Z","iopub.status.idle":"2021-08-15T16:32:02.717117Z","shell.execute_reply.started":"2021-08-15T16:32:01.183487Z","shell.execute_reply":"2021-08-15T16:32:02.715888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**lets see last 10 landmarks with lowest number of images in dataset**\n\n  * these landmarks only have 2 images ","metadata":{}},{"cell_type":"code","source":"landmark_df1=sorted_df.tail(10)\n\nlandmark_df1['landmark_id'] =  landmark_df1.landmark_id.apply(lambda x: f'landmark_{x}')\n\nfig = px.bar(landmark_df1, y=\"frequency\", x=\"landmark_id\",color='landmark_id', orientation='v',\n             hover_data=[\"landmark_id\", \"frequency\"],\n             height=1000,\n             title='Number of images per landmark_id (Top 50 landmark_ids)')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-15T16:32:02.718543Z","iopub.execute_input":"2021-08-15T16:32:02.718883Z","iopub.status.idle":"2021-08-15T16:32:02.846528Z","shell.execute_reply.started":"2021-08-15T16:32:02.718849Z","shell.execute_reply":"2021-08-15T16:32:02.845689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Density destribution ","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize = (12, 8))\n\nsns.kdeplot(train['landmark_id'], color=\"yellow\",shade=True)\nplt.xlabel(\"LandMark IDs\")\nplt.ylabel(\"Probability Density\")\nplt.title('Class Distribution - Density plot')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-15T16:32:02.847924Z","iopub.execute_input":"2021-08-15T16:32:02.848227Z","iopub.status.idle":"2021-08-15T16:32:11.229219Z","shell.execute_reply.started":"2021-08-15T16:32:02.848198Z","shell.execute_reply":"2021-08-15T16:32:11.228339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# landmark with higgest number of images \n\n  * id number 138982","metadata":{}},{"cell_type":"code","source":"import PIL\nfrom PIL import Image, ImageDraw\n\n\ndef display_images(images, title=None): \n    f, ax = plt.subplots(6,6, figsize=(18,22))\n    if title:\n        f.suptitle(title, fontsize = 30)\n\n    for i, image_id in enumerate(images):\n        image_path = os.path.join(TRAIN_DIR, f'{image_id[0]}/{image_id[1]}/{image_id[2]}/{image_id}.jpg')\n        image = Image.open(image_path)\n        \n        ax[i//6, i%6].imshow(image) \n        image.close()       \n        ax[i//6, i%6].axis('off')\n    plt.show() \n  \nsamples = train[train.landmark_id == 138982].sample(30).id.values\n\ndisplay_images(samples)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T16:32:11.231283Z","iopub.execute_input":"2021-08-15T16:32:11.231801Z","iopub.status.idle":"2021-08-15T16:32:15.801488Z","shell.execute_reply.started":"2021-08-15T16:32:11.231766Z","shell.execute_reply":"2021-08-15T16:32:15.799712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**landmark with 2nd most number of images (landmark_id: 126637)**","metadata":{}},{"cell_type":"code","source":"samples = train[train.landmark_id == 126637].sample(30).id.values\n\ndisplay_images(samples)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T16:32:15.803690Z","iopub.execute_input":"2021-08-15T16:32:15.804269Z","iopub.status.idle":"2021-08-15T16:32:20.247097Z","shell.execute_reply.started":"2021-08-15T16:32:15.804208Z","shell.execute_reply":"2021-08-15T16:32:20.245310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}