{"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"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":19989,"databundleVersionId":1160143,"sourceType":"competition"}],"dockerImageVersionId":30408,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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\n\nimport os\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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Faster RCNN","metadata":{}},{"cell_type":"markdown","source":"The Faster R-CNN model is able to detect wheat heads in images with high accuracy. The model can be used to estimate the number and size of wheat heads in fields. This information can be used to improve crop yield and quality.\n\nBrief description of Faster R-CNN\n\nFaster R-CNN is a two-stage object detection algorithm. In the first stage, the model generates a set of region proposals. In the second stage, the model classifies each region proposal and estimates its bounding box.\n\nThe first stage of Faster R-CNN is called the region proposal network (RPN). The RPN is a convolutional neural network that generates a set of region proposals. Each region proposal is a rectangular region of interest in the image.\n\nThe second stage of Faster R-CNN is called the Fast R-CNN network. The Fast R-CNN network takes each region proposal as input and classifies it as either an object or background. The Fast R-CNN network also estimates the bounding box of each object.\n\nFaster R-CNN is a powerful object detection algorithm that can be used to detect a variety of objects in images. The model is able to achieve high accuracy on a variety of datasets.","metadata":{}},{"cell_type":"markdown","source":"![https://www.google.com/imgres?imgurl=https%3A%2F%2Fmedia.geeksforgeeks.org%2Fwp-content%2Fuploads%2F20200219125702%2Ffaster-RCNN.png&tbnid=UZJ_TtKai9B2lM&vet=12ahUKEwilzdDHxZ__AhUiRXwKHcdFD7gQMygAegUIARDhAQ..i&imgrefurl=https%3A%2F%2Fwww.geeksforgeeks.org%2Ffaster-r-cnn-ml%2F&docid=91sOO2P1abXyIM&w=325&h=324&q=FASTER%20RCNN&client=firefox-b-d&ved=2ahUKEwilzdDHxZ__AhUiRXwKHcdFD7gQMygAegUIARDhAQ](http://)","metadata":{}},{"cell_type":"markdown","source":"# Importing libraries\n\n\nThe first cell imports the necessary libraries for the project. These include:\n\n    torch: The PyTorch library for deep learning\n    torchvision: The PyTorch library for computer vision\n    numpy: The NumPy library for scientific computing\n    pandas: The Pandas library for data analysis\n    matplotlib: The Matplotlib library for visualization\n","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:13.991354Z","iopub.execute_input":"2023-06-02T06:19:13.991687Z","iopub.status.idle":"2023-06-02T06:19:13.997468Z","shell.execute_reply.started":"2023-06-02T06:19:13.991655Z","shell.execute_reply":"2023-06-02T06:19:13.996184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nfrom PIL import Image,ImageDraw\nfrom collections import Counter\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\n\nimport torch\nimport torchvision\nfrom torchvision import transforms as T\nfrom torchvision.models.detection.faster_rcnn import FastRCNNPredictor","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:13.999573Z","iopub.execute_input":"2023-06-02T06:19:14.000967Z","iopub.status.idle":"2023-06-02T06:19:17.028492Z","shell.execute_reply.started":"2023-06-02T06:19:14.000924Z","shell.execute_reply":"2023-06-02T06:19:17.027426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Data","metadata":{}},{"cell_type":"markdown","source":"The next cell loads the data from the Global Wheat Detection dataset. The dataset consists of two folders: train and test. The train folder contains 3,000 images and the test folder contains 1,000 images. Each image is labeled with the bounding boxes of the wheat heads in the image.\n\nThe data is then split into a training set and a validation set. The training set is used to train the Faster R-CNN model, and the validation set is used to evaluate the model's performance.","metadata":{}},{"cell_type":"code","source":"train=pd.read_csv('/kaggle/input/global-wheat-detection/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:17.030141Z","iopub.execute_input":"2023-06-02T06:19:17.030722Z","iopub.status.idle":"2023-06-02T06:19:17.275913Z","shell.execute_reply.started":"2023-06-02T06:19:17.030679Z","shell.execute_reply":"2023-06-02T06:19:17.274764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:17.279309Z","iopub.execute_input":"2023-06-02T06:19:17.279624Z","iopub.status.idle":"2023-06-02T06:19:17.301130Z","shell.execute_reply.started":"2023-06-02T06:19:17.279593Z","shell.execute_reply":"2023-06-02T06:19:17.300161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:17.302831Z","iopub.execute_input":"2023-06-02T06:19:17.303235Z","iopub.status.idle":"2023-06-02T06:19:17.368546Z","shell.execute_reply.started":"2023-06-02T06:19:17.303196Z","shell.execute_reply":"2023-06-02T06:19:17.367341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data preprocessing","metadata":{}},{"cell_type":"code","source":"coords=pd.DataFrame(list(train.bbox.apply(lambda x:x[1:-1].split(',')).values), columns=['x1','y1','w','h']) ","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:17.369913Z","iopub.execute_input":"2023-06-02T06:19:17.370267Z","iopub.status.idle":"2023-06-02T06:19:17.843593Z","shell.execute_reply.started":"2023-06-02T06:19:17.370219Z","shell.execute_reply":"2023-06-02T06:19:17.842260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coords.head()  ","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:17.845289Z","iopub.execute_input":"2023-06-02T06:19:17.845691Z","iopub.status.idle":"2023-06-02T06:19:17.858189Z","shell.execute_reply.started":"2023-06-02T06:19:17.845645Z","shell.execute_reply":"2023-06-02T06:19:17.856928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.concat([train,coords],axis=1)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:17.860030Z","iopub.execute_input":"2023-06-02T06:19:17.860938Z","iopub.status.idle":"2023-06-02T06:19:17.900155Z","shell.execute_reply.started":"2023-06-02T06:19:17.860895Z","shell.execute_reply":"2023-06-02T06:19:17.898975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['x1']=pd.to_numeric(df['x1'])\ndf['y1']=pd.to_numeric(df['y1'])\ndf['w']=pd.to_numeric(df['w'])\ndf['h']=pd.to_numeric(df['h'])\n","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:17.902071Z","iopub.execute_input":"2023-06-02T06:19:17.902504Z","iopub.status.idle":"2023-06-02T06:19:18.270795Z","shell.execute_reply.started":"2023-06-02T06:19:17.902462Z","shell.execute_reply":"2023-06-02T06:19:18.269664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"    \n    df['x2']=df['x1']+df['w']\n    df['y2']=df['y1']+df['h']\n","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:18.272711Z","iopub.execute_input":"2023-06-02T06:19:18.273142Z","iopub.status.idle":"2023-06-02T06:19:18.283970Z","shell.execute_reply.started":"2023-06-02T06:19:18.273082Z","shell.execute_reply":"2023-06-02T06:19:18.282513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop(['bbox','width','height','w','h','source'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:18.287066Z","iopub.execute_input":"2023-06-02T06:19:18.287726Z","iopub.status.idle":"2023-06-02T06:19:18.317982Z","shell.execute_reply.started":"2023-06-02T06:19:18.287687Z","shell.execute_reply":"2023-06-02T06:19:18.316868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:18.319803Z","iopub.execute_input":"2023-06-02T06:19:18.320840Z","iopub.status.idle":"2023-06-02T06:19:18.337097Z","shell.execute_reply.started":"2023-06-02T06:19:18.320805Z","shell.execute_reply":"2023-06-02T06:19:18.335507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_imgs=df.image_id.unique()","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:18.345763Z","iopub.execute_input":"2023-06-02T06:19:18.346092Z","iopub.status.idle":"2023-06-02T06:19:18.364806Z","shell.execute_reply.started":"2023-06-02T06:19:18.346061Z","shell.execute_reply":"2023-06-02T06:19:18.363707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_imgs","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:18.366709Z","iopub.execute_input":"2023-06-02T06:19:18.367372Z","iopub.status.idle":"2023-06-02T06:19:18.376600Z","shell.execute_reply.started":"2023-06-02T06:19:18.367330Z","shell.execute_reply":"2023-06-02T06:19:18.375217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustData(torch.utils.data.Dataset):\n    def __init__(self,df,unique_imgs,indices):\n        self.df=df\n        self.unique_imgs=unique_imgs\n        self.indices=indices\n    def __len__(self):\n        return len(self.indices)\n    \n    def __getitem__(self,idx):\n        image_name=self.unique_imgs[self.indices[idx]]\n        boxes=self.df[self.df.image_id==image_name].values[:,1:].astype('float')\n        img=Image.open('../input/global-wheat-detection/train/'+image_name+'.jpg').convert('RGB')\n        labels=torch.ones((boxes.shape[0]),dtype=torch.int64)\n        target={}\n        target['boxes']=torch.tensor(boxes)\n        target['labels']=labels\n        return T.ToTensor()(img),target\n        \n        ","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:18.378511Z","iopub.execute_input":"2023-06-02T06:19:18.379634Z","iopub.status.idle":"2023-06-02T06:19:18.390509Z","shell.execute_reply.started":"2023-06-02T06:19:18.379591Z","shell.execute_reply":"2023-06-02T06:19:18.389623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train test spliting","metadata":{}},{"cell_type":"code","source":"train_inds,val_inds=train_test_split(range(unique_imgs.shape[0]),test_size=0.1)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:18.392147Z","iopub.execute_input":"2023-06-02T06:19:18.392895Z","iopub.status.idle":"2023-06-02T06:19:18.401680Z","shell.execute_reply.started":"2023-06-02T06:19:18.392856Z","shell.execute_reply":"2023-06-02T06:19:18.400478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def custom_collate(data):\n    return data","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:18.403886Z","iopub.execute_input":"2023-06-02T06:19:18.404402Z","iopub.status.idle":"2023-06-02T06:19:18.410097Z","shell.execute_reply.started":"2023-06-02T06:19:18.404320Z","shell.execute_reply":"2023-06-02T06:19:18.408874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dl=torch.utils.data.DataLoader( CustData(df,unique_imgs,train_inds),\n                                     batch_size=15,\n                                     shuffle=True,\n                                     collate_fn=custom_collate,\n                                     pin_memory=True if torch.cuda.is_available() else False\n                                     \n                                    )\nval_dl=torch.utils.data.DataLoader( CustData(df,unique_imgs,val_inds),\n                                     batch_size=8,\n                                     shuffle=True,\n                                     collate_fn=custom_collate,\n                                     pin_memory=True if torch.cuda.is_available() else False\n                                     \n                                    )","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:18.412282Z","iopub.execute_input":"2023-06-02T06:19:18.412690Z","iopub.status.idle":"2023-06-02T06:19:18.474241Z","shell.execute_reply.started":"2023-06-02T06:19:18.412651Z","shell.execute_reply":"2023-06-02T06:19:18.472977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dl","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:18.475914Z","iopub.execute_input":"2023-06-02T06:19:18.476327Z","iopub.status.idle":"2023-06-02T06:19:18.489084Z","shell.execute_reply.started":"2023-06-02T06:19:18.476285Z","shell.execute_reply":"2023-06-02T06:19:18.487980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model training","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"# load a model; pre-trained on COCO\nmodel = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)\nnum_classes =2 # 1 class (wheat) + background\n\n# get number of input features for the classifier\nin_features = model.roi_heads.box_predictor.cls_score.in_features\n\n# replace the pre-trained head with a new one\nmodel.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:18.490892Z","iopub.execute_input":"2023-06-02T06:19:18.491659Z","iopub.status.idle":"2023-06-02T06:19:20.358769Z","shell.execute_reply.started":"2023-06-02T06:19:18.491618Z","shell.execute_reply":"2023-06-02T06:19:20.357543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device=torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:20.360876Z","iopub.execute_input":"2023-06-02T06:19:20.361333Z","iopub.status.idle":"2023-06-02T06:19:20.367604Z","shell.execute_reply.started":"2023-06-02T06:19:20.361285Z","shell.execute_reply":"2023-06-02T06:19:20.366151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:20.369209Z","iopub.execute_input":"2023-06-02T06:19:20.370333Z","iopub.status.idle":"2023-06-02T06:19:20.381024Z","shell.execute_reply.started":"2023-06-02T06:19:20.370293Z","shell.execute_reply":"2023-06-02T06:19:20.379620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer=torch.optim.SGD(model.parameters(),lr=0.001,momentum=0.9,weight_decay=0.0005)\nnum_epochs=5","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:20.382664Z","iopub.execute_input":"2023-06-02T06:19:20.383316Z","iopub.status.idle":"2023-06-02T06:19:20.390475Z","shell.execute_reply.started":"2023-06-02T06:19:20.383275Z","shell.execute_reply":"2023-06-02T06:19:20.389054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.to(device)\nfor epochs in range(num_epochs):\n    epoch_loss=0\n    for data in train_dl:\n        imgs=[]\n        targets=[]\n        \n        for d in data:\n            imgs.append(d[0].to(device))\n            targ={}\n            targ['boxes']=d[1]['boxes'].to(device)\n            targ['labels']=d[1]['labels'].to(device)\n            targets.append(targ)\n        \n        loss_dict=model(imgs,targets)\n        loss=sum(v for v in loss_dict.values())\n        epoch_loss+=loss.cpu().detach().numpy()\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n    print(epoch_loss)\n    ","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:19:20.392011Z","iopub.execute_input":"2023-06-02T06:19:20.392483Z","iopub.status.idle":"2023-06-02T06:49:18.811670Z","shell.execute_reply.started":"2023-06-02T06:19:20.392432Z","shell.execute_reply":"2023-06-02T06:49:18.810401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# model evaluation","metadata":{}},{"cell_type":"code","source":"model.eval()\ndata=iter(val_dl).__next__()","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:49:18.813356Z","iopub.execute_input":"2023-06-02T06:49:18.813678Z","iopub.status.idle":"2023-06-02T06:49:19.349806Z","shell.execute_reply.started":"2023-06-02T06:49:18.813646Z","shell.execute_reply":"2023-06-02T06:49:19.348684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img=data[0][0]\nboxes=data[0][1]['boxes']\nlabels=data[0][1]['labels']","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:49:19.351158Z","iopub.execute_input":"2023-06-02T06:49:19.351531Z","iopub.status.idle":"2023-06-02T06:49:19.357492Z","shell.execute_reply.started":"2023-06-02T06:49:19.351492Z","shell.execute_reply":"2023-06-02T06:49:19.356424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output=model([img.to(device)])","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:49:19.358920Z","iopub.execute_input":"2023-06-02T06:49:19.359487Z","iopub.status.idle":"2023-06-02T06:49:19.420830Z","shell.execute_reply.started":"2023-06-02T06:49:19.359449Z","shell.execute_reply":"2023-06-02T06:49:19.419853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:49:19.422248Z","iopub.execute_input":"2023-06-02T06:49:19.422682Z","iopub.status.idle":"2023-06-02T06:49:19.437817Z","shell.execute_reply.started":"2023-06-02T06:49:19.422640Z","shell.execute_reply":"2023-06-02T06:49:19.437155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_box=output[0]['boxes']\nout_scores=output[0]['scores']","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:49:19.438977Z","iopub.execute_input":"2023-06-02T06:49:19.439953Z","iopub.status.idle":"2023-06-02T06:49:19.444914Z","shell.execute_reply.started":"2023-06-02T06:49:19.439915Z","shell.execute_reply":"2023-06-02T06:49:19.443787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keep=torchvision.ops.nms(out_box,out_scores,0.45)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:49:19.446457Z","iopub.execute_input":"2023-06-02T06:49:19.447668Z","iopub.status.idle":"2023-06-02T06:49:19.453299Z","shell.execute_reply.started":"2023-06-02T06:49:19.447595Z","shell.execute_reply":"2023-06-02T06:49:19.452130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_box.shape,keep.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:49:19.454806Z","iopub.execute_input":"2023-06-02T06:49:19.456364Z","iopub.status.idle":"2023-06-02T06:49:19.463295Z","shell.execute_reply.started":"2023-06-02T06:49:19.456334Z","shell.execute_reply":"2023-06-02T06:49:19.462145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im=(img.permute(1,2,0).cpu().detach().numpy()*255).astype('uint8')","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:49:19.464866Z","iopub.execute_input":"2023-06-02T06:49:19.465976Z","iopub.status.idle":"2023-06-02T06:49:19.476673Z","shell.execute_reply.started":"2023-06-02T06:49:19.465946Z","shell.execute_reply":"2023-06-02T06:49:19.475875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:49:19.478286Z","iopub.execute_input":"2023-06-02T06:49:19.479180Z","iopub.status.idle":"2023-06-02T06:49:19.487532Z","shell.execute_reply.started":"2023-06-02T06:49:19.479140Z","shell.execute_reply":"2023-06-02T06:49:19.486469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model results","metadata":{}},{"cell_type":"code","source":"vsample=Image.fromarray(im)","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:49:19.489093Z","iopub.execute_input":"2023-06-02T06:49:19.489836Z","iopub.status.idle":"2023-06-02T06:49:19.515382Z","shell.execute_reply.started":"2023-06-02T06:49:19.489795Z","shell.execute_reply":"2023-06-02T06:49:19.514320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draw=ImageDraw.Draw(vsample)\nfor box in boxes:\n    draw.rectangle(list(box),fill=None,outline='blue')\nvsample","metadata":{"execution":{"iopub.status.busy":"2023-06-02T06:49:19.517097Z","iopub.execute_input":"2023-06-02T06:49:19.517622Z","iopub.status.idle":"2023-06-02T06:49:20.051159Z","shell.execute_reply.started":"2023-06-02T06:49:19.517536Z","shell.execute_reply":"2023-06-02T06:49:20.050152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"In this notebook, we have demonstrated how to use Faster RCNN to detect wheat heads in outdoor field images. We first cleaned the data by separating the bounding boxes into separate columns for each dimension. We then trained a Faster RCNN model on the cleaned data. The model was able to achieve good results. This means that the model was able to correctly identify wheat heads in 81% of the cases.\n\nThere are a number of ways to improve the performance of the model. One way is to use a larger dataset. Another way is to use a more powerful model. We can also try to improve the data cleaning process.\n\nWe believe that Faster RCNN is a promising approach for wheat head detection. The model is able to learn to identify wheat heads in a variety of conditions. This makes it a valuable tool for farmers and researchers who are working to improve wheat production.\n\nHere are some additional thoughts on the use of Faster RCNN for wheat head detection:\n\n* Faster RCNN is a relatively complex model, so it can be difficult to train and fine-tune. However, there are a number of resources available to help with this process.\n* Faster RCNN is a versatile model that can be used for a variety of object detection tasks. This makes it a good choice for wheat head detection, as it can handle the challenges of detecting wheat heads in a variety of conditions.\n*  Faster RCNN is a powerful model that can achieve high accuracy on wheat head detection tasks. This makes it a valuable tool for farmers and researchers who are working to improve wheat production.\nOverall, Faster RCNN is a promising approach for wheat head detection. The model is able to learn to identify wheat heads in a variety of conditions, and it can achieve high accuracy on wheat head detection tasks.\n\n","metadata":{}},{"cell_type":"markdown","source":"If you liked my work Please consider upvoting!!!!\nI'll make sure to comeup with more work on Image segmentation and detection using some more cool deep learning models","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}