{"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":"**This notebook is copy of this one https://www.kaggle.com/steamedsheep/yolov5-is-all-you-need I just follow the advices from discussion and changed the input size from 3000 to 10000**","metadata":{}},{"cell_type":"code","source":"import greatbarrierreef\nimport sys\nsys.path.append('../input/tensorflow-great-barrier-reef')\nenv = 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":"2022-01-20T15:19:49.357235Z","iopub.execute_input":"2022-01-20T15:19:49.357673Z","iopub.status.idle":"2022-01-20T15:19:49.533517Z","shell.execute_reply.started":"2022-01-20T15:19:49.357614Z","shell.execute_reply":"2022-01-20T15:19:49.528526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# pytorch ","metadata":{}},{"cell_type":"code","source":"!pip install '/kaggle/input/pytorch-190/torch-1.9.0+cu111-cp37-cp37m-linux_x86_64.whl' --no-deps\n!pip install '/kaggle/input/pytorch-190/torchvision-0.10.0+cu111-cp37-cp37m-linux_x86_64.whl' --no-deps","metadata":{"execution":{"iopub.status.busy":"2022-01-20T15:19:49.536868Z","iopub.status.idle":"2022-01-20T15:19:49.539512Z","shell.execute_reply.started":"2022-01-20T15:19:49.539224Z","shell.execute_reply":"2022-01-20T15:19:49.539258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# mmcv and mmdet ","metadata":{}},{"cell_type":"code","source":"\n!cp -r /kaggle/input/mmdetectionv2140/* /kaggle/working/\n!cp -r /kaggle/input/cbnet-custom/* /kaggle/working/\n!pip install '/kaggle/working/addict-2.4.0-py3-none-any.whl' --no-deps\n!pip install '/kaggle/working/yapf-0.31.0-py2.py3-none-any.whl' --no-deps\n!pip install '/kaggle/working/terminal-0.4.0-py3-none-any.whl' --no-deps\n!pip install '/kaggle/working/terminaltables-3.1.0-py3-none-any.whl' --no-deps\n!pip install '/kaggle/working/pycocotools-2.0.2/pycocotools-2.0.2' --no-deps\n!pip install '/kaggle/working/mmpycocotools-12.0.3/mmpycocotools-12.0.3' --no-deps\n\n!pip install '/kaggle/input/timm-package/timm-0.4.12-py3-none-any.whl' --no-deps\n!pip install '/kaggle/input/mmcv1-3-18-cp37/mmcv_full-1.3.16-cp37-cp37m-manylinux1_x86_64.whl' --no-deps\n!pip install '/kaggle/input/mmcv1-3-18-cp37/icecream-2.1.1-py2.py3-none-any.whl' --no-deps\n!pip install '/kaggle/input/needbao/executing-0.8.2-py2.py3-none-any.whl' --no-deps","metadata":{"execution":{"iopub.status.busy":"2022-01-20T15:19:49.543822Z","iopub.status.idle":"2022-01-20T15:19:49.546545Z","shell.execute_reply.started":"2022-01-20T15:19:49.546271Z","shell.execute_reply":"2022-01-20T15:19:49.546301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mv /kaggle/working/CBNetV2-main /kaggle/working/CBNetV2\n%cd /kaggle/working/CBNetV2\n!pip install -e .\n","metadata":{"execution":{"iopub.status.busy":"2022-01-20T15:19:49.547882Z","iopub.status.idle":"2022-01-20T15:19:49.548471Z","shell.execute_reply.started":"2022-01-20T15:19:49.548229Z","shell.execute_reply":"2022-01-20T15:19:49.548255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p /root/.config/Ultralytics\n!cp /kaggle/input/yolov5-font/Arial.ttf /root/.config/Ultralytics/","metadata":{"execution":{"iopub.status.busy":"2022-01-20T15:19:49.55007Z","iopub.status.idle":"2022-01-20T15:19:49.550717Z","shell.execute_reply.started":"2022-01-20T15:19:49.550463Z","shell.execute_reply":"2022-01-20T15:19:49.550488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport torch\nfrom tqdm import tqdm\n\n\nimport mmdet\nimport mmcv\n# from mmdet.datasets import build_dataset\n# from mmdet.models import build_detector\n# from mmdet.apis import train_detector\nfrom mmdet.apis import inference_detector, init_detector, show_result_pyplot, set_random_seed","metadata":{"execution":{"iopub.status.busy":"2022-01-20T15:19:49.552015Z","iopub.status.idle":"2022-01-20T15:19:49.552867Z","shell.execute_reply.started":"2022-01-20T15:19:49.552409Z","shell.execute_reply":"2022-01-20T15:19:49.552434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ..","metadata":{"execution":{"iopub.status.busy":"2022-01-20T15:19:49.554909Z","iopub.status.idle":"2022-01-20T15:19:49.555782Z","shell.execute_reply.started":"2022-01-20T15:19:49.555468Z","shell.execute_reply":"2022-01-20T15:19:49.555496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model\n","metadata":{}},{"cell_type":"code","source":"#----------------yolo-----------------------------\n# model = torch.hub.load('../input/yolov5-lib-ds', \n#                        'custom', \n#                        path='../input/reef-baseline-fold12/l6_3600_uflip_vm5_f12_up/f1/best.pt',\n#                        source='local',\n#                        force_reload=True)  # local repo\n# model.conf = 0.01","metadata":{"execution":{"iopub.status.busy":"2022-01-20T15:19:49.557253Z","iopub.status.idle":"2022-01-20T15:19:49.558128Z","shell.execute_reply.started":"2022-01-20T15:19:49.557838Z","shell.execute_reply":"2022-01-20T15:19:49.557865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#--------------------cbnet----------------\nconfig_file = '../input/pt-starfish/cbnet_starfish/cascade_mask_rcnn_cbv2_swin_tiny_patch4_window7_mstrain_480-800_adamw_3x_coco.py'\ncheckpoint_file= '../input/pt-starfish/cbnet_starfish/epoch_12_cs.pth'\nmodel = init_detector(config_file, checkpoint_file, device='cuda:0')","metadata":{"execution":{"iopub.status.busy":"2022-01-20T15:19:49.559553Z","iopub.status.idle":"2022-01-20T15:19:49.56033Z","shell.execute_reply.started":"2022-01-20T15:19:49.560078Z","shell.execute_reply":"2022-01-20T15:19:49.560105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# infer","metadata":{}},{"cell_type":"code","source":"#---------------------yolo---------------------------------\n# for idx, (img, pred_df) in enumerate(tqdm(iter_test)):\n#     anno = ''\n#     r = model(img, size=9000, augment=True)\n#     if r.pandas().xyxy[0].shape[0] == 0:\n#         anno = ''\n#     else:\n#         for idx, row in r.pandas().xyxy[0].iterrows():\n#             if row.confidence > 0.3:\n#                 anno += '{} {} {} {} {} '.format(row.confidence, int(row.xmin), int(row.ymin), int(row.xmax-row.xmin), int(row.ymax-row.ymin))\n# #                 pred.append([row.confidence, row.xmin, row.ymin, row.xmax-row.xmin, row.ymax-row.ymin])\n#     pred_df['annotations'] = anno.strip(' ')\n#     env.predict(pred_df)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T15:19:49.561806Z","iopub.status.idle":"2022-01-20T15:19:49.562573Z","shell.execute_reply.started":"2022-01-20T15:19:49.562322Z","shell.execute_reply":"2022-01-20T15:19:49.562347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#----------------------cbnet------------------------------\nfor idx, (img, pred_df) in enumerate(tqdm(iter_test)):\n    anno = ''\n    rs = inference_detector(model, img)\n    if len(rs[0])==0:\n        anno = ''\n        \n    else:\n        for r in rs[0]:\n            print(r[-1])\n            if r[-1]>0.3:\n                anno += '{} {} {} {} {} '.format(r[-1], int(r[0]), int(r[1]), int(r[2]-r[0]), int(r[3]-r[1]))\n    pred_df['annotations'] = anno.strip(' ')\n    env.predict(pred_df)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T15:19:49.69916Z","iopub.execute_input":"2022-01-20T15:19:49.699472Z","iopub.status.idle":"2022-01-20T15:19:49.723682Z","shell.execute_reply.started":"2022-01-20T15:19:49.699438Z","shell.execute_reply":"2022-01-20T15:19:49.722992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working\n!rm -rf ./mmpycocotools-12.0.3\n!rm -rf ./mmcv-1.3.8\n!rm -rf ./mmdetection-2.14.0\n!rm -rf ./mmcv_full-1_3_8-cu110-torch1_7_0\n!rm -rf ./CBNetV2\n!rm -rf ./pycocotools-2.0.2\n!rm -rf ./terminaltables-3.1.0-py3-none-any.whl\n!rm -rf ./terminal-0.4.0-py3-none-any.whl\n!rm -rf ./addict-2.4.0-py3-none-any.whl\n!rm -rf ./yapf-0.31.0-py2.py3-none-any.whl\n","metadata":{"execution":{"iopub.status.busy":"2022-01-20T15:19:49.727902Z","iopub.execute_input":"2022-01-20T15:19:49.728381Z","iopub.status.idle":"2022-01-20T15:19:51.502488Z","shell.execute_reply.started":"2022-01-20T15:19:49.728344Z","shell.execute_reply":"2022-01-20T15:19:51.50151Z"},"trusted":true},"execution_count":null,"outputs":[]}]}