{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","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":1219292,"sourceType":"datasetVersion","datasetId":648709},{"sourceId":6063037,"sourceType":"datasetVersion","datasetId":3469773},{"sourceId":6063590,"sourceType":"datasetVersion","datasetId":3470137},{"sourceId":178645948,"sourceType":"kernelVersion"}],"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Previous Training method\n\n1. coco_dataset: https://www.kaggle.com/code/ishlove7/how-to-make-coco-dataset \n2. MM Detection FasterRCNN[Training] : https://www.kaggle.com/code/ishlove7/mm-detection-faster-rcnn-train\n3. MM Detection Resnext + CascadeRNN[Training] : https://www.kaggle.com/code/ishlove7/mm-detection-resnext-101-cascadercnn-training\n4. MM Detection Convnext + CascadeRCNN[Training] : https://www.kaggle.com/code/ishlove7/global-wheat-detection-convnext-cascadercnn\n5. MM Detection Submission[Infer] : https://www.kaggle.com/code/ishlove7/wheat-detection-inference-tta-wbf/notebook?scriptVersionId=178769791","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 --no-index --no-deps /kaggle/input/mmdetectron-31-wheel/yapf-0.40.1-py3-none-any.whl\n!pip install --no-index --no-deps /kaggle/input/mmdetectron-31-wheel/mmdet-3.1.0-py3-none-any.whl\n!pip install --no-index --no-deps /kaggle/input/mmdetectron-31-wheel/terminaltables-3.1.10-py2.py3-none-any.whl\n\nclear_output()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install --no-index --no-deps /kaggle/input/mmpretrain/einops-0.6.1-py3-none-any.whl\n!pip install --no-index --no-deps /kaggle/input/mmpretrain/mat4py-0.5.0-py2.py3-none-any.whl\n!pip install --no-index --no-deps /kaggle/input/mmpretrain/ordered_set-4.1.0-py3-none-any.whl\n!pip install --no-index --no-deps /kaggle/input/mmpretrain/model_index-0.1.11-py3-none-any.whl\n!pip install --no-index --no-deps /kaggle/input/mmpretrain/modelindex-0.0.2-py3-none-any.whl\n!pip install --no-index --no-deps /kaggle/input/mmpretrain/mmpretrain-1.0.0rc8-py2.py3-none-any.whl\nclear_output()","metadata":{"trusted":true},"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"notebook_path = '/kaggle/input/global-wheat-detection-convnext-cascadercnn'\nnum_epochs = [9, 11, 12]\nthr = 0.4","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r {notebook_path}/mmdetection /kaggle/working/","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch, torchvision\nprint(torch.__version__, torch.cuda.is_available())\n\nimport mmdet as mmdet\nprint(mmdet.__version__)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Test Dataset","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import GroupKFold\nfrom pathlib import Path\nfrom pycocotools.coco import COCO\nfrom PIL import Image\nimport cv2\nimport os","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/global-wheat-detection/sample_submission.csv')\ndf.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir = '/kaggle/input/global-wheat-detection/test'\nfrom tqdm.notebook import tqdm\nfrom pycocotools import mask as maskUtils\nfrom joblib import Parallel, delayed\nimport json, itertools\n\ndef annotate(idx, row, cat_ids):\n    annotation = {\n        'segmentation' : [],\n        'bbox' : [0, 0, 0, 0],\n        'area' : 0,\n        'image_id' : row['image_id'],\n        'category_id' : 1,\n        'iscrowd' : 0,\n        'id' : idx\n    }\n    return annotation\n\ndef coco_structure(df, workers=4):\n    \n    cat_ids = {'wheat':1}\n    cats =[{'name':name, 'id':id} for name,id in cat_ids.items()]\n    images = []\n    for idx, row in df.iterrows():\n        image = cv2.imread(os.path.join(test_dir, row['image_id'] + '.jpg'))\n        images.append({'id':row['image_id'], 'width':int(image.shape[1]), 'height':int(image.shape[0]), 'file_name':row['image_id']+'.jpg'})  \n    annotations = Parallel(n_jobs=workers)(delayed(annotate)(idx, row, cat_ids) for idx, row in tqdm(df.iterrows(), total=len(df)))\n    \n    return {'categories':cats, 'images':images, 'annotations':annotations}","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_json = coco_structure(df)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('annotations_test.json' , 'w', encoding='utf-8') as f:\n    json.dump(test_json, f, ensure_ascii=True, indent=4)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p test2017","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\ndef run_copy(row):\n    img_path = os.path.join(test_dir,f'{row.image_id}.jpg')\n    shutil.copy(img_path, './test2017/')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp_df = df\n_ = Parallel(n_jobs=-1, backend='threading')(delayed(run_copy)(row) for _, row in tqdm(tmp_df.iterrows(), total=len(tmp_df)))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Copy epoch[8, 10 ,12]","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport cupy as cp\nfrom glob import glob\nimport os\nimport cv2\nfrom tqdm.notebook import tqdm\nimport pickle\nfrom itertools import groupby\nfrom pycocotools import mask as mutils\nfrom pycocotools import _mask as coco_mask\nimport matplotlib.pyplot as plt\nimport os\nimport base64\nimport typing as t\nimport zlib\nimport random\nrandom.seed(0)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p /kaggle/working/mmdetection/checkpoints\n!mkdir -p /kaggle/working/mmdetection/configs/wheat","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/mmdetection/mmdetection/work_dirs/custom_config/epoch_8.pth /kaggle/working/mmdetection/checkpoints","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/mmdetection/mmdetection/work_dirs/custom_config/epoch_10.pth /kaggle/working/mmdetection/checkpoints","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/mmdetection/mmdetection/work_dirs/custom_config/epoch_12.pth /kaggle/working/mmdetection/checkpoints","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Config","metadata":{}},{"cell_type":"code","source":"%%writefile /kaggle/working/mmdetection/configs/wheat/test_config.py\n\n_base_ = '/kaggle/working/mmdetection/configs/wheat/custom_config.py'\ndata_root = '/kaggle/working/'\n\n\nval_dataloader = dict(\n    batch_size=3,\n    num_workers=2,\n    persistent_workers=True,\n    drop_last=False,\n    dataset=dict(\n        data_root=data_root,\n        ann_file=data_root + 'annotations_test.json',\n        data_prefix=dict(img='test2017/'),\n        test_mode=True,)\n)\ntest_dataloader = val_dataloader\nval_evaluator = dict(ann_file=data_root + 'annotations_test.json',format_only=True, outfile_prefix='/kaggle/working/mmdetection')\ntest_evaluator = val_evaluator\ntrain_cfg = dict(\n    type='EpochBasedTrainLoop',  # The training loop type. Refer to https://github.com/open-mmlab/mmengine/blob/main/mmengine/runner/loops.py\n    max_epochs=12,  # Maximum training epochs\n    val_interval=1)\nval_cfg = dict(type='ValLoop')\ntest_cfg = dict(type='TestLoop', _delete_=True) ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Infer epoch[8, 10 ,12]","metadata":{}},{"cell_type":"code","source":"%cd /kaggle/working/mmdetection","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python tools/test.py '/kaggle/working/mmdetection/configs/wheat/test_config.py' '/kaggle/working/mmdetection/checkpoints/epoch_8.pth' --out 'result0.pkl'\nclear_output()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python tools/test.py '/kaggle/working/mmdetection/configs/wheat/test_config.py' '/kaggle/working/mmdetection/checkpoints/epoch_10.pth' --out 'result1.pkl'\nclear_output()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python tools/test.py '/kaggle/working/mmdetection/configs/wheat/test_config.py' '/kaggle/working/mmdetection/checkpoints/epoch_12.pth' --out 'result2.pkl'\nclear_output()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## WBF  epoch[8, 10 ,12]","metadata":{}},{"cell_type":"code","source":"%cd /kaggle","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/global-wheat-detection/sample_submission.csv', index_col=False)\ndf.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from input.weightedboxesfusion import ensemble_boxes","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/mmdetection","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=[]\nimage_size = 1024\niou_thr = 0.4\nskip_box_thr = 0.4\nweights=[1, 2, 1]\nscore_thr = 0.4\nresults = [pickle.load(open(f'result{i}.pkl', 'rb')) for i in range(3) ]\nfor id in tqdm(range(len(df))):\n    boxes=[]\n    scores=[]\n    labels=[]\n    for i in range(3):\n        result = results[i]\n        box = (result[id]['pred_instances']['bboxes'].numpy()/image_size).tolist()\n        score = result[id]['pred_instances']['scores'].tolist()\n        label = np.ones(len(box)).astype(int).tolist()\n        boxes.append(box)\n        scores.append(score)\n        labels.append(label)\n    boxes, scores, labels = ensemble_boxes.ensemble_boxes_wbf.weighted_boxes_fusion(boxes, scores, labels, weights=weights, iou_thr=iou_thr, skip_box_thr=skip_box_thr)\n    PredictionString = ''\n    #img = cv2.imread( '/kaggle/input/global-wheat-detection/test/' + results[0][id]['img_id'] + '.jpg')\n    for box, score in zip(boxes,scores):\n        box = (np.array(box)*1024).astype(int).tolist()\n        if score > score_thr:\n            x1, y1, x2, y2 = box\n            #img = cv2.rectangle(img, (x1, y1), (x2, y2), (255,0,0), 3)\n            width, height = x2 - x1, y2 - y1\n            if len(PredictionString)!=0:\n                PredictionString+=' '\n            PredictionString += str(score)\n            PredictionString += ' ' + str(x1) + ' ' + str(y1) + ' ' + str(width) + ' ' + str(height)\n    #plt.imshow(img)\n    #plt.show()\n    data.append([results[0][id]['img_id'], PredictionString])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_df = pd.DataFrame(data=data, columns=['image_id','PredictionString'])\npred_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df = pd.read_csv('/kaggle/input/global-wheat-detection/sample_submission.csv', index_col=False)\ndel sub_df['PredictionString']\nsub_df = sub_df.merge(pred_df, on='image_id', how='left')\nsub_df.to_csv('/kaggle/working/submission.csv', index=False)\nsub_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualize","metadata":{}},{"cell_type":"code","source":"result = results[0]\nfor i in range(10):\n    img = cv2.imread( '/kaggle/input/global-wheat-detection/test/' + result[i]['img_id'] + '.jpg')\n    for box, score in zip(result[i]['pred_instances']['bboxes'],result[i]['pred_instances']['scores']):\n        if score.data > 0.6:\n            xmin, ymin, xmax, ymax= box.data.tolist()\n            img = cv2.rectangle(img, (int(xmin), int(ymin)), (int(xmax), int(ymax)), (255,0,0), 3)\n    plt.imshow(img)\n    plt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Delete","metadata":{}},{"cell_type":"code","source":"import shutil\nshutil.rmtree(\"/kaggle/working/mmdetection\")\nshutil.rmtree('/kaggle/working/test2017')\nos.remove('/kaggle/working/mmdetection.bbox.json')\nos.remove('/kaggle/working/annotations_test.json')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}