{"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}],"dockerImageVersionId":30588,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Install package","metadata":{}},{"cell_type":"code","source":"!pip install -Uqqq pycocotools","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Importing the required pakages","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","metadata":{"execution":{"iopub.status.busy":"2024-05-17T14:51:55.784419Z","iopub.execute_input":"2024-05-17T14:51:55.784900Z","iopub.status.idle":"2024-05-17T14:51:56.046723Z","shell.execute_reply.started":"2024-05-17T14:51:55.784864Z","shell.execute_reply":"2024-05-17T14:51:56.045418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Split train/val","metadata":{}},{"cell_type":"code","source":"FOLD = 0\ndataDir=Path('/kaggle/input/global-wheat-detection/train')","metadata":{"execution":{"iopub.status.busy":"2024-05-17T07:15:54.948989Z","iopub.execute_input":"2024-05-17T07:15:54.949389Z","iopub.status.idle":"2024-05-17T07:15:54.953790Z","shell.execute_reply.started":"2024-05-17T07:15:54.949360Z","shell.execute_reply":"2024-05-17T07:15:54.953054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/global-wheat-detection/train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-17T14:51:08.655527Z","iopub.execute_input":"2024-05-17T14:51:08.655944Z","iopub.status.idle":"2024-05-17T14:51:08.922657Z","shell.execute_reply.started":"2024-05-17T14:51:08.655910Z","shell.execute_reply":"2024-05-17T14:51:08.921519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.reset_index(drop=True)\ndf['fold'] = -1\nskf = GroupKFold(n_splits=5)\nfor fold, (_, val_idx) in enumerate(skf.split(X=df, y=df['source'], groups=df['image_id'])):\n    df.loc[val_idx, 'fold'] = fold","metadata":{"execution":{"iopub.status.busy":"2024-05-17T07:32:03.739651Z","iopub.execute_input":"2024-05-17T07:32:03.740049Z","iopub.status.idle":"2024-05-17T07:32:03.903879Z","shell.execute_reply.started":"2024-05-17T07:32:03.740015Z","shell.execute_reply":"2024-05-17T07:32:03.902721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Make COCO_dataset","metadata":{}},{"cell_type":"code","source":"from 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    bbox = row['bbox'].strip().split(',')\n    x, y, width, height = bbox[0][1:].strip(), bbox[1].strip(), bbox[2].strip(), bbox[3][:-1].strip()\n    bbox=[float(x),float(y),float(width),float(height)]\n    bbox = [int(num) for num in bbox]\n    area = bbox[2] * bbox[3]\n    annotation = {\n        'segmentation' : [],\n        'bbox' : bbox,\n        'area' : area,\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 = [{'id':id, 'width':int(row.width), 'height':int(row.height), 'file_name':f'{id}.jpg'} for id, row in df.groupby('image_id').agg('first').iterrows()]\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":{"execution":{"iopub.status.busy":"2024-05-17T14:51:24.021719Z","iopub.execute_input":"2024-05-17T14:51:24.022198Z","iopub.status.idle":"2024-05-17T14:51:24.035389Z","shell.execute_reply.started":"2024-05-17T14:51:24.022162Z","shell.execute_reply":"2024-05-17T14:51:24.034325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = df.query('fold!=@FOLD')\nvalid_df = df.query('fold==@FOLD')","metadata":{"execution":{"iopub.status.busy":"2024-05-17T07:49:20.580781Z","iopub.execute_input":"2024-05-17T07:49:20.581161Z","iopub.status.idle":"2024-05-17T07:49:20.604490Z","shell.execute_reply.started":"2024-05-17T07:49:20.581129Z","shell.execute_reply":"2024-05-17T07:49:20.603389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(train_json['annotations'][0])\ndisplay(valid_json['annotations'][0])","metadata":{"execution":{"iopub.status.busy":"2024-05-17T07:49:59.550839Z","iopub.execute_input":"2024-05-17T07:49:59.551236Z","iopub.status.idle":"2024-05-17T07:49:59.561304Z","shell.execute_reply.started":"2024-05-17T07:49:59.551202Z","shell.execute_reply":"2024-05-17T07:49:59.560166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('annotations_train.json' , 'w', encoding='utf-8') as f:\n    json.dump(train_json, f, ensure_ascii=True, indent=4)\nwith open('annotations_valid.json' , 'w', encoding='utf-8') as f:\n    json.dump(valid_json, f, ensure_ascii=True, indent=4)","metadata":{"execution":{"iopub.status.busy":"2024-05-17T07:50:04.747168Z","iopub.execute_input":"2024-05-17T07:50:04.747572Z","iopub.status.idle":"2024-05-17T07:50:07.979753Z","shell.execute_reply.started":"2024-05-17T07:50:04.747541Z","shell.execute_reply":"2024-05-17T07:50:07.978960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p train2017\n!mkdir -p valid2017","metadata":{"execution":{"iopub.status.busy":"2024-05-17T07:50:12.027029Z","iopub.execute_input":"2024-05-17T07:50:12.028018Z","iopub.status.idle":"2024-05-17T07:50:14.092275Z","shell.execute_reply.started":"2024-05-17T07:50:12.027980Z","shell.execute_reply":"2024-05-17T07:50:14.090958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\ndef run_copy(row):\n    img_path = dataDir/f'{row.image_id}.jpg'\n    if row.fold!=FOLD:\n        shutil.copy(img_path, './train2017/')\n    else:\n        shutil.copy(img_path, './valid2017/')","metadata":{"execution":{"iopub.status.busy":"2024-05-17T07:51:42.165634Z","iopub.execute_input":"2024-05-17T07:51:42.166645Z","iopub.status.idle":"2024-05-17T07:51:42.171214Z","shell.execute_reply.started":"2024-05-17T07:51:42.166608Z","shell.execute_reply":"2024-05-17T07:51:42.170501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp_df = df.groupby('image_id').agg('first').reset_index()\n_ = Parallel(n_jobs=-1, backend='threading')(delayed(run_copy)(row) for _, row in tqdm(tmp_df.iterrows(), total=len(tmp_df)))","metadata":{"execution":{"iopub.status.busy":"2024-05-17T07:51:51.406335Z","iopub.execute_input":"2024-05-17T07:51:51.406691Z","iopub.status.idle":"2024-05-17T07:51:58.289977Z","shell.execute_reply.started":"2024-05-17T07:51:51.406665Z","shell.execute_reply":"2024-05-17T07:51:58.289091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualize","metadata":{}},{"cell_type":"code","source":"annFile = Path('./annotations_valid.json')\ncoco = COCO(annFile)\nimgIds = coco.getImgIds()\n\nimgs = coco.loadImgs(imgIds[-3:])\n_, axs = plt.subplots(len(imgs), 2, figsize=(40,15 * len(imgs)))\nfor img, ax in zip(imgs,axs):\n    I = Image.open(dataDir/img['file_name'])\n    annIds = coco.getAnnIds(imgIds=[img['id']])\n    anns = coco.loadAnns(annIds)\n    ax[0].imshow(I)\n    ax[1].imshow(I)\n    plt.sca(ax[1])\n    coco.showAnns(anns, draw_bbox=True)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-17T07:52:04.726818Z","iopub.execute_input":"2024-05-17T07:52:04.727212Z","iopub.status.idle":"2024-05-17T07:52:16.067725Z","shell.execute_reply.started":"2024-05-17T07:52:04.727181Z","shell.execute_reply":"2024-05-17T07:52:16.066406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}