{"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":"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\nimport cv2\nimport time\nfrom matplotlib import pyplot as plt\n\nimport albumentations as A\nfrom albumentations.pytorch.transforms import ToTensorV2\n\nimport torch\nimport torchvision\nfrom torch.utils.data import DataLoader, Dataset\nfrom torchvision.models.detection.faster_rcnn import FastRCNNPredictor\nfrom torchvision.models.detection import FasterRCNN\n\nfrom sklearn.model_selection import train_test_split\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\nimport 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 20GB 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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-12T13:07:52.044997Z","iopub.execute_input":"2021-12-12T13:07:52.045276Z","iopub.status.idle":"2021-12-12T13:07:52.05234Z","shell.execute_reply.started":"2021-12-12T13:07:52.045249Z","shell.execute_reply":"2021-12-12T13:07:52.051732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DEVICE = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')\n\n#image file path\nBASE_DIR = \"../input/tensorflow-great-barrier-reef/train_images/\"\n\nNUM_EPOCHS = 5","metadata":{"execution":{"iopub.status.busy":"2021-12-12T13:07:52.103624Z","iopub.execute_input":"2021-12-12T13:07:52.104096Z","iopub.status.idle":"2021-12-12T13:07:52.108612Z","shell.execute_reply.started":"2021-12-12T13:07:52.104046Z","shell.execute_reply":"2021-12-12T13:07:52.107843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_all = pd.read_csv(\"/kaggle/input/tensorflow-great-barrier-reef/train.csv\")\ntrain_all.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-12T13:07:52.252164Z","iopub.execute_input":"2021-12-12T13:07:52.252426Z","iopub.status.idle":"2021-12-12T13:07:52.299801Z","shell.execute_reply.started":"2021-12-12T13:07:52.252398Z","shell.execute_reply":"2021-12-12T13:07:52.299019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(\"/kaggle/input/tensorflow-great-barrier-reef/train.csv\")\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-12T13:07:52.373191Z","iopub.execute_input":"2021-12-12T13:07:52.373449Z","iopub.status.idle":"2021-12-12T13:07:52.414585Z","shell.execute_reply.started":"2021-12-12T13:07:52.373421Z","shell.execute_reply":"2021-12-12T13:07:52.413855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_all['image_path'] = \"video_\" + train_all['video_id'].astype(str) + \"/\" + train_all['video_frame'].astype(str) + \".jpg\"\ntrain_all.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-12T13:07:52.484657Z","iopub.execute_input":"2021-12-12T13:07:52.484956Z","iopub.status.idle":"2021-12-12T13:07:52.573583Z","shell.execute_reply.started":"2021-12-12T13:07:52.484915Z","shell.execute_reply":"2021-12-12T13:07:52.572732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomDataset(Dataset):\n    '''\n      data : train, valid, test dataset\n      transforms: data transform (resize, crop, Totensor, etc,,,)\n    '''\n\n    def __init__(self, data, transforms=None):\n        super(CustomDataset).__init__()\n        self.data = data\n        self.transforms = transforms\n\n    def __getitem__(self, index: int):\n        \n        image_path = self.data.iloc[index]\n        \n        image = cv2.imread(os.path.join(BASE_DIR, image_path['image_path']))\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)\n        image /= 255.0\n\n        \n\n        boxes = pd.DataFrame(image_path['annotations'], columns=['x', 'y', 'width', 'height']).astype(float).values # bbox -> bounding box\n\n        # boxex (x_min, y_min, x_max, y_max)\n        boxes[:, 2] = boxes[:, 0] + boxes[:, 2]\n        boxes[:, 3] = boxes[:, 1] + boxes[:, 3]\n        \n        n_boxes = boxes.shape[0] # bounding boxes\n        \n        areas = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])\n        \n\n        target = {\n            'boxes': torch.as_tensor(boxes, dtype=torch.float32),\n            'area': torch.as_tensor(area, dtype=torch.float32),\n            \n            'image_id': torch.tensor([i]),\n            \n            # There is only one class\n            'labels': torch.ones((n_boxes,), dtype=torch.int64),\n            \n            # Suppose all instances are not crowd\n            'iscrowd': torch.zeros((n_boxes,), dtype=torch.int64)            \n        }\n\n        # transform\n        if self.transforms:\n            sample = {\n                'image': image,\n                'bboxes': target['boxes'],\n                'labels': target['labels']\n            }\n            sample = self.transforms(**sample)\n            image = sample['image']\n            if n_boxes > 0:\n                target['boxes'] = torch.stack(tuple(map(torch.tensor, zip(*sample['bboxes'])))).permute(1, 0)\n            else:\n                image = ToTensorV2(p=1.0)(image=image)['image']\n\n\n        return image, target\n    \n    def __len__(self) -> int:\n        return len(self.data)","metadata":{"execution":{"iopub.status.busy":"2021-12-12T13:07:52.645528Z","iopub.execute_input":"2021-12-12T13:07:52.645802Z","iopub.status.idle":"2021-12-12T13:07:52.661493Z","shell.execute_reply.started":"2021-12-12T13:07:52.645772Z","shell.execute_reply":"2021-12-12T13:07:52.660529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data Augmentation\n# albumentation module provides more faster & efficient augmentations than torch module\ndef get_train_transform():\n    return A.Compose([\n        A.Flip(p=0.5), # albumentations.Flip => horizontally, vertically or both horizontally and vertically.\n        ToTensorV2(p=1.0)\n    ], bbox_params={'format': 'pascal_voc', 'label_fields': ['labels']})\n\n\ndef get_valid_transform():\n    return A.Compose([\n        ToTensorV2(p=1.0)\n    ], bbox_params={'format': 'pascal_voc', 'label_fields': ['labels']})","metadata":{"execution":{"iopub.status.busy":"2021-12-12T13:07:53.430366Z","iopub.execute_input":"2021-12-12T13:07:53.430612Z","iopub.status.idle":"2021-12-12T13:07:53.436151Z","shell.execute_reply.started":"2021-12-12T13:07:53.430585Z","shell.execute_reply":"2021-12-12T13:07:53.435394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}