{"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":30747,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os \nimport random\nimport warnings\nwarnings.filterwarnings('ignore')\nfrom collections import Counter\n\nimport numpy as np \nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n\nfrom PIL import Image, ImageDraw\nfrom sklearn.model_selection import train_test_split\n\nimport torch\nimport torchvision\n\nfrom torchvision import transforms as T\nfrom torchvision.models.detection.faster_rcnn import FastRCNNPredictor","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-21T06:26:53.561636Z","iopub.execute_input":"2024-08-21T06:26:53.561965Z","iopub.status.idle":"2024-08-21T06:27:06.022217Z","shell.execute_reply.started":"2024-08-21T06:26:53.561920Z","shell.execute_reply":"2024-08-21T06:27:06.021382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/global-wheat-detection/train.csv')\ntrain_data","metadata":{"execution":{"iopub.status.busy":"2024-08-21T06:27:06.023741Z","iopub.execute_input":"2024-08-21T06:27:06.024343Z","iopub.status.idle":"2024-08-21T06:27:06.355164Z","shell.execute_reply.started":"2024-08-21T06:27:06.024316Z","shell.execute_reply":"2024-08-21T06:27:06.354255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coord = pd.DataFrame(list(train_data.bbox.apply(lambda x : x[1: -1].split(\",\")).values), columns= ['x1', 'y1', 'w', 'h'])","metadata":{"execution":{"iopub.status.busy":"2024-08-21T06:27:06.356227Z","iopub.execute_input":"2024-08-21T06:27:06.356483Z","iopub.status.idle":"2024-08-21T06:27:06.846907Z","shell.execute_reply.started":"2024-08-21T06:27:06.356461Z","shell.execute_reply":"2024-08-21T06:27:06.845905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.concat([train_data, coord], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2024-08-21T06:27:06.849939Z","iopub.execute_input":"2024-08-21T06:27:06.850634Z","iopub.status.idle":"2024-08-21T06:27:06.871193Z","shell.execute_reply.started":"2024-08-21T06:27:06.850606Z","shell.execute_reply":"2024-08-21T06:27:06.870371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2024-08-21T06:27:06.872317Z","iopub.execute_input":"2024-08-21T06:27:06.872659Z","iopub.status.idle":"2024-08-21T06:27:06.888622Z","shell.execute_reply.started":"2024-08-21T06:27:06.872631Z","shell.execute_reply":"2024-08-21T06:27:06.887745Z"},"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'])","metadata":{"execution":{"iopub.status.busy":"2024-08-21T06:27:06.889775Z","iopub.execute_input":"2024-08-21T06:27:06.890082Z","iopub.status.idle":"2024-08-21T06:27:07.276629Z","shell.execute_reply.started":"2024-08-21T06:27:06.890057Z","shell.execute_reply":"2024-08-21T06:27:07.275858Z"},"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-08-21T06:27:07.277970Z","iopub.execute_input":"2024-08-21T06:27:07.278264Z","iopub.status.idle":"2024-08-21T06:27:07.285800Z","shell.execute_reply.started":"2024-08-21T06:27:07.278239Z","shell.execute_reply":"2024-08-21T06:27:07.284923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop(columns = ['width', 'height', 'bbox', 'source', 'w', 'h'], axis = 1, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2024-08-21T06:27:07.286818Z","iopub.execute_input":"2024-08-21T06:27:07.287091Z","iopub.status.idle":"2024-08-21T06:27:07.305808Z","shell.execute_reply.started":"2024-08-21T06:27:07.287067Z","shell.execute_reply":"2024-08-21T06:27:07.304981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2024-08-21T06:27:07.306869Z","iopub.execute_input":"2024-08-21T06:27:07.307208Z","iopub.status.idle":"2024-08-21T06:27:07.326538Z","shell.execute_reply.started":"2024-08-21T06:27:07.307176Z","shell.execute_reply":"2024-08-21T06:27:07.325705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_imgs = df.image_id.unique()","metadata":{"execution":{"iopub.status.busy":"2024-08-21T06:27:07.327720Z","iopub.execute_input":"2024-08-21T06:27:07.328086Z","iopub.status.idle":"2024-08-21T06:27:07.347626Z","shell.execute_reply.started":"2024-08-21T06:27:07.328053Z","shell.execute_reply":"2024-08-21T06:27:07.346810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class custData(torch.utils.data.Dataset):\n    \n    def __init__(self, df, unique_imgs, indices):\n        \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        \n        image_name = self.unique_imgs[self.indices[idx]]\n        \n        boxes = self.df[self.df.image_id == image_name].values[:, 1:].astype('float')\n        \n        img = Image.open('../input/global-wheat-detection/train/' + image_name + '.jpg').convert('RGB')\n        \n        labels = torch.ones(boxes.shape[0], dtype = torch.int64)\n        \n        target = {}\n        target['boxes'] = torch.tensor(boxes)\n        target['label'] = labels\n        \n        return T.ToTensor()(img), target","metadata":{"execution":{"iopub.status.busy":"2024-08-21T06:27:07.352176Z","iopub.execute_input":"2024-08-21T06:27:07.353094Z","iopub.status.idle":"2024-08-21T06:27:07.361107Z","shell.execute_reply.started":"2024-08-21T06:27:07.353071Z","shell.execute_reply":"2024-08-21T06:27:07.359774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-21T06:27:07.362822Z","iopub.execute_input":"2024-08-21T06:27:07.363267Z","iopub.status.idle":"2024-08-21T06:27:07.377886Z","shell.execute_reply.started":"2024-08-21T06:27:07.363227Z","shell.execute_reply":"2024-08-21T06:27:07.376861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def custom_collate(data):\n    return data","metadata":{"execution":{"iopub.status.busy":"2024-08-21T06:27:07.379284Z","iopub.execute_input":"2024-08-21T06:27:07.380087Z","iopub.status.idle":"2024-08-21T06:27:07.388072Z","shell.execute_reply.started":"2024-08-21T06:27:07.380051Z","shell.execute_reply":"2024-08-21T06:27:07.387086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### ***Data Loader***","metadata":{}},{"cell_type":"code","source":"train_dl = torch.utils.data.DataLoader(custData(df, unique_imgs, train_inds),\n                                      batch_size = 16,\n                                      shuffle = True,\n                                      collate_fn = custom_collate,\n                                      pin_memory = True if torch.cuda.is_available() else False)\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)","metadata":{"execution":{"iopub.status.busy":"2024-08-21T06:27:07.390278Z","iopub.execute_input":"2024-08-21T06:27:07.391078Z","iopub.status.idle":"2024-08-21T06:27:07.456557Z","shell.execute_reply.started":"2024-08-21T06:27:07.391042Z","shell.execute_reply":"2024-08-21T06:27:07.455557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### ***Modelling***","metadata":{}},{"cell_type":"code","source":"model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights=True)","metadata":{"execution":{"iopub.status.busy":"2024-08-21T06:27:07.457997Z","iopub.execute_input":"2024-08-21T06:27:07.458635Z","iopub.status.idle":"2024-08-21T06:27:09.698314Z","shell.execute_reply.started":"2024-08-21T06:27:07.458600Z","shell.execute_reply":"2024-08-21T06:27:09.697512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model","metadata":{"execution":{"iopub.status.busy":"2024-08-21T06:27:09.699518Z","iopub.execute_input":"2024-08-21T06:27:09.699932Z","iopub.status.idle":"2024-08-21T06:27:09.708914Z","shell.execute_reply.started":"2024-08-21T06:27:09.699900Z","shell.execute_reply":"2024-08-21T06:27:09.708014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = 2","metadata":{"execution":{"iopub.status.busy":"2024-08-21T06:27:09.710151Z","iopub.execute_input":"2024-08-21T06:27:09.710477Z","iopub.status.idle":"2024-08-21T06:27:09.717526Z","shell.execute_reply.started":"2024-08-21T06:27:09.710447Z","shell.execute_reply":"2024-08-21T06:27:09.716751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"in_features = model.roi_heads.box_predictor.cls_score.in_features\n\nmodel.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)\n\nmodel","metadata":{"execution":{"iopub.status.busy":"2024-08-21T06:27:09.718462Z","iopub.execute_input":"2024-08-21T06:27:09.718699Z","iopub.status.idle":"2024-08-21T06:27:09.733336Z","shell.execute_reply.started":"2024-08-21T06:27:09.718673Z","shell.execute_reply":"2024-08-21T06:27:09.732228Z"},"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-08-21T06:27:09.734618Z","iopub.execute_input":"2024-08-21T06:27:09.735163Z","iopub.status.idle":"2024-08-21T06:27:09.739732Z","shell.execute_reply.started":"2024-08-21T06:27:09.735134Z","shell.execute_reply":"2024-08-21T06:27:09.738920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device","metadata":{"execution":{"iopub.status.busy":"2024-08-21T06:27:09.740842Z","iopub.execute_input":"2024-08-21T06:27:09.741181Z","iopub.status.idle":"2024-08-21T06:27:09.751767Z","shell.execute_reply.started":"2024-08-21T06:27:09.741153Z","shell.execute_reply":"2024-08-21T06:27:09.750893Z"},"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-08-21T06:27:09.752860Z","iopub.execute_input":"2024-08-21T06:27:09.753155Z","iopub.status.idle":"2024-08-21T06:27:09.763795Z","shell.execute_reply.started":"2024-08-21T06:27:09.753133Z","shell.execute_reply":"2024-08-21T06:27:09.762986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.to(device)\n\nfor epoch in range(num_epochs):\n    epoch_loss = 0\n    \n    for data in train_dl:\n        imgs = []\n        targets = []\n        \n        for d in data:\n            imgs.append(d[0].to(device))\n            \n            targ = {}\n            \n            targ['boxes'] = d[1]['boxes'].to(device)\n            targ['labels'] = d[1]['label'].to(device)\n            \n            targets.append(targ)\n            \n        loss_dict = model(imgs, targets)\n        \n        loss = sum(v for v in loss_dict.values())\n        \n        epoch_loss += loss.cpu().detach().numpy()\n        \n        optimizer.zero_grad()\n        \n        loss.backward()\n        \n        optimizer.step()\n        \n    print(epoch_loss)","metadata":{"execution":{"iopub.status.busy":"2024-08-21T06:27:09.765125Z","iopub.execute_input":"2024-08-21T06:27:09.765415Z","iopub.status.idle":"2024-08-21T07:12:38.235452Z","shell.execute_reply.started":"2024-08-21T06:27:09.765383Z","shell.execute_reply":"2024-08-21T07:12:38.234468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### ***Model Evaluation***","metadata":{}},{"cell_type":"code","source":"model.eval()\n\ndata = iter(val_dl).__next__()","metadata":{"execution":{"iopub.status.busy":"2024-08-21T07:12:38.236612Z","iopub.execute_input":"2024-08-21T07:12:38.236896Z","iopub.status.idle":"2024-08-21T07:12:38.647100Z","shell.execute_reply.started":"2024-08-21T07:12:38.236872Z","shell.execute_reply":"2024-08-21T07:12:38.646126Z"},"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-08-21T07:12:38.648298Z","iopub.execute_input":"2024-08-21T07:12:38.648582Z","iopub.status.idle":"2024-08-21T07:12:38.655682Z","shell.execute_reply.started":"2024-08-21T07:12:38.648559Z","shell.execute_reply":"2024-08-21T07:12:38.654919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = model([img.to(device)])","metadata":{"execution":{"iopub.status.busy":"2024-08-21T07:12:38.656792Z","iopub.execute_input":"2024-08-21T07:12:38.657307Z","iopub.status.idle":"2024-08-21T07:12:39.623251Z","shell.execute_reply.started":"2024-08-21T07:12:38.657217Z","shell.execute_reply":"2024-08-21T07:12:39.622440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output","metadata":{"execution":{"iopub.status.busy":"2024-08-21T07:12:39.624226Z","iopub.execute_input":"2024-08-21T07:12:39.624498Z","iopub.status.idle":"2024-08-21T07:12:39.710622Z","shell.execute_reply.started":"2024-08-21T07:12:39.624475Z","shell.execute_reply":"2024-08-21T07:12:39.709752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_bbox = output[0]['boxes']\n\nout_scores = output[0]['scores']","metadata":{"execution":{"iopub.status.busy":"2024-08-21T07:12:39.711660Z","iopub.execute_input":"2024-08-21T07:12:39.711930Z","iopub.status.idle":"2024-08-21T07:12:39.716084Z","shell.execute_reply.started":"2024-08-21T07:12:39.711907Z","shell.execute_reply":"2024-08-21T07:12:39.715176Z"},"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-08-21T07:12:39.717319Z","iopub.execute_input":"2024-08-21T07:12:39.717803Z","iopub.status.idle":"2024-08-21T07:12:39.726317Z","shell.execute_reply.started":"2024-08-21T07:12:39.717773Z","shell.execute_reply":"2024-08-21T07:12:39.725493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_bbox.shape, keep.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-21T07:12:39.727463Z","iopub.execute_input":"2024-08-21T07:12:39.727785Z","iopub.status.idle":"2024-08-21T07:12:39.737982Z","shell.execute_reply.started":"2024-08-21T07:12:39.727756Z","shell.execute_reply":"2024-08-21T07:12:39.737128Z"},"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-08-21T07:12:39.739049Z","iopub.execute_input":"2024-08-21T07:12:39.740205Z","iopub.status.idle":"2024-08-21T07:12:39.752551Z","shell.execute_reply.started":"2024-08-21T07:12:39.740175Z","shell.execute_reply":"2024-08-21T07:12:39.751623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vsample = Image.fromarray(im)\ndraw = ImageDraw.Draw(vsample)\n\nfor box in boxes:\n    draw.rectangle(list(box), fill = None, outline = 'red')\n\nvsample","metadata":{"execution":{"iopub.status.busy":"2024-08-21T07:12:39.753612Z","iopub.execute_input":"2024-08-21T07:12:39.753965Z","iopub.status.idle":"2024-08-21T07:12:40.262164Z","shell.execute_reply.started":"2024-08-21T07:12:39.753916Z","shell.execute_reply":"2024-08-21T07:12:40.261224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(model.state_dict(), 'modified_fasterrcnn_resnet50_fpn.pth')","metadata":{"execution":{"iopub.status.busy":"2024-08-21T07:12:40.266885Z","iopub.execute_input":"2024-08-21T07:12:40.267197Z","iopub.status.idle":"2024-08-21T07:12:40.544022Z","shell.execute_reply.started":"2024-08-21T07:12:40.267173Z","shell.execute_reply":"2024-08-21T07:12:40.543227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Load Saved Model**","metadata":{}},{"cell_type":"code","source":"def create_model(num_classes):\n    model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights = False)\n    \n    in_features = model.roi_heads.box_predictor.cls_score.in_features\n    \n    model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)\n    \n    return model\n\n\nnum_classes = 2\n\nmodel2 = create_model(num_classes)\n\nmodel2.load_state_dict(torch.load('/kaggle/working/modified_fasterrcnn_resnet50_fpn.pth'))\n\ndevice = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')\n\nmodel2.to(device)","metadata":{"execution":{"iopub.status.busy":"2024-08-21T07:21:54.138108Z","iopub.execute_input":"2024-08-21T07:21:54.138445Z","iopub.status.idle":"2024-08-21T07:21:55.073448Z","shell.execute_reply.started":"2024-08-21T07:21:54.138413Z","shell.execute_reply":"2024-08-21T07:21:55.072576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2.eval()\n\ndata = iter(val_dl).__next__()\n\nimg = data[0][0]\nboxes = data[0][1]['boxes']\nlabels = data[0][1]['label']\n\noutput = model([img.to(device)])\n\nout_bbox = output[0]['boxes']\nout_scores = output[0]['scores']\n\nkeep = torchvision.ops.nms(out_bbox, out_scores, 0.45)\n\nim = (img.permute(1, 2, 0).cpu().detach().numpy()*255).astype('uint8')\n\nvsample = Image.fromarray(im)\ndraw = ImageDraw.Draw(vsample)\n\nfor box in boxes:\n    draw.rectangle(list(box), fill = None, outline = 'red')\n\nvsample","metadata":{"execution":{"iopub.status.busy":"2024-08-21T07:22:00.385231Z","iopub.execute_input":"2024-08-21T07:22:00.385917Z","iopub.status.idle":"2024-08-21T07:22:01.466641Z","shell.execute_reply.started":"2024-08-21T07:22:00.385883Z","shell.execute_reply":"2024-08-21T07:22:01.465572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}