{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nfrom math import log2, ceil\nfrom pathlib import Path\n\nimport pandas as pd \nfrom matplotlib import pyplot as plt \nimport numpy as np\nimport cv2\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Import train.csv","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"path_dir_project = Path('/kaggle/input/global-wheat-detection')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"df_train = pd.read_csv(path_dir_project/'train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bboxes = np.stack(df_train['bbox'].apply(lambda x: np.fromstring(x[1:-1], sep=','))).astype(int)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"display(bboxes[:5])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i, column in enumerate(['x_min', 'y_min', 'width', 'height']):\n    df_train[column] = bboxes[:,i]\n    \ndf_train[\"x_max\"] = df_train.apply(lambda col: col.x_min + col.width, axis=1)\ndf_train[\"y_max\"] = df_train.apply(lambda col: col.y_min + col.height, axis = 1)\ndf_train.drop(columns=['bbox'], inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train[['x_min', 'y_min', 'width', 'height', 'x_max', 'y_max']].head(5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train['area'] = df_train['width'] * df_train['height']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train[['x_min', 'y_min', 'width', 'height', 'x_max', 'y_max', 'area']].head(5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(df_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train['area'].describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sr_area = df_train['area'].value_counts()\ndisplay(sr_area)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = plt.figure(figsize=(20, 5))\n\nplt.bar(sr_area.sort_index().index, sr_area.sort_index().values)\n\nplt.title('Bounding-box distribution')\nplt.xlabel('Area [dots]')\nplt.ylabel('counts')\n\nplt.show()\nplt.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def calc_starges(num_data):\n    return int(np.round(log2(num_data) + 1, 0))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = plt.figure(figsize=(20, 5))\n\nplt.hist(df_train['area'], bins=calc_starges(len(df_train)))\n\nplt.title('Bounding-box distribution')\nplt.xlabel('Area [dots]')\nplt.ylabel('counts')\n\nplt.show()\nplt.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = plt.figure(figsize=(20, 5))\n\nplt.hist(df_train['area'], bins=calc_starges(len(df_train)))\n\nplt.title('Bounding-box distribution')\nplt.xlabel('Area [dots]')\nplt.ylabel('counts')\nplt.yscale('log')\n\nplt.show()\nplt.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = plt.figure(figsize=(20, 5))\n\nplt.bar(sr_area.sort_index().index, sr_area.sort_index().values)\n\nplt.title('Bounding-box distribution')\nplt.xlabel('Area [dots]')\nplt.ylabel('counts')\nplt.xscale('log')\nplt.yscale('log')\nplt.show()\nplt.close()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Image with max area bbox","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"query_max_area = df_train['area'] == df_train['area'].max()\nimage_id_max_area = df_train[query_max_area]['image_id']\nprint(image_id_max_area)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig_filename = f'{image_id_max_area.values[0]}.jpg'\nfilepath = path_dir_project/f'train/{fig_filename}'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image = cv2.imread(str(filepath))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = plt.figure(figsize=(8, 8))\n\nplt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\nplt.show()\nplt.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"query_target = df_train['image_id'] == image_id_max_area.values[0]\nbboxes_target = df_train[query_target]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(bboxes_target)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"bboxes_target.head(5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_bboxes = image.copy()\nfor idx in range(len(bboxes_target)): \n    bbox = bboxes_target.iloc[idx][['x_min', 'y_min', 'x_max', 'y_max']].values\n    image_bboxes = cv2.rectangle(image_bboxes, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 0, 255), 3)\n    \nfig = plt.figure(figsize=(8, 8))\n\nplt.imshow(cv2.cvtColor(image_bboxes, cv2.COLOR_BGR2RGB))\nplt.show()\nplt.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"query_large = df_train['area'] >= 100000 \ndf_train_large = df_train[query_large]\ndisplay(df_train_large)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(df_train[query_large]['image_id'].unique())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_ids_large_area = df_train[query_large]['image_id'].unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def draw_bbox(image_id, df_train):\n    fig_filename = f'{image_id}.jpg'\n    filepath = path_dir_project/f'train/{fig_filename}'\n\n    image = cv2.imread(str(filepath))\n\n    query_target = df_train['image_id'] == image_id\n    bboxes_target = df_train[query_target][['x_min', 'y_min', 'x_max', 'y_max']]\n\n    for idx in range(len(bboxes_target)): \n        bbox = bboxes_target.iloc[idx].values\n        image = cv2.rectangle(image, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 0, 255), 5)\n        \n    return image\n\ndef plot_images(df_train, image_ids):\n    num_images = len(image_ids)\n    num_row = ceil(num_images / 2)\n    if num_images >= 2:\n        fig, axes = plt.subplots(num_row, 2, figsize=(20, 10 * num_row))\n        for idx_img, image_id in enumerate(image_ids):\n            image_bboxes = draw_bbox(image_id, df_train)\n            axes[idx_img // 2][idx_img % 2].imshow(cv2.cvtColor(image_bboxes, cv2.COLOR_BGR2RGB))\n            axes[idx_img // 2][idx_img % 2].set_title(image_id)\n            \n    else: \n        image_id = image_ids[0]\n        fig = plt.figure(figsize=(7, 6))\n\n        image_bboxes = draw_bbox(image_id, df_train)\n        plt.imshow(cv2.cvtColor(image_bboxes, cv2.COLOR_BGR2RGB))\n        plt.title(image_id)        \n\n    plt.show()\n    plt.close()    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_images(df_train, image_ids_large_area)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"images_with_anormally_large_bbox = [\n    '41c0123cc', \n    'a1321ca95', \n    '2cc75e9f5', \n    '42e6efaaa', \n    '409a8490c', \n    'd7a02151d', \n    'd067ac2b1',\n    'd60e832a5'\n]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"query_min = df_train['area'] == df_train['area'].min()\ndf_train_min = df_train[query_min]\ndisplay(df_train_min)\n\nimage_ids_min_area = df_train_min['image_id'].unique()\ndisplay(len(image_ids_min_area))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_images(df_train, image_ids_min_area)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"query_smaller = df_train['area'] <= 100\ndf_train_smaller = df_train[query_smaller]\ndisplay(df_train_smaller)\n\nimage_ids_smaller_area = df_train_smaller['image_id'].unique()\ndisplay(len(image_ids_smaller_area))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_images(df_train, image_ids_smaller_area)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"images_with_anormally_small_bbox = [\n    '809d816dd', \n    '8e1543437', \n    'c3fa071f6', \n    '666e0a853', \n    'b2f2a5dfd', \n    '6284044ed', \n    '0a97d54ae', \n    'ad256655b', \n    '088d3df51', \n    '233cb8750', \n    '6a8522f06', \n    'ca5e51e59', \n    '5e182e37e', \n    'ce3999eb9', \n    'd0912a485', \n    'f24698e88', \n    'bf2027ed3', \n    '4ffeedce2', \n    '229716799'\n]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"query_small = (df_train['area'] > 100) & (400 >= df_train['area'])\ndf_train_small = df_train[query_small]\ndisplay(df_train_small)\n\nimage_ids_small_area = df_train_small['image_id'].unique()\ndisplay(len(image_ids_small_area))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ids_random = np.random.choice(image_ids_small_area, 10)\nplot_images(df_train, ids_random)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"## limit to width and height less than 50 dots","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"query_small_width_height = (df_train['width'] <= 50) & (df_train['height'] <= 50)\ndf_train_query_small_width_height = df_train[query_small_width_height]\ndisplay(df_train_query_small_width_height)\n\nimage_ids_small_width_height = df_train_query_small_width_height['image_id'].unique()\ndisplay(len(image_ids_small_width_height))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ids_random_small_width_height = np.random.choice(image_ids_small_width_height, 10)\nplot_images(df_train, ids_random_small_width_height)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"query_small_width_height_or = (df_train['width'] <= 50) | (df_train['height'] <= 50)\ndf_train_query_small_width_height_or = df_train[query_small_width_height_or]\ndisplay(df_train_query_small_width_height_or)\n\nimage_ids_small_width_height_or = df_train_query_small_width_height_or['image_id'].unique()\ndisplay(len(image_ids_small_width_height_or))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ids_random_small_width_height_or = np.random.choice(image_ids_small_width_height_or, 10)\nplot_images(df_train, ids_random_small_width_height_or)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"...it's difficult to detect with small width and height.\nThus, I detect tiny bboxes by area and my eyes directly now.","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}