{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session\n\n# Use the kagglehub client library to attach Kaggle resources like competitions, datasets, and models to your session\n# Learn more about kagglehub: https://github.com/Kaggle/kagglehub/blob/main/README.md\n\nimport kagglehub\n# kagglehub.dataset_download('<owner>/<dataset-slug>')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-08-01T12:25:17.822184Z","iopub.execute_input":"2026-08-01T12:25:17.822798Z","iopub.status.idle":"2026-08-01T12:25:26.346599Z","shell.execute_reply.started":"2026-08-01T12:25:17.822767Z","shell.execute_reply":"2026-08-01T12:25:26.345969Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"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\nimport os\nos.environ[\"PYTORCH_CUDA_ALLOC_CONF\"] = \"expandable_segments:True\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T13:02:20.968283Z","iopub.execute_input":"2026-08-01T13:02:20.968817Z","iopub.status.idle":"2026-08-01T13:02:20.974749Z","shell.execute_reply.started":"2026-08-01T13:02:20.968785Z","shell.execute_reply":"2026-08-01T13:02:20.974078Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install torch torchvision","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T13:02:44.245858Z","iopub.execute_input":"2026-08-01T13:02:44.246126Z","iopub.status.idle":"2026-08-01T13:02:49.178729Z","shell.execute_reply.started":"2026-08-01T13:02:44.246104Z","shell.execute_reply":"2026-08-01T13:02:49.177493Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Load Train Data**","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/competitions/global-wheat-detection/train.csv\")\ntrain.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T12:25:55.964825Z","iopub.execute_input":"2026-08-01T12:25:55.965237Z","iopub.status.idle":"2026-08-01T12:25:56.234618Z","shell.execute_reply.started":"2026-08-01T12:25:55.965214Z","shell.execute_reply":"2026-08-01T12:25:56.233830Z"}},"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":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T12:25:59.055359Z","iopub.execute_input":"2026-08-01T12:25:59.055641Z","iopub.status.idle":"2026-08-01T12:25:59.377444Z","shell.execute_reply.started":"2026-08-01T12:25:59.055603Z","shell.execute_reply":"2026-08-01T12:25:59.376862Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.concat([train,coord],axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T12:26:03.350211Z","iopub.execute_input":"2026-08-01T12:26:03.350695Z","iopub.status.idle":"2026-08-01T12:26:03.370676Z","shell.execute_reply.started":"2026-08-01T12:26:03.350667Z","shell.execute_reply":"2026-08-01T12:26:03.369716Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T12:26:04.686321Z","iopub.execute_input":"2026-08-01T12:26:04.686961Z","iopub.status.idle":"2026-08-01T12:26:04.700693Z","shell.execute_reply.started":"2026-08-01T12:26:04.686932Z","shell.execute_reply":"2026-08-01T12:26:04.700030Z"}},"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":"2026-08-01T12:26:06.999283Z","iopub.execute_input":"2026-08-01T12:26:06.999534Z","iopub.status.idle":"2026-08-01T12:26:07.205574Z","shell.execute_reply.started":"2026-08-01T12:26:06.999513Z","shell.execute_reply":"2026-08-01T12:26:07.204810Z"}},"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":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T12:26:09.742337Z","iopub.execute_input":"2026-08-01T12:26:09.743081Z","iopub.status.idle":"2026-08-01T12:26:09.751006Z","shell.execute_reply.started":"2026-08-01T12:26:09.743050Z","shell.execute_reply":"2026-08-01T12:26:09.750229Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T12:26:10.307394Z","iopub.execute_input":"2026-08-01T12:26:10.307847Z","iopub.status.idle":"2026-08-01T12:26:10.322785Z","shell.execute_reply.started":"2026-08-01T12:26:10.307822Z","shell.execute_reply":"2026-08-01T12:26:10.321967Z"}},"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":"2026-08-01T12:26:12.615412Z","iopub.execute_input":"2026-08-01T12:26:12.615913Z","iopub.status.idle":"2026-08-01T12:26:12.630511Z","shell.execute_reply.started":"2026-08-01T12:26:12.615888Z","shell.execute_reply":"2026-08-01T12:26:12.629859Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T12:26:13.116064Z","iopub.execute_input":"2026-08-01T12:26:13.116768Z","iopub.status.idle":"2026-08-01T12:26:13.127658Z","shell.execute_reply.started":"2026-08-01T12:26:13.116740Z","shell.execute_reply":"2026-08-01T12:26:13.126689Z"}},"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":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T12:26:15.816671Z","iopub.execute_input":"2026-08-01T12:26:15.817095Z","iopub.status.idle":"2026-08-01T12:26:15.831085Z","shell.execute_reply.started":"2026-08-01T12:26:15.817068Z","shell.execute_reply":"2026-08-01T12:26:15.830264Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Make custom class for preparing the data:","metadata":{}},{"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/competitions/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","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T13:05:50.897677Z","iopub.execute_input":"2026-08-01T13:05:50.897947Z","iopub.status.idle":"2026-08-01T13:05:50.904497Z","shell.execute_reply.started":"2026-08-01T13:05:50.897926Z","shell.execute_reply":"2026-08-01T13:05:50.903698Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Split 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":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T13:05:55.575586Z","iopub.execute_input":"2026-08-01T13:05:55.575874Z","iopub.status.idle":"2026-08-01T13:05:55.581532Z","shell.execute_reply.started":"2026-08-01T13:05:55.575853Z","shell.execute_reply":"2026-08-01T13:05:55.580914Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def custom_collate(data):\n    return data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T13:05:56.021225Z","iopub.execute_input":"2026-08-01T13:05:56.021719Z","iopub.status.idle":"2026-08-01T13:05:56.025174Z","shell.execute_reply.started":"2026-08-01T13:05:56.021692Z","shell.execute_reply":"2026-08-01T13:05:56.024591Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"The 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.","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(\n    custDat(df, unique_imgs, train_inds),\n    batch_size=2,                 # was 16 — this alone fixes most of the OOM\n    shuffle=True,\n    collate_fn=custom_collate,\n    num_workers=2,\n    pin_memory=True if torch.cuda.is_available() else False,\n)\n\nval_dl = torch.utils.data.DataLoader(\n    custDat(df, unique_imgs, val_inds),\n    batch_size=2,                 # was 8\n    shuffle=True,\n    collate_fn=custom_collate,\n    num_workers=2,\n    pin_memory=True if torch.cuda.is_available() else False,\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T13:05:59.488217Z","iopub.execute_input":"2026-08-01T13:05:59.489073Z","iopub.status.idle":"2026-08-01T13:05:59.494913Z","shell.execute_reply.started":"2026-08-01T13:05:59.489015Z","shell.execute_reply":"2026-08-01T13:05:59.494282Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T13:05:59.967097Z","iopub.execute_input":"2026-08-01T13:05:59.967778Z","iopub.status.idle":"2026-08-01T13:06:00.675237Z","shell.execute_reply.started":"2026-08-01T13:05:59.967749Z","shell.execute_reply":"2026-08-01T13:06:00.674565Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T13:06:02.085117Z","iopub.execute_input":"2026-08-01T13:06:02.085537Z","iopub.status.idle":"2026-08-01T13:06:02.093794Z","shell.execute_reply.started":"2026-08-01T13:06:02.085503Z","shell.execute_reply":"2026-08-01T13:06:02.092986Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_classes=2\nin_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":"2026-08-01T13:06:08.363185Z","iopub.execute_input":"2026-08-01T13:06:08.363535Z","iopub.status.idle":"2026-08-01T13:06:08.372023Z","shell.execute_reply.started":"2026-08-01T13:06:08.363508Z","shell.execute_reply":"2026-08-01T13:06:08.371404Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"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":"2026-08-01T13:06:12.368560Z","iopub.execute_input":"2026-08-01T13:06:12.369019Z","iopub.status.idle":"2026-08-01T13:06:12.373829Z","shell.execute_reply.started":"2026-08-01T13:06:12.368987Z","shell.execute_reply":"2026-08-01T13:06:12.372927Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T13:06:13.016581Z","iopub.execute_input":"2026-08-01T13:06:13.017263Z","iopub.status.idle":"2026-08-01T13:06:13.022727Z","shell.execute_reply.started":"2026-08-01T13:06:13.017223Z","shell.execute_reply":"2026-08-01T13:06:13.021950Z"}},"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":"2026-08-01T13:06:13.775598Z","iopub.execute_input":"2026-08-01T13:06:13.776395Z","iopub.status.idle":"2026-08-01T13:06:13.782033Z","shell.execute_reply.started":"2026-08-01T13:06:13.776367Z","shell.execute_reply":"2026-08-01T13:06:13.781284Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Train the model with 5 epochs and prin the loss:","metadata":{}},{"cell_type":"code","source":"from torch.amp import autocast, GradScaler\n\n# Create the gradient scaler once, before the epoch loop\nscaler = GradScaler(\"cuda\")\n\n# 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.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            imgs.append(d[0].to(device))\n            \n            targ = {}\n            targ['boxes']  = d[1]['boxes'].to(device)\n            targ['labels'] = d[1]['label'].to(device)   # your dataset uses 'label'\n            targets.append(targ)\n        \n        # Zero the gradients BEFORE the forward pass\n        optimizer.zero_grad()\n        \n        # Mixed-precision forward pass — halves activation memory\n        with autocast(\"cuda\", dtype=torch.float16):\n            loss_dict = model(imgs, targets)\n            loss = sum(v for v in loss_dict.values())\n        \n        # Accumulate loss as a plain Python float (no autograd graph kept)\n        epoch_loss += loss.item()\n        \n        # Backprop and step with the scaler\n        scaler.scale(loss).backward()\n        scaler.step(optimizer)\n        scaler.update()\n        \n        # Free per-batch tensors so they don't linger into the next iteration\n        del imgs, targets, loss_dict, loss\n    \n    print(f\"Epoch {epoch+1}: {epoch_loss:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T13:06:17.933061Z","iopub.execute_input":"2026-08-01T13:06:17.933560Z","iopub.status.idle":"2026-08-01T13:37:46.487786Z","shell.execute_reply.started":"2026-08-01T13:06:17.933533Z","shell.execute_reply":"2026-08-01T13:37:46.486895Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Evaluate the model","metadata":{}},{"cell_type":"code","source":"model.eval()\ndata = iter(val_dl).__next__()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T13:40:15.982338Z","iopub.execute_input":"2026-08-01T13:40:15.982861Z","iopub.status.idle":"2026-08-01T13:40:16.372112Z","shell.execute_reply.started":"2026-08-01T13:40:15.982834Z","shell.execute_reply":"2026-08-01T13:40:16.371163Z"}},"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":"2026-08-01T13:40:17.485458Z","iopub.execute_input":"2026-08-01T13:40:17.486091Z","iopub.status.idle":"2026-08-01T13:40:17.490788Z","shell.execute_reply.started":"2026-08-01T13:40:17.486054Z","shell.execute_reply":"2026-08-01T13:40:17.489996Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Output of the Evaluation","metadata":{}},{"cell_type":"code","source":"output = model([img.to(device)])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T13:40:24.005712Z","iopub.execute_input":"2026-08-01T13:40:24.006010Z","iopub.status.idle":"2026-08-01T13:40:24.135090Z","shell.execute_reply.started":"2026-08-01T13:40:24.005987Z","shell.execute_reply":"2026-08-01T13:40:24.134201Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T13:43:35.587282Z","iopub.execute_input":"2026-08-01T13:43:35.588088Z","iopub.status.idle":"2026-08-01T13:43:35.663085Z","shell.execute_reply.started":"2026-08-01T13:43:35.588053Z","shell.execute_reply":"2026-08-01T13:43:35.662445Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"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":"2026-08-01T13:40:40.227601Z","iopub.execute_input":"2026-08-01T13:40:40.228070Z","iopub.status.idle":"2026-08-01T13:40:40.232043Z","shell.execute_reply.started":"2026-08-01T13:40:40.228043Z","shell.execute_reply":"2026-08-01T13:40:40.231292Z"}},"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":"2026-08-01T13:41:00.761400Z","iopub.execute_input":"2026-08-01T13:41:00.762224Z","iopub.status.idle":"2026-08-01T13:41:00.766798Z","shell.execute_reply.started":"2026-08-01T13:41:00.762185Z","shell.execute_reply":"2026-08-01T13:41:00.765839Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"out_bbox.shape , keep.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T13:41:17.521373Z","iopub.execute_input":"2026-08-01T13:41:17.522392Z","iopub.status.idle":"2026-08-01T13:41:17.528211Z","shell.execute_reply.started":"2026-08-01T13:41:17.522358Z","shell.execute_reply":"2026-08-01T13:41:17.527428Z"}},"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":"2026-08-01T13:41:30.513323Z","iopub.execute_input":"2026-08-01T13:41:30.514105Z","iopub.status.idle":"2026-08-01T13:41:30.532074Z","shell.execute_reply.started":"2026-08-01T13:41:30.514075Z","shell.execute_reply":"2026-08-01T13:41:30.531386Z"},"collapsed":true,"jupyter":{"outputs_hidden":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":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T13:41:43.033345Z","iopub.execute_input":"2026-08-01T13:41:43.034154Z","iopub.status.idle":"2026-08-01T13:41:43.220508Z","shell.execute_reply.started":"2026-08-01T13:41:43.034125Z","shell.execute_reply":"2026-08-01T13:41:43.219281Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#  Save the model state","metadata":{}},{"cell_type":"code","source":"\ntorch.save(model.state_dict(), 'modified_fasterrcnn_resnet50_fpn.pth')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T13:44:18.168420Z","iopub.execute_input":"2026-08-01T13:44:18.169315Z","iopub.status.idle":"2026-08-01T13:44:18.407413Z","shell.execute_reply.started":"2026-08-01T13:44:18.169286Z","shell.execute_reply":"2026-08-01T13:44:18.406759Z"}},"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":"2026-08-01T13:44:32.651287Z","iopub.execute_input":"2026-08-01T13:44:32.651951Z","iopub.status.idle":"2026-08-01T13:44:33.390780Z","shell.execute_reply.started":"2026-08-01T13:44:32.651922Z","shell.execute_reply":"2026-08-01T13:44:33.389587Z"}},"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":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-01T13:45:43.244376Z","iopub.execute_input":"2026-08-01T13:45:43.244682Z","iopub.status.idle":"2026-08-01T13:45:44.603389Z","shell.execute_reply.started":"2026-08-01T13:45:43.244652Z","shell.execute_reply":"2026-08-01T13:45:44.602792Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"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":"2026-08-01T13:46:13.596338Z","iopub.execute_input":"2026-08-01T13:46:13.596923Z","iopub.status.idle":"2026-08-01T13:46:14.271406Z","shell.execute_reply.started":"2026-08-01T13:46:13.596894Z","shell.execute_reply":"2026-08-01T13:46:14.270418Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}