{"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 os\nimport shutil as sh\nimport glob\nimport numpy as np\nimport scipy as sp\nimport pandas as pd\nimport cv2\nimport PIL\nfrom skimage.io import imshow, imread, imsave\nimport imageio\nimport imgaug as ia\nimport imgaug.augmenters as iaa\nimport albumentations as A\n\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n%matplotlib inline\nimport seaborn as sns\nfrom IPython.display import HTML, Image\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-09T15:22:34.230796Z","iopub.execute_input":"2022-08-09T15:22:34.232092Z","iopub.status.idle":"2022-08-09T15:22:34.245798Z","shell.execute_reply.started":"2022-08-09T15:22:34.232038Z","shell.execute_reply":"2022-08-09T15:22:34.244287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_path = '../input/global-wheat-detection'","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:22:34.249744Z","iopub.execute_input":"2022-08-09T15:22:34.250776Z","iopub.status.idle":"2022-08-09T15:22:34.264608Z","shell.execute_reply.started":"2022-08-09T15:22:34.250731Z","shell.execute_reply":"2022-08-09T15:22:34.263020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_image(image_id):\n    file_path = image_id\n    image = imread(Image_Data_Path + file_path)\n    return image","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:22:34.267171Z","iopub.execute_input":"2022-08-09T15:22:34.269040Z","iopub.status.idle":"2022-08-09T15:22:34.277245Z","shell.execute_reply.started":"2022-08-09T15:22:34.268961Z","shell.execute_reply":"2022-08-09T15:22:34.275633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def gallery(array, ncols=3):\n    nindex, height, width, intensity = array.shape\n    nrows = nindex//ncols\n    assert nindex == nrows*ncols\n    result = (array.reshape(nrows, ncols, height, width, intensity)\n        .swapaxes(1,2)\n        .reshape(height*nrows, width*ncols, intensity))\n    return result\n","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:22:34.279819Z","iopub.execute_input":"2022-08-09T15:22:34.281406Z","iopub.status.idle":"2022-08-09T15:22:34.290756Z","shell.execute_reply.started":"2022-08-09T15:22:34.281362Z","shell.execute_reply":"2022-08-09T15:22:34.289084Z"},"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-09T15:22:34.293909Z","iopub.execute_input":"2022-08-09T15:22:34.294904Z","iopub.status.idle":"2022-08-09T15:22:34.486249Z","shell.execute_reply.started":"2022-08-09T15:22:34.294845Z","shell.execute_reply":"2022-08-09T15:22:34.484789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bboxs = np.stack(df['bbox'].apply(lambda x: np.fromstring(x[1:-1],\n sep=',')))\nbboxs","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:22:34.488866Z","iopub.execute_input":"2022-08-09T15:22:34.489191Z","iopub.status.idle":"2022-08-09T15:22:34.972088Z","shell.execute_reply.started":"2022-08-09T15:22:34.489159Z","shell.execute_reply":"2022-08-09T15:22:34.970586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for 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-09T15:22:34.975947Z","iopub.execute_input":"2022-08-09T15:22:34.976786Z","iopub.status.idle":"2022-08-09T15:22:35.024840Z","shell.execute_reply.started":"2022-08-09T15:22:34.976739Z","shell.execute_reply":"2022-08-09T15:22:35.023574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm.notebook import tqdm","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:22:35.027193Z","iopub.execute_input":"2022-08-09T15:22:35.028193Z","iopub.status.idle":"2022-08-09T15:22:35.035338Z","shell.execute_reply.started":"2022-08-09T15:22:35.028147Z","shell.execute_reply":"2022-08-09T15:22:35.032960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['fold'] = np.random.randint(0, 5, df.shape[0])","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:22:35.038381Z","iopub.execute_input":"2022-08-09T15:22:35.039868Z","iopub.status.idle":"2022-08-09T15:22:35.051865Z","shell.execute_reply.started":"2022-08-09T15:22:35.039803Z","shell.execute_reply":"2022-08-09T15:22:35.050326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = list(set(df.image_id))","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:22:35.055583Z","iopub.execute_input":"2022-08-09T15:22:35.057987Z","iopub.status.idle":"2022-08-09T15:22:35.077024Z","shell.execute_reply.started":"2022-08-09T15:22:35.057914Z","shell.execute_reply":"2022-08-09T15:22:35.075321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = df.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:22:35.080152Z","iopub.execute_input":"2022-08-09T15:22:35.080732Z","iopub.status.idle":"2022-08-09T15:22:35.088676Z","shell.execute_reply.started":"2022-08-09T15:22:35.080681Z","shell.execute_reply":"2022-08-09T15:22:35.086881Z"},"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))\n","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:22:35.094669Z","iopub.execute_input":"2022-08-09T15:22:35.095944Z","iopub.status.idle":"2022-08-09T15:22:46.568267Z","shell.execute_reply.started":"2022-08-09T15:22:35.095886Z","shell.execute_reply":"2022-08-09T15:22:46.566050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/ultralytics/yolov5 && cd yolov5 && pip install -r requirements.txt ","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:22:46.569888Z","iopub.status.idle":"2022-08-09T15:22:46.571111Z","shell.execute_reply.started":"2022-08-09T15:22:46.570709Z","shell.execute_reply":"2022-08-09T15:22:46.570747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yaml_text = \"\"\"train: ../convertor/fold0/images/train2017/\nval: ../convertor/fold0/images/val2017/\nnc: 1\nnames: ['wheat']\"\"\"\nwith open(\"wheat.yaml\", 'w') as f:\n    f.write(yaml_text)\n!cat wheat.yaml","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:22:46.573577Z","iopub.status.idle":"2022-08-09T15:22:46.574240Z","shell.execute_reply.started":"2022-08-09T15:22:46.573862Z","shell.execute_reply":"2022-08-09T15:22:46.573893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python ./yolov5/train.py --img 512 --batch 2 --epochs 1 --workers 2 --data wheat.yaml --cfg \"./yolov5/models/yolov5s.yaml\" --name yolov5x_fold0 --cache","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:22:46.577165Z","iopub.status.idle":"2022-08-09T15:22:46.577801Z","shell.execute_reply.started":"2022-08-09T15:22:46.577453Z","shell.execute_reply":"2022-08-09T15:22:46.577495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python ./yolov5/detect.py --weights ./yolov5/runs/train/yolov5x_fold0/weights/best.pt --img 512 --conf 0.1 --source /kaggle/input/global-wheat-detection/test --save-txt --save-conf --exist-ok","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:22:46.579740Z","iopub.status.idle":"2022-08-09T15:22:46.581068Z","shell.execute_reply.started":"2022-08-09T15:22:46.580694Z","shell.execute_reply":"2022-08-09T15:22:46.580738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls ./yolov5/runs/detect/exp/labels/","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:22:46.585584Z","iopub.status.idle":"2022-08-09T15:22:46.586204Z","shell.execute_reply.started":"2022-08-09T15:22:46.585868Z","shell.execute_reply":"2022-08-09T15:22:46.585898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert(s):\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(str(s[5]) + ' ' + str(x) + ' ' + str(y) + ' ' + str(w) + ' ' + str(h))\n","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:22:46.588027Z","iopub.status.idle":"2022-08-09T15:22:46.590051Z","shell.execute_reply.started":"2022-08-09T15:22:46.589632Z","shell.execute_reply":"2022-08-09T15:22:46.589668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('submission.csv', 'w') as myfile:\n    wfolder = './yolov5/runs/detect/exp/labels/'\n    \n    for f in os.listdir(wfolder):\n        fname = wfolder + f\n        xdat = pd.read_csv(fname, sep = ' ', header = None)\n        outline = f[:-4] + ' ' + ' '.join(list(xdat.apply(lambda s : convert(s), axis = 1)))\n        myfile.write(outline + '\\n')\n    myfile.close()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:22:46.592529Z","iopub.status.idle":"2022-08-09T15:22:46.593149Z","shell.execute_reply.started":"2022-08-09T15:22:46.592836Z","shell.execute_reply":"2022-08-09T15:22:46.592867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.read_csv('submission.csv', header=None)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:22:46.595206Z","iopub.status.idle":"2022-08-09T15:22:46.596732Z","shell.execute_reply.started":"2022-08-09T15:22:46.596321Z","shell.execute_reply":"2022-08-09T15:22:46.596367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df[0]","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:22:46.598950Z","iopub.status.idle":"2022-08-09T15:22:46.599647Z","shell.execute_reply.started":"2022-08-09T15:22:46.599295Z","shell.execute_reply":"2022-08-09T15:22:46.599324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df[[\"image_id\", \"PredictionString\"]] = (\n    submission_df[0].str.split(\" \", n=1, expand=True)\n)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:22:46.602349Z","iopub.status.idle":"2022-08-09T15:22:46.603047Z","shell.execute_reply.started":"2022-08-09T15:22:46.602718Z","shell.execute_reply":"2022-08-09T15:22:46.602747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = submission_df.drop(columns=0)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:22:46.604983Z","iopub.status.idle":"2022-08-09T15:22:46.606327Z","shell.execute_reply.started":"2022-08-09T15:22:46.605979Z","shell.execute_reply":"2022-08-09T15:22:46.606019Z"},"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-09T15:22:46.608822Z","iopub.status.idle":"2022-08-09T15:22:46.609406Z","shell.execute_reply.started":"2022-08-09T15:22:46.609112Z","shell.execute_reply":"2022-08-09T15:22:46.609142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:22:46.612048Z","iopub.status.idle":"2022-08-09T15:22:46.612734Z","shell.execute_reply.started":"2022-08-09T15:22:46.612360Z","shell.execute_reply":"2022-08-09T15:22:46.612391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sh.rmtree('convertor')\nsh.rmtree('yolov5')","metadata":{"execution":{"iopub.status.busy":"2022-08-09T16:06:58.442285Z","iopub.execute_input":"2022-08-09T16:06:58.442725Z","iopub.status.idle":"2022-08-09T16:06:59.754517Z","shell.execute_reply.started":"2022-08-09T16:06:58.442695Z","shell.execute_reply":"2022-08-09T16:06:59.752919Z"},"trusted":true},"execution_count":null,"outputs":[]}]}