{"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":"# !cd ..\n!pip install ../input/detectron-05/whls/pycocotools-2.0.2/dist/pycocotools-2.0.2.tar --no-index --find-links ../input/detectron-05/whls \n!pip install ../input/detectron-05/whls/fvcore-0.1.5.post20211019/fvcore-0.1.5.post20211019 --no-index --find-links ../input/detectron-05/whls \n!pip install ../input/detectron-05/whls/antlr4-python3-runtime-4.8/antlr4-python3-runtime-4.8 --no-index --find-links ../input/detectron-05/whls \n!pip install ../input/detectron-05/whls/detectron2-0.5/detectron2 --no-index --find-links ../input/detectron-05/whls","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-11-26T09:50:01.376650Z","iopub.execute_input":"2021-11-26T09:50:01.377421Z","iopub.status.idle":"2021-11-26T09:53:19.014213Z","shell.execute_reply.started":"2021-11-26T09:50:01.377318Z","shell.execute_reply":"2021-11-26T09:53:19.013322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\n# import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport json\nimport cv2\nimport random\nimport time\nfrom tqdm import tqdm\n# (OPTIONAL) step\n# Preparing the Dataset\n# Convert all images to .jpg (This is not necessary but made my life easier down the line)\n\nfrom PIL import Image\n# check pytorch installation: \nimport torch, torchvision\nprint(torch.__version__, torch.cuda.is_available())\nassert torch.__version__.startswith(\"1.9\")\n\n# Some basic setup:\n# Setup detectron2 logger\nimport detectron2\nfrom detectron2.utils.logger import setup_logger\nsetup_logger()\n\n# import some common libraries\nimport numpy as np\nimport os, json, cv2, random\n# from google.colab.patches import cv2_imshow\n\n# import some common detectron2 utilities\nfrom detectron2 import model_zoo\nfrom detectron2.engine import DefaultPredictor\nfrom detectron2.config import get_cfg\nfrom detectron2.utils.visualizer import Visualizer\nfrom detectron2.data import MetadataCatalog, DatasetCatalog\nimport greatbarrierreef","metadata":{"execution":{"iopub.status.busy":"2021-11-26T09:54:34.102037Z","iopub.execute_input":"2021-11-26T09:54:34.102826Z","iopub.status.idle":"2021-11-26T09:54:35.514850Z","shell.execute_reply.started":"2021-11-26T09:54:34.102785Z","shell.execute_reply":"2021-11-26T09:54:35.514165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from detectron2.engine import DefaultTrainer\nfrom detectron2.config import get_cfg\nimport os\n\ncfg = get_cfg()\ncfg.merge_from_file(model_zoo.get_config_file(\"COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml\"))\n# cfg.DATASETS.TRAIN = (\"experiment\",)\n# cfg.DATASETS.TEST = ()   # no metrics implemented for this dataset\ncfg.DATALOADER.NUM_WORKERS = 2\n# cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(\"COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml\")# initialize from model zoo\n# cfg.SOLVER.IMS_PER_BATCH = 2\n# cfg.SOLVER.BASE_LR = 0.0005 \n# cfg.SOLVER.MAX_ITER = 5000   # 10000 iterations seems good enough, but you can certainly train longer\n# cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 128   # faster, and good enough for this toy dataset\ncfg.MODEL.ROI_HEADS.NUM_CLASSES = 1  # 3 classes (Person, Helmet, Car)\ncfg.MODEL.WEIGHTS = \"../input/detectron2-fasterrcnn-model-5000-epc/model_final.pth\"\ncfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5   # set the testing threshold for this model\ncfg.DATASETS.TEST = (\"experiment\",)\npredictor = DefaultPredictor(cfg)","metadata":{"execution":{"iopub.status.busy":"2021-11-26T09:54:52.489745Z","iopub.execute_input":"2021-11-26T09:54:52.490455Z","iopub.status.idle":"2021-11-26T09:54:53.322489Z","shell.execute_reply.started":"2021-11-26T09:54:52.490419Z","shell.execute_reply":"2021-11-26T09:54:53.321738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_image_into_numpy_array(path):\n    \"\"\"Load an image from file into a numpy array.\n\n    Puts image into numpy array to feed into tensorflow graph.\n    Note that by convention we put it into a numpy array with shape\n    (height, width, channels), where channels=3 for RGB.\n\n    Args:\n    path: a file path (this can be local or on colossus)\n\n    Returns:\n    uint8 numpy array with shape (img_height, img_width, 3)\n    \"\"\"\n    img_data = tf.io.gfile.GFile(path, 'rb').read()\n    image = Image.open(io.BytesIO(img_data))\n    (im_width, im_height) = image.size\n    \n    return np.array(image.getdata()).reshape(\n      (im_height, im_width, 3)).astype(np.uint8)\n\ndef detect(image_np):\n    \"\"\"Detect COTS from a given numpy image.\"\"\"\n\n#     input_tensor = np.expand_dims(image_np, 0)\n#     start_time = time.time()\n    detections = predictor(image_np)\n    return detections","metadata":{"execution":{"iopub.status.busy":"2021-11-26T09:54:54.815799Z","iopub.execute_input":"2021-11-26T09:54:54.818934Z","iopub.status.idle":"2021-11-26T09:54:54.826617Z","shell.execute_reply.started":"2021-11-26T09:54:54.818866Z","shell.execute_reply":"2021-11-26T09:54:54.825594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"env = greatbarrierreef.make_env()   # initialize the environment\niter_test = env.iter_test()    # an iterator which loops over the test set and sample submission","metadata":{"execution":{"iopub.status.busy":"2021-11-26T09:54:56.323462Z","iopub.execute_input":"2021-11-26T09:54:56.324078Z","iopub.status.idle":"2021-11-26T09:54:56.329716Z","shell.execute_reply.started":"2021-11-26T09:54:56.324036Z","shell.execute_reply":"2021-11-26T09:54:56.327673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DETECTION_THRESHOLD = 0.3\n\nsubmission_dict = {\n    'id': [],\n    'prediction_string': [],\n}\n\nfor (image_np, sample_prediction_df) in iter_test:\n\n    height, width, _ = image_np.shape\n    detections = detect(image_np)\n\n    num_detections = len(detections[\"instances\"].pred_boxes)\n    predictions = []\n    for index in range(num_detections):\n        score = detections[\"instances\"].scores.to(\"cpu\").numpy()[index]\n        if score < DETECTION_THRESHOLD:\n            continue\n\n        x1, y1, x2, y2 = np.asarray(detections[\"instances\"].pred_boxes)[0].to(\"cpu\").numpy()\n\n        predictions.append('{:.2f} {} {} {} {}'.format(score, int(x1), int(y1), int(x2), int(y2)))\n    \n    \n    prediction_str = ' '.join(predictions)\n    sample_prediction_df['annotations'] = prediction_str\n    env.predict(sample_prediction_df)\n\n    print('Prediction:', prediction_str)","metadata":{"execution":{"iopub.status.busy":"2021-11-26T09:54:58.048566Z","iopub.execute_input":"2021-11-26T09:54:58.049166Z","iopub.status.idle":"2021-11-26T09:55:04.205133Z","shell.execute_reply.started":"2021-11-26T09:54:58.049128Z","shell.execute_reply":"2021-11-26T09:55:04.203692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from pandas.compat import StringIO","metadata":{"execution":{"iopub.status.busy":"2021-11-26T09:42:27.142297Z","iopub.execute_input":"2021-11-26T09:42:27.142670Z","iopub.status.idle":"2021-11-26T09:42:27.189843Z","shell.execute_reply.started":"2021-11-26T09:42:27.142629Z","shell.execute_reply":"2021-11-26T09:42:27.187907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import pandas\n# print(pandas.__version__)","metadata":{"execution":{"iopub.status.busy":"2021-11-26T09:43:08.683473Z","iopub.execute_input":"2021-11-26T09:43:08.683739Z","iopub.status.idle":"2021-11-26T09:43:08.688493Z","shell.execute_reply.started":"2021-11-26T09:43:08.683708Z","shell.execute_reply":"2021-11-26T09:43:08.687649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !cat /opt/conda/lib/python3.7/site-packages/pandas/io/formats/csvs.py","metadata":{"execution":{"iopub.status.busy":"2021-11-26T09:43:35.907527Z","iopub.execute_input":"2021-11-26T09:43:35.907786Z","iopub.status.idle":"2021-11-26T09:43:36.574513Z","shell.execute_reply.started":"2021-11-26T09:43:35.907757Z","shell.execute_reply":"2021-11-26T09:43:36.573665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !cat /opt/conda/lib/python3.7/site-packages/pandas/compat/__init__.py","metadata":{"execution":{"iopub.status.busy":"2021-11-26T09:42:29.593696Z","iopub.execute_input":"2021-11-26T09:42:29.593976Z","iopub.status.idle":"2021-11-26T09:42:30.284408Z","shell.execute_reply.started":"2021-11-26T09:42:29.593932Z","shell.execute_reply":"2021-11-26T09:42:30.283330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#     print(detections)\n#     print(\"======================================================\")\n#     preds = detections[\"instances\"].to(\"cpu\")\n#     print(f\"preds cpu: {preds}\")\n#     print(\"======================================================\")\n# #     print(f\"num of instances: {len(detections[\"instances\"].pred_boxes)}\")\n#     print(len(detections[\"instances\"].pred_boxes))\n#     print(\"======================================================\")\n#     print(np.asarray(detections[\"instances\"].pred_boxes)[0].to(\"cpu\").numpy())\n# #     print(\"======================================================\")\n# #     print(detections[\"instances\"].pred_boxes.to(\"cpu\").numpy())\n#     print(\"======================================================\")\n#     print(detections[\"instances\"].scores.to(\"cpu\").numpy())\n#     print(\"======================================================\")\n","metadata":{},"execution_count":null,"outputs":[]}]}