{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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":30776,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"! pip install torch torchvision","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-09-29T07:07:11.269341Z","iopub.execute_input":"2024-09-29T07:07:11.269644Z","iopub.status.idle":"2024-09-29T07:07:24.403624Z","shell.execute_reply.started":"2024-09-29T07:07:11.269610Z","shell.execute_reply":"2024-09-29T07:07:24.402465Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Important Library","metadata":{}},{"cell_type":"code","source":"# Standard library imports\nimport os  # For handling file paths\nimport random  # For generating random numbers\nfrom collections import Counter  # For counting hashable objects\n\n# Third-party imports\nimport numpy as np  # For numerical operations\nimport pandas as pd  # For data manipulation and analysis\nfrom PIL import Image, ImageDraw  # For image handling and drawing\nimport matplotlib.pyplot as plt  # For plotting\nfrom sklearn.model_selection import train_test_split  # For splitting data into training and testing sets\n\n# PyTorch imports\nimport torch  # For tensor operations and neural networks\nimport torchvision  # For computer vision tasks\n\n# Torchvision imports\nfrom torchvision import transforms as T  # For data transformations\nfrom torchvision.models.detection.faster_rcnn import FastRCNNPredictor  # For Faster R-CNN model\n","metadata":{"execution":{"iopub.status.busy":"2024-09-29T07:07:24.406347Z","iopub.execute_input":"2024-09-29T07:07:24.406773Z","iopub.status.idle":"2024-09-29T07:07:29.929367Z","shell.execute_reply.started":"2024-09-29T07:07:24.406723Z","shell.execute_reply":"2024-09-29T07:07:29.928432Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/global-wheat-detection/train.csv\")\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-29T07:08:06.762886Z","iopub.execute_input":"2024-09-29T07:08:06.763544Z","iopub.status.idle":"2024-09-29T07:08:07.079445Z","shell.execute_reply.started":"2024-09-29T07:08:06.763501Z","shell.execute_reply":"2024-09-29T07:08:07.078447Z"},"trusted":true},"outputs":[],"execution_count":null},{"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-09-29T07:15:33.194265Z","iopub.execute_input":"2024-09-29T07:15:33.194727Z","iopub.status.idle":"2024-09-29T07:15:33.840430Z","shell.execute_reply.started":"2024-09-29T07:15:33.194669Z","shell.execute_reply":"2024-09-29T07:15:33.839528Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.concat([train,coord], axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-09-29T07:15:55.412223Z","iopub.execute_input":"2024-09-29T07:15:55.412625Z","iopub.status.idle":"2024-09-29T07:15:55.433416Z","shell.execute_reply.started":"2024-09-29T07:15:55.412587Z","shell.execute_reply":"2024-09-29T07:15:55.432397Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### make the value of the columns numeric values","metadata":{}},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2024-09-29T07:17:17.928858Z","iopub.execute_input":"2024-09-29T07:17:17.929239Z","iopub.status.idle":"2024-09-29T07:17:17.948483Z","shell.execute_reply.started":"2024-09-29T07:17:17.929203Z","shell.execute_reply":"2024-09-29T07:17:17.947469Z"},"trusted":true},"outputs":[],"execution_count":null},{"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-09-29T07:18:59.679133Z","iopub.execute_input":"2024-09-29T07:18:59.679532Z","iopub.status.idle":"2024-09-29T07:19:00.087972Z","shell.execute_reply.started":"2024-09-29T07:18:59.679495Z","shell.execute_reply":"2024-09-29T07:19:00.086969Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### calculating x2, y2","metadata":{}},{"cell_type":"code","source":"df['x2']=df['x1']+df['w']\ndf['y2']=df['y1']+df['h']","metadata":{"execution":{"iopub.status.busy":"2024-09-29T07:19:00.491207Z","iopub.execute_input":"2024-09-29T07:19:00.491852Z","iopub.status.idle":"2024-09-29T07:19:00.498604Z","shell.execute_reply.started":"2024-09-29T07:19:00.491813Z","shell.execute_reply":"2024-09-29T07:19:00.497823Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2024-09-29T07:19:01.880750Z","iopub.execute_input":"2024-09-29T07:19:01.881117Z","iopub.status.idle":"2024-09-29T07:19:01.905999Z","shell.execute_reply.started":"2024-09-29T07:19:01.881084Z","shell.execute_reply":"2024-09-29T07:19:01.905000Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### drop unnecessary columns","metadata":{}},{"cell_type":"code","source":"df.drop(['bbox', 'width', 'height', 'w', 'h', 'source'], axis =1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-09-29T07:22:53.163207Z","iopub.execute_input":"2024-09-29T07:22:53.163623Z","iopub.status.idle":"2024-09-29T07:22:53.176611Z","shell.execute_reply.started":"2024-09-29T07:22:53.163585Z","shell.execute_reply":"2024-09-29T07:22:53.175725Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-29T07:23:05.937449Z","iopub.execute_input":"2024-09-29T07:23:05.938314Z","iopub.status.idle":"2024-09-29T07:23:05.955178Z","shell.execute_reply.started":"2024-09-29T07:23:05.938262Z","shell.execute_reply":"2024-09-29T07:23:05.953993Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### taking unique ids of the image","metadata":{}},{"cell_type":"code","source":"unique_imgs = df.image_id.unique()\nunique_imgs","metadata":{"execution":{"iopub.status.busy":"2024-09-29T07:33:57.454735Z","iopub.execute_input":"2024-09-29T07:33:57.455114Z","iopub.status.idle":"2024-09-29T07:33:57.474099Z","shell.execute_reply.started":"2024-09-29T07:33:57.455076Z","shell.execute_reply":"2024-09-29T07:33:57.473263Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### making custom class for perparing data","metadata":{}},{"cell_type":"code","source":"# defining a custom dataset class inheriting from torch.utils.data.Dataset\n\nclass custData(torch.utils.data.Dataset):\n    def __init__(self, df, unique_imgs, indices):\n        \"\"\"\n        Initialize the dataset with a DataFrame, a list of unique image names, and indices.\n        \n        Args:\n            df (pd.DataFrame): DataFrame containing image IDs and bounding box information.\n            unique_imgs (list): List of unique image names.\n            indices (list): List of indices to be used for accessing the dataset.\n        \"\"\"\n        self.df = df\n        self.unique_imgs = unique_imgs\n        self.indices = indices   # Indices for creating the images in the dataset\n        \n    def __len__(self):\n        \"\"\"\n        eturn the length of the dataset.\n        \n        Returns:\n            int: Number of samples in the dataset.\n        \"\"\"\n        return len(self.indices)\n    \n    def __getitem__(self, idx):\n        \"\"\"\n        Get an item from the dataset at the specified index.\n        \n        Args:\n            idx (int): Index of the item to be fetched.\n        \n        Returns:\n            tuple: Tuple containing the image tensor and the target dictionary.\n        \"\"\"\n        # get the image name using the index\n        image_name = self.unique_imgs[self.indices[idx]]\n        \n        # get the bounding box for the images\n        boxes = self.df[self.df.image_id == image_name].values[:,1:].astype(\"float\")\n        \n        #opening the image and converting it to RGB format\n        img = Image.open(\"../input/global-wheat-detection/train/\" + image_name + \".jpg\").convert(\"RGB\")\n        \n        #create label tensors with value 1 for each bounding box \n        labels = torch.ones(boxes.shape[0], dtype= torch.int64)\n        \n        # creating the target dictionary containing boxes and labels\n        target = {}\n        target[\"boxes\"] = torch.tensor(boxes)\n        target['label'] = labels\n        \n        #return the image converted to a tensor and the target dictionary\n        return T.ToTensor()(img), target","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:56:31.234053Z","iopub.execute_input":"2024-09-29T08:56:31.234791Z","iopub.status.idle":"2024-09-29T08:56:31.243783Z","shell.execute_reply.started":"2024-09-29T08:56:31.234752Z","shell.execute_reply":"2024-09-29T08:56:31.242763Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### splitting the data into 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-09-29T08:56:31.620376Z","iopub.execute_input":"2024-09-29T08:56:31.620743Z","iopub.status.idle":"2024-09-29T08:56:31.626615Z","shell.execute_reply.started":"2024-09-29T08:56:31.620706Z","shell.execute_reply":"2024-09-29T08:56:31.625587Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def custom_collate(data):\n    return data","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:56:31.830561Z","iopub.execute_input":"2024-09-29T08:56:31.831286Z","iopub.status.idle":"2024-09-29T08:56:31.834955Z","shell.execute_reply.started":"2024-09-29T08:56:31.831250Z","shell.execute_reply":"2024-09-29T08:56:31.833988Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Need for custom_collate in Object Detection\nHandling Variable Number of Annotations:\n\nIn object detection, each image can have a different number of objects, and consequently, a different number of annotations (bounding boxes). The default collate function in PyTorch (default_collate) expects each sample in the batch to have the same shape, which is not the case here.\nMaintaining Data Structure:\n\nThe custom_collate function helps maintain the structure of the data when it is batched together. Specifically, it returns the data as a list of tuples, where each tuple contains an image tensor and its corresponding target dictionary. This structure is essential for the Faster R-CNN model, which expects a list of such tuples as input.\nAvoiding Collate Errors:\n\nWithout a custom collate function, the DataLoader might try to convert the list of dictionaries (targets) into a single tensor, which would fail due to the variable number of bounding boxes per image.","metadata":{}},{"cell_type":"markdown","source":"Input: data is a list of tuples. Each tuple contains:\n\nAn image tensor (e.g., T.ToTensor()(img)).\nA target dictionary (e.g., target with keys \"boxes\" and \"labels\").\nOutput: The same list of tuples is returned without any modification.","metadata":{}},{"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(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)\ntest_dl = torch.utils.data.DataLoader(custData(df, unique_imgs, val_inds),\n                                      batch_size=16,\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-09-29T08:56:32.576887Z","iopub.execute_input":"2024-09-29T08:56:32.577257Z","iopub.status.idle":"2024-09-29T08:56:32.583797Z","shell.execute_reply.started":"2024-09-29T08:56:32.577220Z","shell.execute_reply":"2024-09-29T08:56:32.582757Z"},"trusted":true},"outputs":[],"execution_count":null},{"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-09-29T08:56:32.979025Z","iopub.execute_input":"2024-09-29T08:56:32.979357Z","iopub.status.idle":"2024-09-29T08:56:33.669895Z","shell.execute_reply.started":"2024-09-29T08:56:32.979321Z","shell.execute_reply":"2024-09-29T08:56:33.669041Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:56:33.671875Z","iopub.execute_input":"2024-09-29T08:56:33.672335Z","iopub.status.idle":"2024-09-29T08:56:33.681884Z","shell.execute_reply.started":"2024-09-29T08:56:33.672291Z","shell.execute_reply":"2024-09-29T08:56:33.680870Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_classes =2","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:56:33.683122Z","iopub.execute_input":"2024-09-29T08:56:33.683881Z","iopub.status.idle":"2024-09-29T08:56:33.689786Z","shell.execute_reply.started":"2024-09-29T08:56:33.683837Z","shell.execute_reply":"2024-09-29T08:56:33.688859Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# updating the out_features on the model according to our need\n\nin_features = model.roi_heads.box_predictor.cls_score.in_features\nmodel.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)\nmodel","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:56:33.704932Z","iopub.execute_input":"2024-09-29T08:56:33.705216Z","iopub.status.idle":"2024-09-29T08:56:33.715564Z","shell.execute_reply.started":"2024-09-29T08:56:33.705184Z","shell.execute_reply":"2024-09-29T08:56:33.714665Z"},"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-09-29T08:56:33.901935Z","iopub.execute_input":"2024-09-29T08:56:33.902245Z","iopub.status.idle":"2024-09-29T08:56:33.906592Z","shell.execute_reply.started":"2024-09-29T08:56:33.902198Z","shell.execute_reply":"2024-09-29T08:56:33.905588Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:56:34.098811Z","iopub.execute_input":"2024-09-29T08:56:34.099094Z","iopub.status.idle":"2024-09-29T08:56:34.104503Z","shell.execute_reply.started":"2024-09-29T08:56:34.099063Z","shell.execute_reply":"2024-09-29T08:56:34.103630Z"},"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-09-29T08:56:34.348528Z","iopub.execute_input":"2024-09-29T08:56:34.348849Z","iopub.status.idle":"2024-09-29T08:56:34.354206Z","shell.execute_reply.started":"2024-09-29T08:56:34.348816Z","shell.execute_reply":"2024-09-29T08:56:34.353328Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### training the model and observing loss","metadata":{}},{"cell_type":"code","source":"# moving the model to the specified device \nmodel.to(device)\n\n# looping over the number of epochs\nfor epoch in range(num_epochs):\n    epoch_loss = 0 # initializing the loss for current epoch\n    \n    # looping over the data in the training dataloader\n    for data in train_dl:\n        imgs = []\n        targets = []\n        \n        # looping over each data sample in batch\n        for d in data:\n            # append the image tensor to the imgs list and move it to the device\n            imgs.append(d[0].to(device))\n            \n            # create a target dictionary for the current sample\n            targ = {}\n            # moving the bounding boxes tensor to the device\n            targ['boxes'] = d[1]['boxes'].to(device)\n            # moving the label tensor to the device\n            targ['labels'] = d[1]['label'].to(device)\n            \n            # append the target dictionary to the target list\n            targets.append(targ)\n            \n        # perform a forward pass through the model and get the loss dictionary\n        loss_dict = model(imgs, targets)\n        \n        # sum all the losses in the loss dictionary to get the total loss\n        loss = sum(v for v in loss_dict.values())\n        \n        # add the loss for the current batch to the epoch loss and convert it to the numpy value\n        epoch_loss += loss.cpu().detach().numpy()\n        \n        # zero the gradients of the optimizer\n        optimizer.zero_grad()\n        \n        # Backpropagate the loss\n        loss.backward()\n        \n        # update the model parameters\n        optimizer.step()\n        \n    # print the total loss for the current epoch\n    print(epoch_loss)","metadata":{"execution":{"iopub.status.busy":"2024-09-29T08:56:34.835165Z","iopub.execute_input":"2024-09-29T08:56:34.835506Z","iopub.status.idle":"2024-09-29T09:20:59.920732Z","shell.execute_reply.started":"2024-09-29T08:56:34.835470Z","shell.execute_reply":"2024-09-29T09:20:59.919747Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"evaluate model","metadata":{}},{"cell_type":"code","source":"model.eval()\ndata = iter(test_dl).__next__()","metadata":{"execution":{"iopub.status.busy":"2024-09-29T09:38:56.949009Z","iopub.execute_input":"2024-09-29T09:38:56.949401Z","iopub.status.idle":"2024-09-29T09:38:57.835062Z","shell.execute_reply.started":"2024-09-29T09:38:56.949362Z","shell.execute_reply":"2024-09-29T09:38:57.834044Z"},"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-09-29T09:39:04.841270Z","iopub.execute_input":"2024-09-29T09:39:04.841669Z","iopub.status.idle":"2024-09-29T09:39:04.846686Z","shell.execute_reply.started":"2024-09-29T09:39:04.841626Z","shell.execute_reply":"2024-09-29T09:39:04.845754Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output = model([img.to(device)])","metadata":{"execution":{"iopub.status.busy":"2024-09-29T09:39:11.440466Z","iopub.execute_input":"2024-09-29T09:39:11.440887Z","iopub.status.idle":"2024-09-29T09:39:11.541965Z","shell.execute_reply.started":"2024-09-29T09:39:11.440848Z","shell.execute_reply":"2024-09-29T09:39:11.541149Z"},"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-09-29T09:39:34.663145Z","iopub.execute_input":"2024-09-29T09:39:34.664019Z","iopub.status.idle":"2024-09-29T09:39:34.706274Z","shell.execute_reply.started":"2024-09-29T09:39:34.663978Z","shell.execute_reply":"2024-09-29T09:39:34.705369Z"},"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-09-29T09:39:43.961391Z","iopub.execute_input":"2024-09-29T09:39:43.961778Z","iopub.status.idle":"2024-09-29T09:39:43.966041Z","shell.execute_reply.started":"2024-09-29T09:39:43.961741Z","shell.execute_reply":"2024-09-29T09:39:43.965133Z"},"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-09-29T09:39:49.884189Z","iopub.execute_input":"2024-09-29T09:39:49.885210Z","iopub.status.idle":"2024-09-29T09:39:49.889678Z","shell.execute_reply.started":"2024-09-29T09:39:49.885168Z","shell.execute_reply":"2024-09-29T09:39:49.888926Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"out_bbox.shape , keep.shape","metadata":{"execution":{"iopub.status.busy":"2024-09-29T09:39:57.490785Z","iopub.execute_input":"2024-09-29T09:39:57.491687Z","iopub.status.idle":"2024-09-29T09:39:57.497525Z","shell.execute_reply.started":"2024-09-29T09:39:57.491646Z","shell.execute_reply":"2024-09-29T09:39:57.496569Z"},"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-09-29T09:40:14.883660Z","iopub.execute_input":"2024-09-29T09:40:14.884063Z","iopub.status.idle":"2024-09-29T09:40:14.895837Z","shell.execute_reply.started":"2024-09-29T09:40:14.884027Z","shell.execute_reply":"2024-09-29T09:40:14.894908Z"},"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-09-29T09:40:28.591365Z","iopub.execute_input":"2024-09-29T09:40:28.591757Z","iopub.status.idle":"2024-09-29T09:40:28.738001Z","shell.execute_reply.started":"2024-09-29T09:40:28.591720Z","shell.execute_reply":"2024-09-29T09:40:28.736763Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Save the model state\ntorch.save(model.state_dict(), 'modified_fasterrcnn_resnet50_fpn.pth')","metadata":{"execution":{"iopub.status.busy":"2024-09-29T10:00:35.625106Z","iopub.execute_input":"2024-09-29T10:00:35.625518Z","iopub.status.idle":"2024-09-29T10:00:35.877454Z","shell.execute_reply.started":"2024-09-29T10:00:35.625480Z","shell.execute_reply":"2024-09-29T10:00:35.876659Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.eval()\ndata = iter(test_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\nout_bbox.shape , keep.shape\nim = (img.permute(1,2,0).cpu().detach().numpy()*255).astype('uint8')\n\nvsample = 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-09-29T10:00:51.814438Z","iopub.execute_input":"2024-09-29T10:00:51.815144Z","iopub.status.idle":"2024-09-29T10:00:52.843954Z","shell.execute_reply.started":"2024-09-29T10:00:51.815101Z","shell.execute_reply":"2024-09-29T10:00:52.842566Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.eval()\ndata = iter(test_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\nout_bbox.shape , keep.shape\nim = (img.permute(1,2,0).cpu().detach().numpy()*255).astype('uint8')\n\nvsample = 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-09-29T10:01:07.554444Z","iopub.execute_input":"2024-09-29T10:01:07.554851Z","iopub.status.idle":"2024-09-29T10:01:08.739342Z","shell.execute_reply.started":"2024-09-29T10:01:07.554813Z","shell.execute_reply":"2024-09-29T10:01:08.736584Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Load the saved model","metadata":{}},{"cell_type":"code","source":"import torch\nimport torchvision\nfrom torchvision.models.detection.faster_rcnn import FastRCNNPredictor\n\n# Function to create the model and replace the classifier head\ndef create_model(num_classes):\n    # Load a pre-trained model\n    model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights=False)\n    \n    # Get the number of input features for the classifier\n    in_features = model.roi_heads.box_predictor.cls_score.in_features\n    \n    # Replace the pre-trained head with a new one (adjusting for the number of classes)\n    model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)\n    \n    return model\n\n# Specify the number of classes (1 class + background)\nnum_classes = 2\n\n# Create a model instance\nmodel2 = create_model(num_classes)\n\n# Load the saved model state dictionary\nmodel2.load_state_dict(torch.load('/kaggle/working/modified_fasterrcnn_resnet50_fpn.pth'))\n\n# Move the model to the specified device\ndevice = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')\nmodel2.to(device)\n\n# Now the model is ready to be used for inference or further training","metadata":{"execution":{"iopub.status.busy":"2024-09-29T10:01:35.716312Z","iopub.execute_input":"2024-09-29T10:01:35.716731Z","iopub.status.idle":"2024-09-29T10:01:37.175840Z","shell.execute_reply.started":"2024-09-29T10:01:35.716681Z","shell.execute_reply":"2024-09-29T10:01:37.174764Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model2.eval()\ndata = iter(test_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\nout_bbox.shape , keep.shape\nim = (img.permute(1,2,0).cpu().detach().numpy()*255).astype('uint8')\n\nvsample = 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-09-29T10:01:50.740881Z","iopub.execute_input":"2024-09-29T10:01:50.741277Z","iopub.status.idle":"2024-09-29T10:01:51.891737Z","shell.execute_reply.started":"2024-09-29T10:01:50.741240Z","shell.execute_reply":"2024-09-29T10:01:51.890637Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null}]}