{"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":"markdown","source":"#### I already Make Trainning into My google Colab \n##### The Link Of My NoteBook : \nhttps://colab.research.google.com/drive/1qBbaH2YkiJH5yGMy-BqKbb3iPK_WcBZ2?usp=sharing ","metadata":{}},{"cell_type":"markdown","source":"### 1.1 Download Model and requirments:","metadata":{}},{"cell_type":"code","source":"!git clone https://github.com/ultralytics/yolov5","metadata":{"execution":{"iopub.status.busy":"2022-10-08T15:59:27.257646Z","iopub.execute_input":"2022-10-08T15:59:27.258348Z","iopub.status.idle":"2022-10-08T15:59:30.310185Z","shell.execute_reply.started":"2022-10-08T15:59:27.258257Z","shell.execute_reply":"2022-10-08T15:59:30.309004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ./yolov5\n!pip install -r requirements.txt","metadata":{"execution":{"iopub.status.busy":"2022-10-08T15:59:55.994627Z","iopub.execute_input":"2022-10-08T15:59:55.995041Z","iopub.status.idle":"2022-10-08T16:00:07.208363Z","shell.execute_reply.started":"2022-10-08T15:59:55.995007Z","shell.execute_reply":"2022-10-08T16:00:07.207230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 1.2 Load The Model:","metadata":{}},{"cell_type":"code","source":"import torch \nimport numpy as np \nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:00:07.212164Z","iopub.execute_input":"2022-10-08T16:00:07.212514Z","iopub.status.idle":"2022-10-08T16:00:09.086699Z","shell.execute_reply.started":"2022-10-08T16:00:07.212476Z","shell.execute_reply":"2022-10-08T16:00:09.085698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = torch.hub.load('ultralytics/yolov5', 'yolov5s')\nmodel","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:00:11.897100Z","iopub.execute_input":"2022-10-08T16:00:11.897887Z","iopub.status.idle":"2022-10-08T16:00:24.410068Z","shell.execute_reply.started":"2022-10-08T16:00:11.897839Z","shell.execute_reply":"2022-10-08T16:00:24.409134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = r\"https://images.unsplash.com/photo-1616753677823-bc8270e35ec1?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1000&q=80\"\nresults = model(img)\nresults.print()","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:00:33.530087Z","iopub.execute_input":"2022-10-08T16:00:33.530471Z","iopub.status.idle":"2022-10-08T16:00:34.146435Z","shell.execute_reply.started":"2022-10-08T16:00:33.530427Z","shell.execute_reply":"2022-10-08T16:00:34.145284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\nplt.figure(figsize=(20,10))\nplt.imshow(np.squeeze(results.render()))\nplt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:00:35.280739Z","iopub.execute_input":"2022-10-08T16:00:35.281099Z","iopub.status.idle":"2022-10-08T16:00:35.869705Z","shell.execute_reply.started":"2022-10-08T16:00:35.281069Z","shell.execute_reply":"2022-10-08T16:00:35.868501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2.1 Visualizing & Loading Data:","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nfrom tensorflow.keras.utils import load_img,img_to_array","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:00:42.148594Z","iopub.execute_input":"2022-10-08T16:00:42.149308Z","iopub.status.idle":"2022-10-08T16:00:47.086672Z","shell.execute_reply.started":"2022-10-08T16:00:42.149263Z","shell.execute_reply":"2022-10-08T16:00:47.085680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Number OF Trainning Images:\"+ str(len(os.listdir(\"/kaggle/input/global-wheat-detection/train\"))))\nprint(\"Number OF Testing Images:\"+ str(len(os.listdir(\"/kaggle/input/global-wheat-detection/test\"))))","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:01:24.591216Z","iopub.execute_input":"2022-10-08T16:01:24.591598Z","iopub.status.idle":"2022-10-08T16:01:24.606003Z","shell.execute_reply.started":"2022-10-08T16:01:24.591563Z","shell.execute_reply":"2022-10-08T16:01:24.604944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Train_img=load_img(\"/kaggle/input/global-wheat-detection/train/00333207f.jpg\")\ntrain_img=img_to_array(Train_img)\nprint(\"Size of the image is :\", train_img.shape)\n%matplotlib inline\nplt.figure(figsize=(20,10))\nplt.imshow(Train_img)","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:01:50.282022Z","iopub.execute_input":"2022-10-08T16:01:50.282413Z","iopub.status.idle":"2022-10-08T16:01:50.867185Z","shell.execute_reply.started":"2022-10-08T16:01:50.282381Z","shell.execute_reply":"2022-10-08T16:01:50.862536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv(r'/kaggle/input/global-wheat-detection/train.csv')\ndf","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:02:01.562928Z","iopub.execute_input":"2022-10-08T16:02:01.563313Z","iopub.status.idle":"2022-10-08T16:02:01.833742Z","shell.execute_reply.started":"2022-10-08T16:02:01.563278Z","shell.execute_reply":"2022-10-08T16:02:01.832801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bboxs = np.stack(df['bbox'].apply(lambda x: np.fromstring(x[1:-1], sep=',')))\nfor i, column in enumerate(['x', 'y', 'width', 'hieght']):\n    df[column] = bboxs[:,i] \ndf.drop(columns=['bbox'], inplace=True)\ndf['x_center'] = df['x'] + df['width']/2\ndf['y_center'] = df['y'] + df['hieght']/2\ndf['classes'] = 0\ndf['x_center'] = df['x_center']/1024\ndf['y_center'] = df['y_center']/1024\ndf['width'] = df['width']/1024\ndf['hieght'] = df['hieght']/1024\ndf = df[['image_id', 'width', 'hieght','x_center','y_center','classes']]\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:02:05.537973Z","iopub.execute_input":"2022-10-08T16:02:05.538340Z","iopub.status.idle":"2022-10-08T16:02:05.993335Z","shell.execute_reply.started":"2022-10-08T16:02:05.538307Z","shell.execute_reply":"2022-10-08T16:02:05.992360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2.2 Convert Train Labels To Yolo Format:","metadata":{}},{"cell_type":"code","source":"%cd ..\nif not os.path.isdir(\"Data\"):\n  os.makedirs(\"Data\")\n%cd Data","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:02:08.607729Z","iopub.execute_input":"2022-10-08T16:02:08.608779Z","iopub.status.idle":"2022-10-08T16:02:08.617718Z","shell.execute_reply.started":"2022-10-08T16:02:08.608740Z","shell.execute_reply":"2022-10-08T16:02:08.616499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm \nimport shutil as sh\n\n\nindex = list(set(df.image_id))\n\nsource = '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 = '/val/'\n            else:\n                path2save = '/train/'\n            if not os.path.exists('/kaggle/working/Data/labels/'+path2save):\n                os.makedirs('/kaggle/working/Data/labels/'+path2save)\n            with open('/kaggle/working/Data/labels/'+path2save+name+\".txt\", 'w+') as f:\n                row = mini[['classes','x_center','y_center','width','hieght']].astype(float).values\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('/kaggle/working/Data/images/{}'.format(path2save)):\n                os.makedirs('/kaggle/working/Data/images/{}'.format(path2save))\n            sh.copy(\"/kaggle/input/global-wheat-detection/train/{}.jpg\".format(name),'/kaggle/working/Data/images/{}/{}.jpg'.format(path2save,name))","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:02:14.007015Z","iopub.execute_input":"2022-10-08T16:02:14.007743Z","iopub.status.idle":"2022-10-08T16:02:44.002671Z","shell.execute_reply.started":"2022-10-08T16:02:14.007683Z","shell.execute_reply":"2022-10-08T16:02:44.000985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2.3 Creating .Yaml file format for Yolo","metadata":{}},{"cell_type":"code","source":"Data_Path = \"/kaggle/working/Data\"\nwith open(f\"/kaggle/working/yolov5/Wheat.yaml\", \"w+\") as file_:\n        file_.write(\n           f\"\"\"\n           train: /{Data_Path}/images/train  \n           val: /{Data_Path}/images/val  \n           test:  # test images (optional)\n           nc: 1\n           names: [\"wheat\"]\n            \n            \"\"\"\n)","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:02:49.818017Z","iopub.execute_input":"2022-10-08T16:02:49.818368Z","iopub.status.idle":"2022-10-08T16:02:49.823926Z","shell.execute_reply.started":"2022-10-08T16:02:49.818338Z","shell.execute_reply":"2022-10-08T16:02:49.822939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3.1 Training:","metadata":{}},{"cell_type":"code","source":"%cd  /kaggle/working/yolov5","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:03:19.924434Z","iopub.execute_input":"2022-10-08T16:03:19.924863Z","iopub.status.idle":"2022-10-08T16:03:19.935074Z","shell.execute_reply.started":"2022-10-08T16:03:19.924831Z","shell.execute_reply":"2022-10-08T16:03:19.932549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python train.py --batch 32 --epochs 25 --data  /kaggle/working/yolov5/Wheat.yaml --cfg /kaggle/working/yolov5/models/yolov5s.yaml --name global-wheat-detection","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:03:34.400385Z","iopub.execute_input":"2022-10-08T16:03:34.401362Z","iopub.status.idle":"2022-10-08T16:10:04.502690Z","shell.execute_reply.started":"2022-10-08T16:03:34.401309Z","shell.execute_reply":"2022-10-08T16:10:04.501440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3.2 Make Predictions & visualize:","metadata":{}},{"cell_type":"code","source":"!python detect.py --weights /kaggle/working/yolov5/runs/train/global-wheat-detection15/weights/best.pt  --source ../input/global-wheat-detection/test","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_files = []\nfor (dirpath, dirnames, filenames) in os.walk(\"/kaggle/working/yolov5/runs/detect/exp\"):\n    predicted_files.extend(filenames)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\npath=\"/content/drive/MyDrive/YoloV5/Data/global-wheat-detection/test\"\n\n\nindex=random.choice(predicted_files)\n\noriginal_path=str(path)+\"/\"+str(index)\nimg=cv2.imread(original_path)\nimg=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)\n\n\ndetected_path=str(dirpath)+\"/\"+str(index)\ndetected_img=cv2.imread(detected_path)\ndetected_img=cv2.cvtColor(detected_img,cv2.COLOR_BGR2RGB)\n\n\n#plotting:\nplt.figure(figsize=(25,17))\nplt.subplot(1,2,1)\nplt.imshow(img)\nplt.title(\"Original\")\nplt.axis(\"off\")\nplt.subplot(1,2,2)\nplt.imshow(detected_img)\nplt.title(\"predicted\")\nplt.axis(\"off\")","metadata":{},"execution_count":null,"outputs":[]}]}