{"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":"nvidiaTeslaT4","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","trusted":true,"execution":{"iopub.status.busy":"2024-12-18T15:47:19.496470Z","iopub.execute_input":"2024-12-18T15:47:19.497138Z","iopub.status.idle":"2024-12-18T15:47:30.504412Z","shell.execute_reply.started":"2024-12-18T15:47:19.497103Z","shell.execute_reply":"2024-12-18T15:47:30.503602Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T15:47:44.629703Z","iopub.execute_input":"2024-12-18T15:47:44.630012Z","iopub.status.idle":"2024-12-18T15:47:44.634876Z","shell.execute_reply.started":"2024-12-18T15:47:44.629988Z","shell.execute_reply":"2024-12-18T15:47:44.634041Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T15:47:46.660782Z","iopub.execute_input":"2024-12-18T15:47:46.661568Z","iopub.status.idle":"2024-12-18T15:47:46.954441Z","shell.execute_reply.started":"2024-12-18T15:47:46.661537Z","shell.execute_reply":"2024-12-18T15:47:46.953483Z"}},"outputs":[],"execution_count":null},{"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-12-18T15:47:50.101242Z","iopub.execute_input":"2024-12-18T15:47:50.101578Z","iopub.status.idle":"2024-12-18T15:47:50.434651Z","shell.execute_reply.started":"2024-12-18T15:47:50.101554Z","shell.execute_reply":"2024-12-18T15:47:50.433693Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.concat([train,coord],axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-12-18T15:47:52.373685Z","iopub.execute_input":"2024-12-18T15:47:52.374284Z","iopub.status.idle":"2024-12-18T15:47:52.396125Z","shell.execute_reply.started":"2024-12-18T15:47:52.374257Z","shell.execute_reply":"2024-12-18T15:47:52.395381Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T15:48:00.791468Z","iopub.execute_input":"2024-12-18T15:48:00.792312Z","iopub.status.idle":"2024-12-18T15:48:00.810375Z","shell.execute_reply.started":"2024-12-18T15:48:00.792273Z","shell.execute_reply":"2024-12-18T15:48:00.809472Z"}},"outputs":[],"execution_count":null},{"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-12-18T15:48:21.965209Z","iopub.execute_input":"2024-12-18T15:48:21.965988Z","iopub.status.idle":"2024-12-18T15:48:22.163189Z","shell.execute_reply.started":"2024-12-18T15:48:21.965959Z","shell.execute_reply":"2024-12-18T15:48:22.162242Z"},"trusted":true},"outputs":[],"execution_count":null},{"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-12-18T15:48:27.499177Z","iopub.execute_input":"2024-12-18T15:48:27.499958Z","iopub.status.idle":"2024-12-18T15:48:27.508128Z","shell.execute_reply.started":"2024-12-18T15:48:27.499927Z","shell.execute_reply":"2024-12-18T15:48:27.507474Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2024-12-18T15:48:30.491772Z","iopub.execute_input":"2024-12-18T15:48:30.492057Z","iopub.status.idle":"2024-12-18T15:48:30.510558Z","shell.execute_reply.started":"2024-12-18T15:48:30.492038Z","shell.execute_reply":"2024-12-18T15:48:30.509668Z"},"trusted":true},"outputs":[],"execution_count":null},{"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-12-18T15:48:40.965047Z","iopub.execute_input":"2024-12-18T15:48:40.965354Z","iopub.status.idle":"2024-12-18T15:48:40.979811Z","shell.execute_reply.started":"2024-12-18T15:48:40.965329Z","shell.execute_reply":"2024-12-18T15:48:40.979172Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-12-18T15:48:43.543056Z","iopub.execute_input":"2024-12-18T15:48:43.543739Z","iopub.status.idle":"2024-12-18T15:48:43.553970Z","shell.execute_reply.started":"2024-12-18T15:48:43.543710Z","shell.execute_reply":"2024-12-18T15:48:43.553093Z"},"trusted":true},"outputs":[],"execution_count":null},{"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-12-18T15:48:48.244872Z","iopub.execute_input":"2024-12-18T15:48:48.245151Z","iopub.status.idle":"2024-12-18T15:48:48.261646Z","shell.execute_reply.started":"2024-12-18T15:48:48.245131Z","shell.execute_reply":"2024-12-18T15:48:48.260763Z"},"trusted":true},"outputs":[],"execution_count":null},{"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-12-18T15:49:13.346313Z","iopub.execute_input":"2024-12-18T15:49:13.347121Z","iopub.status.idle":"2024-12-18T15:49:13.352811Z","shell.execute_reply.started":"2024-12-18T15:49:13.347091Z","shell.execute_reply":"2024-12-18T15:49:13.351994Z"},"trusted":true},"outputs":[],"execution_count":null},{"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-12-18T15:49:25.363862Z","iopub.execute_input":"2024-12-18T15:49:25.364180Z","iopub.status.idle":"2024-12-18T15:49:25.371605Z","shell.execute_reply.started":"2024-12-18T15:49:25.364156Z","shell.execute_reply":"2024-12-18T15:49:25.370606Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def custom_collate(data):\n    return data","metadata":{"execution":{"iopub.status.busy":"2024-12-18T15:49:43.041628Z","iopub.execute_input":"2024-12-18T15:49:43.042277Z","iopub.status.idle":"2024-12-18T15:49:43.045937Z","shell.execute_reply.started":"2024-12-18T15:49:43.042244Z","shell.execute_reply":"2024-12-18T15:49:43.045140Z"},"trusted":true},"outputs":[],"execution_count":null},{"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-12-18T15:49:59.494008Z","iopub.execute_input":"2024-12-18T15:49:59.494336Z","iopub.status.idle":"2024-12-18T15:49:59.556485Z","shell.execute_reply.started":"2024-12-18T15:49:59.494311Z","shell.execute_reply":"2024-12-18T15:49:59.555517Z"},"trusted":true},"outputs":[],"execution_count":null},{"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-12-18T15:50:03.637035Z","iopub.execute_input":"2024-12-18T15:50:03.637351Z","iopub.status.idle":"2024-12-18T15:50:05.682820Z","shell.execute_reply.started":"2024-12-18T15:50:03.637327Z","shell.execute_reply":"2024-12-18T15:50:05.681884Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')","metadata":{"execution":{"iopub.status.busy":"2024-12-18T15:50:10.514579Z","iopub.execute_input":"2024-12-18T15:50:10.514887Z","iopub.status.idle":"2024-12-18T15:50:10.519058Z","shell.execute_reply.started":"2024-12-18T15:50:10.514864Z","shell.execute_reply":"2024-12-18T15:50:10.518231Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device","metadata":{"execution":{"iopub.status.busy":"2024-12-18T15:50:24.018997Z","iopub.execute_input":"2024-12-18T15:50:24.019320Z","iopub.status.idle":"2024-12-18T15:50:24.024257Z","shell.execute_reply.started":"2024-12-18T15:50:24.019295Z","shell.execute_reply":"2024-12-18T15:50:24.023477Z"},"trusted":true},"outputs":[],"execution_count":null},{"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-12-18T15:50:28.976811Z","iopub.execute_input":"2024-12-18T15:50:28.977117Z","iopub.status.idle":"2024-12-18T15:50:28.982260Z","shell.execute_reply.started":"2024-12-18T15:50:28.977094Z","shell.execute_reply":"2024-12-18T15:50:28.981459Z"},"trusted":true},"outputs":[],"execution_count":null},{"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-12-18T15:50:39.648576Z","iopub.execute_input":"2024-12-18T15:50:39.648898Z","iopub.status.idle":"2024-12-18T16:38:27.569924Z","shell.execute_reply.started":"2024-12-18T15:50:39.648874Z","shell.execute_reply":"2024-12-18T16:38:27.568982Z"},"trusted":true},"outputs":[],"execution_count":null},{"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-12-18T16:38:27.571721Z","iopub.execute_input":"2024-12-18T16:38:27.572059Z","iopub.status.idle":"2024-12-18T16:38:27.876579Z","shell.execute_reply.started":"2024-12-18T16:38:27.572028Z","shell.execute_reply":"2024-12-18T16:38:27.875641Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img= data[0][0]\nboxes=data[0][1]['boxes']\nlabels=data[0][1]['label']","metadata":{"execution":{"iopub.status.busy":"2024-12-18T16:38:27.877712Z","iopub.execute_input":"2024-12-18T16:38:27.878053Z","iopub.status.idle":"2024-12-18T16:38:27.882393Z","shell.execute_reply.started":"2024-12-18T16:38:27.878020Z","shell.execute_reply":"2024-12-18T16:38:27.881624Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output = model([img.to(device)])\n","metadata":{"execution":{"iopub.status.busy":"2024-12-18T16:38:27.884063Z","iopub.execute_input":"2024-12-18T16:38:27.884295Z","iopub.status.idle":"2024-12-18T16:38:28.992360Z","shell.execute_reply.started":"2024-12-18T16:38:27.884276Z","shell.execute_reply":"2024-12-18T16:38:28.991428Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"- output os the evaluation:","metadata":{}},{"cell_type":"code","source":"output","metadata":{"execution":{"iopub.status.busy":"2024-12-18T16:38:28.993481Z","iopub.execute_input":"2024-12-18T16:38:28.993744Z","iopub.status.idle":"2024-12-18T16:38:29.072096Z","shell.execute_reply.started":"2024-12-18T16:38:28.993723Z","shell.execute_reply":"2024-12-18T16:38:29.071272Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"out_bbox=output[0]['boxes']\nout_scores=output[0]['scores']","metadata":{"execution":{"iopub.status.busy":"2024-12-18T16:38:29.073083Z","iopub.execute_input":"2024-12-18T16:38:29.073330Z","iopub.status.idle":"2024-12-18T16:38:29.077040Z","shell.execute_reply.started":"2024-12-18T16:38:29.073310Z","shell.execute_reply":"2024-12-18T16:38:29.076162Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"keep = torchvision.ops.nms(out_bbox,out_scores,0.45)","metadata":{"execution":{"iopub.status.busy":"2024-12-18T16:38:29.077989Z","iopub.execute_input":"2024-12-18T16:38:29.078221Z","iopub.status.idle":"2024-12-18T16:38:29.089404Z","shell.execute_reply.started":"2024-12-18T16:38:29.078202Z","shell.execute_reply":"2024-12-18T16:38:29.088628Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"out_bbox.shape , keep.shape","metadata":{"execution":{"iopub.status.busy":"2024-12-18T16:38:29.090436Z","iopub.execute_input":"2024-12-18T16:38:29.090798Z","iopub.status.idle":"2024-12-18T16:38:29.104675Z","shell.execute_reply.started":"2024-12-18T16:38:29.090770Z","shell.execute_reply":"2024-12-18T16:38:29.103916Z"},"trusted":true},"outputs":[],"execution_count":null},{"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-12-18T16:38:29.105684Z","iopub.execute_input":"2024-12-18T16:38:29.105970Z","iopub.status.idle":"2024-12-18T16:38:29.128225Z","shell.execute_reply.started":"2024-12-18T16:38:29.105942Z","shell.execute_reply":"2024-12-18T16:38:29.127327Z"},"trusted":true},"outputs":[],"execution_count":null},{"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-12-18T16:38:29.130524Z","iopub.execute_input":"2024-12-18T16:38:29.131069Z","iopub.status.idle":"2024-12-18T16:38:29.611251Z","shell.execute_reply.started":"2024-12-18T16:38:29.131041Z","shell.execute_reply":"2024-12-18T16:38:29.610237Z"},"trusted":true},"outputs":[],"execution_count":null},{"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-12-18T16:38:29.612800Z","iopub.execute_input":"2024-12-18T16:38:29.613153Z","iopub.status.idle":"2024-12-18T16:38:29.850500Z","shell.execute_reply.started":"2024-12-18T16:38:29.613122Z","shell.execute_reply":"2024-12-18T16:38:29.849803Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}