{"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":"!pip install pycocotools\nimport pycocotools","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:43:03.536092Z","iopub.execute_input":"2022-02-13T03:43:03.536966Z","iopub.status.idle":"2022-02-13T03:43:42.455948Z","shell.execute_reply.started":"2022-02-13T03:43:03.536833Z","shell.execute_reply":"2022-02-13T03:43:42.454847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n!mkdir /kaggle/working/pytorch_od\nos.chdir(\"/kaggle/working/pytorch_od\")","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:43:42.458565Z","iopub.execute_input":"2022-02-13T03:43:42.459225Z","iopub.status.idle":"2022-02-13T03:43:43.220578Z","shell.execute_reply.started":"2022-02-13T03:43:42.459174Z","shell.execute_reply":"2022-02-13T03:43:43.219399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !rm -r /kaggle/working/pytorch_od/*","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:43:43.223952Z","iopub.execute_input":"2022-02-13T03:43:43.224316Z","iopub.status.idle":"2022-02-13T03:43:43.23115Z","shell.execute_reply.started":"2022-02-13T03:43:43.224266Z","shell.execute_reply":"2022-02-13T03:43:43.230068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%bash\ngit clone https://github.com/pytorch/vision.git\ncd vision\ngit checkout -f v0.3.0\ncp references/detection/utils.py ../\ncp references/detection/transforms.py ../\ncp references/detection/coco_eval.py ../\ncp references/detection/engine.py ../\ncp references/detection/coco_utils.py ../","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:43:43.235283Z","iopub.execute_input":"2022-02-13T03:43:43.236228Z","iopub.status.idle":"2022-02-13T03:44:08.877601Z","shell.execute_reply.started":"2022-02-13T03:43:43.23618Z","shell.execute_reply":"2022-02-13T03:44:08.876647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport torch\nimport torch.utils.data\nfrom PIL import Image\nimport pandas as pd\nfrom torchvision.models.detection.faster_rcnn import FastRCNNPredictor\nfrom engine import train_one_epoch, evaluate\nimport utils\nimport transforms as T","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:44:08.879828Z","iopub.execute_input":"2022-02-13T03:44:08.880209Z","iopub.status.idle":"2022-02-13T03:44:10.989132Z","shell.execute_reply.started":"2022-02-13T03:44:08.880146Z","shell.execute_reply":"2022-02-13T03:44:10.988262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_PATH = '/kaggle/input/tensorflow-great-barrier-reef'\nimages_path = os.path.join(DATA_PATH,'train_images')\ndf_test = pd.read_csv(\"/kaggle/input/tensorflow-great-barrier-reef/test.csv\")\ndf_train = pd.read_csv(\"/kaggle/input/tensorflow-great-barrier-reef/train.csv\")\nsample_submission = pd.read_csv(\"/kaggle/input/tensorflow-great-barrier-reef/example_sample_submission.csv\")\nexample = np.load(\"/kaggle/input/tensorflow-great-barrier-reef/example_test.npy\")","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:44:10.994251Z","iopub.execute_input":"2022-02-13T03:44:10.996593Z","iopub.status.idle":"2022-02-13T03:44:11.368205Z","shell.execute_reply.started":"2022-02-13T03:44:10.996553Z","shell.execute_reply":"2022-02-13T03:44:11.367322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import ast\ndf_train['img_path'] = os.path.join('../input/tensorflow-great-barrier-reef/train_images')+\"/video_\"+df_train.video_id.astype(str)+\"/\"+df_train.video_frame.astype(str)+\".jpg\"\ndf_train['annotations'] = df_train['annotations'].apply(lambda x: ast.literal_eval(x))\ndf_train['Number_bbox'] = df_train['annotations'].apply(lambda x:len(x)) ","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:44:11.3734Z","iopub.execute_input":"2022-02-13T03:44:11.37557Z","iopub.status.idle":"2022-02-13T03:44:11.954721Z","shell.execute_reply.started":"2022-02-13T03:44:11.375529Z","shell.execute_reply":"2022-02-13T03:44:11.953573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def bbox_areas(annotations):\n    if not annotations:\n        return [0]\n    area_list = []\n    for annotation in annotations:\n        area_list.append(annotation['width']*annotation['height'])\n    return area_list\ndf_train[\"bbox_area\"] = df_train[\"annotations\"].apply(bbox_areas)\ndf_train[\"max_area\"] = df_train[\"bbox_area\"].apply(lambda x : max(x))\ndf_train[\"min_area\"] = df_train[\"bbox_area\"].apply(lambda x : min(x))","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:44:11.956503Z","iopub.execute_input":"2022-02-13T03:44:11.956818Z","iopub.status.idle":"2022-02-13T03:44:12.014306Z","shell.execute_reply.started":"2022-02-13T03:44:11.956773Z","shell.execute_reply":"2022-02-13T03:44:12.013237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ndef img_viz(df_train, id):\n    image = df_train['img_path'][id]\n    img = Image.open(image)\n    \n    for box in df_train['annotations'][id]:\n        shape = [box['x'], box['y'], box['x']+box['width'], box['y']+box['height']]\n        ImageDraw.Draw(img).rectangle(shape, outline =\"red\", width=3)\n    display(img)\ndf_train.sort_values(\"max_area\", ascending=False).head()\n\n","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:44:12.015777Z","iopub.execute_input":"2022-02-13T03:44:12.016104Z","iopub.status.idle":"2022-02-13T03:44:12.054225Z","shell.execute_reply.started":"2022-02-13T03:44:12.016066Z","shell.execute_reply":"2022-02-13T03:44:12.053195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = df_train.query(\"Number_bbox>0\")\nlen(train_data)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:44:12.058798Z","iopub.execute_input":"2022-02-13T03:44:12.059027Z","iopub.status.idle":"2022-02-13T03:44:12.080544Z","shell.execute_reply.started":"2022-02-13T03:44:12.059Z","shell.execute_reply":"2022-02-13T03:44:12.079621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['class'] = 'starfish'\ntrain_data['filename'] = train_data.apply(lambda x: x['img_path'].split('/')[-1], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:44:12.081937Z","iopub.execute_input":"2022-02-13T03:44:12.082351Z","iopub.status.idle":"2022-02-13T03:44:12.169787Z","shell.execute_reply.started":"2022-02-13T03:44:12.082308Z","shell.execute_reply":"2022-02-13T03:44:12.168825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = train_data.explode(\"annotations\").reset_index()\ntrain_data = train_data.drop(['index'],axis=1)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:44:12.17118Z","iopub.execute_input":"2022-02-13T03:44:12.17196Z","iopub.status.idle":"2022-02-13T03:44:12.202668Z","shell.execute_reply.started":"2022-02-13T03:44:12.171895Z","shell.execute_reply":"2022-02-13T03:44:12.201696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:44:12.20424Z","iopub.execute_input":"2022-02-13T03:44:12.204571Z","iopub.status.idle":"2022-02-13T03:44:12.236105Z","shell.execute_reply.started":"2022-02-13T03:44:12.204531Z","shell.execute_reply":"2022-02-13T03:44:12.234891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_bbox_dim(annotation):\n#     print(annotation)\n    return [annotation['x'], annotation['y'],annotation['x'] + annotation['width'],annotation['y'] + annotation['height'],  annotation['width'],annotation['height'] ]\n\n","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:44:12.238119Z","iopub.execute_input":"2022-02-13T03:44:12.238759Z","iopub.status.idle":"2022-02-13T03:44:12.246712Z","shell.execute_reply.started":"2022-02-13T03:44:12.238715Z","shell.execute_reply":"2022-02-13T03:44:12.245304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"annotations_data = train_data.apply(lambda row: get_bbox_dim(row[\"annotations\"]), axis=1)\nannotations_df = pd.DataFrame(list(annotations_data), columns=['xmin', 'ymin', 'xmax', 'ymax', 'width', 'height'])","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:44:12.248729Z","iopub.execute_input":"2022-02-13T03:44:12.249415Z","iopub.status.idle":"2022-02-13T03:44:12.489204Z","shell.execute_reply.started":"2022-02-13T03:44:12.249339Z","shell.execute_reply":"2022-02-13T03:44:12.488302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\n\ntrain_data = pd.concat([annotations_df, train_data], axis=1)\nxmax = np.array(train_data[\"xmax\"].values.tolist())\nymax = np.array(train_data[\"ymax\"].values.tolist())\ntrain_data[\"xmax\"] = np.where(xmax > 1280, 1280, xmax).tolist()\ntrain_data[\"ymax\"] = np.where(ymax > 720, 720, ymax).tolist()\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:44:12.490827Z","iopub.execute_input":"2022-02-13T03:44:12.491185Z","iopub.status.idle":"2022-02-13T03:44:12.520499Z","shell.execute_reply.started":"2022-02-13T03:44:12.491147Z","shell.execute_reply":"2022-02-13T03:44:12.51957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_video1 = train_data[train_data['video_id']==1].reset_index()\n\n","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:44:12.521973Z","iopub.execute_input":"2022-02-13T03:44:12.522493Z","iopub.status.idle":"2022-02-13T03:44:12.534058Z","shell.execute_reply.started":"2022-02-13T03:44:12.522453Z","shell.execute_reply":"2022-02-13T03:44:12.532892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_video1.to_csv('/kaggle/working/train_data.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:44:12.535793Z","iopub.execute_input":"2022-02-13T03:44:12.536487Z","iopub.status.idle":"2022-02-13T03:44:12.63606Z","shell.execute_reply.started":"2022-02-13T03:44:12.536444Z","shell.execute_reply":"2022-02-13T03:44:12.635148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# COLUMNS_TO_KEEP = ['width', 'height', 'xmin', 'ymin', 'xmax', 'ymax', 'filename','class']\n# train_csv = train_data_video1[COLUMNS_TO_KEEP]\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:44:12.637658Z","iopub.execute_input":"2022-02-13T03:44:12.637964Z","iopub.status.idle":"2022-02-13T03:44:12.644878Z","shell.execute_reply.started":"2022-02-13T03:44:12.637925Z","shell.execute_reply":"2022-02-13T03:44:12.642372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def parse_one_annot(path_to_data_file, filename):\n    data = pd.read_csv(path_to_data_file)\n    boxes_array = data[data[\"filename\"] == filename][[\"xmin\", \"ymin\",\"xmax\", \"ymax\"]].values\n    return boxes_array","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:44:12.646845Z","iopub.execute_input":"2022-02-13T03:44:12.648293Z","iopub.status.idle":"2022-02-13T03:44:12.655783Z","shell.execute_reply.started":"2022-02-13T03:44:12.648229Z","shell.execute_reply":"2022-02-13T03:44:12.654146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class GreatReefDataset(torch.utils.data.Dataset):\n    def __init__(self, root, data_file, transforms=None, data_df=None):\n        self.root = root\n        self.transforms = transforms\n        if data_df:\n            self.data_df = data_df\n        else:\n            self.data_df = pd.read_csv(data_file)\n        self.imgs = list(self.data_df['filename'])\n        self.path_to_data_file = data_file\n      \n    def __getitem__(self, idx):\n        img_path = os.path.join(self.root, self.imgs[idx])\n        img = Image.open(img_path).convert(\"RGB\")\n        box_list = parse_one_annot(self.path_to_data_file,self.imgs[idx])\n#         print(box_list)\n        boxes = torch.as_tensor(box_list, dtype=torch.float32)\n        num_objs = len(box_list)\n      # there is only one class\n        labels = torch.ones((num_objs,), dtype=torch.int64)\n        image_id = torch.tensor([idx])\n        area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:,0])\n      # suppose all instances are not crowd\n        iscrowd = torch.zeros((num_objs,), dtype=torch.int64)\n        target = {}\n        target[\"boxes\"] = boxes\n        target[\"labels\"] = labels\n        target[\"image_id\"] = image_id\n        target[\"area\"] = area\n        target[\"iscrowd\"] = iscrowd\n        \n        if self.transforms is not None:\n           img, target = self.transforms(img, target)\n        return img, target\n\n    def __len__(self):\n        return len(self.imgs)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T04:30:25.101437Z","iopub.execute_input":"2022-02-13T04:30:25.101997Z","iopub.status.idle":"2022-02-13T04:30:25.122152Z","shell.execute_reply.started":"2022-02-13T04:30:25.101914Z","shell.execute_reply":"2022-02-13T04:30:25.120751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = GreatReefDataset(root= \"/kaggle/input/tensorflow-great-barrier-reef/train_images/video_1/\",\n                         data_file= \"/kaggle/working/train_data.csv\")\ntest = dataset.__getitem__(0)[1]","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:44:12.673981Z","iopub.execute_input":"2022-02-13T03:44:12.674514Z","iopub.status.idle":"2022-02-13T03:44:12.826701Z","shell.execute_reply.started":"2022-02-13T03:44:12.674468Z","shell.execute_reply":"2022-02-13T03:44:12.825747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision\ndef get_model(num_classes):\n   # load an object detection model pre-trained on COCO\n    model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)# get the number of input features for the classifier\n    in_features = model.roi_heads.box_predictor.cls_score.in_features\n   # replace the pre-trained head with a new on\n    model.roi_heads.box_predictor = FastRCNNPredictor(in_features,num_classes)\n   \n    return model","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:44:12.830515Z","iopub.execute_input":"2022-02-13T03:44:12.832452Z","iopub.status.idle":"2022-02-13T03:44:12.848553Z","shell.execute_reply.started":"2022-02-13T03:44:12.832418Z","shell.execute_reply":"2022-02-13T03:44:12.8435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_transform(train):\n    transforms = []\n   # converts the image, a PIL image, into a PyTorch Tensor\n    transforms.append(T.ToTensor())\n    if train:\n      # during training, randomly flip the training images\n      # and ground-truth for data augmentation\n        transforms.append(T.RandomHorizontalFlip(0.5))\n    return T.Compose(transforms)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:44:12.850146Z","iopub.execute_input":"2022-02-13T03:44:12.850494Z","iopub.status.idle":"2022-02-13T03:44:12.866693Z","shell.execute_reply.started":"2022-02-13T03:44:12.850454Z","shell.execute_reply":"2022-02-13T03:44:12.86233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = GreatReefDataset(root= \"/kaggle/input/tensorflow-great-barrier-reef/train_images/video_1/\",\n                         data_file= \"/kaggle/working/train_data.csv\",transforms = get_transform(train=True))\ndataset_test = GreatReefDataset(root= \"/kaggle/input/tensorflow-great-barrier-reef/train_images/video_1/\",\n                         data_file= \"/kaggle/working/train_data.csv\",transforms = get_transform(train=False))# split the dataset in train and test settorch.manual_seed(1)\nindices = torch.randperm(len(dataset)).tolist()\ndataset = torch.utils.data.Subset(dataset, indices[:-400])\ndataset_test = torch.utils.data.Subset(dataset_test, indices[-400:])# define training and validation data loaders\ndata_loader = torch.utils.data.DataLoader(\n              dataset, batch_size=8, shuffle=True, num_workers=4,\n              collate_fn=utils.collate_fn)\ndata_loader_test = torch.utils.data.DataLoader(\n         dataset_test, batch_size=1, shuffle=False, num_workers=4,\n         collate_fn=utils.collate_fn)\nprint(\"We have: {} examples, {} are training and {} testing\".format(len(indices), len(dataset), len(dataset_test)))","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:44:12.871149Z","iopub.execute_input":"2022-02-13T03:44:12.872264Z","iopub.status.idle":"2022-02-13T03:44:12.993567Z","shell.execute_reply.started":"2022-02-13T03:44:12.872149Z","shell.execute_reply":"2022-02-13T03:44:12.992333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.cuda.is_available()","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:44:12.994965Z","iopub.execute_input":"2022-02-13T03:44:12.998727Z","iopub.status.idle":"2022-02-13T03:44:13.070001Z","shell.execute_reply.started":"2022-02-13T03:44:12.99868Z","shell.execute_reply":"2022-02-13T03:44:13.068929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')# our dataset has two classes only - raccoon and not racoon\nnum_classes = 2\n# get the model using our helper function\nmodel = get_model(num_classes)\n# move model to the right device\nmodel.to(device)# construct an optimizer\nparams = [p for p in model.parameters() if p.requires_grad]\noptimizer = torch.optim.SGD(params, lr=0.005,\n                            momentum=0.9, weight_decay=0.0005)# and a learning rate scheduler which decreases the learning rate by # 10x every 3 epochslr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer,\nlr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer,\n                                               step_size=3,\n                                               gamma=0.1)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:44:13.074494Z","iopub.execute_input":"2022-02-13T03:44:13.074841Z","iopub.status.idle":"2022-02-13T03:44:23.833458Z","shell.execute_reply.started":"2022-02-13T03:44:13.074795Z","shell.execute_reply":"2022-02-13T03:44:23.832368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_epochs = 1\nfor epoch in range(num_epochs):\n   # train for one epoch, printing every 10 iterations\n    train_one_epoch(model, optimizer, data_loader, device, epoch,\n                   print_freq=10)# update the learning rate\n    lr_scheduler.step()\n   # evaluate on the test dataset\n    evaluate(model, data_loader_test, device=device)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T03:44:23.8392Z","iopub.execute_input":"2022-02-13T03:44:23.839794Z","iopub.status.idle":"2022-02-13T04:09:05.956829Z","shell.execute_reply.started":"2022-02-13T03:44:23.839753Z","shell.execute_reply":"2022-02-13T04:09:05.955748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir(\"/kaggle/working/saved_models\")\ntorch.save(model.state_dict(), \"/kaggle/working/saved_models/faster_rcnn\")","metadata":{"execution":{"iopub.status.busy":"2022-02-13T04:09:05.960483Z","iopub.execute_input":"2022-02-13T04:09:05.960739Z","iopub.status.idle":"2022-02-13T04:09:06.345374Z","shell.execute_reply.started":"2022-02-13T04:09:05.960706Z","shell.execute_reply":"2022-02-13T04:09:06.344302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loaded_model = get_model(num_classes = 2)\nloaded_model.load_state_dict(torch.load( \"/kaggle/working/saved_models/faster_rcnn\"))","metadata":{"execution":{"iopub.status.busy":"2022-02-13T04:09:06.347305Z","iopub.execute_input":"2022-02-13T04:09:06.347638Z","iopub.status.idle":"2022-02-13T04:09:07.344095Z","shell.execute_reply.started":"2022-02-13T04:09:06.347585Z","shell.execute_reply":"2022-02-13T04:09:07.34312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image, ImageDraw\nidx = 0\nimg, _ = dataset_test[idx]\nlabel_boxes = np.array(dataset_test[idx][1][\"boxes\"])#put the model in evaluation mode\nloaded_model.eval()\nwith torch.no_grad():\n    prediction = loaded_model([img])\n    image = Image.fromarray(img.mul(255).permute(1, 2,0).byte().numpy())\n    draw = ImageDraw.Draw(image)# draw groundtruth\n    print(prediction)\n    for elem in range(len(label_boxes)):\n        draw.rectangle([(label_boxes[elem][0], label_boxes[elem][1]),\n       (label_boxes[elem][2], label_boxes[elem][3])], \n        outline =\"green\", width =3)\n    for element in range(len(prediction[0][\"boxes\"])):\n        boxes = prediction[0][\"boxes\"][element].cpu().numpy()\n        score = np.round(prediction[0][\"scores\"][element].cpu().numpy(),\n                        decimals= 4)\n        if score > 0.8:\n            draw.rectangle([(boxes[0], boxes[1]), (boxes[2], boxes[3])], \n            outline =\"red\", width =3)\n            draw.text((boxes[0], boxes[1]), text = str(score))\n    display(image)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T04:31:52.339054Z","iopub.execute_input":"2022-02-13T04:31:52.339407Z","iopub.status.idle":"2022-02-13T04:32:01.046348Z","shell.execute_reply.started":"2022-02-13T04:31:52.339374Z","shell.execute_reply":"2022-02-13T04:32:01.045569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n!pwd\nos.chdir(\"/\")\n# ../input/tensorflow-great-barrier-reef/train_images\nos.listdir(\"/kaggle/input\")","metadata":{"execution":{"iopub.status.busy":"2022-02-13T04:09:16.176717Z","iopub.execute_input":"2022-02-13T04:09:16.177041Z","iopub.status.idle":"2022-02-13T04:09:16.980203Z","shell.execute_reply.started":"2022-02-13T04:09:16.176996Z","shell.execute_reply":"2022-02-13T04:09:16.979231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test","metadata":{"execution":{"iopub.status.busy":"2022-02-13T04:09:16.982006Z","iopub.execute_input":"2022-02-13T04:09:16.982789Z","iopub.status.idle":"2022-02-13T04:09:16.997129Z","shell.execute_reply.started":"2022-02-13T04:09:16.982741Z","shell.execute_reply":"2022-02-13T04:09:16.996019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T04:15:58.428652Z","iopub.execute_input":"2022-02-13T04:15:58.429006Z","iopub.status.idle":"2022-02-13T04:15:58.447297Z","shell.execute_reply.started":"2022-02-13T04:15:58.428972Z","shell.execute_reply":"2022-02-13T04:15:58.446239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission","metadata":{"execution":{"iopub.status.busy":"2022-02-13T04:09:16.999063Z","iopub.execute_input":"2022-02-13T04:09:16.99963Z","iopub.status.idle":"2022-02-13T04:09:17.016403Z","shell.execute_reply.started":"2022-02-13T04:09:16.999569Z","shell.execute_reply":"2022-02-13T04:09:17.015348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import greatbarrierreef\n# env = greatbarrierreef.make_env()# initialize the environment\niter_test = env.iter_test()      #","metadata":{"execution":{"iopub.status.busy":"2022-02-13T04:25:52.836147Z","iopub.execute_input":"2022-02-13T04:25:52.836519Z","iopub.status.idle":"2022-02-13T04:25:52.840962Z","shell.execute_reply.started":"2022-02-13T04:25:52.836487Z","shell.execute_reply":"2022-02-13T04:25:52.839811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image, ImageDraw\nidx = 0\nimg, _ = dataset_test[idx]\nlabel_boxes = np.array(dataset_test[idx][1][\"boxes\"])#put the model in evaluation mode\nloaded_model.eval()\nwith torch.no_grad():\n    prediction = loaded_model([img])\n    print(prediction)\n    display(image)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(img):\n    prediction = loaded_model([img])\n    return prediction\n","metadata":{"execution":{"iopub.status.busy":"2022-02-13T04:22:43.691735Z","iopub.execute_input":"2022-02-13T04:22:43.692044Z","iopub.status.idle":"2022-02-13T04:22:43.697788Z","shell.execute_reply.started":"2022-02-13T04:22:43.691998Z","shell.execute_reply":"2022-02-13T04:22:43.696814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict(img)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T04:28:47.910831Z","iopub.execute_input":"2022-02-13T04:28:47.911123Z","iopub.status.idle":"2022-02-13T04:28:47.943596Z","shell.execute_reply.started":"2022-02-13T04:28:47.911091Z","shell.execute_reply":"2022-02-13T04:28:47.942145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img, pred_df = (iter_test)\nprint(pred_df)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T04:33:18.302861Z","iopub.execute_input":"2022-02-13T04:33:18.303168Z","iopub.status.idle":"2022-02-13T04:33:18.321456Z","shell.execute_reply.started":"2022-02-13T04:33:18.303135Z","shell.execute_reply":"2022-02-13T04:33:18.320013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"env.predict(img)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T04:27:45.14302Z","iopub.execute_input":"2022-02-13T04:27:45.143347Z","iopub.status.idle":"2022-02-13T04:27:45.172866Z","shell.execute_reply.started":"2022-02-13T04:27:45.143315Z","shell.execute_reply":"2022-02-13T04:27:45.170882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for idx, (img, pred_df) in enumerate(iter_test):\n    print(idx)\n    display(img)\n    prediction = predict(img)\n    print(prediction)\n    break","metadata":{"execution":{"iopub.status.busy":"2022-02-13T04:23:03.034173Z","iopub.execute_input":"2022-02-13T04:23:03.0345Z","iopub.status.idle":"2022-02-13T04:23:03.055419Z","shell.execute_reply.started":"2022-02-13T04:23:03.03447Z","shell.execute_reply":"2022-02-13T04:23:03.053633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}