{"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 uninstall -y torch\n# !pip install torch==1.7.1\n# !pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu110/torch1.7.0/index.html\n# !pip install mmdet\n# !git clone https://github.com/open-mmlab/mmdetection.git","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:40:58.170923Z","iopub.execute_input":"2021-12-14T02:40:58.171282Z","iopub.status.idle":"2021-12-14T02:40:58.193746Z","shell.execute_reply.started":"2021-12-14T02:40:58.171189Z","shell.execute_reply":"2021-12-14T02:40:58.193089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cp -r ../input/faster-config ./\n%cp -r ../input/faster-r ./\n%cp -r ../input/mmdetection -r ./","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:40:58.195154Z","iopub.execute_input":"2021-12-14T02:40:58.195754Z","iopub.status.idle":"2021-12-14T02:41:18.520207Z","shell.execute_reply.started":"2021-12-14T02:40:58.195715Z","shell.execute_reply":"2021-12-14T02:41:18.519229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/faster-r/打包库\n!ls","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:41:18.52291Z","iopub.execute_input":"2021-12-14T02:41:18.523177Z","iopub.status.idle":"2021-12-14T02:41:19.235224Z","shell.execute_reply.started":"2021-12-14T02:41:18.52314Z","shell.execute_reply":"2021-12-14T02:41:19.234331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip uninstall -y torch\n!pip install torch-1.7.1+cu110-cp37-cp37m-linux_x86_64.whl -f ./ --no-index","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:41:19.238699Z","iopub.execute_input":"2021-12-14T02:41:19.23933Z","iopub.status.idle":"2021-12-14T02:42:11.330269Z","shell.execute_reply.started":"2021-12-14T02:41:19.239286Z","shell.execute_reply":"2021-12-14T02:42:11.329432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install addict-2.4.0-py3-none-any.whl -f ./ --no-index\n!pip install terminaltables-3.1.10-py2.py3-none-any.whl -f ./ --no-index\n!pip install yapf-0.31.0-py2.py3-none-any.whl -f ./ --no-index","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:42:11.332109Z","iopub.execute_input":"2021-12-14T02:42:11.332447Z","iopub.status.idle":"2021-12-14T02:42:34.290136Z","shell.execute_reply.started":"2021-12-14T02:42:11.332407Z","shell.execute_reply":"2021-12-14T02:42:34.28932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/faster-r/打包库/pycocotools-2.0.3/\n!tar -xvf pycocotools-2.0.3.tar\n%cd /kaggle/working/faster-r/打包库/pycocotools-2.0.3/pycocotools-2.0.3\n# !python setup.py install --prefix=/kaggle/working\n!python setup.py install build_ext --inplace ","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:42:34.291931Z","iopub.execute_input":"2021-12-14T02:42:34.2922Z","iopub.status.idle":"2021-12-14T02:42:43.542051Z","shell.execute_reply.started":"2021-12-14T02:42:34.292162Z","shell.execute_reply":"2021-12-14T02:42:43.541243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pycocotools","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:42:43.543686Z","iopub.execute_input":"2021-12-14T02:42:43.543945Z","iopub.status.idle":"2021-12-14T02:42:43.552365Z","shell.execute_reply.started":"2021-12-14T02:42:43.543907Z","shell.execute_reply":"2021-12-14T02:42:43.551715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/faster-r/打包库\n!pip install mmcv_full-1.4.0-cp37-cp37m-manylinux1_x86_64.whl -f ./ --no-index","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:42:43.553861Z","iopub.execute_input":"2021-12-14T02:42:43.554121Z","iopub.status.idle":"2021-12-14T02:42:52.623336Z","shell.execute_reply.started":"2021-12-14T02:42:43.554085Z","shell.execute_reply":"2021-12-14T02:42:52.622443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/mmdetection\n!pip install -r requirements/build.txt\n!pip install -v -e .","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:42:52.627032Z","iopub.execute_input":"2021-12-14T02:42:52.627285Z","iopub.status.idle":"2021-12-14T02:43:54.156859Z","shell.execute_reply.started":"2021-12-14T02:42:52.627242Z","shell.execute_reply":"2021-12-14T02:43:54.156009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check Pytorch installation\nimport torch, torchvision\nprint(torch.__version__, torch.cuda.is_available())\n\n# Check MMDetection installation\nimport mmdet\nprint(mmdet.__version__)\n\n# Check mmcv installation\nfrom mmcv.ops import get_compiling_cuda_version, get_compiler_version\nprint(get_compiling_cuda_version())\nprint(get_compiler_version())","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:43:54.160741Z","iopub.execute_input":"2021-12-14T02:43:54.160953Z","iopub.status.idle":"2021-12-14T02:44:02.755594Z","shell.execute_reply.started":"2021-12-14T02:43:54.160927Z","shell.execute_reply":"2021-12-14T02:44:02.754687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import copy\nimport os.path as osp\nimport pandas as pd\nimport mmcv\nfrom mmcv import Config\nimport numpy as np\nfrom mmdet.apis import inference_detector, init_detector, show_result_pyplot\nfrom mmdet.datasets.builder import DATASETS\nfrom mmdet.datasets.custom import CustomDataset","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:44:02.757165Z","iopub.execute_input":"2021-12-14T02:44:02.757627Z","iopub.status.idle":"2021-12-14T02:44:08.898631Z","shell.execute_reply.started":"2021-12-14T02:44:02.757584Z","shell.execute_reply":"2021-12-14T02:44:08.897923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/mmdetection/","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:44:08.89987Z","iopub.execute_input":"2021-12-14T02:44:08.900132Z","iopub.status.idle":"2021-12-14T02:44:08.905979Z","shell.execute_reply.started":"2021-12-14T02:44:08.900099Z","shell.execute_reply":"2021-12-14T02:44:08.904988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# config = 'configs/yolox/yolox_tiny_8x8_300e_coco.py'\ncfg = Config.fromfile('configs/faster_rcnn/faster_rcnn_r50_caffe_fpn_mstrain_1x_coco.py')\nfrom mmdet.apis import set_random_seed\ncfg.model.roi_head.bbox_head.num_classes = 1\ncfg.optimizer.lr = 0.02/8\ncfg.seed = 0\nset_random_seed(0, deterministic=False)\ncfg.gpu_ids = range(1)","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:44:08.907113Z","iopub.execute_input":"2021-12-14T02:44:08.907565Z","iopub.status.idle":"2021-12-14T02:44:08.941277Z","shell.execute_reply.started":"2021-12-14T02:44:08.907527Z","shell.execute_reply":"2021-12-14T02:44:08.94062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Setup a checkpoint file to load\n# checkpoint = 'checkpoints/yolox_tiny_8x8_300e_coco_20210806_234250-4ff3b67e.pth'\n%cd /kaggle/working\ncheckpoint = 'faster-config/config/epoch_12.pth'\n# initialize the detector\nmodel = init_detector(cfg, checkpoint, device='cuda:0')\nmodel.CLASSES = 'Starfish'","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:44:08.942484Z","iopub.execute_input":"2021-12-14T02:44:08.942715Z","iopub.status.idle":"2021-12-14T02:44:12.460588Z","shell.execute_reply.started":"2021-12-14T02:44:08.942683Z","shell.execute_reply":"2021-12-14T02:44:12.459812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\ndef file_name(file_dir):\n    dirs = os.listdir(file_dir)\n    L = []\n    for file in dirs:\n        L.append(file_dir + file )\n    return L","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:44:12.461985Z","iopub.execute_input":"2021-12-14T02:44:12.462248Z","iopub.status.idle":"2021-12-14T02:44:12.46657Z","shell.execute_reply.started":"2021-12-14T02:44:12.462215Z","shell.execute_reply":"2021-12-14T02:44:12.465926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_list = file_name('../input/tensorflow-great-barrier-reef/train_images/video_2/')[0:200]","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:44:12.468231Z","iopub.execute_input":"2021-12-14T02:44:12.468865Z","iopub.status.idle":"2021-12-14T02:44:12.616785Z","shell.execute_reply.started":"2021-12-14T02:44:12.468816Z","shell.execute_reply":"2021-12-14T02:44:12.616124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_list[0]","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:44:12.618114Z","iopub.execute_input":"2021-12-14T02:44:12.618372Z","iopub.status.idle":"2021-12-14T02:44:12.626907Z","shell.execute_reply.started":"2021-12-14T02:44:12.618338Z","shell.execute_reply":"2021-12-14T02:44:12.626277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\nimport plotly.express as px\nimport plotly.graph_objects as go\nimport seaborn as sns\nsns.set_style('darkgrid')\n\nfrom PIL import Image, ImageDraw\nimport tensorflow as tf\n\nimport os\nimport ast  ## Change str -> list.\nimport sys\nimport time\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\nimport greatbarrierreef","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:44:12.62796Z","iopub.execute_input":"2021-12-14T02:44:12.628617Z","iopub.status.idle":"2021-12-14T02:44:19.205893Z","shell.execute_reply.started":"2021-12-14T02:44:12.628576Z","shell.execute_reply":"2021-12-14T02:44:19.204972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def img_viz(img, annotations):\n    img = Image.open(img)\n    for box in annotations:\n        box = box.split(' ')\n        \n        shape = [int(box[1]), int(box[2]), int(box[1])+int(box[3]), int(box[2]) + int(box[4])]\n        ImageDraw.Draw(img).rectangle(shape, outline =\"red\", width=3)\n    display(img)","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:44:19.207179Z","iopub.execute_input":"2021-12-14T02:44:19.207629Z","iopub.status.idle":"2021-12-14T02:44:19.214735Z","shell.execute_reply.started":"2021-12-14T02:44:19.207586Z","shell.execute_reply":"2021-12-14T02:44:19.214076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction_str =[]\nfor image_np in test_list:\n    img = mmcv.imread(image_np)\n    result = inference_detector(model, img)\n#     show_result_pyplot(model, img, result, score_thr=0.3)\n    predictions = []\n    for i in result[0]:\n        score = i[4]\n        try:\n            if score > 0.8:\n\n                x_min = int(i[0])\n                y_min = int(i[1])\n                x_max = int(i[2])\n                y_max = int(i[3])\n\n                bbox_width = x_max - x_min\n                bbox_height = y_max - y_min\n\n                predictions.append('{:.2f} {} {} {} {}'.format(score, x_min, y_min, bbox_width, bbox_height))\n        except:\n            continue\n    prediction_str.append([image_np,predictions])","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:44:19.21594Z","iopub.execute_input":"2021-12-14T02:44:19.216354Z","iopub.status.idle":"2021-12-14T02:44:45.12284Z","shell.execute_reply.started":"2021-12-14T02:44:19.216312Z","shell.execute_reply":"2021-12-14T02:44:45.122083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ture = pd.read_csv('../input/tensorflow-great-barrier-reef/train.csv')\ntest_ture.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:44:45.124223Z","iopub.execute_input":"2021-12-14T02:44:45.12463Z","iopub.status.idle":"2021-12-14T02:44:45.194848Z","shell.execute_reply.started":"2021-12-14T02:44:45.124593Z","shell.execute_reply":"2021-12-14T02:44:45.194151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_2 = test_ture[test_ture['video_id'] == 2]\ntest_2","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:44:45.196166Z","iopub.execute_input":"2021-12-14T02:44:45.196462Z","iopub.status.idle":"2021-12-14T02:44:45.218849Z","shell.execute_reply.started":"2021-12-14T02:44:45.196425Z","shell.execute_reply":"2021-12-14T02:44:45.218165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#%%\nfor i in range(len(prediction_str)):\n    mask = prediction_str[i][0][60:-4]\n    bbox_str = prediction_str[i][1]\n    bbox_pred = []\n    for q in bbox_str:\n        lq = q.split(\" \")\n        x1 = int(lq[1])\n        x2 = int(lq[2])\n        x3 = int(lq[3])\n        x4 = int(lq[4])\n        bbox_pred.append([x1,x2,x3,x4])\n    bbox_pd = test_2[test_2['image_id'] == '2-'+ str(mask)]['annotations'].tolist()\n    bbox_true = []\n    for m in eval(bbox_pd[0]):\n        x1 = int(m['x'])\n        x2 = int(m['y'])\n        x3 = int(m['width'])\n        x4 = int(m['height'])\n        bbox_true.append([x1,x2,x3,x4])\n    print(mask)\n    print(bbox_pred)\n    print(bbox_true)\n    print('==================')","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:44:45.220078Z","iopub.execute_input":"2021-12-14T02:44:45.220344Z","iopub.status.idle":"2021-12-14T02:44:45.676769Z","shell.execute_reply.started":"2021-12-14T02:44:45.220309Z","shell.execute_reply":"2021-12-14T02:44:45.676051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = mmcv.imread('../input/tensorflow-great-barrier-reef/train_images/video_2/5882.jpg')\nresult = inference_detector(model, img)\nshow_result_pyplot(model, img, result, score_thr=0.3)","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:44:45.677944Z","iopub.execute_input":"2021-12-14T02:44:45.678421Z","iopub.status.idle":"2021-12-14T02:44:46.828779Z","shell.execute_reply.started":"2021-12-14T02:44:45.678383Z","shell.execute_reply":"2021-12-14T02:44:46.826976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working\n!rm -rf faster-r\n!rm -rf mmdetection\n!rm -rf  faster-config\nimport greatbarrierreef\n\nenv = greatbarrierreef.make_env()   # initialize the environment\niter_test = env.iter_test()  ","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:44:46.829988Z","iopub.execute_input":"2021-12-14T02:44:46.830714Z","iopub.status.idle":"2021-12-14T02:44:49.157647Z","shell.execute_reply.started":"2021-12-14T02:44:46.830664Z","shell.execute_reply":"2021-12-14T02:44:49.156717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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 = detect_fn_tf_odt(input_tensor)\n    return detections","metadata":{"execution":{"iopub.status.busy":"2021-12-14T03:00:01.0264Z","iopub.execute_input":"2021-12-14T03:00:01.027222Z","iopub.status.idle":"2021-12-14T03:00:01.033747Z","shell.execute_reply.started":"2021-12-14T03:00:01.027173Z","shell.execute_reply":"2021-12-14T03:00:01.032869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_dict = {\n    'id': [],\n    'prediction_string': [],\n}\n\nfor (image_np, sample_prediction_df) in iter_test:\n    result = inference_detector(model, image_np)\n#     show_result_pyplot(model, image_np, result, score_thr=0.3)\n    predictions = []\n    for i in result[0]:\n        score = i[4]\n        if score < 0.2:\n            continue\n        x_min = int(i[0])\n        y_min = int(i[1])\n        x_max = int(i[2])\n        y_max = int(i[3])\n\n        bbox_width = x_max - x_min\n        bbox_height = y_max - y_min\n        \n\n        predictions.append('{:.2f} {} {} {} {}'.format(score, x_min, y_min, bbox_width, bbox_height))\n#         show_result_pyplot(model, image_np, result, score_thr=0.3)\n    prediction_str = ' '.join(predictions)\n    sample_prediction_df['annotations'] = prediction_str\n    env.predict(sample_prediction_df)\n#     print(prediction_str)","metadata":{"execution":{"iopub.status.busy":"2021-12-14T03:01:28.679833Z","iopub.execute_input":"2021-12-14T03:01:28.680098Z","iopub.status.idle":"2021-12-14T03:01:28.688748Z","shell.execute_reply.started":"2021-12-14T03:01:28.680067Z","shell.execute_reply":"2021-12-14T03:01:28.687935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df","metadata":{"execution":{"iopub.status.busy":"2021-12-14T02:36:22.022514Z","iopub.execute_input":"2021-12-14T02:36:22.023154Z","iopub.status.idle":"2021-12-14T02:36:22.035946Z","shell.execute_reply.started":"2021-12-14T02:36:22.023118Z","shell.execute_reply":"2021-12-14T02:36:22.034714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df = pd.read_csv('submission.csv')\nsub_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-14T03:03:34.742902Z","iopub.execute_input":"2021-12-14T03:03:34.743177Z","iopub.status.idle":"2021-12-14T03:03:34.75473Z","shell.execute_reply.started":"2021-12-14T03:03:34.743149Z","shell.execute_reply":"2021-12-14T03:03:34.754028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df","metadata":{"execution":{"iopub.status.busy":"2021-12-14T03:03:34.977533Z","iopub.execute_input":"2021-12-14T03:03:34.978221Z","iopub.status.idle":"2021-12-14T03:03:34.987056Z","shell.execute_reply.started":"2021-12-14T03:03:34.978186Z","shell.execute_reply":"2021-12-14T03:03:34.986332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}