{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Libraries Required","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nplt.style.use('fivethirtyeight')\nimport plotly_express as px\nimport plotly.graph_objects as go\nimport glob\nfrom tqdm import tqdm_notebook\nimport cv2\nimport os\nimport random\nimport seaborn as sns\n\nimport warnings\nwarnings.filterwarnings('ignore')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Read the Data","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/landmark-recognition-2020/train.csv')\ntest = glob.glob('/kaggle/input/landmark-recognition-2020/test/*/*/*/*.jpg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Total Train Images: {}'.format(len(df_train))) \nprint('Total Test Images: {}'.format(len(test)))\nprint('Total Unique Landmarks: {}'.format(df_train.landmark_id.nunique()))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Landmarks\n### Top and Bottom Landmarks","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"landmarks = df_train.groupby('landmark_id',as_index=False)['id'].count()\\\n    .sort_values('id',ascending=False).reset_index(drop=True)\nlandmarks.rename(columns={'id':'count'},inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def add_text(ax,fontsize=12):\n    for p in ax.patches:\n        x=p.get_bbox().get_points()[:,0]\n        y=p.get_bbox().get_points()[1,1]\n        ax.annotate('{}'.format(int(y)), (x.mean(), y), ha='center', va='bottom',size=fontsize)\nfig, (ax1,ax2) = plt.subplots(2,1,figsize=(16,8))\nsns.barplot(data=landmarks[:50],x='landmark_id',y='count',ax=ax1,color='#30a2da',\n           order=landmarks[:50]['landmark_id'])\nadd_text(ax1,fontsize=8)\nax1.set_title('Top 50 Landmarks')\nax1.set_ylabel('Number of Images')\nax1.set_xticklabels(ax1.get_xticklabels(), rotation=40, ha=\"right\",size=8)\nsns.barplot(data=landmarks[-50:],x='landmark_id',y='count',ax=ax2,color='#fc4f30')\nax2.set_title('Bottom 50 Landmarks')\nax2.set_ylabel('Number of Images')\nax2.set_xticklabels(ax2.get_xticklabels(), rotation=40, ha=\"right\",size=8)\nplt.tight_layout()\nprint(f\"Number of Landmarks with less than 10 images are {len(landmarks[landmarks['count']<10])}\")\nprint(f\"Number of Landmarks with less than 20 images are {len(landmarks[landmarks['count']<20])}\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Distribution of Landmarks","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(16,4))\nax = sns.distplot(df_train['landmark_id'],bins=500)\nax.set_title('Distribution of Landmarks')\nplt.tight_layout()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Landmark Images","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_image(id):\n    path = os.path.join('/kaggle/input/landmark-recognition-2020/train',\n                        id[0],id[1],id[2],id+'.jpg')\n    img = cv2.imread(path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    return img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def show_data(df,rows,cols):\n    df.reset_index(inplace=True,drop=True)\n    fig = plt.figure(figsize=(24,24))\n    i = 1\n    for r in range(rows):\n        for c in range(cols):\n            id = df.loc[i-1,'id']\n            label = df.loc[i-1,'landmark_id']\n            ax = fig.add_subplot(rows,cols,i)\n            img = get_image(id)\n            ax.set_xticks([])\n            ax.set_yticks([])\n            ax.set_title(label)\n            ax.imshow(img)\n            i+=1\n    return fig","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#some random images from the train set\ninds = np.random.choice(df_train.index.tolist(),20)\nfig = show_data(df_train.iloc[inds,:],4,5)\nfig.tight_layout()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_images = df_train.drop_duplicates(subset=['landmark_id'])\ndf_images = df_images.sample(n=1000,random_state=23)\ndf_images.reset_index(inplace=True,drop=True)\ndf_images['height'] = 0\ndf_images['width'] = 0\ndf_images['channels'] = 0\nfor i in tqdm_notebook(range(len(df_images))):\n    img = get_image(df_images.loc[i,'id'])\n    df_images.loc[i,'height'] = img.shape[0]\n    df_images.loc[i,'width'] = img.shape[1]\n    df_images.loc[i,'channels'] = img.shape[2]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def img_distribution(df):\n    shape = (np.min(df['width']), np.max(df['width']),\n            np.min(df['height']), np.max(df['height']))\n    fig = px.scatter(df,x='width',y='height')\n    fig.add_shape(\n        x0 = shape[0],\n        x1 = shape[1],\n        y0 = shape[2],\n        y1 = shape[3],\n        fillcolor = 'yellow',\n        opacity=0.3,\n        layer='below'\n    )\n    fig.add_trace(go.Scatter(name='mean',x=[np.mean(df['width'])],y=[np.mean(df['height'])],\n                         marker=dict(color='red',size=10)))\n    #fig.update_traces(marker_line_color='black',marker_line_width=1)\n    fig.update_layout(width=700,height=400,margin=dict(l=0,b=0,r=0,t=40),template='seaborn',\n                 title='Distribution of Image Dimensions', showlegend=False,\n                 xaxis=dict(title='Width', mirror=True, linewidth=2, linecolor='black',showgrid=False),\n                 yaxis=dict(title='Height', mirror=True, linewidth=2, linecolor='black',showgrid=False),\n                 plot_bgcolor='rgb(255,255,255)')\n    return fig","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_distribution(df_images)","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}