{"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 ../input/wheat-detection/yacs-0.1.7-py3-none-any.whl\n!pip install ../input/wheat-detection/torch-1.5.0cu101-cp37-cp37m-linux_x86_64.whl\n!pip install ../input/wheat-detection/fvcore-0.1.1.post20200716-py3-none-any.whl\n!pip install ../input/wheat-detection/pycocotools-2.0.1-cp37-cp37m-linux_x86_64.whl\n!pip install ../input/wheat-detection/torchvision-0.6.0cu101-cp37-cp37m-linux_x86_64.whl\n!pip install ../input/wheat-detection/detectron2-0.2cu101-cp37-cp37m-linux_x86_64.whl","metadata":{"execution":{"iopub.status.busy":"2022-02-03T03:52:00.444695Z","iopub.execute_input":"2022-02-03T03:52:00.445458Z","iopub.status.idle":"2022-02-03T03:55:24.141094Z","shell.execute_reply.started":"2022-02-03T03:52:00.445358Z","shell.execute_reply":"2022-02-03T03:55:24.140103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import detectron2\nfrom detectron2.config import get_cfg\nfrom detectron2.engine import DefaultPredictor\nfrom detectron2 import model_zoo\nfrom detectron2.utils.visualizer import Visualizer\n\n\nimport matplotlib.pyplot as plt\nimport cv2\nimport pandas as pd\n\nfrom tqdm.notebook import tqdm\ntqdm.pandas()","metadata":{"execution":{"iopub.status.busy":"2022-02-03T03:55:24.142959Z","iopub.execute_input":"2022-02-03T03:55:24.143231Z","iopub.status.idle":"2022-02-03T03:55:25.158793Z","shell.execute_reply.started":"2022-02-03T03:55:24.143203Z","shell.execute_reply":"2022-02-03T03:55:25.158089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg = get_cfg() # initialize cfg object\ncfg.merge_from_file(model_zoo.get_config_file(\"COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x.yaml\"))  # load default parameters for Mask R-CNN\ncfg.MODEL.DEVICE='cuda'  # 'cpu' to force model to run on cpu, 'cuda' if you have a compatible gpu\ncfg.MODEL.ROI_HEADS.NUM_CLASSES = 1\ncfg.MODEL.WEIGHTS = '../input/model-25000/model_final.pth'\ncfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.25\npredictor = DefaultPredictor(cfg)","metadata":{"execution":{"iopub.status.busy":"2022-02-03T04:05:58.585776Z","iopub.execute_input":"2022-02-03T04:05:58.586089Z","iopub.status.idle":"2022-02-03T04:06:00.879526Z","shell.execute_reply.started":"2022-02-03T04:05:58.586047Z","shell.execute_reply":"2022-02-03T04:06:00.878775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import ast\nimport json as json\n\n\n\ndf = pd.read_csv(\"../input/tensorflow-great-barrier-reef/train.csv\")\n\n\ncoco = {\n    \"info\": {\n        \"year\": \"2021\",\n        \"version\": \"1\",\n        \"description\": \"Help Protect the Great Barrier Reef\",\n        \"contributor\": \"Tensorflow\",\n        \"url\": \"\",\n        \"date_created\": \"2021-11-29T19:36:39+00:00\"\n    },\n    \"licenses\": [\n        {\n            \"id\": 1,\n            \"url\": \"https://creativecommons.org/publicdomain/zero/1.0/\",\n            \"name\": \"Public Domain\"\n        }\n    ],\n    \"categories\": [\n        {\n            \"id\": 1,\n            \"name\": \"class 1\"}\n    ],\n    \"images\": [],\n    \"annotations\": []\n}\n\ndf_2 = df[df.video_id==2]\nfor i, anotation in enumerate(df_2.annotations):\n    if len(anotation)>2:\n        coco[\"images\"].append({\"id\": str(df_2.iloc[i].image_id),\n                               \"license\": 1,\n                               \"file_name\": \"video_2/\" + str(df_2.iloc[i].video_frame) + \".jpg\" ,\n                               \"height\": 720,\n                               \"width\":1280\n                               })\n        boxes = ast.literal_eval(df_2.iloc[i].annotations)\n        for j, box in enumerate(boxes):\n            bbox = list(box.values())\n            coco[\"annotations\"].append({\n                \"id\": str(df_2.iloc[i].image_id).replace(\"-\", \"\") + \"2\" + str(j),\n                \"area\": bbox[2] * bbox[3],\n                \"image_id\": str(df_2.iloc[i].image_id),\n                \"category_id\": 1,\n                \"bbox\": bbox,\n                \"segmentation\": [],\n                \"iscrowd\": 0\n            })\n\nwith open('test_dataset_coco.json', 'w') as f:\n    json.dump(coco, f)","metadata":{"execution":{"iopub.status.busy":"2022-02-03T04:06:00.881279Z","iopub.execute_input":"2022-02-03T04:06:00.881529Z","iopub.status.idle":"2022-02-03T04:06:02.037331Z","shell.execute_reply.started":"2022-02-03T04:06:00.881498Z","shell.execute_reply":"2022-02-03T04:06:02.036454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def format_prediction(bboxes, confs):\n    annot = ''\n    if len(bboxes)>0:\n        for idx in range(len(bboxes)):\n            xmin, ymin, xmax, ymax = bboxes[idx]\n            w=xmax-xmin\n            h=ymax-ymin\n            conf             = confs[idx]\n            annot += f'{conf} {xmin} {ymin} {w} {h}'\n            annot +=' '\n        annot = annot.strip(' ')\n    return annot","metadata":{"execution":{"iopub.status.busy":"2022-02-03T04:06:02.041114Z","iopub.execute_input":"2022-02-03T04:06:02.041342Z","iopub.status.idle":"2022-02-03T04:06:02.049889Z","shell.execute_reply.started":"2022-02-03T04:06:02.041315Z","shell.execute_reply":"2022-02-03T04:06:02.049072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from detectron2.data.datasets import register_coco_instances\nfrom detectron2.data.catalog import DatasetCatalog\n\nDatasetCatalog.clear()\n\nregister_coco_instances(\"coco_test_dataset2\", {}, \"test_dataset_coco.json\" ,   \"../input/tensorflow-great-barrier-reef/train_images\")\n","metadata":{"execution":{"iopub.status.busy":"2022-02-03T04:06:02.062025Z","iopub.execute_input":"2022-02-03T04:06:02.062546Z","iopub.status.idle":"2022-02-03T04:06:02.068775Z","shell.execute_reply.started":"2022-02-03T04:06:02.062515Z","shell.execute_reply":"2022-02-03T04:06:02.066322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from detectron2.data import MetadataCatalog\ndataset_test_metadata = MetadataCatalog.get(\"coco_test_dataset2\")\ndataset_test = DatasetCatalog.get(\"coco_test_dataset2\")","metadata":{"execution":{"iopub.status.busy":"2022-02-03T04:06:03.644418Z","iopub.execute_input":"2022-02-03T04:06:03.645213Z","iopub.status.idle":"2022-02-03T04:06:03.816394Z","shell.execute_reply.started":"2022-02-03T04:06:03.645160Z","shell.execute_reply":"2022-02-03T04:06:03.812531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_img(img, bboxes, bbox_format='yolo'):\n    names  = ['starfish']*len(bboxes)\n    labels = [0]*len(bboxes)\n    img    = draw_bboxes(img = img,\n                           bboxes = bboxes, \n                           classes = names,\n                           class_ids = labels,\n                           class_name = True, \n                           colors = colors, \n                           bbox_format = bbox_format,\n                           line_thickness = 2)\n    return Image.fromarray(img).resize((800, 400))","metadata":{"execution":{"iopub.status.busy":"2022-02-03T04:06:10.546388Z","iopub.execute_input":"2022-02-03T04:06:10.546901Z","iopub.status.idle":"2022-02-03T04:06:10.552295Z","shell.execute_reply.started":"2022-02-03T04:06:10.546864Z","shell.execute_reply":"2022-02-03T04:06:10.551436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\n\nrandom_testing = random.sample(dataset_test, 20)\n\nfor i in range(20):\n    print(i)\n    fig, ax = plt.subplots(2, 1, figsize =(40,20))\n    img = cv2.imread(random_testing[i]['file_name'])\n\n    visualizer = Visualizer(img[:, :, ::-1],\n                          metadata=dataset_test_metadata,\n                          scale=0.5)\n    out = visualizer.draw_dataset_dict(random_testing[i])\n    ax[0].imshow(cv2.cvtColor(out.get_image()[:, :, ::-1], cv2.COLOR_BGR2RGB))\n    ax[0].set_title(\"Imagen original\")\n    outputs = predictor(img)\n    visualizer_pred = Visualizer(img[:, :, ::-1],\n                              metadata=dataset_test_metadata,\n                              scale=0.5)\n    pred = visualizer_pred.draw_instance_predictions(outputs['instances'].to('cpu'))\n  \n    ax[1].imshow(cv2.cvtColor(pred.get_image()[:, :, ::-1], cv2.COLOR_BGR2RGB))\n    ax[1].set_title(\"Predicción\")\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-03T04:06:12.027883Z","iopub.execute_input":"2022-02-03T04:06:12.028412Z","iopub.status.idle":"2022-02-03T04:06:40.608462Z","shell.execute_reply.started":"2022-02-03T04:06:12.028372Z","shell.execute_reply":"2022-02-03T04:06:40.606150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import greatbarrierreef\nenv = greatbarrierreef.make_env()# initialize the environment\niter_test = env.iter_test()","metadata":{"execution":{"iopub.status.busy":"2022-02-03T04:01:58.696782Z","iopub.execute_input":"2022-02-03T04:01:58.697263Z","iopub.status.idle":"2022-02-03T04:01:58.728607Z","shell.execute_reply.started":"2022-02-03T04:01:58.697223Z","shell.execute_reply":"2022-02-03T04:01:58.727919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 1, figsize =(100,80))\nfor idx, (img, pred_df) in enumerate(tqdm(iter_test)):\n    outputs = predictor(img[:,:,::-1])\n    pred_boxes = outputs['instances'].pred_boxes.tensor\n    pred_scores =outputs['instances'].scores\n    pred_boxes = pred_boxes.cpu().numpy()\n    pred_scores = pred_scores.cpu().numpy()\n    annot = format_prediction(pred_boxes,pred_scores)\n    pred_df['annotations'] = annot\n    env.predict(pred_df)\n    if idx<3:\n        visualizer_pred = Visualizer(img[:, :, ::-1],   \n                                 scale=0.5)\n        pred = visualizer_pred.draw_instance_predictions(outputs['instances'].to('cpu'))\n        ax.grid(False)\n        ax.axis('off')\n        ax.imshow(cv2.cvtColor(pred.get_image()[:, :, ::-1], cv2.COLOR_BGR2RGB))","metadata":{"execution":{"iopub.status.busy":"2022-02-02T21:00:57.412842Z","iopub.execute_input":"2022-02-02T21:00:57.413249Z","iopub.status.idle":"2022-02-02T21:01:03.995099Z","shell.execute_reply.started":"2022-02-02T21:00:57.413216Z","shell.execute_reply":"2022-02-02T21:01:03.994388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}