{"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":"This is an inference notebook.\nThe notebook to train the model can be found [here](https://www.kaggle.com/palash97/gbr-fasterrcnn-pytorch-training/)","metadata":{}},{"cell_type":"markdown","source":"## Import Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport torch\nimport torchvision\n\nfrom albumentations.pytorch.transforms import ToTensorV2\nfrom torchvision.models.detection.faster_rcnn import FastRCNNPredictor\nfrom torchvision.models.detection import FasterRCNN","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-30T03:48:19.048803Z","iopub.execute_input":"2022-01-30T03:48:19.049569Z","iopub.status.idle":"2022-01-30T03:48:22.953011Z","shell.execute_reply.started":"2022-01-30T03:48:19.049473Z","shell.execute_reply":"2022-01-30T03:48:22.9522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load fasterrcnn model","metadata":{}},{"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available() else 'cpu'\nmodel = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=False, pretrained_backbone=False)\nmodel = model.to(device)","metadata":{"execution":{"iopub.status.busy":"2022-01-30T03:48:22.955014Z","iopub.execute_input":"2022-01-30T03:48:22.955287Z","iopub.status.idle":"2022-01-30T03:48:26.569265Z","shell.execute_reply.started":"2022-01-30T03:48:22.95525Z","shell.execute_reply":"2022-01-30T03:48:26.568546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = 2  # 1 class (cots) + background\n\n# get number of input features for the classifier\nin_features = model.roi_heads.box_predictor.cls_score.in_features\n\n# replace the pre-trained head with a new one\nmodel.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes).to(device)","metadata":{"execution":{"iopub.status.busy":"2022-01-30T03:48:26.571861Z","iopub.execute_input":"2022-01-30T03:48:26.572368Z","iopub.status.idle":"2022-01-30T03:48:26.578385Z","shell.execute_reply.started":"2022-01-30T03:48:26.572324Z","shell.execute_reply":"2022-01-30T03:48:26.577596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load our trained weights ","metadata":{}},{"cell_type":"code","source":"best_model = '../input/gbr-fasterrcnn-models/gbr_fasterrcnn_resnet50_fpn.pth.tar'\nmodel.load_state_dict(torch.load(best_model, map_location=device))","metadata":{"execution":{"iopub.status.busy":"2022-01-30T03:48:36.899147Z","iopub.execute_input":"2022-01-30T03:48:36.899845Z","iopub.status.idle":"2022-01-30T03:48:40.181403Z","shell.execute_reply.started":"2022-01-30T03:48:36.8998Z","shell.execute_reply":"2022-01-30T03:48:40.180699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Make submission","metadata":{}},{"cell_type":"code","source":"import greatbarrierreef\nenv = greatbarrierreef.make_env()\niter_test = env.iter_test()","metadata":{"execution":{"iopub.status.busy":"2022-01-30T03:48:42.336527Z","iopub.execute_input":"2022-01-30T03:48:42.336811Z","iopub.status.idle":"2022-01-30T03:48:42.360573Z","shell.execute_reply.started":"2022-01-30T03:48:42.33676Z","shell.execute_reply":"2022-01-30T03:48:42.359883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# define object detection threshold\nthreshold = 0.45","metadata":{"execution":{"iopub.status.busy":"2022-01-30T03:48:45.08704Z","iopub.execute_input":"2022-01-30T03:48:45.087586Z","iopub.status.idle":"2022-01-30T03:48:45.091603Z","shell.execute_reply.started":"2022-01-30T03:48:45.087545Z","shell.execute_reply":"2022-01-30T03:48:45.090668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for idx, (image, pred_df) in enumerate(iter_test):\n    \n    image = image.astype(np.float32) / 255.0\n    image = ToTensorV2()(image=image)['image']\n    images = image.unsqueeze(0)   # (1, 3, 720, 1280)\n    \n    model.eval()\n    \n    # Forward pass\n    output = model(images.to(device))[0]\n    \n    # output is a dictionary\n    boxes = output['boxes'].detach().cpu().numpy()\n    scores = output['scores'].detach().cpu().numpy()\n    \n    boxes = boxes[scores >= threshold].astype(np.int32)\n    scores = scores[scores >= threshold]\n\n    # Convert xyxy to xywh\n    boxes[:, 2] = boxes[:, 2] - boxes[:, 0]\n    boxes[:, 3] = boxes[:, 3] - boxes[:, 1]\n    \n    predictions = []\n\n    for i in zip(scores, boxes):\n        confidence_score = i[0]\n        x_min = i[1][0]\n        y_min = i[1][1]\n        width = i[1][2]\n        height = i[1][3]\n        predictions.append(f'{confidence_score:.2f} {x_min} {y_min} {width} {height}')\n\n    pred_df['annotations'] = ' '.join(predictions)\n    env.predict(pred_df)","metadata":{"execution":{"iopub.status.busy":"2022-01-30T03:48:54.555419Z","iopub.execute_input":"2022-01-30T03:48:54.555689Z","iopub.status.idle":"2022-01-30T03:49:01.026119Z","shell.execute_reply.started":"2022-01-30T03:48:54.555657Z","shell.execute_reply":"2022-01-30T03:49:01.025366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Done!')","metadata":{"execution":{"iopub.status.busy":"2022-01-30T03:49:01.027843Z","iopub.execute_input":"2022-01-30T03:49:01.028097Z","iopub.status.idle":"2022-01-30T03:49:01.034409Z","shell.execute_reply.started":"2022-01-30T03:49:01.028063Z","shell.execute_reply":"2022-01-30T03:49:01.033721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}