{"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\nimport ast\nimport os\nimport shutil\nfrom tqdm import tqdm\n!pip install ultralytics\n\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB 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":"2023-08-19T04:31:38.610032Z","iopub.execute_input":"2023-08-19T04:31:38.611190Z","iopub.status.idle":"2023-08-19T04:31:54.390191Z","shell.execute_reply.started":"2023-08-19T04:31:38.611147Z","shell.execute_reply":"2023-08-19T04:31:54.389045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df= pd.read_csv(\"/kaggle/input/global-wheat-detection/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-08-19T04:31:54.392057Z","iopub.execute_input":"2023-08-19T04:31:54.393149Z","iopub.status.idle":"2023-08-19T04:31:54.685351Z","shell.execute_reply.started":"2023-08-19T04:31:54.393111Z","shell.execute_reply":"2023-08-19T04:31:54.684380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-19T04:31:54.686623Z","iopub.execute_input":"2023-08-19T04:31:54.687013Z","iopub.status.idle":"2023-08-19T04:31:54.705215Z","shell.execute_reply.started":"2023-08-19T04:31:54.686980Z","shell.execute_reply":"2023-08-19T04:31:54.704216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.rename(columns={\"width\":\"image_width\",\"height\":\"image_height\"},inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-08-19T04:31:54.707966Z","iopub.execute_input":"2023-08-19T04:31:54.708511Z","iopub.status.idle":"2023-08-19T04:31:54.714773Z","shell.execute_reply.started":"2023-08-19T04:31:54.708478Z","shell.execute_reply":"2023-08-19T04:31:54.712742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"bbox\"][0]","metadata":{"execution":{"iopub.status.busy":"2023-08-19T04:31:54.716105Z","iopub.execute_input":"2023-08-19T04:31:54.716795Z","iopub.status.idle":"2023-08-19T04:31:54.725567Z","shell.execute_reply.started":"2023-08-19T04:31:54.716763Z","shell.execute_reply":"2023-08-19T04:31:54.724313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"bbox\"]= df[\"bbox\"].apply(ast.literal_eval)","metadata":{"execution":{"iopub.status.busy":"2023-08-19T04:31:54.727073Z","iopub.execute_input":"2023-08-19T04:31:54.727525Z","iopub.status.idle":"2023-08-19T04:31:57.137146Z","shell.execute_reply.started":"2023-08-19T04:31:54.727436Z","shell.execute_reply":"2023-08-19T04:31:57.136140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2023-08-19T04:31:57.138704Z","iopub.execute_input":"2023-08-19T04:31:57.139083Z","iopub.status.idle":"2023-08-19T04:31:57.159503Z","shell.execute_reply.started":"2023-08-19T04:31:57.139046Z","shell.execute_reply":"2023-08-19T04:31:57.158362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Preparing to yolo format: status_before normalization\ndf[\"x_center\"]= [ i[0]+(i[2]/2) for i in df[\"bbox\"]]\ndf[\"y_center\"]= [ i[1]+(i[3]/2) for i in df[\"bbox\"]]\ndf[\"bbox_width\"]=[i[2] for i in df[\"bbox\"]]\ndf[\"bbox_height\"]=[i[3] for i in df[\"bbox\"]]\ndf[\"label\"]=0","metadata":{"execution":{"iopub.status.busy":"2023-08-19T04:31:57.161460Z","iopub.execute_input":"2023-08-19T04:31:57.161893Z","iopub.status.idle":"2023-08-19T04:31:57.428710Z","shell.execute_reply.started":"2023-08-19T04:31:57.161854Z","shell.execute_reply":"2023-08-19T04:31:57.427744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Changing to yolo format: status_normalized\n#You can merge these steps at once. I will leave it incase if i need to reference in future \ndf[\"bbox_width_normalized\"]=df[\"bbox_width\"]/ df[\"image_width\"]\ndf[\"x_center_normalized\"]=df[\"x_center\"]/ df[\"image_width\"]\ndf[\"bbox_height_normalized\"]=df[\"bbox_height\"]/ df[\"image_height\"]\ndf[\"y_center_normalized\"]=df[\"y_center\"]/ df[\"image_height\"]","metadata":{"execution":{"iopub.status.busy":"2023-08-19T04:31:57.430057Z","iopub.execute_input":"2023-08-19T04:31:57.430494Z","iopub.status.idle":"2023-08-19T04:31:57.443405Z","shell.execute_reply.started":"2023-08-19T04:31:57.430461Z","shell.execute_reply":"2023-08-19T04:31:57.442277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-19T04:31:57.447890Z","iopub.execute_input":"2023-08-19T04:31:57.448214Z","iopub.status.idle":"2023-08-19T04:31:57.472806Z","shell.execute_reply.started":"2023-08-19T04:31:57.448190Z","shell.execute_reply":"2023-08-19T04:31:57.471881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = list(set(df.image_id))\nfor fold in [0]:\n        val_index = index[len(index)*fold//5:len(index)*(fold+1)//5]\n        for id_name, df_with_id_name in tqdm(df.groupby(\"image_id\")):\n            if id_name in val_index:\n                path2save=\"val/\"\n            else:\n                path2save=\"train/\"\n            if not os.path.exists('data/fold{}/labels/'.format(fold)+path2save):\n                os.makedirs('data/fold{}/labels/'.format(fold)+path2save)\n            with open(\"data/fold{}/labels/\".format(fold)+path2save+id_name+'.txt','w+') as f:\n                row= df_with_id_name[[\"label\",\"x_center_normalized\",\"y_center_normalized\",\"bbox_width_normalized\",\"bbox_height_normalized\"]].astype(float).values\n                row= row.astype(str)\n                for x in range(len(row)):\n                    value= \" \".join(row[x])\n                    f.write(value)\n                    f.write(\"\\n\")\n            if not os.path.exists('data/fold{}/images/'.format(fold)+path2save):\n                os.makedirs('data/fold{}/images/'.format(fold)+path2save)\n            source = \"../input/global-wheat-detection/train/{}.jpg\".format(id_name)\n            dest = \"data/fold{}/images/{}/{}.jpg\".format(fold, path2save, id_name)\n            shutil.copy(source, dest)\n                    \n            \n                \n        ","metadata":{"execution":{"iopub.status.busy":"2023-08-19T04:31:57.474456Z","iopub.execute_input":"2023-08-19T04:31:57.475339Z","iopub.status.idle":"2023-08-19T04:32:27.883061Z","shell.execute_reply.started":"2023-08-19T04:31:57.475293Z","shell.execute_reply":"2023-08-19T04:32:27.882112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(val_index)","metadata":{"execution":{"iopub.status.busy":"2023-08-19T04:32:27.884660Z","iopub.execute_input":"2023-08-19T04:32:27.885293Z","iopub.status.idle":"2023-08-19T04:32:27.892261Z","shell.execute_reply.started":"2023-08-19T04:32:27.885252Z","shell.execute_reply":"2023-08-19T04:32:27.891251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.getcwd()","metadata":{"execution":{"iopub.status.busy":"2023-08-19T04:32:27.894045Z","iopub.execute_input":"2023-08-19T04:32:27.894385Z","iopub.status.idle":"2023-08-19T04:32:28.498429Z","shell.execute_reply.started":"2023-08-19T04:32:27.894353Z","shell.execute_reply":"2023-08-19T04:32:28.496977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import yaml\n\n# Define the YAML content\nconfig_content = \"\"\"\npath: /kaggle/working/data/fold0/\ntrain: images/train\nval: images/val\nnc: 1\nnames: [\"Wheat\"]\n\"\"\"\n\n# Save the content to a YAML file\nwith open('yolo_config.yaml', 'w') as config_file:\n    config_file.write(config_content)","metadata":{"execution":{"iopub.status.busy":"2023-08-19T04:32:28.500144Z","iopub.execute_input":"2023-08-19T04:32:28.500535Z","iopub.status.idle":"2023-08-19T04:32:28.508351Z","shell.execute_reply.started":"2023-08-19T04:32:28.500478Z","shell.execute_reply":"2023-08-19T04:32:28.507384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wandb off","metadata":{"execution":{"iopub.status.busy":"2023-08-19T04:32:28.510059Z","iopub.execute_input":"2023-08-19T04:32:28.510507Z","iopub.status.idle":"2023-08-19T04:32:31.210930Z","shell.execute_reply.started":"2023-08-19T04:32:28.510475Z","shell.execute_reply":"2023-08-19T04:32:31.209774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ultralytics import YOLO\n\nmodel= YOLO(\"yolov8n.pt\")\nresults= model.train(data=\"yolo_config.yaml\", epochs=40)","metadata":{"execution":{"iopub.status.busy":"2023-08-19T04:32:31.213007Z","iopub.execute_input":"2023-08-19T04:32:31.213377Z","iopub.status.idle":"2023-08-19T06:02:02.105681Z","shell.execute_reply.started":"2023-08-19T04:32:31.213342Z","shell.execute_reply":"2023-08-19T06:02:02.104362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import Image","metadata":{"execution":{"iopub.status.busy":"2023-08-19T06:02:02.123834Z","iopub.execute_input":"2023-08-19T06:02:02.124145Z","iopub.status.idle":"2023-08-19T06:02:02.136684Z","shell.execute_reply.started":"2023-08-19T06:02:02.124119Z","shell.execute_reply":"2023-08-19T06:02:02.135614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image(filename='/kaggle/working/runs/detect/train/results.png',width=600)","metadata":{"execution":{"iopub.status.busy":"2023-08-19T06:02:02.193339Z","iopub.execute_input":"2023-08-19T06:02:02.193645Z","iopub.status.idle":"2023-08-19T06:02:02.225920Z","shell.execute_reply.started":"2023-08-19T06:02:02.193617Z","shell.execute_reply":"2023-08-19T06:02:02.224747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# path to best model\nbest=\"/kaggle/working/runs/detect/train/weights/best.pt\"\n#path to lalst model\nlast=\"/kaggle/working/runs/detect/train/weights/last.pt\"\n\nimage_path=\"/kaggle/input/global-wheat-detection/test/2fd875eaa.jpg\"","metadata":{"execution":{"iopub.status.busy":"2023-08-19T06:26:22.587591Z","iopub.execute_input":"2023-08-19T06:26:22.588258Z","iopub.status.idle":"2023-08-19T06:26:22.599810Z","shell.execute_reply.started":"2023-08-19T06:26:22.588210Z","shell.execute_reply":"2023-08-19T06:26:22.594484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2","metadata":{"execution":{"iopub.status.busy":"2023-08-19T06:26:38.238838Z","iopub.execute_input":"2023-08-19T06:26:38.239218Z","iopub.status.idle":"2023-08-19T06:26:38.246326Z","shell.execute_reply.started":"2023-08-19T06:26:38.239189Z","shell.execute_reply":"2023-08-19T06:26:38.245145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=YOLO(best)\nresults= model(source=\"/kaggle/input/global-wheat-detection/test\", save=True)","metadata":{"execution":{"iopub.status.busy":"2023-08-19T06:49:49.128180Z","iopub.execute_input":"2023-08-19T06:49:49.128578Z","iopub.status.idle":"2023-08-19T06:49:50.084679Z","shell.execute_reply.started":"2023-08-19T06:49:49.128545Z","shell.execute_reply":"2023-08-19T06:49:50.083646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nfrom IPython.display import Image, display\n\nfor image_path in glob.glob('/kaggle/working/runs/detect/predict2/*.jpg'):\n    display(Image(filename=image_path, width=600))\n    print(\"\\n\")\n","metadata":{"execution":{"iopub.status.busy":"2023-08-19T06:56:42.439923Z","iopub.execute_input":"2023-08-19T06:56:42.440294Z","iopub.status.idle":"2023-08-19T06:56:42.637852Z","shell.execute_reply.started":"2023-08-19T06:56:42.440263Z","shell.execute_reply":"2023-08-19T06:56:42.636534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}