{"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":"#to display images\nfrom IPython.display import Image, clear_output","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-02T09:47:44.785187Z","iopub.execute_input":"2023-08-02T09:47:44.785454Z","iopub.status.idle":"2023-08-02T09:47:44.798356Z","shell.execute_reply.started":"2023-08-02T09:47:44.785430Z","shell.execute_reply":"2023-08-02T09:47:44.797461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install tree","metadata":{"execution":{"iopub.status.busy":"2023-08-02T09:47:44.800453Z","iopub.execute_input":"2023-08-02T09:47:44.800816Z","iopub.status.idle":"2023-08-02T09:48:02.541590Z","shell.execute_reply.started":"2023-08-02T09:47:44.800783Z","shell.execute_reply":"2023-08-02T09:48:02.540290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import required dependencies\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nimport tensorflow as tf\nfrom  tqdm.auto import tqdm\nimport shutil as sh\n%matplotlib inline\nnp.random.seed(42)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T09:48:02.547680Z","iopub.execute_input":"2023-08-02T09:48:02.550184Z","iopub.status.idle":"2023-08-02T09:48:02.678754Z","shell.execute_reply.started":"2023-08-02T09:48:02.550144Z","shell.execute_reply":"2023-08-02T09:48:02.677640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/ultralytics/yolov5.git","metadata":{"execution":{"iopub.status.busy":"2023-08-02T09:48:02.683140Z","iopub.execute_input":"2023-08-02T09:48:02.685623Z","iopub.status.idle":"2023-08-02T09:48:06.703469Z","shell.execute_reply.started":"2023-08-02T09:48:02.685588Z","shell.execute_reply":"2023-08-02T09:48:06.701962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ckeck for the cloned repo\n!ls -R\n","metadata":{"execution":{"iopub.status.busy":"2023-08-02T09:48:06.710389Z","iopub.execute_input":"2023-08-02T09:48:06.718887Z","iopub.status.idle":"2023-08-02T09:48:08.005898Z","shell.execute_reply.started":"2023-08-02T09:48:06.718845Z","shell.execute_reply":"2023-08-02T09:48:08.004805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mv yolov5/* ./","metadata":{"execution":{"iopub.status.busy":"2023-08-02T09:48:08.007346Z","iopub.execute_input":"2023-08-02T09:48:08.009466Z","iopub.status.idle":"2023-08-02T09:48:09.226639Z","shell.execute_reply.started":"2023-08-02T09:48:08.009426Z","shell.execute_reply":"2023-08-02T09:48:09.225113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls\n","metadata":{"execution":{"iopub.status.busy":"2023-08-02T09:48:09.231883Z","iopub.execute_input":"2023-08-02T09:48:09.232513Z","iopub.status.idle":"2023-08-02T09:48:10.405187Z","shell.execute_reply.started":"2023-08-02T09:48:09.232475Z","shell.execute_reply":"2023-08-02T09:48:10.403903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#intall dependencies\n!pip install -r requirements.txt","metadata":{"execution":{"iopub.status.busy":"2023-08-02T09:48:10.408616Z","iopub.execute_input":"2023-08-02T09:48:10.408979Z","iopub.status.idle":"2023-08-02T09:48:40.940169Z","shell.execute_reply.started":"2023-08-02T09:48:10.408944Z","shell.execute_reply":"2023-08-02T09:48:40.938936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for dirname,_,filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname,filename))","metadata":{"execution":{"iopub.status.busy":"2023-08-02T09:48:40.942196Z","iopub.execute_input":"2023-08-02T09:48:40.942595Z","iopub.status.idle":"2023-08-02T09:48:43.159044Z","shell.execute_reply.started":"2023-08-02T09:48:40.942555Z","shell.execute_reply":"2023-08-02T09:48:43.158075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#read tthe training data\ndf=pd.read_csv('/kaggle/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]\n    \ndf.drop(columns=['bbox'],inplace=True)\n#the center coordinates of each bounding box\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']]","metadata":{"execution":{"iopub.status.busy":"2023-08-02T09:48:43.160275Z","iopub.execute_input":"2023-08-02T09:48:43.160665Z","iopub.status.idle":"2023-08-02T09:48:44.175356Z","shell.execute_reply.started":"2023-08-02T09:48:43.160627Z","shell.execute_reply":"2023-08-02T09:48:44.174272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-02T09:48:44.179371Z","iopub.execute_input":"2023-08-02T09:48:44.179689Z","iopub.status.idle":"2023-08-02T09:48:44.204412Z","shell.execute_reply.started":"2023-08-02T09:48:44.179661Z","shell.execute_reply":"2023-08-02T09:48:44.203279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df.shape)\nindex=list(set(df.image_id))\nlen(index)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T09:48:44.206112Z","iopub.execute_input":"2023-08-02T09:48:44.206493Z","iopub.status.idle":"2023-08-02T09:48:44.239432Z","shell.execute_reply.started":"2023-08-02T09:48:44.206456Z","shell.execute_reply":"2023-08-02T09:48:44.238396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#trybto display a sample \nImage(filename=\"/kaggle/input/global-wheat-detection/train/00333207f.jpg\",width=600)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T09:48:44.241130Z","iopub.execute_input":"2023-08-02T09:48:44.241538Z","iopub.status.idle":"2023-08-02T09:48:44.258610Z","shell.execute_reply.started":"2023-08-02T09:48:44.241506Z","shell.execute_reply":"2023-08-02T09:48:44.257684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"****data creation****","metadata":{}},{"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                \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":"2023-08-02T09:48:44.260187Z","iopub.execute_input":"2023-08-02T09:48:44.260495Z","iopub.status.idle":"2023-08-02T09:49:13.283799Z","shell.execute_reply.started":"2023-08-02T09:48:44.260467Z","shell.execute_reply":"2023-08-02T09:49:13.282814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir('../input'))","metadata":{"execution":{"iopub.status.busy":"2023-08-02T09:49:13.286712Z","iopub.execute_input":"2023-08-02T09:49:13.287019Z","iopub.status.idle":"2023-08-02T09:49:13.292512Z","shell.execute_reply.started":"2023-08-02T09:49:13.286993Z","shell.execute_reply":"2023-08-02T09:49:13.291451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!tree convertor","metadata":{"execution":{"iopub.status.busy":"2023-08-02T09:49:13.294015Z","iopub.execute_input":"2023-08-02T09:49:13.294641Z","iopub.status.idle":"2023-08-02T09:49:14.414038Z","shell.execute_reply.started":"2023-08-02T09:49:13.294593Z","shell.execute_reply":"2023-08-02T09:49:14.412925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* img: define input image size\n* batch: determine batch size\n* epochs: define the number of training epochs. (Note: often, 3000+ are common here!)\n* data: set the path to our yaml file\n* cfg: specify our model configuration\n* weights: specify a custom path to weights. (Note: you can download weights from the Ultralytics Google Drive folder)\n* name: model name\n* nosave: only save the final checkpoint\n* cache: cache images for faster training","metadata":{}},{"cell_type":"code","source":"!python train.py --img 640 --batch 3 --epochs 8 --data ./data/GlobalWheat2020.yaml \\\n--cfg ./models/yolov5x.yaml \\\n--weights yolov5x.pt \\\n--name yolov5x_wheat --cache ","metadata":{"execution":{"iopub.status.busy":"2023-08-02T09:53:04.228855Z","iopub.execute_input":"2023-08-02T09:53:04.229234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}