{"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-24T04:35:20.925769Z","iopub.execute_input":"2022-08-24T04:35:20.926255Z","iopub.status.idle":"2022-08-24T04:35:20.951359Z","shell.execute_reply.started":"2022-08-24T04:35:20.926176Z","shell.execute_reply":"2022-08-24T04:35:20.950542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r ../input/yolo-evolve/yolov5 .","metadata":{"execution":{"iopub.status.busy":"2022-08-24T04:35:20.954430Z","iopub.execute_input":"2022-08-24T04:35:20.955034Z","iopub.status.idle":"2022-08-24T04:35:25.199322Z","shell.execute_reply.started":"2022-08-24T04:35:20.954996Z","shell.execute_reply":"2022-08-24T04:35:25.197624Z"},"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-24T04:35:25.207766Z","iopub.execute_input":"2022-08-24T04:35:25.208668Z","iopub.status.idle":"2022-08-24T04:35:32.685165Z","shell.execute_reply.started":"2022-08-24T04:35:25.208621Z","shell.execute_reply":"2022-08-24T04:35:32.683882Z"},"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-24T04:35:32.687260Z","iopub.execute_input":"2022-08-24T04:35:32.687677Z","iopub.status.idle":"2022-08-24T04:35:32.925690Z","shell.execute_reply.started":"2022-08-24T04:35:32.687634Z","shell.execute_reply":"2022-08-24T04:35:32.924698Z"},"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-24T04:35:32.929628Z","iopub.execute_input":"2022-08-24T04:35:32.929905Z","iopub.status.idle":"2022-08-24T04:35:33.470197Z","shell.execute_reply.started":"2022-08-24T04:35:32.929880Z","shell.execute_reply":"2022-08-24T04:35:33.469253Z"},"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-24T04:35:33.471750Z","iopub.execute_input":"2022-08-24T04:35:33.472115Z","iopub.status.idle":"2022-08-24T04:36:07.500733Z","shell.execute_reply.started":"2022-08-24T04:35:33.472063Z","shell.execute_reply":"2022-08-24T04:36:07.499560Z"},"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-24T04:36:07.502593Z","iopub.execute_input":"2022-08-24T04:36:07.503190Z","iopub.status.idle":"2022-08-24T04:36:07.934115Z","shell.execute_reply.started":"2022-08-24T04:36:07.503151Z","shell.execute_reply":"2022-08-24T04:36:07.933181Z"},"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-24T04:36:07.935530Z","iopub.execute_input":"2022-08-24T04:36:07.936225Z","iopub.status.idle":"2022-08-24T04:36:13.196711Z","shell.execute_reply.started":"2022-08-24T04:36:07.936186Z","shell.execute_reply":"2022-08-24T04:36:13.195675Z"},"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-24T04:36:13.198098Z","iopub.execute_input":"2022-08-24T04:36:13.198438Z","iopub.status.idle":"2022-08-24T04:36:13.209180Z","shell.execute_reply.started":"2022-08-24T04:36:13.198403Z","shell.execute_reply":"2022-08-24T04:36:13.208160Z"},"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-24T04:36:13.210636Z","iopub.execute_input":"2022-08-24T04:36:13.211130Z","iopub.status.idle":"2022-08-24T04:36:13.219988Z","shell.execute_reply.started":"2022-08-24T04:36:13.211028Z","shell.execute_reply":"2022-08-24T04:36:13.218965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!  cat \"yolov5/data/wheat.yaml\"","metadata":{"execution":{"iopub.status.busy":"2022-08-24T04:36:13.225062Z","iopub.execute_input":"2022-08-24T04:36:13.225327Z","iopub.status.idle":"2022-08-24T04:36:14.357879Z","shell.execute_reply.started":"2022-08-24T04:36:13.225303Z","shell.execute_reply":"2022-08-24T04:36:14.356712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python ./yolov5/train.py --img 512 --batch 64 --epochs 600 --workers 2 --data yolov5/data/wheat.yaml --weights yolov5/yolov5s.pt --name yolov5s_wheat --cache","metadata":{"execution":{"iopub.status.busy":"2022-08-24T04:36:14.359954Z","iopub.execute_input":"2022-08-24T04:36:14.360332Z","iopub.status.idle":"2022-08-24T04:39:34.116100Z","shell.execute_reply.started":"2022-08-24T04:36:14.360287Z","shell.execute_reply":"2022-08-24T04:39:34.114810Z"},"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-24T04:39:34.117990Z","iopub.execute_input":"2022-08-24T04:39:34.118402Z","iopub.status.idle":"2022-08-24T04:39:34.124548Z","shell.execute_reply.started":"2022-08-24T04:39:34.118357Z","shell.execute_reply":"2022-08-24T04:39:34.123461Z"},"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-24T04:39:34.126143Z","iopub.execute_input":"2022-08-24T04:39:34.126934Z","iopub.status.idle":"2022-08-24T04:39:42.667554Z","shell.execute_reply.started":"2022-08-24T04:39:34.126900Z","shell.execute_reply":"2022-08-24T04:39:42.666357Z"},"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":{"execution":{"iopub.status.busy":"2022-08-24T04:39:42.669693Z","iopub.execute_input":"2022-08-24T04:39:42.670095Z","iopub.status.idle":"2022-08-24T04:39:42.677229Z","shell.execute_reply.started":"2022-08-24T04:39:42.670051Z","shell.execute_reply":"2022-08-24T04:39:42.676142Z"},"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":{"execution":{"iopub.status.busy":"2022-08-24T04:39:42.678992Z","iopub.execute_input":"2022-08-24T04:39:42.679341Z","iopub.status.idle":"2022-08-24T04:39:42.690560Z","shell.execute_reply.started":"2022-08-24T04:39:42.679307Z","shell.execute_reply":"2022-08-24T04:39:42.689463Z"},"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":{"execution":{"iopub.status.busy":"2022-08-24T04:39:42.692122Z","iopub.execute_input":"2022-08-24T04:39:42.692605Z","iopub.status.idle":"2022-08-24T04:39:42.739081Z","shell.execute_reply.started":"2022-08-24T04:39:42.692569Z","shell.execute_reply":"2022-08-24T04:39:42.738270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-24T04:39:42.740548Z","iopub.execute_input":"2022-08-24T04:39:42.741144Z","iopub.status.idle":"2022-08-24T04:39:42.749517Z","shell.execute_reply.started":"2022-08-24T04:39:42.741110Z","shell.execute_reply":"2022-08-24T04:39:42.748527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sh.rmtree('datasets')\nsh.rmtree('yolov5')","metadata":{"execution":{"iopub.status.busy":"2022-08-24T04:39:42.751012Z","iopub.execute_input":"2022-08-24T04:39:42.751644Z","iopub.status.idle":"2022-08-24T04:39:43.084835Z","shell.execute_reply.started":"2022-08-24T04:39:42.751609Z","shell.execute_reply":"2022-08-24T04:39:43.083456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}