{"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":"## Reference Notebook - https://www.kaggle.com/julian3833/reef-starter-torch-fasterrcnn-infer-lb-0-416/notebook","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom albumentations.pytorch.transforms import ToTensorV2\n\nimport torch\nimport torchvision\nfrom torchvision.models.detection.faster_rcnn import FastRCNNPredictor\nfrom torchvision.models.detection import FasterRCNN\n\ndevice = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')","metadata":{"execution":{"iopub.status.busy":"2022-01-23T19:27:43.987881Z","iopub.execute_input":"2022-01-23T19:27:43.988258Z","iopub.status.idle":"2022-01-23T19:27:48.376565Z","shell.execute_reply.started":"2022-01-23T19:27:43.988159Z","shell.execute_reply":"2022-01-23T19:27:48.375558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"WEIGHTS_FILE = \"../input/cots-torch-fasterrcnn-weights/fasterrcnn_resnet50_fpn-e10.bin\"","metadata":{"execution":{"iopub.status.busy":"2022-01-23T19:27:48.379950Z","iopub.execute_input":"2022-01-23T19:27:48.380554Z","iopub.status.idle":"2022-01-23T19:27:48.385592Z","shell.execute_reply.started":"2022-01-23T19:27:48.380490Z","shell.execute_reply":"2022-01-23T19:27:48.384411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load Model","metadata":{}},{"cell_type":"code","source":"def get_model():\n    model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=False, pretrained_backbone=False)\n    num_classes = 2  # 1 class (starfish) + background\n\n    # get number of input features for the classifier\n    in_features = model.roi_heads.box_predictor.cls_score.in_features\n\n    # replace the pre-trained head with a new one\n    model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)\n\n    # Load the trained weights\n    model.load_state_dict(torch.load(WEIGHTS_FILE))\n    model.eval()\n\n    model = model.to(device)\n    return model\n\nmodel = get_model()","metadata":{"execution":{"iopub.status.busy":"2022-01-23T19:27:48.389279Z","iopub.execute_input":"2022-01-23T19:27:48.389768Z","iopub.status.idle":"2022-01-23T19:27:55.742401Z","shell.execute_reply.started":"2022-01-23T19:27:48.389594Z","shell.execute_reply":"2022-01-23T19:27:55.741401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predict","metadata":{}},{"cell_type":"code","source":"detection_threshold = 0.5","metadata":{"execution":{"iopub.status.busy":"2022-01-23T19:27:55.744583Z","iopub.execute_input":"2022-01-23T19:27:55.744879Z","iopub.status.idle":"2022-01-23T19:27:55.748963Z","shell.execute_reply.started":"2022-01-23T19:27:55.744846Z","shell.execute_reply":"2022-01-23T19:27:55.748029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def format_prediction_string(boxes, scores):\n    # Format as specified in the evaluation page\n    pred_strings = []\n    for j in zip(scores, boxes):\n        pred_strings.append(\"{0:.2f} {1} {2} {3} {4}\".format(j[0], j[1][0], j[1][1], j[1][2], j[1][3]))\n\n    return \" \".join(pred_strings)\n\n\ndef predict(model, pixel_array):\n    # Predictions for a single image\n    \n    # Apply all the transformations that are required\n    pixel_array = pixel_array.astype(np.float32) / 255.\n    tensor_img = ToTensorV2(p=1.0)(image=pixel_array)['image'].unsqueeze(0)\n    \n    # Get predictions\n    with torch.no_grad():\n        outputs = model(tensor_img.to(device))[0]\n    \n    # Move predictions to cpu and numpy\n    boxes = outputs['boxes'].data.cpu().numpy()\n    scores = outputs['scores'].data.cpu().numpy()\n    \n    # Filter predictions with low score\n    boxes = boxes[scores >= detection_threshold].astype(np.int32)\n    scores = scores[scores >= detection_threshold]\n    \n    # Go back from x_min, y_min, x_max, y_max to x_min, y_min, w, h\n    boxes[:, 2] = boxes[:, 2] - boxes[:, 0]\n    boxes[:, 3] = boxes[:, 3] - boxes[:, 1]\n  \n    # Format results as requested in the Evaluation tab\n    return format_prediction_string(boxes, scores)","metadata":{"execution":{"iopub.status.busy":"2022-01-23T19:27:55.750548Z","iopub.execute_input":"2022-01-23T19:27:55.751151Z","iopub.status.idle":"2022-01-23T19:27:55.765811Z","shell.execute_reply.started":"2022-01-23T19:27:55.751086Z","shell.execute_reply":"2022-01-23T19:27:55.764772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import greatbarrierreef\nenv = greatbarrierreef.make_env()\niter_test = env.iter_test() \n\nfor (pixel_array, df_pred) in iter_test:  # iterate through all test set images\n    df_pred['annotations'] = predict(model, pixel_array)\n    env.predict(df_pred)\n","metadata":{"execution":{"iopub.status.busy":"2022-01-23T19:27:55.767526Z","iopub.execute_input":"2022-01-23T19:27:55.768111Z","iopub.status.idle":"2022-01-23T19:28:02.087515Z","shell.execute_reply.started":"2022-01-23T19:27:55.768052Z","shell.execute_reply":"2022-01-23T19:28:02.086528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-01-23T19:28:02.089426Z","iopub.execute_input":"2022-01-23T19:28:02.089771Z","iopub.status.idle":"2022-01-23T19:28:02.111100Z","shell.execute_reply.started":"2022-01-23T19:28:02.089727Z","shell.execute_reply":"2022-01-23T19:28:02.110151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}