{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":19989,"databundleVersionId":1160143,"sourceType":"competition"}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install torch torchvision\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-15T20:38:45.526719Z","iopub.execute_input":"2024-05-15T20:38:45.527597Z","iopub.status.idle":"2024-05-15T20:39:00.951974Z","shell.execute_reply.started":"2024-05-15T20:38:45.527561Z","shell.execute_reply":"2024-05-15T20:39:00.950991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# important library:","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport random\nfrom PIL import Image , ImageDraw \nfrom collections import Counter\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nimport torch\nimport torchvision\nfrom torchvision import transforms as T\n\nfrom torchvision.models.detection.faster_rcnn import FastRCNNPredictor\n\n","metadata":{"execution":{"iopub.status.busy":"2024-05-15T20:39:00.954197Z","iopub.execute_input":"2024-05-15T20:39:00.954546Z","iopub.status.idle":"2024-05-15T20:39:08.621201Z","shell.execute_reply.started":"2024-05-15T20:39:00.954509Z","shell.execute_reply":"2024-05-15T20:39:08.620228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# load train data:","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/global-wheat-detection/train.csv\")\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-15T20:39:08.622541Z","iopub.execute_input":"2024-05-15T20:39:08.623053Z","iopub.status.idle":"2024-05-15T20:39:08.985275Z","shell.execute_reply.started":"2024-05-15T20:39:08.622991Z","shell.execute_reply":"2024-05-15T20:39:08.984214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- split the bbox column to x1,y1,w,h","metadata":{}},{"cell_type":"code","source":"coord = pd.DataFrame(list(train.bbox.apply(lambda x : x[1:-1].split(\",\")).values),columns=[\"x1\",\"y1\",\"w\",\"h\"])","metadata":{"execution":{"iopub.status.busy":"2024-05-15T20:39:08.987941Z","iopub.execute_input":"2024-05-15T20:39:08.988337Z","iopub.status.idle":"2024-05-15T20:39:09.406839Z","shell.execute_reply.started":"2024-05-15T20:39:08.988306Z","shell.execute_reply":"2024-05-15T20:39:09.405894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.concat([train,coord],axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T20:39:09.408114Z","iopub.execute_input":"2024-05-15T20:39:09.408436Z","iopub.status.idle":"2024-05-15T20:39:09.429893Z","shell.execute_reply.started":"2024-05-15T20:39:09.408407Z","shell.execute_reply":"2024-05-15T20:39:09.428928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- make the values of the columns numeric values:","metadata":{}},{"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":"2024-05-15T20:39:09.431206Z","iopub.execute_input":"2024-05-15T20:39:09.431607Z","iopub.status.idle":"2024-05-15T20:39:09.865482Z","shell.execute_reply.started":"2024-05-15T20:39:09.431572Z","shell.execute_reply":"2024-05-15T20:39:09.864292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- calcualte the y2,x2 :","metadata":{}},{"cell_type":"code","source":"df['x2']=df['x1']+df['w']\ndf['y2']=df['y1']+df['h']","metadata":{"execution":{"iopub.status.busy":"2024-05-15T20:39:09.866946Z","iopub.execute_input":"2024-05-15T20:39:09.867399Z","iopub.status.idle":"2024-05-15T20:39:09.877698Z","shell.execute_reply.started":"2024-05-15T20:39:09.867346Z","shell.execute_reply":"2024-05-15T20:39:09.876541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2024-05-15T20:39:09.878907Z","iopub.execute_input":"2024-05-15T20:39:09.879232Z","iopub.status.idle":"2024-05-15T20:39:09.910883Z","shell.execute_reply.started":"2024-05-15T20:39:09.879197Z","shell.execute_reply":"2024-05-15T20:39:09.909819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- Drop unneeded column:","metadata":{}},{"cell_type":"code","source":"df.drop(['bbox','width','height','w','h','source'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T20:39:09.911981Z","iopub.execute_input":"2024-05-15T20:39:09.912359Z","iopub.status.idle":"2024-05-15T20:39:09.930070Z","shell.execute_reply.started":"2024-05-15T20:39:09.912313Z","shell.execute_reply":"2024-05-15T20:39:09.929162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-15T20:39:09.933751Z","iopub.execute_input":"2024-05-15T20:39:09.934075Z","iopub.status.idle":"2024-05-15T20:39:09.949036Z","shell.execute_reply.started":"2024-05-15T20:39:09.934042Z","shell.execute_reply":"2024-05-15T20:39:09.947996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- Take the unique ids of the images:","metadata":{}},{"cell_type":"code","source":"unique_imgs=df.image_id.unique()\nunique_imgs","metadata":{"execution":{"iopub.status.busy":"2024-05-15T20:39:09.950593Z","iopub.execute_input":"2024-05-15T20:39:09.951086Z","iopub.status.idle":"2024-05-15T20:39:09.975908Z","shell.execute_reply.started":"2024-05-15T20:39:09.951007Z","shell.execute_reply":"2024-05-15T20:39:09.974896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- Make custom class for preparing the data:","metadata":{}},{"cell_type":"code","source":"class custDat(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    \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[\"label\"] = labels\n        return T.ToTensor()(img), target\n","metadata":{"execution":{"iopub.status.busy":"2024-05-15T20:39:09.977143Z","iopub.execute_input":"2024-05-15T20:39:09.977469Z","iopub.status.idle":"2024-05-15T20:39:09.986806Z","shell.execute_reply.started":"2024-05-15T20:39:09.977442Z","shell.execute_reply":"2024-05-15T20:39:09.985849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# spit the data int train and validation:","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":"2024-05-15T20:39:09.987873Z","iopub.execute_input":"2024-05-15T20:39:09.988171Z","iopub.status.idle":"2024-05-15T20:39:10.000648Z","shell.execute_reply.started":"2024-05-15T20:39:09.988145Z","shell.execute_reply":"2024-05-15T20:39:09.999648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def custom_collate(data):\n    return data","metadata":{"execution":{"iopub.status.busy":"2024-05-15T20:39:10.002002Z","iopub.execute_input":"2024-05-15T20:39:10.002434Z","iopub.status.idle":"2024-05-15T20:39:10.010696Z","shell.execute_reply.started":"2024-05-15T20:39:10.002406Z","shell.execute_reply":"2024-05-15T20:39:10.009756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Call the data loader to prepare it to the model:","metadata":{}},{"cell_type":"code","source":"train_dl = torch.utils.data.DataLoader(custDat(df,unique_imgs,train_inds),batch_size=16,shuffle=True,collate_fn=custom_collate,pin_memory=True if torch.cuda.is_available() else False)\nval_dl = torch.utils.data.DataLoader(custDat(df,unique_imgs,val_inds),batch_size=8,shuffle=True,collate_fn=custom_collate,pin_memory=True if torch.cuda.is_available() else False)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-15T20:39:10.011940Z","iopub.execute_input":"2024-05-15T20:39:10.012250Z","iopub.status.idle":"2024-05-15T20:39:10.051719Z","shell.execute_reply.started":"2024-05-15T20:39:10.012223Z","shell.execute_reply":"2024-05-15T20:39:10.050560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modeling:","metadata":{}},{"cell_type":"code","source":"model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)\nnum_classes=2\nin_features=model.roi_heads.box_predictor.cls_score.in_features\nmodel.roi_heads.box_predictor =  FastRCNNPredictor(in_features,num_classes)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T20:39:10.053259Z","iopub.execute_input":"2024-05-15T20:39:10.054224Z","iopub.status.idle":"2024-05-15T20:39:12.326703Z","shell.execute_reply.started":"2024-05-15T20:39:10.054187Z","shell.execute_reply":"2024-05-15T20:39:12.325600Z"},"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":"2024-05-15T20:39:12.328354Z","iopub.execute_input":"2024-05-15T20:39:12.328788Z","iopub.status.idle":"2024-05-15T20:39:12.334305Z","shell.execute_reply.started":"2024-05-15T20:39:12.328747Z","shell.execute_reply":"2024-05-15T20:39:12.333203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device","metadata":{"execution":{"iopub.status.busy":"2024-05-15T20:39:12.335705Z","iopub.execute_input":"2024-05-15T20:39:12.336121Z","iopub.status.idle":"2024-05-15T20:39:12.347626Z","shell.execute_reply.started":"2024-05-15T20:39:12.336082Z","shell.execute_reply":"2024-05-15T20:39:12.346582Z"},"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.005)\nnum_epochs=5","metadata":{"execution":{"iopub.status.busy":"2024-05-15T20:39:12.348888Z","iopub.execute_input":"2024-05-15T20:39:12.349298Z","iopub.status.idle":"2024-05-15T20:39:12.357419Z","shell.execute_reply.started":"2024-05-15T20:39:12.349270Z","shell.execute_reply":"2024-05-15T20:39:12.356375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"_ train the model with 5 epochs and prin the loss:","metadata":{}},{"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        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]['label'].to(device)\n            targets.append(targ)\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            \n            ","metadata":{"execution":{"iopub.status.busy":"2024-05-15T20:39:12.358789Z","iopub.execute_input":"2024-05-15T20:39:12.359191Z","iopub.status.idle":"2024-05-15T21:04:07.289968Z","shell.execute_reply.started":"2024-05-15T20:39:12.359155Z","shell.execute_reply":"2024-05-15T21:04:07.288928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- Evaluate the model","metadata":{}},{"cell_type":"code","source":"model.eval()\ndata = iter(val_dl).__next__()","metadata":{"execution":{"iopub.status.busy":"2024-05-15T21:04:07.291666Z","iopub.execute_input":"2024-05-15T21:04:07.291974Z","iopub.status.idle":"2024-05-15T21:04:07.730855Z","shell.execute_reply.started":"2024-05-15T21:04:07.291946Z","shell.execute_reply":"2024-05-15T21:04:07.729969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img= data[0][0]\nboxes=data[0][1]['boxes']\nlabels=data[0][1]['label']","metadata":{"execution":{"iopub.status.busy":"2024-05-15T21:04:07.732057Z","iopub.execute_input":"2024-05-15T21:04:07.732432Z","iopub.status.idle":"2024-05-15T21:04:07.737307Z","shell.execute_reply.started":"2024-05-15T21:04:07.732404Z","shell.execute_reply":"2024-05-15T21:04:07.736331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = model([img.to(device)])\n","metadata":{"execution":{"iopub.status.busy":"2024-05-15T21:04:07.738498Z","iopub.execute_input":"2024-05-15T21:04:07.738777Z","iopub.status.idle":"2024-05-15T21:04:07.971357Z","shell.execute_reply.started":"2024-05-15T21:04:07.738753Z","shell.execute_reply":"2024-05-15T21:04:07.970539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- output os the evaluation:","metadata":{}},{"cell_type":"code","source":"output","metadata":{"execution":{"iopub.status.busy":"2024-05-15T21:04:07.972537Z","iopub.execute_input":"2024-05-15T21:04:07.972857Z","iopub.status.idle":"2024-05-15T21:04:08.014980Z","shell.execute_reply.started":"2024-05-15T21:04:07.972829Z","shell.execute_reply":"2024-05-15T21:04:08.013999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_bbox=output[0]['boxes']\nout_scores=output[0]['scores']","metadata":{"execution":{"iopub.status.busy":"2024-05-15T21:04:08.016290Z","iopub.execute_input":"2024-05-15T21:04:08.016599Z","iopub.status.idle":"2024-05-15T21:04:08.022317Z","shell.execute_reply.started":"2024-05-15T21:04:08.016572Z","shell.execute_reply":"2024-05-15T21:04:08.021241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keep = torchvision.ops.nms(out_bbox,out_scores,0.45)","metadata":{"execution":{"iopub.status.busy":"2024-05-15T21:04:08.023784Z","iopub.execute_input":"2024-05-15T21:04:08.024179Z","iopub.status.idle":"2024-05-15T21:04:08.033821Z","shell.execute_reply.started":"2024-05-15T21:04:08.024136Z","shell.execute_reply":"2024-05-15T21:04:08.032674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_bbox.shape , keep.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-15T21:04:08.035104Z","iopub.execute_input":"2024-05-15T21:04:08.035423Z","iopub.status.idle":"2024-05-15T21:04:08.043718Z","shell.execute_reply.started":"2024-05-15T21:04:08.035389Z","shell.execute_reply":"2024-05-15T21:04:08.042704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- prediction detection:","metadata":{}},{"cell_type":"code","source":"im = (img.permute(1,2,0).cpu().detach().numpy()*255).astype('uint8')\nim","metadata":{"execution":{"iopub.status.busy":"2024-05-15T21:04:08.049288Z","iopub.execute_input":"2024-05-15T21:04:08.049573Z","iopub.status.idle":"2024-05-15T21:04:08.062432Z","shell.execute_reply.started":"2024-05-15T21:04:08.049549Z","shell.execute_reply":"2024-05-15T21:04:08.061223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vsample = Image.fromarray(im)\ndraw = ImageDraw.Draw(vsample)\nfor box in boxes :\n    draw.rectangle(list(box),fill=None,outline='red')\nvsample","metadata":{"execution":{"iopub.status.busy":"2024-05-15T21:04:08.063682Z","iopub.execute_input":"2024-05-15T21:04:08.064106Z","iopub.status.idle":"2024-05-15T21:04:08.617849Z","shell.execute_reply.started":"2024-05-15T21:04:08.064060Z","shell.execute_reply":"2024-05-15T21:04:08.616582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# if you like it please upvote....","metadata":{}},{"cell_type":"code","source":"# Save the model state\ntorch.save(model.state_dict(), 'modified_fasterrcnn_resnet50_fpn.pth')\n","metadata":{"execution":{"iopub.status.busy":"2024-05-15T21:04:08.619108Z","iopub.execute_input":"2024-05-15T21:04:08.619416Z","iopub.status.idle":"2024-05-15T21:04:08.892616Z","shell.execute_reply.started":"2024-05-15T21:04:08.619387Z","shell.execute_reply":"2024-05-15T21:04:08.891690Z"},"trusted":true},"execution_count":null,"outputs":[]}]}