{"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":"code","source":"import numpy as np\nimport pandas as pd\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-25T09:55:09.118553Z","iopub.execute_input":"2022-08-25T09:55:09.119290Z","iopub.status.idle":"2022-08-25T09:55:09.151134Z","shell.execute_reply.started":"2022-08-25T09:55:09.119202Z","shell.execute_reply":"2022-08-25T09:55:09.150175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r ../input/yolo-pt/yolov5 .","metadata":{"execution":{"iopub.status.busy":"2022-08-25T09:55:09.153288Z","iopub.execute_input":"2022-08-25T09:55:09.154061Z","iopub.status.idle":"2022-08-25T09:55:12.721358Z","shell.execute_reply.started":"2022-08-25T09:55:09.154019Z","shell.execute_reply":"2022-08-25T09:55:12.720054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir datasets\n!mkdir datasets/images\n!mkdir datasets/labels\n!mkdir datasets/images/train\n!mkdir datasets/images/valid\n!mkdir datasets/labels/train\n!mkdir datasets/labels/valid","metadata":{"execution":{"iopub.status.busy":"2022-08-25T09:55:12.723902Z","iopub.execute_input":"2022-08-25T09:55:12.725119Z","iopub.status.idle":"2022-08-25T09:55:19.609102Z","shell.execute_reply.started":"2022-08-25T09:55:12.725064Z","shell.execute_reply":"2022-08-25T09:55:19.607791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/global-wheat-detection/train.csv')\ndf.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-08-25T09:55:19.613294Z","iopub.execute_input":"2022-08-25T09:55:19.613605Z","iopub.status.idle":"2022-08-25T09:55:19.860543Z","shell.execute_reply.started":"2022-08-25T09:55:19.613570Z","shell.execute_reply":"2022-08-25T09:55:19.859585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nimage_ids = df['image_id'].unique()\ntrain, valid = train_test_split(image_ids, test_size=0.05)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-25T09:55:19.861853Z","iopub.execute_input":"2022-08-25T09:55:19.862612Z","iopub.status.idle":"2022-08-25T09:55:20.429400Z","shell.execute_reply.started":"2022-08-25T09:55:19.862575Z","shell.execute_reply":"2022-08-25T09:55:20.427608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil as sh\nfor image_id in train:\n    sh.copy(f'../input/global-wheat-detection/train/{image_id}.jpg', f'datasets/images/train/{image_id}.jpg')\n\nfor image_id in valid:\n    sh.copy(f'../input/global-wheat-detection/train/{image_id}.jpg', f'datasets/images/valid/{image_id}.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-08-25T09:55:20.430962Z","iopub.execute_input":"2022-08-25T09:55:20.431307Z","iopub.status.idle":"2022-08-25T09:55:42.330736Z","shell.execute_reply.started":"2022-08-25T09:55:20.431272Z","shell.execute_reply":"2022-08-25T09:55:42.329755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bboxs = 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\ndf = df[['image_id', 'x', 'y', 'w', 'h', 'x_center', 'y_center', 'classes']]\ndf.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-08-25T09:55:42.332167Z","iopub.execute_input":"2022-08-25T09:55:42.333317Z","iopub.status.idle":"2022-08-25T09:55:42.769372Z","shell.execute_reply.started":"2022-08-25T09:55:42.333277Z","shell.execute_reply":"2022-08-25T09:55:42.768384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for name, mini in df.groupby('image_id'):\n    if name in train:\n        subset = 'train'\n    else:\n        subset = 'valid'\n    with open (f'datasets/labels/{subset}/{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    ","metadata":{"execution":{"iopub.status.busy":"2022-08-25T09:55:42.770920Z","iopub.execute_input":"2022-08-25T09:55:42.771266Z","iopub.status.idle":"2022-08-25T09:55:47.696066Z","shell.execute_reply.started":"2022-08-25T09:55:42.771232Z","shell.execute_reply":"2022-08-25T09:55:47.694986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"assert len(os.listdir('datasets/images/train')) == len(os.listdir('datasets/labels/train'))\nassert len(os.listdir('datasets/images/valid')) == len(os.listdir('datasets/labels/valid'))","metadata":{"execution":{"iopub.status.busy":"2022-08-25T09:55:47.697677Z","iopub.execute_input":"2022-08-25T09:55:47.698037Z","iopub.status.idle":"2022-08-25T09:55:47.708071Z","shell.execute_reply.started":"2022-08-25T09:55:47.698000Z","shell.execute_reply":"2022-08-25T09:55:47.706980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yaml_text = \"\"\"path: ../datasets\ntrain: images/train\nval: images/valid\nnc: 1\nnames: ['wheat']\"\"\"\n\nwith open(\"yolov5/data/wheat.yaml\", 'w') as f:\n    f.write(yaml_text)","metadata":{"execution":{"iopub.status.busy":"2022-08-25T09:55:47.712529Z","iopub.execute_input":"2022-08-25T09:55:47.712819Z","iopub.status.idle":"2022-08-25T09:55:47.717540Z","shell.execute_reply.started":"2022-08-25T09:55:47.712793Z","shell.execute_reply":"2022-08-25T09:55:47.716532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!  cat \"yolov5/data/wheat.yaml\"","metadata":{"execution":{"iopub.status.busy":"2022-08-25T09:55:47.718978Z","iopub.execute_input":"2022-08-25T09:55:47.719570Z","iopub.status.idle":"2022-08-25T09:55:48.736354Z","shell.execute_reply.started":"2022-08-25T09:55:47.719533Z","shell.execute_reply":"2022-08-25T09:55:48.735171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python ./yolov5/train.py --img 512 --batch 64 --epochs 300 --workers 2 --data yolov5/data/wheat.yaml --weights yolov5/yolov5s.pt --name yolov5s_wheat --cache --hyp hyp.scratch-low-mu.yaml","metadata":{"execution":{"iopub.status.busy":"2022-08-25T09:55:48.738167Z","iopub.execute_input":"2022-08-25T09:55:48.738958Z","iopub.status.idle":"2022-08-25T09:56:32.016848Z","shell.execute_reply.started":"2022-08-25T09:55:48.738902Z","shell.execute_reply":"2022-08-25T09:56:32.015651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#python ./yolov5/detect.py --weights ./yolov5/runs/train/yolov5s_wheat/weights/best.pt --img 672 --source '../input/global-wheat-detection/test' --save-txt --save-conf --exist-ok --augment","metadata":{"execution":{"iopub.status.busy":"2022-08-25T09:56:32.018813Z","iopub.execute_input":"2022-08-25T09:56:32.019599Z","iopub.status.idle":"2022-08-25T09:56:32.025261Z","shell.execute_reply.started":"2022-08-25T09:56:32.019550Z","shell.execute_reply":"2022-08-25T09:56:32.023908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python ./yolov5/detect.py --weights ./yolov5/runs/train/yolov5s_wheat/weights/best.pt --img 512 --source '../input/global-wheat-detection/test' --save-txt --save-conf --exist-ok","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-08-25T09:56:32.027024Z","iopub.execute_input":"2022-08-25T09:56:32.027667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert(s):\n    try:\n        x = int(1024 * (s[1] - s[3]/2))\n        y = int(1024 * (s[2] - s[4]/2))\n        w = int(1024 * s[3])\n        h = int(1024 * s[4])\n\n        return (f\"{s[5]} {x} {y} {w} {h}\")\n    except:\n        return None\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ids = [f[:-4] for f in os.listdir('../input/global-wheat-detection/test')]\ntest_ids","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wfolder = './yolov5/runs/detect/exp/labels/'\nsubmission_df = pd.DataFrame(columns = ['image_id', 'PredictionString'])\nunlabeled_images = list(set(test_ids) - set([f[:-4] for f in os.listdir(wfolder)]))\nfor f in os.listdir(wfolder):\n    bboxs = \"\"\n    fname = wfolder + f\n    xdat = pd.read_csv(fname, sep = ' ', header = None)\n    converted = xdat.apply(lambda s: convert(s), axis=1)\n    for c in converted:\n        bboxs+=c\n        bboxs+=\" \"\n    bboxs = bboxs[:-1]\n    row = {'image_id': f[:-4], 'PredictionString':bboxs}\n    submission_df=submission_df.append(row, ignore_index=True)\nfor u in unlabeled_images:\n    row = {'image_id': u, 'PredictionString':\"\"}\n    submission_df=submission_df.append(row, ignore_index=True)\n  ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sh.rmtree('datasets')\nsh.rmtree('yolov5')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}