{"cells":[{"metadata":{},"cell_type":"markdown","source":"## This is a YOLOv4 training pipeline with Pytorch. I use coco pre-trained weights. Have fun and feel free to leave any comment!","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"## Reference\nhttps://github.com/Tianxiaomo/pytorch-YOLOv4\nhttps://www.kaggle.com/orkatz2/yolov5-train","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"!rm ./* -r","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"!cp -r ../input/yolov4pytorch/pytorch-YOLOv4/* ./","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"!pip install -U -r requirements.txt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\ndf = 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['x1'] = df['x'] + df['w']\ndf['y1'] = df['y'] + df['h']\ndf['classes'] = 0\nfrom tqdm.auto import tqdm\nimport shutil as sh\ndf = df[['image_id','x', 'y', 'w', 'h','x1','y1','classes']]\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def f7(seq):\n    seen = set()\n    seen_add = seen.add\n    return [x for x in seq if not (x in seen or seen_add(x))]\nindex = f7(df.image_id)\nimport random\nrandom.Random(42).shuffle(index)\nlen(index)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"source = 'train'\nfor 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 not os.path.exists('convertor'):\n            os.makedirs('convertor')\n        sh.copy(\"../input/global-wheat-detection/{}/{}.jpg\".format(source,name),'convertor/{}.jpg'.format(name))\n        if name in val_index:\n            path2save = 'convertor/val.txt'\n        else:\n            path2save = 'convertor/train.txt'\n        with open(path2save, 'a') as f:\n            f.write(f'{name}.jpg')\n            row = mini[['x','y','x1','y1','classes']].astype(int).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')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#!python train.py -l 0.01 -g 0 -classes 1 -dir /kaggle/working/convertor -pretrained ../input/yolov4coco/yolov4.conv.137.pth -optimizer sgd -iou-type giou -train_label_path convertor/train.txt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!rm convertor/*","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}