{"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":"# Methodology\n* In this notebook we'll use pre computed **bounding box** from **YOLOv5** model to create **Cropped Dataset**.\n* To create **Bounding Boxes**, [Happywhale: BoundingBox [YOLOv5] 🐋🐬](https://www.kaggle.com/awsaf49/happywhale-boundingbox-yolov5) notebook was used.\n* You can choose **Image Size** for the image.\n* You can also tune the **Conf** Parameter to get the best suited bbox for this competiiton.","metadata":{}},{"cell_type":"markdown","source":"# Notebooks \nHere are some of my notebooks for this competition, **please upvote if you find them useful**\n* [Happywhale: BoundingBox [YOLOv5] 🐋🐬](https://www.kaggle.com/awsaf49/happywhale-boundingbox-yolov5)\n* [Happywhale: Cropped Dataset [YOLOv5] ✂️](https://www.kaggle.com/awsaf49/happywhale-cropped-dataset-yolov5)\n* [Happywhale: Data Distribution 🐋🐬](https://www.kaggle.com/awsaf49/happywhale-data-distribution)","metadata":{}},{"cell_type":"markdown","source":"# Install Libraries","metadata":{}},{"cell_type":"code","source":"!pip install -q bbox-utility # check https://github.com/awsaf49/bbox for source code","metadata":{"execution":{"iopub.status.busy":"2022-02-11T07:13:34.722442Z","iopub.execute_input":"2022-02-11T07:13:34.723380Z","iopub.status.idle":"2022-02-11T07:13:45.905166Z","shell.execute_reply.started":"2022-02-11T07:13:34.723264Z","shell.execute_reply":"2022-02-11T07:13:45.904150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport glob\nimport shutil\nfrom tqdm.notebook import tqdm\nfrom joblib import Parallel, delayed\n\nfrom bbox.utils import yolo2voc, draw_bboxes\n\nimport cv2\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-11T07:13:45.906862Z","iopub.execute_input":"2022-02-11T07:13:45.907100Z","iopub.status.idle":"2022-02-11T07:13:47.314821Z","shell.execute_reply.started":"2022-02-11T07:13:45.907072Z","shell.execute_reply":"2022-02-11T07:13:47.313943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Config","metadata":{}},{"cell_type":"code","source":"BASE_PATH = '../input/happywhale-boundingbox-yolov5-dataset'\nIMG_SIZE = (256, 256) # new image resolution\nCONF = 0.01 # confidence threshold for bbox","metadata":{"execution":{"iopub.status.busy":"2022-02-11T07:13:49.851436Z","iopub.execute_input":"2022-02-11T07:13:49.852122Z","iopub.status.idle":"2022-02-11T07:13:49.856832Z","shell.execute_reply.started":"2022-02-11T07:13:49.852074Z","shell.execute_reply":"2022-02-11T07:13:49.856058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Meta Data","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv(BASE_PATH+'/train.csv')\ndf['label_path'] = df['label_path'].map(lambda x: x.replace('/kaggle/working',BASE_PATH))\n\ntest_df = pd.read_csv(BASE_PATH+'/test.csv')\ntest_df['label_path'] = test_df['label_path'].map(lambda x: x.replace('/kaggle/working',BASE_PATH))","metadata":{"execution":{"iopub.status.busy":"2022-02-11T07:15:45.015711Z","iopub.execute_input":"2022-02-11T07:15:45.016147Z","iopub.status.idle":"2022-02-11T07:15:45.624378Z","shell.execute_reply.started":"2022-02-11T07:15:45.016099Z","shell.execute_reply":"2022-02-11T07:15:45.623481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Get Annotation","metadata":{}},{"cell_type":"code","source":"df = df.fillna('[]')\ntest_df = test_df.fillna('[]')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['bbox'] = df['bbox'].map(eval)\ntest_df['bbox'] = test_df['bbox'].map(eval)\n\ndf['conf'] = df['conf'].map(eval)\ntest_df['conf'] = test_df['conf'].map(eval)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Crop Utility","metadata":{}},{"cell_type":"code","source":"def load_image(path):\n    return cv2.imread(path)[...,::-1]\n\nnp.random.seed(32)\ncolors = [(np.random.randint(255), np.random.randint(255), np.random.randint(255))\\\n          for idx in range(1)]\n\ndef crop_image(row):\n    image_path = row['image_path']\n    if 'train' in image_path:\n        save_dir = '/tmp/train_images'\n    else:\n        save_dir = '/tmp/test_images'\n    img = load_image(image_path)\n    if len(row['bbox']): # if there is no bbox\n        bbox = row['bbox'][0]\n        conf = row['conf'][0]\n        if conf>=CONF: # don't crop for poor confident bboxes\n            xmin, ymin, xmax, ymax = bbox\n            img = img[ymin:ymax, xmin:xmax] # crop image\n    img = cv2.resize(img[...,::-1], dsize=IMG_SIZE, interpolation=cv2.INTER_AREA)\n    cv2.imwrite(f'{save_dir}/{row.image_id}', img) # save image in the new directory\n    return","metadata":{"execution":{"iopub.status.busy":"2022-02-11T07:17:11.955359Z","iopub.execute_input":"2022-02-11T07:17:11.955914Z","iopub.status.idle":"2022-02-11T07:17:11.966149Z","shell.execute_reply.started":"2022-02-11T07:17:11.955870Z","shell.execute_reply":"2022-02-11T07:17:11.965138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualize","metadata":{}},{"cell_type":"markdown","source":"## Train","metadata":{}},{"cell_type":"code","source":"for i in range(10):\n    row = df.sample(frac=1.0).iloc[i]\n    img = load_image(row.image_path)\n    bbox = row['bbox'][0]\n    xmin, ymin, xmax, ymax = bbox\n\n    plt.figure(figsize=(10, 5))\n    plt.subplot(1, 2, 1)\n#     plt.imshow(img)\n    dim = np.sqrt(np.prod(img.shape[:2]))\n    line_thickness = int(2/512*dim)\n    plt.imshow(\n            draw_bboxes(\n                img=img,\n                bboxes=np.array(row['bbox']),\n                classes=['Whales/Dolphin'],\n                class_ids=[0],\n                class_name=True,\n                colors=colors,\n                bbox_format=\"voc\",\n                line_thickness=line_thickness,\n            ))\n    plt.title('Before')\n    plt.axis('off')\n\n    plt.subplot(1, 2, 2)\n    plt.imshow(img[ymin:ymax, xmin:xmax])\n    plt.title('After')\n    plt.axis('off')\n    \n    plt.suptitle(f'id: {row.image_id}', y=0.94)\n    plt.tight_layout()\n    plt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-11T07:17:51.867193Z","iopub.execute_input":"2022-02-11T07:17:51.867478Z","iopub.status.idle":"2022-02-11T07:18:01.738928Z","shell.execute_reply.started":"2022-02-11T07:17:51.867441Z","shell.execute_reply":"2022-02-11T07:18:01.737813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Test","metadata":{}},{"cell_type":"code","source":"for i in range(10):\n    row = test_df.iloc[i]\n    img = load_image(row.image_path)\n    bbox = row['bbox'][0]\n    xmin, ymin, xmax, ymax = bbox\n\n    plt.figure(figsize=(10, 5))\n    plt.subplot(1, 2, 1)\n#     plt.imshow(img)\n    dim = np.sqrt(np.prod(img.shape[:2]))\n    line_thickness = int(2/512*dim)\n    plt.imshow(\n            draw_bboxes(\n                img=img,\n                bboxes=np.array(row['bbox']),\n                classes=['Whales/Dolphin'],\n                class_ids=[0],\n                class_name=True,\n                colors=colors,\n                bbox_format=\"voc\",\n                line_thickness=line_thickness,\n            ))\n    plt.title('Before')\n    plt.axis('off')\n\n    plt.subplot(1, 2, 2)\n    plt.imshow(img[ymin:ymax, xmin:xmax])\n    plt.title('After')\n    plt.axis('off')\n    \n    plt.tight_layout()\n    plt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-11T07:18:11.320303Z","iopub.execute_input":"2022-02-11T07:18:11.320641Z","iopub.status.idle":"2022-02-11T07:18:18.401882Z","shell.execute_reply.started":"2022-02-11T07:18:11.320558Z","shell.execute_reply":"2022-02-11T07:18:18.401006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create Save Directory","metadata":{}},{"cell_type":"code","source":"!mkdir -p /tmp/train_images && mkdir -p /tmp/test_images","metadata":{"execution":{"iopub.status.busy":"2022-02-07T14:49:01.976486Z","iopub.execute_input":"2022-02-07T14:49:01.976814Z","iopub.status.idle":"2022-02-07T14:49:02.771151Z","shell.execute_reply.started":"2022-02-07T14:49:01.976772Z","shell.execute_reply":"2022-02-07T14:49:02.770105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Crop","metadata":{}},{"cell_type":"code","source":"# Train\n_ = Parallel(n_jobs=-1, backend='threading')(delayed(crop_image)(row)\\\n                                         for _, row in tqdm(df.iterrows(), total=len(df), desc='train '))\n\n# Test\n_ = Parallel(n_jobs=-1, backend='threading')(delayed(crop_image)(row)\\\n                                         for _, row in tqdm(test_df.iterrows(), total=len(test_df), desc='test '))","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-02-07T15:16:06.352735Z","iopub.execute_input":"2022-02-07T15:16:06.353026Z"},"_kg_hide-input":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Save Meta Data","metadata":{}},{"cell_type":"code","source":"df.to_csv('train.csv',index=False)\ntest_df.to_csv('test.csv',index=False)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Archive Files","metadata":{}},{"cell_type":"code","source":"# Train\nshutil.make_archive(base_name='/kaggle/working/train_images',\n                    format='zip',\n                    root_dir='/tmp/',\n                    base_dir='train_images')\n# Test\nshutil.make_archive(base_name='/kaggle/working/test_images',\n                    format='zip',\n                    root_dir='/tmp/',\n                    base_dir='test_images')","metadata":{"_kg_hide-input":false,"trusted":true},"execution_count":null,"outputs":[]}]}