{"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-11T00:28:22.354229Z","iopub.execute_input":"2023-04-11T00:28:22.355138Z","iopub.status.idle":"2023-04-11T00:28:25.325387Z","shell.execute_reply.started":"2023-04-11T00:28:22.355098Z","shell.execute_reply":"2023-04-11T00:28:25.323786Z"},"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-11T00:28:25.327411Z","iopub.execute_input":"2023-04-11T00:28:25.328071Z","iopub.status.idle":"2023-04-11T00:28:25.692428Z","shell.execute_reply.started":"2023-04-11T00:28:25.328032Z","shell.execute_reply":"2023-04-11T00:28:25.691024Z"},"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-11T00:28:25.694684Z","iopub.execute_input":"2023-04-11T00:28:25.695658Z","iopub.status.idle":"2023-04-11T00:28:25.704412Z","shell.execute_reply.started":"2023-04-11T00:28:25.695605Z","shell.execute_reply":"2023-04-11T00:28:25.702789Z"},"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-11T00:29:20.814364Z","iopub.execute_input":"2023-04-11T00:29:20.814813Z","iopub.status.idle":"2023-04-11T00:29:22.041578Z","shell.execute_reply.started":"2023-04-11T00:29:20.814778Z","shell.execute_reply":"2023-04-11T00:29:22.040336Z"},"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-11T00:33:04.940362Z","iopub.execute_input":"2023-04-11T00:33:04.940844Z","iopub.status.idle":"2023-04-11T00:33:04.948152Z","shell.execute_reply.started":"2023-04-11T00:33:04.940804Z","shell.execute_reply":"2023-04-11T00:33:04.946776Z"},"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-11T00:34:58.964811Z","iopub.execute_input":"2023-04-11T00:34:58.965246Z","iopub.status.idle":"2023-04-11T00:35:01.574762Z","shell.execute_reply.started":"2023-04-11T00:34:58.965210Z","shell.execute_reply":"2023-04-11T00:35:01.573473Z"},"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-11T00:36:57.304155Z","iopub.execute_input":"2023-04-11T00:36:57.304604Z","iopub.status.idle":"2023-04-11T00:36:58.916556Z","shell.execute_reply.started":"2023-04-11T00:36:57.304563Z","shell.execute_reply":"2023-04-11T00:36:58.915006Z"},"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-11T00:45:49.565445Z","iopub.execute_input":"2023-04-11T00:45:49.565897Z","iopub.status.idle":"2023-04-11T00:45:49.624870Z","shell.execute_reply.started":"2023-04-11T00:45:49.565859Z","shell.execute_reply":"2023-04-11T00:45:49.623778Z"},"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-11T00:56:13.012267Z","iopub.execute_input":"2023-04-11T00:56:13.013242Z","iopub.status.idle":"2023-04-11T00:56:13.024864Z","shell.execute_reply.started":"2023-04-11T00:56:13.013197Z","shell.execute_reply":"2023-04-11T00:56:13.023609Z"},"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-11T00:56:36.382036Z","iopub.execute_input":"2023-04-11T00:56:36.383186Z","iopub.status.idle":"2023-04-11T00:56:40.073442Z","shell.execute_reply.started":"2023-04-11T00:56:36.383144Z","shell.execute_reply":"2023-04-11T00:56:40.071845Z"},"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-11T00:58:24.370086Z","iopub.execute_input":"2023-04-11T00:58:24.371026Z","iopub.status.idle":"2023-04-11T00:58:27.312386Z","shell.execute_reply.started":"2023-04-11T00:58:24.370984Z","shell.execute_reply":"2023-04-11T00:58:27.310988Z"},"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-11T00:58:49.527339Z","iopub.execute_input":"2023-04-11T00:58:49.527764Z","iopub.status.idle":"2023-04-11T00:58:53.912417Z","shell.execute_reply.started":"2023-04-11T00:58:49.527711Z","shell.execute_reply":"2023-04-11T00:58:53.911119Z"},"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-11T01:40:38.722072Z","iopub.execute_input":"2023-04-11T01:40:38.723117Z","iopub.status.idle":"2023-04-11T01:40:38.730245Z","shell.execute_reply.started":"2023-04-11T01:40:38.723061Z","shell.execute_reply":"2023-04-11T01:40:38.728991Z"},"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-11T01:40:43.297957Z","iopub.execute_input":"2023-04-11T01:40:43.298461Z","iopub.status.idle":"2023-04-11T01:40:46.344366Z","shell.execute_reply.started":"2023-04-11T01:40:43.298418Z","shell.execute_reply":"2023-04-11T01:40:46.342559Z"},"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-11T01:42:03.030959Z","iopub.execute_input":"2023-04-11T01:42:03.031398Z","iopub.status.idle":"2023-04-11T01:42:06.649096Z","shell.execute_reply.started":"2023-04-11T01:42:03.031363Z","shell.execute_reply":"2023-04-11T01:42:06.648291Z"},"trusted":true},"execution_count":null,"outputs":[]}]}