{"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":"import torch, torchvision\nprint(torch.__version__, torch.cuda.is_available())  ","metadata":{"execution":{"iopub.status.busy":"2022-01-21T21:47:07.253629Z","iopub.execute_input":"2022-01-21T21:47:07.253892Z","iopub.status.idle":"2022-01-21T21:47:07.259526Z","shell.execute_reply.started":"2022-01-21T21:47:07.253862Z","shell.execute_reply":"2022-01-21T21:47:07.258247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!nvidia-smi","metadata":{"execution":{"iopub.status.busy":"2022-01-21T21:47:07.744301Z","iopub.execute_input":"2022-01-21T21:47:07.744741Z","iopub.status.idle":"2022-01-21T21:47:08.517345Z","shell.execute_reply.started":"2022-01-21T21:47:07.744704Z","shell.execute_reply":"2022-01-21T21:47:08.516574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.append('../input/detectron-data/mysitepackages')","metadata":{"execution":{"iopub.status.busy":"2022-01-21T21:47:08.727283Z","iopub.execute_input":"2022-01-21T21:47:08.727991Z","iopub.status.idle":"2022-01-21T21:47:08.731833Z","shell.execute_reply.started":"2022-01-21T21:47:08.727952Z","shell.execute_reply":"2022-01-21T21:47:08.730848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport pandas as pd\nimport numpy as np\nimport pandas as pd \n\nfrom tqdm import tqdm\nfrom tqdm import tqdm_notebook as tqdm # progress bar\nfrom tqdm.notebook import tqdm\ntqdm.pandas()\n\nfrom datetime import datetime\nimport time\nimport matplotlib.pyplot as plt\n\nimport os, json, cv2, random\nimport skimage.io as io\nimport copy\nfrom pathlib import Path\nfrom typing import Optional\nimport json\nimport matplotlib.pyplot as plt\n\nimport ast\n\n\nfrom tqdm import tqdm\nimport itertools\n\nimport torch\nimport albumentations as A\nfrom albumentations.pytorch.transforms import ToTensorV2\n\nfrom glob import glob\nimport numba\nfrom numba import jit\n\nfrom pycocotools.coco import COCO\n# detectron2\nfrom detectron2.structures import BoxMode\nfrom detectron2 import model_zoo\nfrom detectron2.config import get_cfg\nfrom detectron2.data import DatasetCatalog, MetadataCatalog\nfrom detectron2.engine import DefaultPredictor, DefaultTrainer, launch\nfrom detectron2.evaluation import COCOEvaluator\nfrom detectron2.structures import BoxMode\nfrom detectron2.utils.visualizer import ColorMode\nfrom detectron2.utils.logger import setup_logger\nfrom detectron2.utils.visualizer import Visualizer\n\nfrom detectron2.data import DatasetCatalog, MetadataCatalog, build_detection_test_loader, build_detection_train_loader\nfrom detectron2.data import detection_utils as utils\n\n\nfrom detectron2.data import DatasetCatalog, MetadataCatalog, build_detection_test_loader, build_detection_train_loader\nfrom detectron2.data import detection_utils as utils\nimport detectron2.data.transforms as T\nfrom detectron2.evaluation import COCOEvaluator, inference_on_dataset\n\n\n\nfrom detectron2.evaluation.evaluator import DatasetEvaluator\nimport pycocotools.mask as mask_util\nfrom detectron2.engine import BestCheckpointer\nfrom detectron2.checkpoint import DetectionCheckpointer\n\nsetup_logger()","metadata":{"execution":{"iopub.status.busy":"2022-01-21T21:47:10.094103Z","iopub.execute_input":"2022-01-21T21:47:10.094742Z","iopub.status.idle":"2022-01-21T21:47:10.119445Z","shell.execute_reply.started":"2022-01-21T21:47:10.094701Z","shell.execute_reply":"2022-01-21T21:47:10.118687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !python -m pip install 'git+https://github.com/facebookresearch/detectron2.git' --target=/kaggle/working/mysitepackages","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import sys\n# sys.path.append('../input/detectron-data/mysitepackages')","metadata":{"execution":{"iopub.status.busy":"2022-01-21T21:24:55.07127Z","iopub.execute_input":"2022-01-21T21:24:55.071971Z","iopub.status.idle":"2022-01-21T21:24:55.080343Z","shell.execute_reply.started":"2022-01-21T21:24:55.071929Z","shell.execute_reply":"2022-01-21T21:24:55.079652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read training dataset\ntrain_df = pd.read_csv(\"../input/tensorflow-great-barrier-reef/train.csv\")\ntest_df = pd.read_csv(\"../input/tensorflow-great-barrier-reef/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-01-21T21:47:18.742851Z","iopub.execute_input":"2022-01-21T21:47:18.743118Z","iopub.status.idle":"2022-01-21T21:47:18.78412Z","shell.execute_reply.started":"2022-01-21T21:47:18.743087Z","shell.execute_reply":"2022-01-21T21:47:18.783357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_df","metadata":{"execution":{"iopub.status.busy":"2022-01-21T21:47:18.932654Z","iopub.execute_input":"2022-01-21T21:47:18.933052Z","iopub.status.idle":"2022-01-21T21:47:18.937081Z","shell.execute_reply.started":"2022-01-21T21:47:18.933018Z","shell.execute_reply":"2022-01-21T21:47:18.936108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# eval(train_df[\"annotations\"][100])[0]","metadata":{"execution":{"iopub.status.busy":"2022-01-21T21:47:19.347249Z","iopub.execute_input":"2022-01-21T21:47:19.347506Z","iopub.status.idle":"2022-01-21T21:47:19.350951Z","shell.execute_reply.started":"2022-01-21T21:47:19.347475Z","shell.execute_reply":"2022-01-21T21:47:19.350209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a \"path\" column containing full path to the frames\nbase_folder = \"../input/tensorflow-great-barrier-reef/train_images\"\n\ntrain_df[\"path\"] = base_folder + \"/video_\" + \\\n                    train_df['video_id'].astype(str) + \"/\" +\\\n                    train_df['video_frame'].astype(str) +\".jpg\"\ntrain_df[\"Width\"]=1280\ntrain_df[\"Height\"]=720","metadata":{"execution":{"iopub.status.busy":"2022-01-21T21:47:19.500057Z","iopub.execute_input":"2022-01-21T21:47:19.500351Z","iopub.status.idle":"2022-01-21T21:47:19.573095Z","shell.execute_reply.started":"2022-01-21T21:47:19.500322Z","shell.execute_reply":"2022-01-21T21:47:19.572472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"NumBBox\"]=train_df['annotations'].apply(lambda x: str.count(x, 'x'))\ntrain_df.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-01-21T21:47:19.663738Z","iopub.execute_input":"2022-01-21T21:47:19.663926Z","iopub.status.idle":"2022-01-21T21:47:19.695627Z","shell.execute_reply.started":"2022-01-21T21:47:19.663903Z","shell.execute_reply":"2022-01-21T21:47:19.694923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_df[\"NumBBox\"].unique())\n","metadata":{"execution":{"iopub.status.busy":"2022-01-21T21:47:20.714298Z","iopub.execute_input":"2022-01-21T21:47:20.715791Z","iopub.status.idle":"2022-01-21T21:47:20.721865Z","shell.execute_reply.started":"2022-01-21T21:47:20.71574Z","shell.execute_reply":"2022-01-21T21:47:20.72107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train=train_df[train_df[\"NumBBox\"]>0]\ndf_train.reset_index(drop=True, inplace=True)\ndf_train","metadata":{"execution":{"iopub.status.busy":"2022-01-21T21:47:21.234974Z","iopub.execute_input":"2022-01-21T21:47:21.235475Z","iopub.status.idle":"2022-01-21T21:47:21.259172Z","shell.execute_reply.started":"2022-01-21T21:47:21.23544Z","shell.execute_reply":"2022-01-21T21:47:21.258514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.shape[0]","metadata":{"execution":{"iopub.status.busy":"2022-01-21T21:47:21.477994Z","iopub.execute_input":"2022-01-21T21:47:21.47879Z","iopub.status.idle":"2022-01-21T21:47:21.487239Z","shell.execute_reply.started":"2022-01-21T21:47:21.478745Z","shell.execute_reply":"2022-01-21T21:47:21.486511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"image_id\"][0]","metadata":{"execution":{"iopub.status.busy":"2022-01-21T21:47:21.703108Z","iopub.execute_input":"2022-01-21T21:47:21.703664Z","iopub.status.idle":"2022-01-21T21:47:21.708815Z","shell.execute_reply.started":"2022-01-21T21:47:21.703599Z","shell.execute_reply":"2022-01-21T21:47:21.708121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df_train[\"NumBBox\"].unique())\n","metadata":{"execution":{"iopub.status.busy":"2022-01-21T21:47:22.239411Z","iopub.execute_input":"2022-01-21T21:47:22.239741Z","iopub.status.idle":"2022-01-21T21:47:22.249812Z","shell.execute_reply.started":"2022-01-21T21:47:22.239704Z","shell.execute_reply":"2022-01-21T21:47:22.248814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dataset(train_df) : \n    base_folder = \"../input/tensorflow-great-barrier-reef/train_images\"\n    dataset_dict = []\n    for index in np.arange(train_df.shape[0]) : \n        record = {}\n        record[\"image_id\"] = train_df[\"image_id\"][index]\n        record[\"file_name\"] = train_df[\"path\"][index]\n        #record[\"file_name\"] = f'../input/tensorflow-great-barrier-reef/train_images/video_{train_df[\"video_id\"][index]}/{train_df[\"video_frame\"][index]}.jpg'\n        #../input/tensorflow-great-barrier-reef/train_images/video_0/0.jpg\n        record[\"width\"] = train_df[\"Width\"][index]\n        record[\"height\"] = train_df[\"Height\"][index]\n        class_id = 0\n        obj = []\n        \n        for box in np.arange(len(eval(train_df.annotations[index]))) : \n            annot = {}\n            annot[\"bbox\"] = [eval(train_df.annotations[index])[box]['x'],\n                            eval(train_df.annotations[index])[box]['y'],\n                            eval(train_df.annotations[index])[box]['width'],\n                            eval(train_df.annotations[index])[box]['height']]\n            annot[\"bbox_mode\"] =  BoxMode.XYWH_ABS\n            annot[\"category_id\"] = class_id\n            obj.append(annot)\n        record[\"annotations\"] = obj\n        dataset_dict.append(record)\n    return dataset_dict \n    ","metadata":{"execution":{"iopub.status.busy":"2022-01-21T21:47:22.499457Z","iopub.execute_input":"2022-01-21T21:47:22.500011Z","iopub.status.idle":"2022-01-21T21:47:22.512581Z","shell.execute_reply.started":"2022-01-21T21:47:22.499971Z","shell.execute_reply":"2022-01-21T21:47:22.511639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_dataset =  load_dataset(df_train)\n","metadata":{"execution":{"iopub.status.busy":"2022-01-21T21:47:22.794345Z","iopub.execute_input":"2022-01-21T21:47:22.795084Z","iopub.status.idle":"2022-01-21T21:47:22.799847Z","shell.execute_reply.started":"2022-01-21T21:47:22.795034Z","shell.execute_reply":"2022-01-21T21:47:22.798036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_dataset","metadata":{"execution":{"iopub.status.busy":"2022-01-21T20:23:36.019119Z","iopub.execute_input":"2022-01-21T20:23:36.019545Z","iopub.status.idle":"2022-01-21T20:23:36.022469Z","shell.execute_reply.started":"2022-01-21T20:23:36.019516Z","shell.execute_reply":"2022-01-21T20:23:36.021844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# len(train_dataset)","metadata":{"execution":{"iopub.status.busy":"2022-01-21T20:23:36.49314Z","iopub.execute_input":"2022-01-21T20:23:36.493742Z","iopub.status.idle":"2022-01-21T20:23:36.499865Z","shell.execute_reply.started":"2022-01-21T20:23:36.493704Z","shell.execute_reply":"2022-01-21T20:23:36.499114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_dataset[90:100]","metadata":{"execution":{"iopub.status.busy":"2022-01-21T20:23:36.667684Z","iopub.execute_input":"2022-01-21T20:23:36.668002Z","iopub.status.idle":"2022-01-21T20:23:36.673533Z","shell.execute_reply.started":"2022-01-21T20:23:36.667972Z","shell.execute_reply":"2022-01-21T20:23:36.672752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_dataset[100]","metadata":{"execution":{"iopub.status.busy":"2022-01-21T20:23:36.851224Z","iopub.execute_input":"2022-01-21T20:23:36.85144Z","iopub.status.idle":"2022-01-21T20:23:36.855212Z","shell.execute_reply.started":"2022-01-21T20:23:36.851413Z","shell.execute_reply":"2022-01-21T20:23:36.853982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# DatasetCatalog.register(\"train_dataset\" ,lambda d=df_train: load_dataset(d)[:])\n#atasetCatalog.register(\"test_dataset\" ,lambda d=df_train: load_dataset(d)[4500:])\n\n# #DatasetCatalog.register(\"test_dataset\" ,lambda d=train_dataset[90:100]: load_dataset(d))\n","metadata":{"execution":{"iopub.status.busy":"2022-01-21T21:47:26.297603Z","iopub.execute_input":"2022-01-21T21:47:26.298166Z","iopub.status.idle":"2022-01-21T21:47:26.3019Z","shell.execute_reply.started":"2022-01-21T21:47:26.298127Z","shell.execute_reply":"2022-01-21T21:47:26.301165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from detectron2.engine import DefaultTrainer\n\n# cfg = get_cfg()\n# cfg.merge_from_file(model_zoo.get_config_file(\"COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml\"))\n# cfg.DATASETS.TRAIN = (\"train_dataset\",)\n# cfg.DATASETS.TEST = ()\n# #cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 \n# cfg.DATALOADER.NUM_WORKERS = 2\n# cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(\"COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml\")  # Let training initialize from model zoo\n# cfg.SOLVER.IMS_PER_BATCH = 2\n# cfg.SOLVER.BASE_LR = 0.001 # pick a good LR\n# cfg.SOLVER.MAX_ITER = 40000  # 300 iterations seems good enough for this toy dataset; you will need to train longer for a practical dataset\n# cfg.SOLVER.GAMMA = 0.1\n# # The iteration number to decrease learning rate by GAMMA.\n# cfg.SOLVER.STEPS = (10000,)\n# #cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 2   # faster, and good enough for this toy dataset (default: 512)\n# cfg.MODEL.ROI_HEADS.NUM_CLASSES = 1 # only has one class (ballon). (see https://detectron2.readthedocs.io/tutorials/datasets.html#update-the-config-for-new-datasets)\n# # NOTE: this config means the number of classes, but a few popular unofficial tutorials incorrect uses num_classes+1 here.\n\n# os.makedirs(cfg.OUTPUT_DIR, exist_ok=True)\n# trainer = DefaultTrainer(cfg) \n# trainer.resume_or_load(resume=False)\n# trainer.train()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from detectron2.engine import DefaultTrainer\n\n# cfg = get_cfg()\n# cfg.merge_from_file(model_zoo.get_config_file(\"COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml\"))\n# cfg.DATASETS.TRAIN = (\"train_dataset\")\n# #cfg.DATASETS.TEST = (\"test_dataset\")\n# cfg.DATALOADER.NUM_WORKERS = 2\n# #cfg.OUTPUT_DIR = '../input/detectron-data/output'\n\n# cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(\"COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml\")  # Let training initialize from model zoo\n# #cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, \"model_final.pth\")\n# cfg.SOLVER.IMS_PER_BATCH = 2\n# cfg.SOLVER.BASE_LR = 0.00025  # pick a good LR\n# cfg.SOLVER.MAX_ITER = 100    # 300 iterations seems good enough for this toy dataset; you will need to train longer for a practical dataset\n# cfg.SOLVER.STEPS = []        # do not decay learning rate\n# #cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 128   # faster, and good enough for this toy dataset (default: 512)\n# cfg.MODEL.ROI_HEADS.NUM_CLASSES = 1  # only has one class (ballon). (see https://detectron2.readthedocs.io/tutorials/datasets.html#update-the-config-for-new-datasets)\n# # NOTE: this config means the number of classes, but a few popular unofficial tutorials incorrect uses num_classes+1 here.\n# os.makedirs(cfg.OUTPUT_DIR, exist_ok=True)\n# trainer = DefaultTrainer(cfg) \n# trainer.resume_or_load(resume=False)\n# trainer.train()\n","metadata":{"execution":{"iopub.status.busy":"2022-01-21T20:23:38.694611Z","iopub.execute_input":"2022-01-21T20:23:38.694879Z","iopub.status.idle":"2022-01-21T20:23:38.699405Z","shell.execute_reply.started":"2022-01-21T20:23:38.694846Z","shell.execute_reply":"2022-01-21T20:23:38.698263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# im = cv2.imread(train_df['path'][100])\n","metadata":{"execution":{"iopub.status.busy":"2022-01-22T01:11:21.801071Z","iopub.execute_input":"2022-01-22T01:11:21.801869Z","iopub.status.idle":"2022-01-22T01:11:21.829997Z","shell.execute_reply.started":"2022-01-22T01:11:21.801833Z","shell.execute_reply":"2022-01-22T01:11:21.829224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_dataset[100]","metadata":{"execution":{"iopub.status.busy":"2022-01-22T00:51:45.082605Z","iopub.execute_input":"2022-01-22T00:51:45.083232Z","iopub.status.idle":"2022-01-22T00:51:45.087345Z","shell.execute_reply.started":"2022-01-22T00:51:45.083195Z","shell.execute_reply":"2022-01-22T00:51:45.086535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Inference should use the config with parameters that are used in training\n# # cfg now already contains everything we've set previously. We changed it a little bit for inference:\nfrom detectron2.engine import DefaultTrainer\n\ncfg = get_cfg()\ncfg.merge_from_file(model_zoo.get_config_file(\"COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml\"))\ncfg.OUTPUT_DIR = '../input/detectron-data/output'\n\ncfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, \"model_final.pth\")  # path to the model we just trained\ncfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.3# set a custom testing threshold\ncfg.MODEL.ROI_HEADS.NUM_CLASSES = 1\n\npredictor = DefaultPredictor(cfg)\n# outputs = predictor(array[0])\n# outputs","metadata":{"execution":{"iopub.status.busy":"2022-01-22T01:11:23.734192Z","iopub.execute_input":"2022-01-22T01:11:23.734446Z","iopub.status.idle":"2022-01-22T01:11:24.718826Z","shell.execute_reply.started":"2022-01-22T01:11:23.734417Z","shell.execute_reply":"2022-01-22T01:11:24.718016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# array = np.load('../input/tensorflow-great-barrier-reef/example_test.npy')","metadata":{"execution":{"iopub.status.busy":"2022-01-22T01:11:24.720904Z","iopub.execute_input":"2022-01-22T01:11:24.721163Z","iopub.status.idle":"2022-01-22T01:11:24.725775Z","shell.execute_reply.started":"2022-01-22T01:11:24.72113Z","shell.execute_reply":"2022-01-22T01:11:24.724754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# array.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-22T01:11:24.727323Z","iopub.execute_input":"2022-01-22T01:11:24.727955Z","iopub.status.idle":"2022-01-22T01:11:24.736415Z","shell.execute_reply.started":"2022-01-22T01:11:24.727918Z","shell.execute_reply":"2022-01-22T01:11:24.735715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_dataset[100]","metadata":{"execution":{"iopub.status.busy":"2022-01-22T01:11:24.924919Z","iopub.execute_input":"2022-01-22T01:11:24.92522Z","iopub.status.idle":"2022-01-22T01:11:24.928633Z","shell.execute_reply.started":"2022-01-22T01:11:24.925192Z","shell.execute_reply":"2022-01-22T01:11:24.927952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# outputs = predictor(im)\n\n# outputs","metadata":{"execution":{"iopub.status.busy":"2022-01-22T01:11:25.670938Z","iopub.execute_input":"2022-01-22T01:11:25.671203Z","iopub.status.idle":"2022-01-22T01:11:25.773263Z","shell.execute_reply.started":"2022-01-22T01:11:25.671173Z","shell.execute_reply":"2022-01-22T01:11:25.772485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_df['annotations'][100]","metadata":{"execution":{"iopub.status.busy":"2022-01-22T00:54:28.073088Z","iopub.execute_input":"2022-01-22T00:54:28.073658Z","iopub.status.idle":"2022-01-22T00:54:28.078779Z","shell.execute_reply.started":"2022-01-22T00:54:28.073601Z","shell.execute_reply":"2022-01-22T00:54:28.078004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test = pd.read_csv('../input/tensorflow-great-barrier-reef/example_sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-01-20T14:58:10.985937Z","iopub.execute_input":"2022-01-20T14:58:10.986203Z","iopub.status.idle":"2022-01-20T14:58:10.989719Z","shell.execute_reply.started":"2022-01-20T14:58:10.98617Z","shell.execute_reply":"2022-01-20T14:58:10.988985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test","metadata":{"execution":{"iopub.status.busy":"2022-01-19T13:54:49.918739Z","iopub.status.idle":"2022-01-19T13:54:49.919221Z","shell.execute_reply.started":"2022-01-19T13:54:49.918974Z","shell.execute_reply":"2022-01-19T13:54:49.919013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def pred(array): \n#     for im,i in zip(array,np.arange(array.shape[0])) : \n#         outputs = predictor(im)\n#         pred_boxes = outputs[\"instances\"].pred_boxes.tensor.cpu().numpy()\n#         scores = outputs[\"instances\"].scores.cpu().numpy()\n#         test.row_num[0] =  i\n#         annotations = []\n#         for annot in np.arange(len(scores)): \n#             annotations.append(np.concatenate((scores[annot],pred_boxes[annot]),axis=None))","metadata":{"execution":{"iopub.status.busy":"2022-01-22T00:42:11.008734Z","iopub.execute_input":"2022-01-22T00:42:11.009Z","iopub.status.idle":"2022-01-22T00:42:11.018346Z","shell.execute_reply.started":"2022-01-22T00:42:11.008971Z","shell.execute_reply":"2022-01-22T00:42:11.017292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from detectron2.modeling import build_model\n# cfg = get_cfg()\n# cfg.merge_from_file(model_zoo.get_config_file(\"COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml\"))\n# cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(\"COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml\")  # Let training initialize from model zoo\n\n# model = build_model(cfg)\n# DetectionCheckpointer(model).load('../input/detectron-data/output/model_final.pth')  # load a file, usually from cfg.MODEL.WEIGHTS\n# model.eval()\n","metadata":{"execution":{"iopub.status.busy":"2022-01-19T13:54:49.922399Z","iopub.status.idle":"2022-01-19T13:54:49.922855Z","shell.execute_reply.started":"2022-01-19T13:54:49.922611Z","shell.execute_reply":"2022-01-19T13:54:49.922635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pred(im): \n    outputs = predictor(im)\n    array = np.array([])\n    pred_boxes = outputs[\"instances\"].pred_boxes.tensor.cpu().numpy()\n    scores = outputs[\"instances\"].scores.cpu().numpy()\n    annotations = []\n    for annot in np.arange(len(scores)): \n        array = np.concatenate((array,scores[annot],pred_boxes[annot][0],pred_boxes[annot][1],pred_boxes[annot][2]-pred_boxes[annot][0],\n                               pred_boxes[annot][3]-pred_boxes[annot][1]),axis=None)\n    return array","metadata":{"execution":{"iopub.status.busy":"2022-01-22T00:53:22.060328Z","iopub.execute_input":"2022-01-22T00:53:22.060643Z","iopub.status.idle":"2022-01-22T00:53:22.06855Z","shell.execute_reply.started":"2022-01-22T00:53:22.060587Z","shell.execute_reply":"2022-01-22T00:53:22.067791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# \" \".join(list(pred(im).astype(str)))","metadata":{"execution":{"iopub.status.busy":"2022-01-19T13:54:49.926825Z","iopub.status.idle":"2022-01-19T13:54:49.927408Z","shell.execute_reply.started":"2022-01-19T13:54:49.927162Z","shell.execute_reply":"2022-01-19T13:54:49.927189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(outputs)","metadata":{"execution":{"iopub.status.busy":"2022-01-19T13:54:49.928526Z","iopub.status.idle":"2022-01-19T13:54:49.929115Z","shell.execute_reply.started":"2022-01-19T13:54:49.928857Z","shell.execute_reply":"2022-01-19T13:54:49.928884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # look at the outputs. See https://detectron2.readthedocs.io/tutorials/models.html#model-output-format for specification\n# print(outputs[\"instances\"].pred_boxes.tensor.cpu().numpy())\n# print(outputs[\"instances\"].scores.cpu().numpy())","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:06:38.770802Z","iopub.execute_input":"2022-01-17T02:06:38.771093Z","iopub.status.idle":"2022-01-17T02:06:38.777178Z","shell.execute_reply.started":"2022-01-17T02:06:38.771048Z","shell.execute_reply":"2022-01-17T02:06:38.776235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # We can use `Visualizer` to draw the predictions on the image.\n# v = Visualizer(im, MetadataCatalog.get(cfg.DATASETS.TRAIN[0]))\n# out = v.draw_instance_predictions(outputs[\"instances\"].to(\"cpu\"))\n# plt.imshow(out.get_image()[:, :, ::-1])","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:06:38.778858Z","iopub.execute_input":"2022-01-17T02:06:38.779277Z","iopub.status.idle":"2022-01-17T02:06:38.785667Z","shell.execute_reply.started":"2022-01-17T02:06:38.779108Z","shell.execute_reply":"2022-01-17T02:06:38.78488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for d in random.sample(train_dataset, 1):    \n#     im = cv2.imread(d[\"file_name\"])\n#     outputs = predictor(im)  # format is documented at https://detectron2.readthedocs.io/tutorials/models.html#model-output-format\n#     # look at the outputs. See https://detectron2.readthedocs.io/tutorials/models.html#model-output-format for specification\n#     print(outputs[\"instances\"].pred_classes)\n#     print(outputs[\"instances\"].pred_boxes)\n#     v = Visualizer(im[:, :, ::-1],\n                    \n#                    scale=0.5, \n#     )\n#     out = v.draw_instance_predictions(outputs[\"instances\"].to(\"cpu\"))\n# plt.imshow(out.get_image()[:, :, ::-1])","metadata":{"execution":{"iopub.status.busy":"2022-01-17T02:06:38.787187Z","iopub.execute_input":"2022-01-17T02:06:38.788065Z","iopub.status.idle":"2022-01-17T02:06:38.793882Z","shell.execute_reply.started":"2022-01-17T02:06:38.788028Z","shell.execute_reply":"2022-01-17T02:06:38.79315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import greatbarrierreef\nenv = greatbarrierreef.make_env()   # initialize the environment\niter_test = env.iter_test()    # an iterator which loops over the test set and sample submission\nfor (pixel_array, sample_prediction_df) in iter_test:\n    sample_prediction_df['annotations'] = \" \".join(list(pred(pixel_array).astype(str)))  # make your predictions here\n    env.predict(sample_prediction_df)   # register your predictions","metadata":{"execution":{"iopub.status.busy":"2022-01-20T13:25:13.364597Z","iopub.execute_input":"2022-01-20T13:25:13.364851Z","iopub.status.idle":"2022-01-20T13:25:13.904147Z","shell.execute_reply.started":"2022-01-20T13:25:13.364825Z","shell.execute_reply":"2022-01-20T13:25:13.902396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}