{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from PIL import Image\nimport matplotlib.pyplot as plt\n\nimport matplotlib.patches as patches\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_bbox(bboxes, col, color='red', bbox_format='coco'):\n    \n    for i in range(len(bboxes)):\n            rect = patches.Rectangle(\n                (bboxes[i][0], bboxes[i][1]),\n                bboxes[i][2], \n                bboxes[i][3], \n                linewidth=2, \n                edgecolor=color, \n                facecolor='none')\n\n            col.add_patch(rect)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import re \ntrain_df = pd.read_csv('/kaggle/input/global-wheat-detection/train.csv')\ntrain_df['x'] = -1\ntrain_df['y'] = -1\ntrain_df['w'] = -1\ntrain_df['h'] = -1\n\ndef expand_bbox(x):\n    r = np.array(re.findall(\"([0-9]+[.]?[0-9]*)\", x))\n    if len(r) == 0:\n        r = [-1, -1, -1, -1]\n    return r\n\ntrain_df[['x', 'y', 'w', 'h']] = np.stack(train_df['bbox'].apply(lambda x: expand_bbox(x)))\ntrain_df.drop(columns=['bbox'], inplace=True)\ntrain_df['x'] = train_df['x'].astype(np.float)\ntrain_df['y'] = train_df['y'].astype(np.float)\ntrain_df['w'] = train_df['w'].astype(np.float)\ntrain_df['h'] = train_df['h'].astype(np.float)\n\ntrain_df.head(5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import cv2 \nimport os\nBASE_DIR = '/kaggle/input/global-wheat-detection'\nimage_id = '7264c58b9'\nimage = cv2.imread(os.path.join(BASE_DIR, 'train', f'{image_id}.jpg'), cv2.IMREAD_COLOR)\nimage = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)\nimage = image/255\nplt.figure(figsize = (10, 10))\nplt.imshow(image)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"boxes = train_df[train_df['image_id'] == image_id][['x', 'y', 'w', 'h']].astype(np.int32).values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots(nrows=1, figsize=(8, 8))\n\nget_bbox(boxes, ax, color='red', bbox_format='coco')\nax.title.set_text('Image')\nax.imshow(image)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import albumentations as A\nfrom albumentations.pytorch.transforms import ToTensorV2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"aug = A.Compose([\n        A.Resize(1024, 1024),   \n        A.VerticalFlip(1),\n       ], bbox_params={'format': 'coco', 'label_fields': ['labels']})\n\naug_result = aug(image=image, bboxes=boxes, labels=np.ones(len(boxes)))\n\n\nfig, ax = plt.subplots(nrows=1, ncols=2, figsize=(16, 16))\nget_bbox(boxes, ax[0], color='red', bbox_format='coco')\nax[0].title.set_text('Original Image')\nax[0].imshow(image)\n\nget_bbox(aug_result['bboxes'], ax[1], color='red', bbox_format='coco')\nax[1].title.set_text('verticle flip')\nax[1].imshow(aug_result['image'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"aug = A.Compose([\n        A.Resize(1024, 1024),   \n        A.HorizontalFlip(p=1),\n       ], bbox_params={'format': 'coco', 'label_fields': ['labels']})\n\naug_result = aug(image=image, bboxes=boxes, labels=np.ones(len(boxes)))\n\n\nfig, ax = plt.subplots(nrows=1, ncols=2, figsize=(16, 16))\nget_bbox(boxes, ax[0], color='red', bbox_format='coco')\nax[0].title.set_text('Original Image')\nax[0].imshow(image)\n\nget_bbox(aug_result['bboxes'], ax[1], color='red', bbox_format='coco')\nax[1].title.set_text('horizontal flip')\nax[1].imshow(aug_result['image'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"aug = A.Compose([\n        A.Resize(1024, 1024),   \n        A.ShiftScaleRotate(p=1),\n       ], bbox_params={'format': 'coco', 'label_fields': ['labels']})\n\naug_result = aug(image=image, bboxes=boxes, labels=np.ones(len(boxes)))\n\n\nfig, ax = plt.subplots(nrows=1, ncols=2, figsize=(16, 16))\nget_bbox(boxes, ax[0], color='red', bbox_format='coco')\nax[0].title.set_text('Original Image')\nax[0].imshow(image)\n\nget_bbox(aug_result['bboxes'], ax[1], color='red', bbox_format='coco')\nax[1].title.set_text('shift scale rotate')\nax[1].imshow(aug_result['image'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"aug = A.Compose([\n        A.Resize(1024, 1024),   \n        A.RandomBrightness(p=1,limit = 0.9),\n       ], bbox_params={'format': 'coco', 'label_fields': ['labels']})\n\naug_result = aug(image=image, bboxes=boxes, labels=np.ones(len(boxes)))\n\n\nfig, ax = plt.subplots(nrows=1, ncols=2, figsize=(16, 16))\nget_bbox(boxes, ax[0], color='red', bbox_format='coco')\nax[0].title.set_text('Original Image')\nax[0].imshow(image)\n\nget_bbox(aug_result['bboxes'], ax[1], color='red', bbox_format='coco')\nax[1].title.set_text('horizontal flip')\nax[1].imshow(aug_result['image'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"aug = A.Compose([\n        A.Resize(1024, 1024),   \n        A.RandomContrast(p=1,limit = 0.9),\n       ], bbox_params={'format': 'coco', 'label_fields': ['labels']})\n\naug_result = aug(image=image, bboxes=boxes, labels=np.ones(len(boxes)))\n\n\nfig, ax = plt.subplots(nrows=1, ncols=2, figsize=(16, 16))\nget_bbox(boxes, ax[0], color='red', bbox_format='coco')\nax[0].title.set_text('Original Image')\nax[0].imshow(image)\n\nget_bbox(aug_result['bboxes'], ax[1], color='red', bbox_format='coco')\nax[1].title.set_text('horizontal flip')\nax[1].imshow(aug_result['image'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"aug = A.Compose([\n        A.Resize(1024, 1024),   \n        A.GaussianBlur(p=1,blur_limit = 14),\n       ], bbox_params={'format': 'coco', 'label_fields': ['labels']})\n\naug_result = aug(image=image, bboxes=boxes, labels=np.ones(len(boxes)))\n\n\nfig, ax = plt.subplots(nrows=1, ncols=2, figsize=(16, 16))\nget_bbox(boxes, ax[0], color='red', bbox_format='coco')\nax[0].title.set_text('Original Image')\nax[0].imshow(image)\n\nget_bbox(aug_result['bboxes'], ax[1], color='red', bbox_format='coco')\nax[1].title.set_text('horizontal flip')\nax[1].imshow(aug_result['image'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"aug = A.Compose([\n        A.Resize(1024, 1024),   \n        A.ChannelShuffle(p=1),\n       ], bbox_params={'format': 'coco', 'label_fields': ['labels']})\n\naug_result = aug(image=image, bboxes=boxes, labels=np.ones(len(boxes)))\n\n\nfig, ax = plt.subplots(nrows=1, ncols=2, figsize=(16, 16))\nget_bbox(boxes, ax[0], color='red', bbox_format='coco')\nax[0].title.set_text('Original Image')\nax[0].imshow(image)\n\nget_bbox(aug_result['bboxes'], ax[1], color='red', bbox_format='coco')\nax[1].title.set_text('horizontal flip')\nax[1].imshow(aug_result['image'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"aug = A.Compose([\n        A.Resize(1024, 1024),   \n        A.RandomSnow(p=1),\n       ], bbox_params={'format': 'coco', 'label_fields': ['labels']})\n\naug_result = aug(image=image, bboxes=boxes, labels=np.ones(len(boxes)))\n\n\nfig, ax = plt.subplots(nrows=1, ncols=2, figsize=(16, 16))\nget_bbox(boxes, ax[0], color='red', bbox_format='coco')\nax[0].title.set_text('Original Image')\nax[0].imshow(image)\n\nget_bbox(aug_result['bboxes'], ax[1], color='red', bbox_format='coco')\nax[1].title.set_text('horizontal flip')\nax[1].imshow(aug_result['image'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"aug = A.Compose([\n        A.Resize(1024, 1024),   \n        A.RandomRain(p=1),\n       ], bbox_params={'format': 'coco', 'label_fields': ['labels']})\n\naug_result = aug(image=image, bboxes=boxes, labels=np.ones(len(boxes)))\n\n\nfig, ax = plt.subplots(nrows=1, ncols=2, figsize=(16, 16))\nget_bbox(boxes, ax[0], color='red', bbox_format='coco')\nax[0].title.set_text('Original Image')\nax[0].imshow(image)\n\nget_bbox(aug_result['bboxes'], ax[1], color='red', bbox_format='coco')\nax[1].title.set_text('horizontal flip')\nax[1].imshow(aug_result['image'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"aug = A.Compose([\n        A.Resize(1024, 1024),   \n        A.RandomFog(p=1),\n       ], bbox_params={'format': 'coco', 'label_fields': ['labels']})\n\naug_result = aug(image=image, bboxes=boxes, labels=np.ones(len(boxes)))\n\n\nfig, ax = plt.subplots(nrows=1, ncols=2, figsize=(16, 16))\nget_bbox(boxes, ax[0], color='red', bbox_format='coco')\nax[0].title.set_text('Original Image')\nax[0].imshow(image)\n\nget_bbox(aug_result['bboxes'], ax[1], color='red', bbox_format='coco')\nax[1].title.set_text('horizontal flip')\nax[1].imshow(aug_result['image'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"aug = A.Compose([\n        A.Resize(1024, 1024),   \n        A.RandomShadow(p=1),\n       ], bbox_params={'format': 'coco', 'label_fields': ['labels']})\n\naug_result = aug(image=image, bboxes=boxes, labels=np.ones(len(boxes)))\n\n\nfig, ax = plt.subplots(nrows=1, ncols=2, figsize=(16, 16))\nget_bbox(boxes, ax[0], color='red', bbox_format='coco')\nax[0].title.set_text('Original Image')\nax[0].imshow(image)\n\nget_bbox(aug_result['bboxes'], ax[1], color='red', bbox_format='coco')\nax[1].title.set_text('horizontal flip')\nax[1].imshow(aug_result['image'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"aug = A.Compose([A.RandomSizedCrop(min_max_height=(600, 600), height=1024, width=1024, p=0.5), ], bbox_params={'format': 'coco', 'label_fields': ['labels']})\n\naug_result = aug(image=image, bboxes=boxes, labels=np.ones(len(boxes)))\n\n\nfig, ax = plt.subplots(nrows=1, ncols=2, figsize=(16, 16))\nget_bbox(boxes, ax[0], color='red', bbox_format='coco')\nax[0].title.set_text('Original Image')\nax[0].imshow(image)\n\nget_bbox(aug_result['bboxes'], ax[1], color='red', bbox_format='coco')\nax[1].title.set_text('horizontal flip')\nax[1].imshow(aug_result['image'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"aug = A.Compose([A.Cutout(num_holes=10, max_h_size=64, max_w_size=64, fill_value=0, p=0.5), ], bbox_params={'format': 'coco', 'label_fields': ['labels']})\n\naug_result = aug(image=image, bboxes=boxes, labels=np.ones(len(boxes)))\n\n\nfig, ax = plt.subplots(nrows=1, ncols=2, figsize=(16, 16))\nget_bbox(boxes, ax[0], color='red', bbox_format='coco')\nax[0].title.set_text('Original Image')\nax[0].imshow(image)\n\nget_bbox(aug_result['bboxes'], ax[1], color='red', bbox_format='coco')\nax[1].title.set_text('horizontal flip')\nax[1].imshow(aug_result['image'])\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}