{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-05-25T12:57:56.040242Z","iopub.execute_input":"2021-05-25T12:57:56.040488Z","iopub.status.idle":"2021-05-25T12:57:56.045424Z","shell.execute_reply.started":"2021-05-25T12:57:56.040462Z","shell.execute_reply":"2021-05-25T12:57:56.044661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone -b v1.0 https://github.com/ultralytics/yolov5","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2021-05-25T12:57:56.069414Z","iopub.execute_input":"2021-05-25T12:57:56.069918Z","iopub.status.idle":"2021-05-25T12:58:00.253831Z","shell.execute_reply.started":"2021-05-25T12:57:56.069880Z","shell.execute_reply":"2021-05-25T12:58:00.252864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mv yolov5/* ./","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:58:00.257286Z","iopub.execute_input":"2021-05-25T12:58:00.257575Z","iopub.status.idle":"2021-05-25T12:58:00.890963Z","shell.execute_reply.started":"2021-05-25T12:58:00.257546Z","shell.execute_reply":"2021-05-25T12:58:00.889872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -r requirements.txt","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:58:00.894459Z","iopub.execute_input":"2021-05-25T12:58:00.894758Z","iopub.status.idle":"2021-05-25T12:58:40.304702Z","shell.execute_reply.started":"2021-05-25T12:58:00.894727Z","shell.execute_reply":"2021-05-25T12:58:40.303886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2021-05-25T12:58:40.306271Z","iopub.execute_input":"2021-05-25T12:58:40.306787Z","iopub.status.idle":"2021-05-25T12:58:41.653075Z","shell.execute_reply.started":"2021-05-25T12:58:40.306746Z","shell.execute_reply":"2021-05-25T12:58:41.652299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:58:41.655182Z","iopub.execute_input":"2021-05-25T12:58:41.655433Z","iopub.status.idle":"2021-05-25T12:58:41.675222Z","shell.execute_reply.started":"2021-05-25T12:58:41.655409Z","shell.execute_reply":"2021-05-25T12:58:41.674225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = list(set(df.image_id))","metadata":{"execution":{"iopub.status.busy":"2021-05-25T12:58:41.677199Z","iopub.execute_input":"2021-05-25T12:58:41.677615Z","iopub.status.idle":"2021-05-25T12:58:41.697676Z","shell.execute_reply.started":"2021-05-25T12:58:41.677579Z","shell.execute_reply":"2021-05-25T12:58:41.696918Z"},"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":"2021-05-25T12:58:41.698859Z","iopub.execute_input":"2021-05-25T12:58:41.699214Z","iopub.status.idle":"2021-05-25T12:59:21.891609Z","shell.execute_reply.started":"2021-05-25T12:58:41.699177Z","shell.execute_reply":"2021-05-25T12:59:21.890625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python train.py --img 1024 --batch 2 --epochs 1 --data ../input/configyolo5/wheat0.yaml --cfg ../input/configyolo5/yolov5x.yaml  --name yolov5x_fold0","metadata":{"execution":{"iopub.status.busy":"2021-05-25T13:00:24.268711Z","iopub.execute_input":"2021-05-25T13:00:24.269062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}