{"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":"# YoloV5 for Global Wheat Detection","metadata":{}},{"cell_type":"markdown","source":"## Competition Dataset : [Global Wheat Detection](https://www.kaggle.com/c/global-wheat-detection)\n## Config: [configyolo5](https://www.kaggle.com/orkatz2/configyolo5) ","metadata":{}},{"cell_type":"markdown","source":"## Repro [YoloV5](https://www.kaggle.com/ultralytics/yolov5)","metadata":{}},{"cell_type":"code","source":"!git clone https://github.com/ultralytics/yolov5\n!mv yolov5/* ./","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-11-01T17:45:18.526947Z","iopub.execute_input":"2023-11-01T17:45:18.527238Z","iopub.status.idle":"2023-11-01T17:45:22.722586Z","shell.execute_reply.started":"2023-11-01T17:45:18.527210Z","shell.execute_reply":"2023-11-01T17:45:22.721511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python -m pip install --upgrade pip\n!pip install -r requirements.txt","metadata":{"execution":{"iopub.status.busy":"2023-11-01T17:45:22.724695Z","iopub.execute_input":"2023-11-01T17:45:22.724978Z","iopub.status.idle":"2023-11-01T17:45:36.776000Z","shell.execute_reply.started":"2023-11-01T17:45:22.724949Z","shell.execute_reply":"2023-11-01T17:45:36.775154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## train from scratch don't need the pretrained models","metadata":{}},{"cell_type":"code","source":"# Download YoloV5 pretrained models\n#!weights/download_weights.sh","metadata":{"execution":{"iopub.status.busy":"2023-11-01T17:45:36.777655Z","iopub.execute_input":"2023-11-01T17:45:36.778052Z","iopub.status.idle":"2023-11-01T17:45:36.781346Z","shell.execute_reply.started":"2023-11-01T17:45:36.778006Z","shell.execute_reply":"2023-11-01T17:45:36.780427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prepare Data","metadata":{}},{"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 os","metadata":{"execution":{"iopub.status.busy":"2023-11-01T17:45:36.783001Z","iopub.execute_input":"2023-11-01T17:45:36.783366Z","iopub.status.idle":"2023-11-01T17:45:36.793266Z","shell.execute_reply.started":"2023-11-01T17:45:36.783328Z","shell.execute_reply":"2023-11-01T17:45:36.792525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### convert Train Label","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('../input/global-wheat-detection/train.csv')\nbboxs = np.stack(df['bbox'].apply(lambda x: np.fromstring(x[1:-1], sep=',')))\nfor i, column in enumerate(['x', 'y', 'w', 'h']):\n    df[column] = bboxs[:,i]\ndf.drop(columns=['bbox'], inplace=True)\ndf['x_center'] = df['x'] + df['w']/2\ndf['y_center'] = df['y'] + df['h']/2\ndf['classes'] = 0\nfrom tqdm.auto import tqdm\nimport shutil as sh\ndf = df[['image_id','x', 'y', 'w', 'h','x_center','y_center','classes']]","metadata":{"execution":{"iopub.status.busy":"2023-11-01T17:45:36.796135Z","iopub.execute_input":"2023-11-01T17:45:36.796412Z","iopub.status.idle":"2023-11-01T17:45:37.837600Z","shell.execute_reply.started":"2023-11-01T17:45:36.796386Z","shell.execute_reply":"2023-11-01T17:45:37.836908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-01T17:45:37.839272Z","iopub.execute_input":"2023-11-01T17:45:37.839650Z","iopub.status.idle":"2023-11-01T17:45:37.860377Z","shell.execute_reply.started":"2023-11-01T17:45:37.839612Z","shell.execute_reply":"2023-11-01T17:45:37.859450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = list(set(df.image_id))","metadata":{"execution":{"iopub.status.busy":"2023-11-01T17:45:37.861417Z","iopub.execute_input":"2023-11-01T17:45:37.861729Z","iopub.status.idle":"2023-11-01T17:45:37.883419Z","shell.execute_reply.started":"2023-11-01T17:45:37.861690Z","shell.execute_reply":"2023-11-01T17:45:37.882716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"source = 'train'\nif True:\n    for fold in [0]:\n        val_index = index[len(index)*fold//5:len(index)*(fold+1)//5]\n        for name,mini in tqdm(df.groupby('image_id')):\n            if name in val_index:\n                path2save = 'val2017/'\n            else:\n                path2save = 'train2017/'\n            if not os.path.exists('convertor/fold{}/labels/'.format(fold)+path2save):\n                os.makedirs('convertor/fold{}/labels/'.format(fold)+path2save)\n            with open('convertor/fold{}/labels/'.format(fold)+path2save+name+\".txt\", 'w+') as f:\n                row = mini[['classes','x_center','y_center','w','h']].astype(float).values\n                row = row/1024\n                row = row.astype(str)\n                for j in range(len(row)):\n                    text = ' '.join(row[j])\n                    f.write(text)\n                    f.write(\"\\n\")\n            if not os.path.exists('convertor/fold{}/images/{}'.format(fold,path2save)):\n                os.makedirs('convertor/fold{}/images/{}'.format(fold,path2save))\n            sh.copy(\"../input/global-wheat-detection/{}/{}.jpg\".format(source,name),'convertor/fold{}/images/{}/{}.jpg'.format(fold,path2save,name))","metadata":{"execution":{"iopub.status.busy":"2023-11-01T17:45:37.884741Z","iopub.execute_input":"2023-11-01T17:45:37.885118Z","iopub.status.idle":"2023-11-01T17:46:23.406677Z","shell.execute_reply.started":"2023-11-01T17:45:37.885078Z","shell.execute_reply":"2023-11-01T17:46:23.405757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train Model","metadata":{}},{"cell_type":"markdown","source":"train 1 epoch takes ~15 mins using GPU","metadata":{}},{"cell_type":"code","source":"# Train Model\n!python train.py --img 1024 --batch 2 --epochs 10 --data ../input/configyolo5/wheat0.yaml --cfg models/yolov5x.yaml --weight \"\"","metadata":{"execution":{"iopub.status.busy":"2023-11-01T17:46:23.407872Z","iopub.execute_input":"2023-11-01T17:46:23.408152Z","iopub.status.idle":"2023-11-01T17:48:33.632610Z","shell.execute_reply.started":"2023-11-01T17:46:23.408124Z","shell.execute_reply":"2023-11-01T17:48:33.631677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"model saved at runs/exp0/weights/best.pt","metadata":{}},{"cell_type":"code","source":"# copy saved model to weights folder\n!cp runs/exp0/weights/best.pt weights","metadata":{"execution":{"iopub.status.busy":"2023-11-01T17:48:33.633919Z","iopub.execute_input":"2023-11-01T17:48:33.634187Z","iopub.status.idle":"2023-11-01T17:48:34.578495Z","shell.execute_reply.started":"2023-11-01T17:48:33.634159Z","shell.execute_reply":"2023-11-01T17:48:34.577494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# remove convertor of training data\n!rm -rf convertor","metadata":{"execution":{"iopub.status.busy":"2023-11-01T17:48:34.580029Z","iopub.execute_input":"2023-11-01T17:48:34.580320Z","iopub.status.idle":"2023-11-01T17:48:35.929476Z","shell.execute_reply.started":"2023-11-01T17:48:34.580289Z","shell.execute_reply":"2023-11-01T17:48:35.928519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Test Model","metadata":{}},{"cell_type":"markdown","source":"10 test images in '../input/global-wheat-detection/test/'","metadata":{}},{"cell_type":"code","source":"# Detect Test Images\n!python detect.py --source '../input/global-wheat-detection/test/' --weight weights/best.pt --output 'inference/output' ","metadata":{"execution":{"iopub.status.busy":"2023-11-01T17:48:35.930943Z","iopub.execute_input":"2023-11-01T17:48:35.931226Z","iopub.status.idle":"2023-11-01T17:48:37.754899Z","shell.execute_reply.started":"2023-11-01T17:48:35.931198Z","shell.execute_reply":"2023-11-01T17:48:37.753932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"output images in 'inference/output'","metadata":{}},{"cell_type":"markdown","source":"### Display Output Images","metadata":{}},{"cell_type":"code","source":"!ls -l inference/output","metadata":{"execution":{"iopub.status.busy":"2023-11-01T17:48:37.756975Z","iopub.execute_input":"2023-11-01T17:48:37.757390Z","iopub.status.idle":"2023-11-01T17:48:38.700351Z","shell.execute_reply.started":"2023-11-01T17:48:37.757348Z","shell.execute_reply":"2023-11-01T17:48:38.699476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import Image, clear_output  # to display images\nImage(filename='/kaggle/input/picturegame/EW6GxdAU8AELS4h.jpg', width=600)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T17:48:38.753882Z","iopub.execute_input":"2023-11-01T17:48:38.754174Z","iopub.status.idle":"2023-11-01T17:48:38.770440Z","shell.execute_reply.started":"2023-11-01T17:48:38.754144Z","shell.execute_reply":"2023-11-01T17:48:38.769298Z"},"trusted":true},"execution_count":null,"outputs":[]}]}