{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os, ast\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nfrom matplotlib import pyplot as plt # plotting\nimport matplotlib.patches as patches\nimport seaborn as sns # plotting","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"!ls /kaggle/input/global-wheat-detection","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Reading the given dataset","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# Some constants\nBASE_DIR = '/kaggle/input/global-wheat-detection'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\ntrain_df = pd.read_csv(os.path.join(BASE_DIR, 'train.csv'))\nsample_sub_df = pd.read_csv(os.path.join(BASE_DIR, 'sample_submission.csv'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_sub_df.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Let's see the size of the dataset","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f'Shape of training data: {train_df.shape}')\nprint(f'Shape of given test data: {sample_sub_df.shape}')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Let's find out number of unique images in training dataset","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f'# of unique images: {train_df[\"image_id\"].nunique()}')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Distribution of the size of training images","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f'Unique heights and widths: {train_df[\"width\"].unique()}, {train_df[\"height\"].unique()}')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"##  Min, Max and Average number of wheat heads","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f'Minimum number of wheat heads: {max(train_df[\"image_id\"].value_counts())}')\nprint(f'Minimum number of wheat heads: {len(train_df)/train_df[\"image_id\"].nunique()}')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f'Total number of images: {len(os.listdir(os.path.join(BASE_DIR, \"train\")))}')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"So there are 3422 images in traing directory and 3373 images are annotated, that means there are 49 images which do not have any wheat heads in it (without annotation)","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"Summary of data:\n * Number of images: 3373\n * Avg. # of wheat heads per image: ~44\n * All images have same dimension: 1024 * 1024\n * Number of minimum bounding boxes (Wheat heads): 0 (Some of the images are without annotation)\n * Number of maximum bounding boxes (Wheat heads): 116","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"## Distribution of number of bounding boxes","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.distplot(train_df['image_id'].value_counts(), kde=False)\nplt.xlabel('# of wheat heads')\nplt.ylabel('# of images')\nplt.title('# of wheat heads vs. # of images')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df[['x_min','y_min', 'width', 'height']] = pd.DataFrame([ast.literal_eval(x) for x in train_df.bbox.tolist()], index= train_df.index)\ntrain_df = train_df[['image_id', 'bbox', 'source', 'x_min', 'y_min', 'width', 'height']]\ntrain_df","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Distribution of size of bounding boxes","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.distplot(train_df['width'] * train_df['height'], kde=False)\nplt.xlabel('Area of bbox')\nplt.ylabel('# of images')\nplt.title('Area of bbox vs. # of images')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Visualizing a few training images without bounding boxes","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# Visualize few samples of current training dataset\nfig, ax = plt.subplots(nrows=2, ncols=4, figsize=(20, 10))\ncount=0\nfor row in ax:\n    for col in row:\n        img = plt.imread(f'{os.path.join(BASE_DIR, \"train\", train_df[\"image_id\"].unique()[count])}.jpg')\n        col.grid(False)\n        col.set_xticks([])\n        col.set_yticks([])\n        col.imshow(img)\n        count += 1\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Visualize few samples of current training dataset\nfig, ax = plt.subplots(nrows=2, ncols=4, figsize=(20, 10))\ncount=0\nfor row in ax:\n    for col in row:\n        img = plt.imread(f'{os.path.join(BASE_DIR, \"train\", train_df[\"image_id\"].unique()[-count])}.jpg')\n        col.grid(False)\n        col.set_xticks([])\n        col.set_yticks([])\n        col.imshow(img)\n        count += 1\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Now let's visualize a few training images with bounding boxes","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_bbox(image_id, df, col, color='white'):\n    bboxes = df[df['image_id'] == image_id]\n    \n    for i in range(len(bboxes)):\n        # Create a Rectangle patch\n        rect = patches.Rectangle(\n            (bboxes['x_min'].iloc[i], bboxes['y_min'].iloc[i]),\n            bboxes['width'].iloc[i], \n            bboxes['height'].iloc[i], \n            linewidth=2, \n            edgecolor=color, \n            facecolor='none')\n\n        # Add the patch to the Axes\n        col.add_patch(rect)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Visualize few samples of current training dataset\nfig, ax = plt.subplots(nrows=2, ncols=2, figsize=(20, 20))\ncount=0\nfor row in ax:\n    for col in row:\n        img_id = train_df[\"image_id\"].unique()[count]\n        img = plt.imread(f'{os.path.join(BASE_DIR, \"train\", img_id)}.jpg')\n        col.grid(False)\n        col.set_xticks([])\n        col.set_yticks([])\n        get_bbox(img_id, train_df, col, color='red')\n        col.imshow(img)\n        count += 1\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## few more...","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# Visualize few samples of current training dataset\nfig, ax = plt.subplots(nrows=2, ncols=2, figsize=(20, 20))\ncount=0\nfor row in ax:\n    for col in row:\n        img_id = train_df[\"image_id\"].unique()[-count]\n        img = plt.imread(f'{os.path.join(BASE_DIR, \"train\", img_id)}.jpg')\n        col.grid(False)\n        col.set_xticks([])\n        col.set_yticks([])\n        get_bbox(img_id, train_df, col)\n        col.imshow(img)\n        count += 1\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Image with highest number of wheat heads","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"image_id = (train_df['image_id'].value_counts() == 116).index[0]\nimg = plt.imread(f'{os.path.join(BASE_DIR, \"train\", image_id)}.jpg')\n\nfig, ax = plt.subplots(1, figsize=(12, 12))\n\nax.grid(False)\nax.set_xticks([])\nax.set_yticks([])\nax.axis('off')\nget_bbox(image_id, train_df, ax, color='red')\nax.imshow(img)\nplt.plot()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Some of the images without any wheat head","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"all_images = os.listdir(os.path.join(BASE_DIR, 'train'))\nall_images = set([x[:-4] for x in all_images])\n\nimages_with_bbox = set(list(train_df['image_id']))\n\nimages_without_bbox = list(all_images - images_with_bbox)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f'Total number of images without wheat heads: {len(images_without_bbox)}')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots(nrows=2, ncols=4, figsize=(20, 10))\ncount=20\nfor row in ax:\n    for col in row:\n        img_id = images_without_bbox[count]\n        img = plt.imread(f'{os.path.join(BASE_DIR, \"train\", img_id)}.jpg')\n        col.grid(False)\n        col.set_xticks([])\n        col.set_yticks([])\n        col.imshow(img)\n        count += 1\nplt.show()","execution_count":null,"outputs":[]},{"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}