{"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":"### 1.1 Download Model and requirments:","metadata":{"execution":{"iopub.status.busy":"2023-04-16T07:25:36.802730Z","iopub.execute_input":"2023-04-16T07:25:36.803334Z","iopub.status.idle":"2023-04-16T07:25:36.834772Z","shell.execute_reply.started":"2023-04-16T07:25:36.803218Z","shell.execute_reply":"2023-04-16T07:25:36.833672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/ultralytics/yolov5","metadata":{"execution":{"iopub.status.busy":"2023-04-16T07:25:36.836867Z","iopub.execute_input":"2023-04-16T07:25:36.842503Z","iopub.status.idle":"2023-04-16T07:25:40.470955Z","shell.execute_reply.started":"2023-04-16T07:25:36.842461Z","shell.execute_reply":"2023-04-16T07:25:40.469817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ./yolov5\n!pip install -r requirements.txt","metadata":{"execution":{"iopub.status.busy":"2023-04-16T07:25:40.475053Z","iopub.execute_input":"2023-04-16T07:25:40.475413Z","iopub.status.idle":"2023-04-16T07:26:06.443686Z","shell.execute_reply.started":"2023-04-16T07:25:40.475384Z","shell.execute_reply":"2023-04-16T07:26:06.442447Z"},"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":"2023-04-16T07:26:06.447196Z","iopub.execute_input":"2023-04-16T07:26:06.447608Z","iopub.status.idle":"2023-04-16T07:26:08.636754Z","shell.execute_reply.started":"2023-04-16T07:26:06.447565Z","shell.execute_reply":"2023-04-16T07:26:08.635743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = torch.hub.load('ultralytics/yolov5', 'yolov5s')\nmodel","metadata":{"execution":{"iopub.status.busy":"2023-04-16T07:26:08.638233Z","iopub.execute_input":"2023-04-16T07:26:08.638847Z","iopub.status.idle":"2023-04-16T07:26:21.940967Z","shell.execute_reply.started":"2023-04-16T07:26:08.638810Z","shell.execute_reply":"2023-04-16T07:26:21.939844Z"},"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":"2023-04-16T07:26:21.943423Z","iopub.execute_input":"2023-04-16T07:26:21.944152Z","iopub.status.idle":"2023-04-16T07:26:27.088476Z","shell.execute_reply.started":"2023-04-16T07:26:21.944114Z","shell.execute_reply":"2023-04-16T07:26:27.087484Z"},"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":"2023-04-16T07:26:27.090137Z","iopub.execute_input":"2023-04-16T07:26:27.090504Z","iopub.status.idle":"2023-04-16T07:26:27.307436Z","shell.execute_reply.started":"2023-04-16T07:26:27.090468Z","shell.execute_reply":"2023-04-16T07:26:27.305978Z"},"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":"2023-04-16T07:26:27.314584Z","iopub.execute_input":"2023-04-16T07:26:27.319070Z","iopub.status.idle":"2023-04-16T07:26:27.942992Z","shell.execute_reply.started":"2023-04-16T07:26:27.319026Z","shell.execute_reply":"2023-04-16T07:26:27.941770Z"},"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":"2023-04-16T07:26:27.944131Z","iopub.execute_input":"2023-04-16T07:26:27.944447Z","iopub.status.idle":"2023-04-16T07:26:28.204691Z","shell.execute_reply.started":"2023-04-16T07:26:27.944419Z","shell.execute_reply":"2023-04-16T07:26:28.203704Z"},"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":"2023-04-16T07:26:28.208037Z","iopub.execute_input":"2023-04-16T07:26:28.208936Z","iopub.status.idle":"2023-04-16T07:26:28.625514Z","shell.execute_reply.started":"2023-04-16T07:26:28.208898Z","shell.execute_reply":"2023-04-16T07:26:28.624389Z"},"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":"2023-04-16T07:26:28.626736Z","iopub.execute_input":"2023-04-16T07:26:28.627299Z","iopub.status.idle":"2023-04-16T07:26:28.637985Z","shell.execute_reply.started":"2023-04-16T07:26:28.627263Z","shell.execute_reply":"2023-04-16T07:26:28.636911Z"},"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":"2023-04-16T07:26:28.639920Z","iopub.execute_input":"2023-04-16T07:26:28.640473Z","iopub.status.idle":"2023-04-16T07:26:56.808106Z","shell.execute_reply.started":"2023-04-16T07:26:28.640436Z","shell.execute_reply":"2023-04-16T07:26:56.806483Z"},"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":"2023-04-16T07:26:56.809729Z","iopub.execute_input":"2023-04-16T07:26:56.810115Z","iopub.status.idle":"2023-04-16T07:26:56.816261Z","shell.execute_reply.started":"2023-04-16T07:26:56.810079Z","shell.execute_reply":"2023-04-16T07:26:56.814954Z"},"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":"2023-04-16T07:26:56.818138Z","iopub.execute_input":"2023-04-16T07:26:56.818570Z","iopub.status.idle":"2023-04-16T07:26:56.828454Z","shell.execute_reply.started":"2023-04-16T07:26:56.818515Z","shell.execute_reply":"2023-04-16T07:26:56.827118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python train.py --batch 16 --epochs 50 --data  /kaggle/working/yolov5/Wheat.yaml --cfg /kaggle/working/yolov5/models/yolov5s.yaml --name global-wheat-detection","metadata":{"execution":{"iopub.status.busy":"2023-04-16T07:26:56.830330Z","iopub.execute_input":"2023-04-16T07:26:56.830732Z","iopub.status.idle":"2023-04-16T11:20:01.829083Z","shell.execute_reply.started":"2023-04-16T07:26:56.830649Z","shell.execute_reply":"2023-04-16T11:20:01.827708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","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!python train.py --batch 32 --epochs 25 --data  /kaggle/working/yolov5/Wheat.yaml --cfg /kaggle/working/yolov5/models/yolov5s.yaml --name global-wheat-detection### 3.2 Make Predictions & visualize:","metadata":{}},{"cell_type":"code","source":"!python detect.py --weights /kaggle/working/yolov5/runs/train/global-wheat-detection/weights/best.pt --img 640 --conf 0.4 --iou-thres 0.45 --source /kaggle/input/global-wheat-detection/test --save-txt --save-conf ","metadata":{"execution":{"iopub.status.busy":"2023-04-16T11:24:08.484994Z","iopub.execute_input":"2023-04-16T11:24:08.485413Z","iopub.status.idle":"2023-04-16T11:24:18.060900Z","shell.execute_reply.started":"2023-04-16T11:24:08.485373Z","shell.execute_reply":"2023-04-16T11:24:18.059712Z"},"trusted":true},"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":{"iopub.status.busy":"2023-04-16T11:24:20.677205Z","iopub.execute_input":"2023-04-16T11:24:20.677837Z","iopub.status.idle":"2023-04-16T11:24:20.684339Z","shell.execute_reply.started":"2023-04-16T11:24:20.677772Z","shell.execute_reply":"2023-04-16T11:24:20.683483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls -R runs/detect/exp","metadata":{"execution":{"iopub.status.busy":"2023-04-16T11:24:24.986574Z","iopub.execute_input":"2023-04-16T11:24:24.986985Z","iopub.status.idle":"2023-04-16T11:24:26.017178Z","shell.execute_reply.started":"2023-04-16T11:24:24.986949Z","shell.execute_reply":"2023-04-16T11:24:26.015854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt\n\nimport random\n\npredicted_files = []\nfor (dirpath, dirnames, filenames) in os.walk(\"//kaggle/working/yolov5/runs/detect/exp\"):\n    predicted_files.extend([f for f in filenames if f.endswith('.jpg')])\n    \npath=\"/kaggle/input/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\ndirpath=\"//kaggle/working/yolov5/runs/detect/exp\"\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\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":{"iopub.status.busy":"2023-04-16T11:24:28.355924Z","iopub.execute_input":"2023-04-16T11:24:28.356314Z","iopub.status.idle":"2023-04-16T11:24:29.625718Z","shell.execute_reply.started":"2023-04-16T11:24:28.356281Z","shell.execute_reply":"2023-04-16T11:24:29.624486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport matplotlib.pyplot as plt\n\ndirpath=\"/kaggle/working/yolov5/runs/detect/exp\"\n\n# อ่านชื่อไฟล์ทั้งหมดในไดเรกทอรี่\nfiles = os.listdir(dirpath)\n# กรองไฟล์ที่เป็นไฟล์รูปภาพเท่านั้น\nimg_files = [f for f in files if f.endswith('.jpg')]\n\n# วนลูปแสดงภาพทั้งหมด\nfor i, img_file in enumerate(img_files):\n    # อ่านภาพและแปลงสีจาก BGR เป็น RGB\n    img = cv2.imread(os.path.join(dirpath, img_file))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\n    # แสดงภาพ\n    plt.figure(figsize=(10,10))\n    plt.imshow(img)\n    plt.title(\"Image {}\".format(i+1))\n    plt.axis(\"off\")\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-16T11:24:37.685098Z","iopub.execute_input":"2023-04-16T11:24:37.685464Z","iopub.status.idle":"2023-04-16T11:24:42.470595Z","shell.execute_reply.started":"2023-04-16T11:24:37.685434Z","shell.execute_reply":"2023-04-16T11:24:42.469770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os, re\nimport glob\n\n\n\nwidth = 640\nheight = 640\nresults = []\n\nresult_dir = '/kaggle/working/yolov5/runs/detect/exp/labels'\nfor filename in glob.glob(os.path.join(result_dir,'*.txt')):\n    prefix = filename.split(\"/\")[-1].split(\".\")[0]\n    predict_str = \"\"\n    with open(filename, 'r') as fp:\n        for cnt, line in enumerate(fp):\n            entries = np.array(line.strip('\\n').split()).astype(np.float)\n            bbox = np.round([(entries[1]-entries[3]/2.0)*width,(entries[2]-entries[4]/2.0)*height,entries[3]*width,entries[4]*height],3).tolist()\n            conf = entries[-1]\n            predict_str = predict_str + str(conf) + \" \" + \" \".join(map(str,bbox)) + \" \" \n        results.append({'image_id':prefix, 'PredictionString':predict_str.rstrip()}) \n        \nresultFrame = pd.DataFrame(results)","metadata":{"execution":{"iopub.status.busy":"2023-04-16T11:22:24.711108Z","iopub.execute_input":"2023-04-16T11:22:24.711505Z","iopub.status.idle":"2023-04-16T11:22:24.737950Z","shell.execute_reply.started":"2023-04-16T11:22:24.711474Z","shell.execute_reply":"2023-04-16T11:22:24.737042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = []\nfor filename in glob.glob('/kaggle/input/global-wheat-detection/test/*.jpg'):\n    \n    prefix = filename.split(\"/\")[-1].split(\".\")[0]\n    if resultFrame['image_id'].str.contains(prefix).any():\n        pass\n    else:\n        temp.append({'image_id':prefix, 'PredictionString':\"\"})\n        \ndf = pd.DataFrame(temp)\nresultFrame = resultFrame.append(df)","metadata":{"execution":{"iopub.status.busy":"2023-04-16T11:22:28.412419Z","iopub.execute_input":"2023-04-16T11:22:28.412800Z","iopub.status.idle":"2023-04-16T11:22:28.426535Z","shell.execute_reply.started":"2023-04-16T11:22:28.412753Z","shell.execute_reply":"2023-04-16T11:22:28.425602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_dir = '/kaggle/working'\nresultFrame.to_csv(submission_dir + \"/submission.csv\", index = False)\nresultFrame.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-04-16T11:22:33.470689Z","iopub.execute_input":"2023-04-16T11:22:33.471629Z","iopub.status.idle":"2023-04-16T11:22:33.488715Z","shell.execute_reply.started":"2023-04-16T11:22:33.471592Z","shell.execute_reply":"2023-04-16T11:22:33.487691Z"},"trusted":true},"execution_count":null,"outputs":[]}]}