How to train faster-rcnn on dataset including negative data in pytorch
I am trying to train the torchvision Faster R-CNN model for object detection on my custom data. I used the code in torchvision object detection fine-tuning tutorial. But getting this error:
Expected target boxes to be a tensor of shape [N, 4], got torch.Size([0])

This has to do with negative data (empty training images / no bounding boxes) in my custom dataset. How can we change the below Dataset class to enable training faster-rcnn on dataset including negative data?
class MyCustomDataset(Dataset):

    def __init__(self, root, transforms):
        self.root = root
        self.transforms = transforms
        # load all image files, sorting them to
        # ensure that they are aligned
        self.imgs = list(sorted(os.listdir(os.path.join(root, &quot;PNGImages&quot;))))
        self.masks = list(sorted(os.listdir(os.path.join(root, &quot;PedMasks&quot;))))

    def __len__(self):
        return len(self.imgs)

    def __getitem__(self, idx):
        # load images ad masks
        img_path = os.path.join(self.root, &quot;PNGImages&quot;, self.imgs[idx])
        mask_path = os.path.join(self.root, &quot;PedMasks&quot;, self.masks[idx])
        img = Image.open(img_path).convert(&quot;RGB&quot;)
        # note that we haven't converted the mask to RGB,
        # because each color corresponds to a different instance
        # with 0 being background
        mask = Image.open(mask_path)
        # convert the PIL Image into a numpy array
        mask = np.array(mask)
        # instances are encoded as different colors
        obj_ids = np.unique(mask)
        # first id is the background, so remove it
        obj_ids = obj_ids[1:]

        # split the color-encoded mask into a set of binary masks
        masks = mask == obj_ids[:, None, None]

        # get bounding box coordinates for each mask
        num_objs = len(obj_ids)
        
        boxes = []
        for i in range(num_objs):
            pos = np.where(masks[i])
            xmin = np.min(pos[1])
            xmax = np.max(pos[1])
            ymin = np.min(pos[0])
            ymax = np.max(pos[0])
            boxes.append([xmin, ymin, xmax, ymax])

        # convert everything into a torch.Tensor
        boxes = torch.as_tensor(boxes, dtype=torch.float32)      
        # there is only one class  
        labels = torch.ones((num_objs,), dtype=torch.int64)
        image_id = torch.tensor([idx])
        area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])
        # suppose all instances are not crowd
        iscrowd = torch.zeros((num_objs,), dtype=torch.int64)

        target = {}
        target[&quot;boxes&quot;] = boxes
        target[&quot;labels&quot;] = labels
        target[&quot;image_id&quot;] =  torch.tensor([idx])
        target[&quot;area&quot;] = area
        target[&quot;iscrowd&quot;] = iscrowd

        return img, target 


We need to make two changes to the Dataset Class.
1- Empty boxes are fed as:
if num_objs == 0:
    boxes = torch.zeros((0, 4), dtype=torch.float32)
else:
    boxes = torch.as_tensor(boxes, dtype=torch.float32)

2- Assign area=0 for empty bounding box case, change the code used for calculating area, and make it a torch tensor:
area = 0
for i in range(num_objs):
    pos = np.where(masks[i])
    xmin = np.min(pos[1])
    xmax = np.max(pos[1])
    ymin = np.min(pos[0])
    ymax = np.max(pos[0])
    area += (xmax-xmin)*(ymax-ymin)
area = torch.as_tensor(area, dtype=torch.float32)

We will incorporate step#2 in existing for loop.
So, the modified Dataset Class will look like:
class MyCustomDataset(Dataset):

    def __init__(self, root, transforms):
        self.root = root
        self.transforms = transforms
        # load all image files, sorting them to
        # ensure that they are aligned
        self.imgs = list(sorted(os.listdir(os.path.join(root, &quot;PNGImages&quot;))))
        self.masks = list(sorted(os.listdir(os.path.join(root, &quot;PedMasks&quot;))))

    def __len__(self):
        return len(self.imgs)

    def __getitem__(self, idx):
        # load images ad masks
        img_path = os.path.join(self.root, &quot;PNGImages&quot;, self.imgs[idx])
        mask_path = os.path.join(self.root, &quot;PedMasks&quot;, self.masks[idx])
        img = Image.open(img_path).convert(&quot;RGB&quot;)
        # note that we haven't converted the mask to RGB,
        # because each color corresponds to a different instance
        # with 0 being background
        mask = Image.open(mask_path)
        # convert the PIL Image into a numpy array
        mask = np.array(mask)
        # instances are encoded as different colors
        obj_ids = np.unique(mask)
        # first id is the background, so remove it
        obj_ids = obj_ids[1:]

        # split the color-encoded mask into a set of binary masks
        masks = mask == obj_ids[:, None, None]

        # get bounding box coordinates for each mask
        num_objs = len(obj_ids)
        
        boxes = []
        area = 0 
        for i in range(num_objs):
            pos = np.where(masks[i])
            xmin = np.min(pos[1])
            xmax = np.max(pos[1])
            ymin = np.min(pos[0])
            ymax = np.max(pos[0])
            boxes.append([xmin, ymin, xmax, ymax])
            area += (xmax-xmin)*(ymax-ymin)
        area = torch.as_tensor(area, dtype=torch.float32)

        # Handle empty bounding boxes
        if num_objs == 0:
            boxes = torch.zeros((0, 4), dtype=torch.float32)
        else:
            boxes = torch.as_tensor(boxes, dtype=torch.float32)   

        # there is only one class  
        labels = torch.ones((num_objs,), dtype=torch.int64)
        image_id = torch.tensor([idx])

        #area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])

        # suppose all instances are not crowd
        iscrowd = torch.zeros((num_objs,), dtype=torch.int64)

        target = {}
        target[&quot;boxes&quot;] = boxes
        target[&quot;labels&quot;] = labels
        target[&quot;image_id&quot;] =  torch.tensor([idx])
        target[&quot;area&quot;] = area
        target[&quot;iscrowd&quot;] = iscrowd

        return img, target 

