{"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":"## 이미지 개수확인","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# import useful tools\nfrom glob import glob\nfrom PIL import Image\nimport cv2\n\n# import data visualization\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nimport seaborn as sns\n\nfrom bokeh.plotting import figure\nfrom bokeh.io import output_notebook, show, output_file\nfrom bokeh.models import ColumnDataSource, HoverTool, Panel\nfrom bokeh.models.widgets import Tabs\n\n# import data augmentation\nimport albumentations as albu","metadata":{"execution":{"iopub.status.busy":"2023-04-20T02:53:14.998928Z","iopub.execute_input":"2023-04-20T02:53:14.999281Z","iopub.status.idle":"2023-04-20T02:53:17.002163Z","shell.execute_reply.started":"2023-04-20T02:53:14.999249Z","shell.execute_reply":"2023-04-20T02:53:17.001380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_DIR = '../input/global-wheat-detection/train/'\nTEST_DIR = '../input/global-wheat-detection/test/'\nTRAIN_CSV_PATH = '../input/global-wheat-detection/train.csv'\n\n# Glob the directories and get the lists of train and test images\ntrain_fns = glob(TRAIN_DIR + '*')\ntest_fns = glob(TEST_DIR + '*')","metadata":{"execution":{"iopub.status.busy":"2023-04-20T02:53:22.920358Z","iopub.execute_input":"2023-04-20T02:53:22.920868Z","iopub.status.idle":"2023-04-20T02:53:23.348164Z","shell.execute_reply.started":"2023-04-20T02:53:22.920835Z","shell.execute_reply":"2023-04-20T02:53:23.347119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Number of train images is {}'.format(len(train_fns)))\nprint('Number of test images is {}'.format(len(test_fns)))","metadata":{"execution":{"iopub.status.busy":"2023-04-20T02:53:26.984334Z","iopub.execute_input":"2023-04-20T02:53:26.984992Z","iopub.status.idle":"2023-04-20T02:53:26.990729Z","shell.execute_reply.started":"2023-04-20T02:53:26.984957Z","shell.execute_reply":"2023-04-20T02:53:26.989348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"박스가 없는 영상에 대한 정보도 all_train_images에 추가 ","metadata":{}},{"cell_type":"code","source":"# Load the dataframe with the bounding boxes\ntrain = pd.read_csv(TRAIN_CSV_PATH)\n\n# Create a dataframe with all train images\nall_train_images = pd.DataFrame([fns.split('/')[-1][:-4] for fns in train_fns])\nall_train_images.columns=['image_id']\n\n# Merge all train images with the bounding boxes dataframe\nall_train_images = all_train_images.merge(train, on='image_id', how='left')\n\n# replace nan values with zeros\nall_train_images['bbox'] = all_train_images.bbox.fillna('[0,0,0,0]')\n\n# split bbox column\nbbox_items = all_train_images.bbox.str.split(',', expand=True)\nall_train_images['bbox_xmin'] = bbox_items[0].str.strip('[ ').astype(float)\nall_train_images['bbox_ymin'] = bbox_items[1].str.strip(' ').astype(float)\nall_train_images['bbox_width'] = bbox_items[2].str.strip(' ').astype(float)\nall_train_images['bbox_height'] = bbox_items[3].str.strip(' ]').astype(float)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-20T02:53:30.187689Z","iopub.execute_input":"2023-04-20T02:53:30.188836Z","iopub.status.idle":"2023-04-20T02:53:31.129113Z","shell.execute_reply.started":"2023-04-20T02:53:30.188779Z","shell.execute_reply":"2023-04-20T02:53:31.127823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"박스가 없는 이미지 개수","metadata":{}},{"cell_type":"code","source":"print('{} images without wheat heads.'.format(len(all_train_images) - len(train)))","metadata":{"execution":{"iopub.status.busy":"2023-04-20T02:53:35.747485Z","iopub.execute_input":"2023-04-20T02:53:35.747832Z","iopub.status.idle":"2023-04-20T02:53:35.753848Z","shell.execute_reply.started":"2023-04-20T02:53:35.747801Z","shell.execute_reply":"2023-04-20T02:53:35.752808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 이미지 별 박스 개수 확인","metadata":{}},{"cell_type":"code","source":"# compute the number of bounding boxes per train image\nall_train_images['count'] = all_train_images.apply(lambda row: 1 if np.isfinite(row.width) else 0, axis=1)\ntrain_images_count = all_train_images.groupby('image_id').sum().reset_index()","metadata":{"execution":{"iopub.status.busy":"2023-04-20T02:53:38.417887Z","iopub.execute_input":"2023-04-20T02:53:38.418404Z","iopub.status.idle":"2023-04-20T02:53:40.668882Z","shell.execute_reply.started":"2023-04-20T02:53:38.418374Z","shell.execute_reply":"2023-04-20T02:53:40.667856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\npx.histogram(train_images_count, x=\"count\", title=\"Number of boxes by images \")","metadata":{"execution":{"iopub.status.busy":"2023-04-20T02:53:42.002194Z","iopub.execute_input":"2023-04-20T02:53:42.002966Z","iopub.status.idle":"2023-04-20T02:53:45.431461Z","shell.execute_reply.started":"2023-04-20T02:53:42.002925Z","shell.execute_reply":"2023-04-20T02:53:45.430463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.histogram(train_images_count, x=\"count\", title=\"Number of boxes by images \", nbins=40)\nfig.update_layout(bargap=0.1)","metadata":{"execution":{"iopub.status.busy":"2023-04-20T02:53:49.969827Z","iopub.execute_input":"2023-04-20T02:53:49.970177Z","iopub.status.idle":"2023-04-20T02:53:50.020329Z","shell.execute_reply.started":"2023-04-20T02:53:49.970149Z","shell.execute_reply":"2023-04-20T02:53:50.019369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"박스개수는 보통 20~55개 정도. 많으면 100개가 넘어가는 경우도 있음","metadata":{}},{"cell_type":"markdown","source":"# 이미지 시각화","metadata":{}},{"cell_type":"code","source":"def get_all_bboxes(df, image_id):\n    image_bboxes = df[df.image_id == image_id]\n    \n    bboxes = []\n    for _,row in image_bboxes.iterrows():\n        bboxes.append((row.bbox_xmin, row.bbox_ymin, row.bbox_width, row.bbox_height))\n        \n    return bboxes\n\ndef plot_image_examples(df, rows=3, cols=3, title='Image examples'):\n    fig, axs = plt.subplots(rows, cols, figsize=(10,10))\n    for row in range(rows):\n        for col in range(cols):\n            idx = np.random.randint(len(df), size=1)[0]\n            img_id = df.iloc[idx].image_id\n            \n            img = Image.open(TRAIN_DIR + img_id + '.jpg')\n            axs[row, col].imshow(img)\n            \n            bboxes = get_all_bboxes(df, img_id)\n            \n            for bbox in bboxes:\n                rect = patches.Rectangle((bbox[0],bbox[1]),bbox[2],bbox[3],linewidth=1,edgecolor='r',facecolor='none')\n                axs[row, col].add_patch(rect)\n            \n            axs[row, col].axis('off')\n            \n    plt.suptitle(title)","metadata":{"execution":{"iopub.status.busy":"2023-04-20T02:53:58.019726Z","iopub.execute_input":"2023-04-20T02:53:58.020071Z","iopub.status.idle":"2023-04-20T02:53:58.030090Z","shell.execute_reply.started":"2023-04-20T02:53:58.020038Z","shell.execute_reply":"2023-04-20T02:53:58.028711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 일반적인 경우","metadata":{}},{"cell_type":"code","source":"plot_image_examples(all_train_images)","metadata":{"execution":{"iopub.status.busy":"2023-04-20T02:54:03.094346Z","iopub.execute_input":"2023-04-20T02:54:03.094899Z","iopub.status.idle":"2023-04-20T02:54:05.990650Z","shell.execute_reply.started":"2023-04-20T02:54:03.094861Z","shell.execute_reply":"2023-04-20T02:54:05.989445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 박스가 적은 경우","metadata":{}},{"cell_type":"code","source":"less_spikes_ids = train_images_count[train_images_count['count'] < 10].image_id\nplot_image_examples(all_train_images[all_train_images.image_id.isin(less_spikes_ids)])","metadata":{"execution":{"iopub.status.busy":"2023-04-20T02:54:10.613717Z","iopub.execute_input":"2023-04-20T02:54:10.614067Z","iopub.status.idle":"2023-04-20T02:54:13.085784Z","shell.execute_reply.started":"2023-04-20T02:54:10.614036Z","shell.execute_reply":"2023-04-20T02:54:13.084678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 박스가 많은 경우","metadata":{}},{"cell_type":"code","source":"less_spikes_ids = train_images_count[train_images_count['count'] > 80].image_id\nplot_image_examples(all_train_images[all_train_images.image_id.isin(less_spikes_ids)])","metadata":{"execution":{"iopub.status.busy":"2023-04-20T02:54:22.654266Z","iopub.execute_input":"2023-04-20T02:54:22.654618Z","iopub.status.idle":"2023-04-20T02:54:25.988258Z","shell.execute_reply.started":"2023-04-20T02:54:22.654588Z","shell.execute_reply":"2023-04-20T02:54:25.987463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 박스 크기 기준 확인","metadata":{}},{"cell_type":"code","source":"# compute bounding box areas\nall_train_images['bbox_area'] = all_train_images['bbox_width'] * all_train_images['bbox_height']","metadata":{"execution":{"iopub.status.busy":"2023-04-20T02:54:53.685105Z","iopub.execute_input":"2023-04-20T02:54:53.685473Z","iopub.status.idle":"2023-04-20T02:54:53.691831Z","shell.execute_reply.started":"2023-04-20T02:54:53.685442Z","shell.execute_reply":"2023-04-20T02:54:53.690564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 박스가 큰 경우","metadata":{}},{"cell_type":"code","source":"large_boxes_ids = all_train_images[all_train_images['bbox_area'] > 200000].image_id\nplot_image_examples(all_train_images[all_train_images.image_id.isin(large_boxes_ids)], title='Example images with large bbox area')","metadata":{"execution":{"iopub.status.busy":"2023-04-20T02:54:56.866580Z","iopub.execute_input":"2023-04-20T02:54:56.867002Z","iopub.status.idle":"2023-04-20T02:54:59.441488Z","shell.execute_reply.started":"2023-04-20T02:54:56.866969Z","shell.execute_reply":"2023-04-20T02:54:59.440518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 박스가 작은 경우","metadata":{}},{"cell_type":"code","source":"small_boxes_ids = all_train_images[all_train_images['bbox_area'] < 50].image_id\nplot_image_examples(all_train_images[all_train_images.image_id.isin(small_boxes_ids)], title='Example images with small bbox area')","metadata":{"execution":{"iopub.status.busy":"2023-04-20T02:55:05.630325Z","iopub.execute_input":"2023-04-20T02:55:05.630685Z","iopub.status.idle":"2023-04-20T02:55:08.590852Z","shell.execute_reply.started":"2023-04-20T02:55:05.630656Z","shell.execute_reply":"2023-04-20T02:55:08.589606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}