{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":19989,"databundleVersionId":1160143,"sourceType":"competition"},{"sourceId":6063590,"sourceType":"datasetVersion","datasetId":3470137},{"sourceId":7210148,"sourceType":"datasetVersion","datasetId":4116260},{"sourceId":8438567,"sourceType":"datasetVersion","datasetId":5026588}],"dockerImageVersionId":30588,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## COCO Dataset \nclick here - > https://www.kaggle.com/code/ishlove7/how-to-make-coco-dataset","metadata":{}},{"cell_type":"markdown","source":"## Install package","metadata":{}},{"cell_type":"code","source":"from IPython.display import clear_output\n\n!pip install --no-index --no-deps /kaggle/input/mmdetectron-31-wheel/pycocotools-2.0.6-cp310-cp310-linux_x86_64.whl\n!pip install --no-index --no-deps /kaggle/input/mmdetectron-31-wheel/torch-1.12.1+cu116-cp310-cp310-linux_x86_64.whl\n!pip install --no-index --no-deps /kaggle/input/mmdetectron-31-wheel/torchvision-0.13.1+cu116-cp310-cp310-linux_x86_64.whl\n!pip install --no-index --no-deps /kaggle/input/mmdetectron-31-wheel/mmcv-2.0.1-cp310-cp310-manylinux1_x86_64.whl \n!pip install --no-index --no-deps /kaggle/input/mmdetectron-31-wheel/openmim-0.3.9-py2.py3-none-any.whl\n!pip install --no-index --no-deps /kaggle/input/mmdetectron-31-wheel/mmengine-0.7.4-py3-none-any.whl\n!pip install --no-index --no-deps /kaggle/input/mmdetectron-31-wheel/addict-2.4.0-py3-none-any.whl\n!pip install yapf==0.40.1\n\nclear_output()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm.notebook import tqdm\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\nfrom PIL import Image\n\nfrom glob import glob\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-05-18T13:45:02.113062Z","iopub.status.idle":"2024-05-18T13:45:02.113430Z","shell.execute_reply.started":"2024-05-18T13:45:02.113256Z","shell.execute_reply":"2024-05-18T13:45:02.113272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone -bv3.1.0 https://github.com/open-mmlab/mmdetection.git\n%cd /kaggle/working/mmdetection\n!pip install -v -e .\n\nclear_output()","metadata":{"execution":{"iopub.status.busy":"2024-05-18T13:45:02.115561Z","iopub.status.idle":"2024-05-18T13:45:02.116261Z","shell.execute_reply.started":"2024-05-18T13:45:02.115906Z","shell.execute_reply":"2024-05-18T13:45:02.115936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch, torchvision,mmdet\nprint(\"PyTorch version= \",torch.__version__,torch.cuda.is_available())\nprint(\"mmdet version= \",mmdet.__version__)\n\nfrom mmcv.ops import get_compiling_cuda_version\nprint(\"THE CUDA version \", get_compiling_cuda_version())","metadata":{"execution":{"iopub.status.busy":"2024-05-18T13:45:02.118129Z","iopub.status.idle":"2024-05-18T13:45:02.118784Z","shell.execute_reply.started":"2024-05-18T13:45:02.118459Z","shell.execute_reply":"2024-05-18T13:45:02.118489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -U openmim\nclear_output()","metadata":{"execution":{"iopub.status.busy":"2024-05-18T13:45:02.120382Z","iopub.status.idle":"2024-05-18T13:45:02.120836Z","shell.execute_reply.started":"2024-05-18T13:45:02.120596Z","shell.execute_reply":"2024-05-18T13:45:02.120617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p configs/wheat","metadata":{"execution":{"iopub.status.busy":"2024-05-18T13:45:02.122395Z","iopub.status.idle":"2024-05-18T13:45:02.122841Z","shell.execute_reply.started":"2024-05-18T13:45:02.122612Z","shell.execute_reply":"2024-05-18T13:45:02.122635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Config","metadata":{}},{"cell_type":"code","source":"%%writefile configs/wheat/custom_config.py\n\n_base_ = '/kaggle/working/mmdetection/configs/faster_rcnn/faster-rcnn_r50_fpn_amp-1x_coco.py'\nmodel = dict(\n    roi_head=dict(\n        bbox_head=dict(\n            num_classes=1\n        )\n    )\n)\n\nclasses = ('wheat',) # Added\nbackend_args = None\ndata_root = '/kaggle/input/wheat-data/' # Modified\ntrain_cfg = dict(type='EpochBasedTrainLoop', max_epochs=12, val_interval=1)\nval_cfg = dict(type='ValLoop')\ntest_cfg = dict(type='TestLoop')\n\n\nparam_scheduler = [\n    dict(type='LinearLR', start_factor=0.001, by_epoch=False, begin=0, end=500),\n    dict(\n        type='MultiStepLR',\n        begin=0,\n        end=12,\n        by_epoch=True,\n        milestones=[8, 11],\n        gamma=0.1)\n]\n\n\n\ntrain_pipeline = [\n    dict(type='LoadImageFromFile', backend_args=backend_args),\n    dict(type='LoadAnnotations', with_bbox=True),\n    dict(type='Resize', scale=(1333, 1333), keep_ratio=True),\n    dict(type='RandomFlip', prob=0.5),\n    dict(type='PhotoMetricDistortion',\n         brightness_delta=32, contrast_range=(0.5, 1.5),\n         saturation_range=(0.5, 1.5), hue_delta=18),\n    dict(type='PackDetInputs')\n]\ntest_pipeline = [\n    dict(type='LoadImageFromFile', backend_args=backend_args),\n    dict(type='Resize', scale=(1333, 133), keep_ratio=True),\n    # If you don't have a gt annotation, delete the pipeline\n    dict(type='LoadAnnotations', with_bbox=True),\n    dict(\n        type='PackDetInputs',\n        meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',\n                   'scale_factor'))\n]\ntrain_dataloader = dict(\n    batch_size=16,\n    num_workers=2,\n    persistent_workers=True,\n    dataset=dict(\n        type='CocoDataset',\n        data_root=data_root,\n        metainfo=dict(classes=classes),\n        ann_file=data_root + 'annotations_train.json',\n        data_prefix=dict(img='train2017/'),\n        pipeline=train_pipeline,\n        backend_args=backend_args))\nval_dataloader = dict(\n    batch_size=16,\n    num_workers=2,\n    persistent_workers=True,\n    drop_last=False,\n    dataset=dict(\n        type='CocoDataset',\n        data_root=data_root,\n        metainfo=dict(classes=classes),\n        ann_file=data_root + 'annotations_valid.json',\n        data_prefix=dict(img='valid2017/'),\n        test_mode=True,\n        pipeline=test_pipeline,\n        backend_args=backend_args))\ntest_dataloader = val_dataloader\n\ntest_cfg=dict(\n        rpn=dict(\n            nms_pre=1000,\n            max_per_img=1000,\n            nms=dict(type='nms', iou_threshold=0.7),\n            min_bbox_size=0),\n        rcnn=dict(\n            score_thr=0.05,\n            nms=dict(type='soft_nms', iou_threshold=0.5),\n            max_per_img=100)\n        # soft-nms is also supported for rcnn testing\n        # e.g., nms=dict(type='soft_nms', iou_threshold=0.5, min_score=0.05)\n    )\n\nval_evaluator = dict(\nann_file=data_root + 'annotations_valid.json', metric=['bbox'])\nload_from='http://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'\ntest_evaluator = val_evaluator\nresume_from = None","metadata":{"execution":{"iopub.status.busy":"2024-05-17T11:19:56.256618Z","iopub.execute_input":"2024-05-17T11:19:56.257629Z","iopub.status.idle":"2024-05-17T11:19:56.266006Z","shell.execute_reply.started":"2024-05-17T11:19:56.257591Z","shell.execute_reply":"2024-05-17T11:19:56.265017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train","metadata":{}},{"cell_type":"code","source":"!python tools/train.py /kaggle/working/mmdetection/configs/wheat/custom_config.py","metadata":{"execution":{"iopub.status.busy":"2024-05-17T11:19:58.559642Z","iopub.execute_input":"2024-05-17T11:19:58.560499Z","iopub.status.idle":"2024-05-17T11:27:18.462407Z","shell.execute_reply.started":"2024-05-17T11:19:58.560463Z","shell.execute_reply":"2024-05-17T11:27:18.461222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}