{"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-06-12T23:46:03.010986Z","iopub.execute_input":"2024-06-12T23:46:03.011724Z","iopub.status.idle":"2024-06-12T23:46:17.992787Z","shell.execute_reply.started":"2024-06-12T23:46:03.011691Z","shell.execute_reply":"2024-06-12T23:46:17.991672Z"},"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-06-12T23:46:23.221659Z","iopub.execute_input":"2024-06-12T23:46:23.222045Z","iopub.status.idle":"2024-06-12T23:46:30.631009Z","shell.execute_reply.started":"2024-06-12T23:46:23.222008Z","shell.execute_reply":"2024-06-12T23:46:30.629877Z"},"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-06-12T23:46:30.632666Z","iopub.execute_input":"2024-06-12T23:46:30.633103Z","iopub.status.idle":"2024-06-12T23:46:30.943449Z","shell.execute_reply.started":"2024-06-12T23:46:30.633076Z","shell.execute_reply":"2024-06-12T23:46:30.942429Z"},"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-06-12T23:46:33.928815Z","iopub.execute_input":"2024-06-12T23:46:33.929544Z","iopub.status.idle":"2024-06-12T23:46:34.326547Z","shell.execute_reply.started":"2024-06-12T23:46:33.929505Z","shell.execute_reply":"2024-06-12T23:46:34.325451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.concat([train,coord],axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-06-12T23:46:34.922307Z","iopub.execute_input":"2024-06-12T23:46:34.923222Z","iopub.status.idle":"2024-06-12T23:46:34.944797Z","shell.execute_reply.started":"2024-06-12T23:46:34.923183Z","shell.execute_reply":"2024-06-12T23:46:34.943907Z"},"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":"2024-06-12T23:46:36.532460Z","iopub.execute_input":"2024-06-12T23:46:36.532902Z","iopub.status.idle":"2024-06-12T23:46:37.004056Z","shell.execute_reply.started":"2024-06-12T23:46:36.532869Z","shell.execute_reply":"2024-06-12T23:46:37.003174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['x2']=df['x1']+df['w']\ndf['y2']=df['y1']+df['h']","metadata":{"execution":{"iopub.status.busy":"2024-06-12T23:46:39.188317Z","iopub.execute_input":"2024-06-12T23:46:39.188687Z","iopub.status.idle":"2024-06-12T23:46:39.198387Z","shell.execute_reply.started":"2024-06-12T23:46:39.188659Z","shell.execute_reply":"2024-06-12T23:46:39.197178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2024-06-12T23:46:40.388095Z","iopub.execute_input":"2024-06-12T23:46:40.388486Z","iopub.status.idle":"2024-06-12T23:46:40.418379Z","shell.execute_reply.started":"2024-06-12T23:46:40.388454Z","shell.execute_reply":"2024-06-12T23:46:40.417259Z"},"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-06-12T23:46:43.565165Z","iopub.execute_input":"2024-06-12T23:46:43.565899Z","iopub.status.idle":"2024-06-12T23:46:43.583304Z","shell.execute_reply.started":"2024-06-12T23:46:43.565854Z","shell.execute_reply":"2024-06-12T23:46:43.582284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-12T23:46:44.618237Z","iopub.execute_input":"2024-06-12T23:46:44.618649Z","iopub.status.idle":"2024-06-12T23:46:44.635831Z","shell.execute_reply.started":"2024-06-12T23:46:44.618618Z","shell.execute_reply":"2024-06-12T23:46:44.634629Z"},"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-06-12T23:46:46.543812Z","iopub.execute_input":"2024-06-12T23:46:46.544619Z","iopub.status.idle":"2024-06-12T23:46:46.566248Z","shell.execute_reply.started":"2024-06-12T23:46:46.544586Z","shell.execute_reply":"2024-06-12T23:46:46.565277Z"},"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-06-12T23:46:49.480092Z","iopub.execute_input":"2024-06-12T23:46:49.481104Z","iopub.status.idle":"2024-06-12T23:46:49.489640Z","shell.execute_reply.started":"2024-06-12T23:46:49.481071Z","shell.execute_reply":"2024-06-12T23:46:49.488397Z"},"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-06-12T23:46:51.812392Z","iopub.execute_input":"2024-06-12T23:46:51.813327Z","iopub.status.idle":"2024-06-12T23:46:51.821467Z","shell.execute_reply.started":"2024-06-12T23:46:51.813273Z","shell.execute_reply":"2024-06-12T23:46:51.820091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def custom_collate(data):\n    return data","metadata":{"execution":{"iopub.status.busy":"2024-06-12T23:46:53.826203Z","iopub.execute_input":"2024-06-12T23:46:53.826654Z","iopub.status.idle":"2024-06-12T23:46:53.831705Z","shell.execute_reply.started":"2024-06-12T23:46:53.826620Z","shell.execute_reply":"2024-06-12T23:46:53.830521Z"},"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-06-12T23:47:01.241373Z","iopub.execute_input":"2024-06-12T23:47:01.241824Z","iopub.status.idle":"2024-06-12T23:47:01.276204Z","shell.execute_reply.started":"2024-06-12T23:47:01.241789Z","shell.execute_reply":"2024-06-12T23:47:01.274881Z"},"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-06-12T23:47:04.035633Z","iopub.execute_input":"2024-06-12T23:47:04.036245Z","iopub.status.idle":"2024-06-12T23:47:06.304189Z","shell.execute_reply.started":"2024-06-12T23:47:04.036215Z","shell.execute_reply":"2024-06-12T23:47:06.303203Z"},"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-06-12T23:47:08.644509Z","iopub.execute_input":"2024-06-12T23:47:08.645254Z","iopub.status.idle":"2024-06-12T23:47:08.650039Z","shell.execute_reply.started":"2024-06-12T23:47:08.645222Z","shell.execute_reply":"2024-06-12T23:47:08.648985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device","metadata":{"execution":{"iopub.status.busy":"2024-06-12T23:47:09.131392Z","iopub.execute_input":"2024-06-12T23:47:09.132116Z","iopub.status.idle":"2024-06-12T23:47:09.138050Z","shell.execute_reply.started":"2024-06-12T23:47:09.132085Z","shell.execute_reply":"2024-06-12T23:47:09.136946Z"},"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-06-12T23:47:11.187447Z","iopub.execute_input":"2024-06-12T23:47:11.187790Z","iopub.status.idle":"2024-06-12T23:47:11.194573Z","shell.execute_reply.started":"2024-06-12T23:47:11.187765Z","shell.execute_reply":"2024-06-12T23:47:11.193484Z"},"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-06-12T23:47:13.258411Z","iopub.execute_input":"2024-06-12T23:47:13.258765Z","iopub.status.idle":"2024-06-13T00:11:53.116198Z","shell.execute_reply.started":"2024-06-12T23:47:13.258739Z","shell.execute_reply":"2024-06-13T00:11:53.115175Z"},"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-06-13T00:12:06.219093Z","iopub.execute_input":"2024-06-13T00:12:06.219501Z","iopub.status.idle":"2024-06-13T00:12:06.638755Z","shell.execute_reply.started":"2024-06-13T00:12:06.219471Z","shell.execute_reply":"2024-06-13T00:12:06.637704Z"},"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-06-13T00:12:08.454607Z","iopub.execute_input":"2024-06-13T00:12:08.455429Z","iopub.status.idle":"2024-06-13T00:12:08.461449Z","shell.execute_reply.started":"2024-06-13T00:12:08.455390Z","shell.execute_reply":"2024-06-13T00:12:08.459720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = model([img.to(device)])\n","metadata":{"execution":{"iopub.status.busy":"2024-06-13T00:12:10.787928Z","iopub.execute_input":"2024-06-13T00:12:10.788740Z","iopub.status.idle":"2024-06-13T00:12:10.910419Z","shell.execute_reply.started":"2024-06-13T00:12:10.788707Z","shell.execute_reply":"2024-06-13T00:12:10.909359Z"},"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-06-13T00:12:13.774270Z","iopub.execute_input":"2024-06-13T00:12:13.774629Z","iopub.status.idle":"2024-06-13T00:12:13.814986Z","shell.execute_reply.started":"2024-06-13T00:12:13.774601Z","shell.execute_reply":"2024-06-13T00:12:13.814187Z"},"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-06-13T00:12:16.000875Z","iopub.execute_input":"2024-06-13T00:12:16.001599Z","iopub.status.idle":"2024-06-13T00:12:16.005752Z","shell.execute_reply.started":"2024-06-13T00:12:16.001564Z","shell.execute_reply":"2024-06-13T00:12:16.004801Z"},"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-06-13T00:12:18.043794Z","iopub.execute_input":"2024-06-13T00:12:18.044222Z","iopub.status.idle":"2024-06-13T00:12:18.049496Z","shell.execute_reply.started":"2024-06-13T00:12:18.044190Z","shell.execute_reply":"2024-06-13T00:12:18.048571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_bbox.shape , keep.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-13T00:12:31.467869Z","iopub.execute_input":"2024-06-13T00:12:31.468315Z","iopub.status.idle":"2024-06-13T00:12:31.474739Z","shell.execute_reply.started":"2024-06-13T00:12:31.468281Z","shell.execute_reply":"2024-06-13T00:12:31.473693Z"},"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-06-13T00:12:34.958637Z","iopub.execute_input":"2024-06-13T00:12:34.959040Z","iopub.status.idle":"2024-06-13T00:12:34.974085Z","shell.execute_reply.started":"2024-06-13T00:12:34.959007Z","shell.execute_reply":"2024-06-13T00:12:34.973148Z"},"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-06-13T00:12:37.742438Z","iopub.execute_input":"2024-06-13T00:12:37.742792Z","iopub.status.idle":"2024-06-13T00:12:38.263421Z","shell.execute_reply.started":"2024-06-13T00:12:37.742766Z","shell.execute_reply":"2024-06-13T00:12:38.262198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the model state\ntorch.save(model.state_dict(), 'fasterrcnn_resnet50_fpn_kelompok2.pth')\n","metadata":{"execution":{"iopub.status.busy":"2024-06-13T00:19:48.715890Z","iopub.execute_input":"2024-06-13T00:19:48.716282Z","iopub.status.idle":"2024-06-13T00:19:49.089156Z","shell.execute_reply.started":"2024-06-13T00:19:48.716253Z","shell.execute_reply":"2024-06-13T00:19:49.088161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import FileLink\n\noutput_file_path = '/kaggle/working/fasterrcnn_resnet50_fpn_kelompok2.pth'  # Path to your output file\nFileLink(output_file_path)","metadata":{"execution":{"iopub.status.busy":"2024-06-13T00:16:10.931497Z","iopub.execute_input":"2024-06-13T00:16:10.932168Z","iopub.status.idle":"2024-06-13T00:16:10.938739Z","shell.execute_reply.started":"2024-06-13T00:16:10.932111Z","shell.execute_reply":"2024-06-13T00:16:10.937695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}