{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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":30823,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **Global Wheat Detection**\n\n### Dataset Description\nMore details on the data acquisition and processes are available at https://arxiv.org/abs/2005.02162\n\n### What should I expect the data format to be?\nThe data is images of wheat fields, with bounding boxes for each identified wheat head. Not all images include wheat heads / bounding boxes. The images were recorded in many locations around the world.\n\nThe CSV data is simple - the image ID matches up with the filename of a given image, and the width and height of the image are included, along with a bounding box (see below). There is a row in train.csv for each bounding box. Not all images have bounding boxes.\n\nMost of the test set images are hidden. A small subset of test images has been included for your use in writing code.\n\n### What am I predicting?\nYou are attempting to predict bounding boxes around each wheat head in images that have them. If there are no wheat heads, you must predict no bounding boxes.\n\n### Files\n- train.csv - the training data\n- sample_submission.csv - a sample submission file in the correct format\n- train.zip - training images\n- test.zip - test images\n### Columns\n- image_id - the unique image ID\n- width, height - the width and height of the images\n- bbox - a bounding box, formatted as a Python-style list of [xmin, ymin, width, height]\netc.","metadata":{}},{"cell_type":"code","source":"!pip install torch torchvision","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:03:26.643000Z","iopub.execute_input":"2025-01-10T16:03:26.643240Z","iopub.status.idle":"2025-01-10T16:03:30.983293Z","shell.execute_reply.started":"2025-01-10T16:03:26.643219Z","shell.execute_reply":"2025-01-10T16:03:30.982344Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"###  **Important library:**","metadata":{}},{"cell_type":"code","source":"import os\nimport random\nfrom collections import Counter\n\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image, ImageDraw\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\n\nimport torch\nimport torchvision\n\nfrom torchvision import transforms as T  # For data transformations\nfrom torchvision.models.detection.faster_rcnn import FastRCNNPredictor ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:06:27.617584Z","iopub.execute_input":"2025-01-10T16:06:27.617933Z","iopub.status.idle":"2025-01-10T16:06:32.176083Z","shell.execute_reply.started":"2025-01-10T16:06:27.617910Z","shell.execute_reply":"2025-01-10T16:06:32.175439Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/global-wheat-detection/train.csv\")\ntrain.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:06:57.281651Z","iopub.execute_input":"2025-01-10T16:06:57.282162Z","iopub.status.idle":"2025-01-10T16:06:57.540439Z","shell.execute_reply.started":"2025-01-10T16:06:57.282134Z","shell.execute_reply":"2025-01-10T16:06:57.539582Z"}},"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:07:30.615370Z","iopub.execute_input":"2025-01-10T16:07:30.615712Z","iopub.status.idle":"2025-01-10T16:07:31.057015Z","shell.execute_reply.started":"2025-01-10T16:07:30.615686Z","shell.execute_reply":"2025-01-10T16:07:31.056016Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.concat([train,coord],axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:07:43.955750Z","iopub.execute_input":"2025-01-10T16:07:43.956216Z","iopub.status.idle":"2025-01-10T16:07:43.977901Z","shell.execute_reply.started":"2025-01-10T16:07:43.956182Z","shell.execute_reply":"2025-01-10T16:07:43.976890Z"}},"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:07:51.660351Z","iopub.execute_input":"2025-01-10T16:07:51.660651Z","iopub.status.idle":"2025-01-10T16:07:51.867631Z","shell.execute_reply.started":"2025-01-10T16:07:51.660630Z","shell.execute_reply":"2025-01-10T16:07:51.866417Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['x2']=df['x1']+df['w']\ndf['y2']=df['y1']+df['h']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:07:59.504659Z","iopub.execute_input":"2025-01-10T16:07:59.504978Z","iopub.status.idle":"2025-01-10T16:07:59.511941Z","shell.execute_reply.started":"2025-01-10T16:07:59.504952Z","shell.execute_reply":"2025-01-10T16:07:59.511132Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:08:06.743483Z","iopub.execute_input":"2025-01-10T16:08:06.743775Z","iopub.status.idle":"2025-01-10T16:08:06.761363Z","shell.execute_reply.started":"2025-01-10T16:08:06.743754Z","shell.execute_reply":"2025-01-10T16:08:06.760698Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.drop(['bbox','width','height','w','h','source'],axis=1,inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:08:51.789285Z","iopub.execute_input":"2025-01-10T16:08:51.789616Z","iopub.status.idle":"2025-01-10T16:08:51.804309Z","shell.execute_reply.started":"2025-01-10T16:08:51.789595Z","shell.execute_reply":"2025-01-10T16:08:51.803612Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:08:56.927023Z","iopub.execute_input":"2025-01-10T16:08:56.927370Z","iopub.status.idle":"2025-01-10T16:08:56.936876Z","shell.execute_reply.started":"2025-01-10T16:08:56.927347Z","shell.execute_reply":"2025-01-10T16:08:56.936132Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"unique_imgs=df.image_id.unique()\nunique_imgs","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:09:16.063306Z","iopub.execute_input":"2025-01-10T16:09:16.063617Z","iopub.status.idle":"2025-01-10T16:09:16.076914Z","shell.execute_reply.started":"2025-01-10T16:09:16.063595Z","shell.execute_reply":"2025-01-10T16:09:16.076272Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(unique_imgs)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:09:37.195345Z","iopub.execute_input":"2025-01-10T16:09:37.195667Z","iopub.status.idle":"2025-01-10T16:09:37.200194Z","shell.execute_reply.started":"2025-01-10T16:09:37.195643Z","shell.execute_reply":"2025-01-10T16:09:37.199523Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define a custom dataset class inheriting from torch.utils.data.Dataset\nclass custDat(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  # DataFrame containing image and bounding box data\n        self.unique_imgs = unique_imgs  # List of unique image names\n        self.indices = indices  # Indices for accessing the images in the dataset\n    \n    def __len__(self):\n        \"\"\"\n        Return the length of the dataset.\n        \n        Returns:\n            int: Number of samples in the dataset.\n        \"\"\"\n        return len(self.indices)  # Length of the dataset based on the 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 boxes for the image\n        boxes = self.df[self.df.image_id == image_name].values[:, 1:].astype(\"float\")\n        \n        # Open the image and convert it to RGB format\n        img = Image.open(\"../input/global-wheat-detection/train/\" + image_name + \".jpg\").convert('RGB')\n        \n        # Create labels tensor with value 1 for each bounding box\n        labels = torch.ones(boxes.shape[0], dtype=torch.int64)\n        \n        # Create the target dictionary containing boxes and labels\n        target = {}\n        target[\"boxes\"] = torch.tensor(boxes)  # Bounding boxes as a tensor\n        target[\"label\"] = labels  # Labels as a tensor\n        \n        # Return the image converted to a tensor and the target dictionary\n        return T.ToTensor()(img), target\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:14:22.823443Z","iopub.execute_input":"2025-01-10T16:14:22.823798Z","iopub.status.idle":"2025-01-10T16:14:22.829747Z","shell.execute_reply.started":"2025-01-10T16:14:22.823776Z","shell.execute_reply":"2025-01-10T16:14:22.828974Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_inds , val_inds =train_test_split(range(unique_imgs.shape[0]),test_size=0.1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:14:34.878390Z","iopub.execute_input":"2025-01-10T16:14:34.878712Z","iopub.status.idle":"2025-01-10T16:14:34.884439Z","shell.execute_reply.started":"2025-01-10T16:14:34.878684Z","shell.execute_reply":"2025-01-10T16:14:34.883643Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def custom_collate(data):\n    return data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:14:39.814979Z","iopub.execute_input":"2025-01-10T16:14:39.815359Z","iopub.status.idle":"2025-01-10T16:14:39.819221Z","shell.execute_reply.started":"2025-01-10T16:14:39.815334Z","shell.execute_reply":"2025-01-10T16:14:39.818135Z"}},"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":"code","source":"train_dl = torch.utils.data.DataLoader(custDat(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)\nval_dl = torch.utils.data.DataLoader(custDat(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":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:15:27.229458Z","iopub.execute_input":"2025-01-10T16:15:27.229792Z","iopub.status.idle":"2025-01-10T16:15:27.305505Z","shell.execute_reply.started":"2025-01-10T16:15:27.229766Z","shell.execute_reply":"2025-01-10T16:15:27.304670Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')\nmodel = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:16:01.246645Z","iopub.execute_input":"2025-01-10T16:16:01.246966Z","iopub.status.idle":"2025-01-10T16:16:01.937045Z","shell.execute_reply.started":"2025-01-10T16:16:01.246940Z","shell.execute_reply":"2025-01-10T16:16:01.936138Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:16:09.881461Z","iopub.execute_input":"2025-01-10T16:16:09.881790Z","iopub.status.idle":"2025-01-10T16:16:09.888343Z","shell.execute_reply.started":"2025-01-10T16:16:09.881764Z","shell.execute_reply":"2025-01-10T16:16:09.887683Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_classes=2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:16:33.094684Z","iopub.execute_input":"2025-01-10T16:16:33.095027Z","iopub.status.idle":"2025-01-10T16:16:33.098269Z","shell.execute_reply.started":"2025-01-10T16:16:33.095006Z","shell.execute_reply":"2025-01-10T16:16:33.097260Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"in_features=model.roi_heads.box_predictor.cls_score.in_features\nmodel.roi_heads.box_predictor =  FastRCNNPredictor(in_features,num_classes)\nmodel","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:16:48.150444Z","iopub.execute_input":"2025-01-10T16:16:48.150740Z","iopub.status.idle":"2025-01-10T16:16:48.158248Z","shell.execute_reply.started":"2025-01-10T16:16:48.150719Z","shell.execute_reply":"2025-01-10T16:16:48.157579Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:16:57.955941Z","iopub.execute_input":"2025-01-10T16:16:57.956284Z","iopub.status.idle":"2025-01-10T16:16:57.959662Z","shell.execute_reply.started":"2025-01-10T16:16:57.956254Z","shell.execute_reply":"2025-01-10T16:16:57.958870Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:17:09.402905Z","iopub.execute_input":"2025-01-10T16:17:09.403246Z","iopub.status.idle":"2025-01-10T16:17:09.407428Z","shell.execute_reply.started":"2025-01-10T16:17:09.403219Z","shell.execute_reply":"2025-01-10T16:17:09.406804Z"}},"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:17:41.165448Z","iopub.execute_input":"2025-01-10T16:17:41.165743Z","iopub.status.idle":"2025-01-10T16:17:41.170187Z","shell.execute_reply.started":"2025-01-10T16:17:41.165721Z","shell.execute_reply":"2025-01-10T16:17:41.169285Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Move the model to the specified device (CPU or GPU)\nmodel.to(device)\n\n# Loop over the number of epochs\nfor epoch in range(num_epochs):\n    epoch_loss = 0  # Initialize the loss for the current epoch\n    \n    # Loop over the data in the training DataLoader\n    for data in train_dl:\n        imgs = []  # List to store the images\n        targets = []  # List to store the target dictionaries\n        \n        # Loop over each data sample in the 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            # Move the bounding boxes tensor to the device\n            targ['boxes'] = d[1]['boxes'].to(device)\n            # Move the labels tensor to the device\n            targ['labels'] = d[1]['label'].to(device)\n            \n            # Append the target dictionary to the targets 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, converting it to a 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":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T16:18:29.347415Z","iopub.execute_input":"2025-01-10T16:18:29.347780Z","iopub.status.idle":"2025-01-10T17:03:52.605397Z","shell.execute_reply.started":"2025-01-10T16:18:29.347754Z","shell.execute_reply":"2025-01-10T17:03:52.604248Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.eval()\ndata = iter(val_dl).__next__()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T17:03:52.606583Z","iopub.execute_input":"2025-01-10T17:03:52.606821Z","iopub.status.idle":"2025-01-10T17:03:52.851225Z","shell.execute_reply.started":"2025-01-10T17:03:52.606800Z","shell.execute_reply":"2025-01-10T17:03:52.850444Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img= data[0][0]\nboxes=data[0][1]['boxes']\nlabels=data[0][1]['label']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T17:03:52.852906Z","iopub.execute_input":"2025-01-10T17:03:52.853247Z","iopub.status.idle":"2025-01-10T17:03:52.856522Z","shell.execute_reply.started":"2025-01-10T17:03:52.853213Z","shell.execute_reply":"2025-01-10T17:03:52.855783Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output = model([img.to(device)])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T17:03:52.857749Z","iopub.execute_input":"2025-01-10T17:03:52.858022Z","iopub.status.idle":"2025-01-10T17:03:53.857529Z","shell.execute_reply.started":"2025-01-10T17:03:52.858001Z","shell.execute_reply":"2025-01-10T17:03:53.856821Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"out_bbox=output[0]['boxes']\nout_scores=output[0]['scores']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T17:03:53.858322Z","iopub.execute_input":"2025-01-10T17:03:53.858675Z","iopub.status.idle":"2025-01-10T17:03:53.862028Z","shell.execute_reply.started":"2025-01-10T17:03:53.858648Z","shell.execute_reply":"2025-01-10T17:03:53.861218Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"keep = torchvision.ops.nms(out_bbox,out_scores,0.45)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T17:03:53.862985Z","iopub.execute_input":"2025-01-10T17:03:53.863302Z","iopub.status.idle":"2025-01-10T17:03:53.866648Z","shell.execute_reply.started":"2025-01-10T17:03:53.863272Z","shell.execute_reply":"2025-01-10T17:03:53.865874Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"out_bbox.shape , keep.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T17:03:53.867449Z","iopub.execute_input":"2025-01-10T17:03:53.867720Z","iopub.status.idle":"2025-01-10T17:03:53.871948Z","shell.execute_reply.started":"2025-01-10T17:03:53.867693Z","shell.execute_reply":"2025-01-10T17:03:53.871224Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"im = (img.permute(1,2,0).cpu().detach().numpy()*255).astype('uint8')\nim","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T17:03:53.873348Z","iopub.execute_input":"2025-01-10T17:03:53.873555Z","iopub.status.idle":"2025-01-10T17:03:53.886087Z","shell.execute_reply.started":"2025-01-10T17:03:53.873537Z","shell.execute_reply":"2025-01-10T17:03:53.885439Z"}},"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T17:10:02.141779Z","iopub.execute_input":"2025-01-10T17:10:02.142176Z","iopub.status.idle":"2025-01-10T17:10:02.378411Z","shell.execute_reply.started":"2025-01-10T17:10:02.142145Z","shell.execute_reply":"2025-01-10T17:10:02.377421Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Save the model state\ntorch.save(model.state_dict(), 'modified_fasterrcnn_resnet50_fpn.pth')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T17:10:22.718936Z","iopub.execute_input":"2025-01-10T17:10:22.719284Z","iopub.status.idle":"2025-01-10T17:10:22.945300Z","shell.execute_reply.started":"2025-01-10T17:10:22.719256Z","shell.execute_reply":"2025-01-10T17:10:22.944376Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.eval()\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\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":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T17:10:32.479623Z","iopub.execute_input":"2025-01-10T17:10:32.479946Z","iopub.status.idle":"2025-01-10T17:10:33.062084Z","shell.execute_reply.started":"2025-01-10T17:10:32.479924Z","shell.execute_reply":"2025-01-10T17:10:33.061178Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.eval()\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\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":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T17:11:03.401685Z","iopub.execute_input":"2025-01-10T17:11:03.402013Z","iopub.status.idle":"2025-01-10T17:11:03.965000Z","shell.execute_reply.started":"2025-01-10T17:11:03.401987Z","shell.execute_reply":"2025-01-10T17:11:03.964125Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T17:12:12.937638Z","iopub.execute_input":"2025-01-10T17:12:12.937976Z","iopub.status.idle":"2025-01-10T17:12:14.458095Z","shell.execute_reply.started":"2025-01-10T17:12:12.937951Z","shell.execute_reply":"2025-01-10T17:12:14.457252Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model2.eval()\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\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":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-10T17:12:25.426633Z","iopub.execute_input":"2025-01-10T17:12:25.426929Z","iopub.status.idle":"2025-01-10T17:12:25.986827Z","shell.execute_reply.started":"2025-01-10T17:12:25.426908Z","shell.execute_reply":"2025-01-10T17:12:25.985903Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}