IndexError in fasterrcnn_resnet50_fpn training

I load PIL.Images as


        with as fobj:
            source = torch.from_numpy(np.asarray(fobj, dtype=np.float32))
            assert source.shape == torch.Size([1000, 1000, 3])

      # Open the boxes from files and do some processing
      # ......

      # We have not done the labeling yet, there will be 3 probably
      # Type A, Type B and background
      return source, {"boxes": boxes, "labels": torch.ones([N, 1]), dtype=torch,int64)}

Model creation:

    def create_model(self, pretrained=False, num_classes=2):
        """Creates faster rcnn 50 model

            pretrained (bool): To use Coco pretrained
            num_classes (int): Number of output classes
        return tv.models.detection.fasterrcnn_resnet50_fpn(

Training howerer causes IndexError

        model = self.create_model(
        model =
        params = [p for p in model.parameters() if p.requires_grad]
        optimizer = torch.optim.SGD(
            params, lr=learning_rate, momentum=.9, weight_decay=5e-4)

        start = time.time()
        for epoch in range(self.epochs):

            for index, (img, targets) in enumerate(dataset):
                img =
                for x in targets:
                    targets[x] = targets[x].to(self.device)

                inputs = [img.T] # Transfrom from 1000, 1000, 3 -> 3, 1000, 1000 tensor
                corrects = [targets] # Training payload
                assert targets["boxes"].shape[1] == 4

                output = model(inputs, corrects)
                print(f"Success {index}")
                losses = sum(output.values())


                    f"Epoch {epoch}"
                    f"/{self.epochs} "
                    f"in {self._timestamp(start)} = {losses}"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "<mypath>/ENV/lib/python3.8/site-packages/torch/nn/modules/", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "<mypath>/ENV/lib/python3.8/site-packages/torchvision/models/detection/", line 71, in forward
    detections, detector_losses = self.roi_heads(features, proposals, images.image_sizes, targets)
  File "<mypath>/ENV/lib/python3.8/site-packages/torch/nn/modules/", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "<mypath>/ENV/lib/python3.8/site-packages/torchvision/models/detection/", line 748, in forward
    proposals, matched_idxs, labels, regression_targets = self.select_training_samples(proposals, targets)
  File "<mypath>/ENV/lib/python3.8/site-packages/torchvision/models/detection/", line 659, in select_training_samples
    sampled_inds = self.subsample(labels)
  File "<mypath>/ENV/lib/python3.8/site-packages/torchvision/models/detection/", line 610, in subsample
    sampled_pos_inds, sampled_neg_inds = self.fg_bg_sampler(labels)
  File "<mypath>/ENV/lib/python3.8/site-packages/torchvision/models/detection/", line 66, in __call__
    pos_idx_per_image = positive[perm1]
IndexError: index 833 is out of bounds for dimension 0 with size 480

I am completely clueless as the tensor shapes seem to obey the documentation and the indices causing the error seem random ranging usually between 10-500 over the maximum values of the indices. The indices causing the error are also non-deterministic changing randomly with constant inputs.

torch.Size([3, 1000, 1000])
torch.Size([479, 4])
torch.Size([479, 1])

Any help is greatly appreciated.

I have found the error.

The classes "labels" should be [N] not [N , 1]. I had been stuck here for multiple days and now feel extremely stupid.