{"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":"# **About author: I'm a beginner in this field trying to learn and discovering the enjoyment of Data Science**\n### Note1: This notebook is a copy version plus some editing and experimenting for my own understanding and learning.\n### Note2: If this notebook is useful for you in anyway, please give an upvote or commenting your gratitude on the notebook in the reference section. ","metadata":{}},{"cell_type":"markdown","source":"# Imports","metadata":{}},{"cell_type":"code","source":"import numpy as np\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')\n\nWEIGHTS_FILE = \"../input/reef-faster-rcnn-12e-v1/fasterrcnn_resnet50_fpn-e11.bin\"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-14T08:28:12.644420Z","iopub.execute_input":"2021-12-14T08:28:12.644740Z","iopub.status.idle":"2021-12-14T08:28:16.467459Z","shell.execute_reply.started":"2021-12-14T08:28:12.644652Z","shell.execute_reply":"2021-12-14T08:28:16.466710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"markdown","source":"### For understanding and learning purpose, I will leave the link to model document [click here](https://pytorch.org/vision/stable/_modules/torchvision/models/detection/faster_rcnn.html)","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":"2021-12-14T08:28:16.469332Z","iopub.execute_input":"2021-12-14T08:28:16.469721Z","iopub.status.idle":"2021-12-14T08:28:23.913720Z","shell.execute_reply.started":"2021-12-14T08:28:16.469684Z","shell.execute_reply":"2021-12-14T08:28:23.912833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predict functions","metadata":{}},{"cell_type":"code","source":"# I'm experimenting right now, so I don't know which threshold is best\ndetection_threshold = 0.5","metadata":{"execution":{"iopub.status.busy":"2021-12-14T08:28:23.915186Z","iopub.execute_input":"2021-12-14T08:28:23.915610Z","iopub.status.idle":"2021-12-14T08:28:23.920841Z","shell.execute_reply.started":"2021-12-14T08:28:23.915545Z","shell.execute_reply":"2021-12-14T08:28:23.919948Z"},"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":"2021-12-14T08:28:23.922143Z","iopub.execute_input":"2021-12-14T08:28:23.922824Z","iopub.status.idle":"2021-12-14T08:28:23.935351Z","shell.execute_reply.started":"2021-12-14T08:28:23.922748Z","shell.execute_reply":"2021-12-14T08:28:23.934528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submit","metadata":{}},{"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)","metadata":{"execution":{"iopub.status.busy":"2021-12-14T08:28:23.937241Z","iopub.execute_input":"2021-12-14T08:28:23.937538Z","iopub.status.idle":"2021-12-14T08:28:30.102093Z","shell.execute_reply.started":"2021-12-14T08:28:23.937502Z","shell.execute_reply":"2021-12-14T08:28:30.101316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Reference**\n###  [🐠 Reef- Starter Torch FasterRCNN Infer [LB=0.416]](https://www.kaggle.com/julian3833/reef-starter-torch-fasterrcnn-infer-lb-0-416)\n###  [Pytorch Starter - FasterRCNN Inference](https://www.kaggle.com/pestipeti/pytorch-starter-fasterrcnn-inference)","metadata":{}},{"cell_type":"markdown","source":"#### You can criticize my work or give your suggestion, your comment is a treasure of knowledge for me\n##### P.S. sorry for a poor grammar","metadata":{}}]}