{"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":{"execution":{"iopub.status.busy":"2023-07-10T01:49:01.558193Z","iopub.execute_input":"2023-07-10T01:49:01.558587Z","iopub.status.idle":"2023-07-10T01:49:01.565307Z","shell.execute_reply.started":"2023-07-10T01:49:01.558551Z","shell.execute_reply":"2023-07-10T01:49:01.564389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/ultralytics/yolov5","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-07-10T01:49:01.567140Z","iopub.execute_input":"2023-07-10T01:49:01.567709Z","iopub.status.idle":"2023-07-10T01:49:05.507379Z","shell.execute_reply.started":"2023-07-10T01:49:01.567678Z","shell.execute_reply":"2023-07-10T01:49:05.506219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mv yolov5/* ./","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-07-10T01:49:05.509792Z","iopub.execute_input":"2023-07-10T01:49:05.510193Z","iopub.status.idle":"2023-07-10T01:49:06.489719Z","shell.execute_reply.started":"2023-07-10T01:49:05.510153Z","shell.execute_reply":"2023-07-10T01:49:06.488449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -r requirements.txt","metadata":{"_kg_hide-output":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-07-10T01:49:06.491442Z","iopub.execute_input":"2023-07-10T01:49:06.491827Z","iopub.status.idle":"2023-07-10T01:49:31.368014Z","shell.execute_reply.started":"2023-07-10T01:49:06.491789Z","shell.execute_reply":"2023-07-10T01:49:31.366775Z"},"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":"2023-07-10T01:49:31.372005Z","iopub.execute_input":"2023-07-10T01:49:31.372484Z","iopub.status.idle":"2023-07-10T01:49:32.507362Z","shell.execute_reply.started":"2023-07-10T01:49:31.372334Z","shell.execute_reply":"2023-07-10T01:49:32.506466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-10T01:49:32.508592Z","iopub.execute_input":"2023-07-10T01:49:32.508952Z","iopub.status.idle":"2023-07-10T01:49:32.532221Z","shell.execute_reply.started":"2023-07-10T01:49:32.508918Z","shell.execute_reply":"2023-07-10T01:49:32.530872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = list(set(df.image_id))","metadata":{"execution":{"iopub.status.busy":"2023-07-10T01:49:32.533937Z","iopub.execute_input":"2023-07-10T01:49:32.534372Z","iopub.status.idle":"2023-07-10T01:49:32.557262Z","shell.execute_reply.started":"2023-07-10T01:49:32.534332Z","shell.execute_reply":"2023-07-10T01:49:32.556257Z"},"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-07-10T01:49:32.559035Z","iopub.execute_input":"2023-07-10T01:49:32.559723Z","iopub.status.idle":"2023-07-10T01:50:04.948001Z","shell.execute_reply.started":"2023-07-10T01:49:32.559686Z","shell.execute_reply":"2023-07-10T01:50:04.946783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls models/*.yaml","metadata":{"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 models/yolov5s.yaml --name yolov5s_fold0","metadata":{"execution":{"iopub.status.busy":"2023-07-10T01:50:05.938963Z","iopub.execute_input":"2023-07-10T01:50:05.939290Z"},"trusted":true},"execution_count":null,"outputs":[]}]}