Target 2 is out of bounds | object detection finetuning tutorial FastRCNNPredictor

smile_0: <p>Hello all,</p>
<p>greetings! I would like to implement just FastRCNNPredictor part in the object detection finetuning tutorial. I made the change in <code>__getitem__</code> as <code>labels = torch.as_tensor(obj_ids, dtype=torch.int64)</code> as per <a href="https://github.com/pytorch/tutorials/issues/960" class="inline-onebox" rel="noopener nofollow ugc">Mask-RCNN tutorial one-line change suggestion · Issue #960 · pytorch/tutorials · GitHub</a></p>
<p>After I run the code initially I got error at <code>train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)</code> as,</p>
<blockquote>
<p>CUDA error: device-side assert triggered</p>
</blockquote>
<p>… which I overcome by simply changing device from cuda to cpu.<br>
Now I face error at the same line of code as, `</p>
<blockquote>
<p>IndexError: Target 2 is out of bounds.</p>
</blockquote>
<p>The changes I made in the tutorial code so far are, <code>labels</code> (as I mentioned above), I removed mask part from <code>get_model_instance_segmentation</code> and I also changed all <code>batch_size=1</code> and <code>num_workers=0</code>.</p>
<p>The traces of the error are as follows,</p>
<pre><code class="lang-auto">File "/../py.py", line **, in &lt;module&gt;
    train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)
  File "/../engine.py", line 31, in train_one_epoch
    loss_dict = model(images, targets)
  File "/../torch/nn/modules/module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "/../torchvision/models/detection/generalized_rcnn.py", line 99, in forward
    detections, detector_losses = self.roi_heads(features, proposals, images.image_sizes, targets)
  File "/../torch/nn/modules/module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "/../torchvision/models/detection/roi_heads.py", line 759, in forward
    loss_classifier, loss_box_reg = fastrcnn_loss(class_logits, box_regression, labels, regression_targets)
  File "/../torchvision/models/detection/roi_heads.py", line 32, in fastrcnn_loss
    classification_loss = F.cross_entropy(class_logits, labels)
  File "/../torch/nn/functional.py", line 2996, in cross_entropy
    return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)
IndexError: Target 2 is out of bounds.
</code></pre>
<p>I would like to know, did anyone also face the same problem while executing the tutorial. It would be very helpful if someone could direct me to overcome this problem in the same. thank you in advance…</p>

ptrblck: <aside class="quote no-group" data-username="smile_0" data-post="1" data-topic="160980">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/8baadc/48.png" class="avatar"> smile_0:</div>
<blockquote>
<p>I removed mask part from <code>get_model_instance_segmentation</code></p>
</blockquote>
</aside>
<p>Could you describe these changes a bit more, please?<br>
The error (both on the GPU via the <code>assert</code> and on the CPU via the indexing error) are caused by a target tensor, which contains class indices which are out of bounds.<br>
<code>nn.CrossEntropyLoss</code> expects a model output in the shape <code>[batch_size, nb_classes, *]</code> and a target in the shape <code>[batch_size, *]</code> containing class indices in the range <code>[0, nb_classes-1]</code>. Since your target contains the class index <code>2</code> it would mean that your model should at least output logits for 3 classes while it seem to output less logits.</p>

smile_0: <p>Hello, thank you for quick reply.<br>
The changes I made are as follows,</p>
<pre><code class="lang-auto">import torchvision
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
#from torchvision.models.detection.mask_rcnn import MaskRCNNPredictor

def get_model_instance_segmentation(num_classes):
    # load an instance segmentation model pre-trained on COCO
    #model = torchvision.models.detection.maskrcnn_resnet50_fpn(weights="DEFAULT")
    model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights="DEFAULT")

    # get number of input features for the classifier
    in_features = model.roi_heads.box_predictor.cls_score.in_features
    # replace the pre-trained head with a new one
    model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)

    # now get the number of input features for the mask classifier
    #in_features_mask = model.roi_heads.mask_predictor.conv5_mask.in_channels
    #hidden_layer = 256
    # and replace the mask predictor with a new one
    #model.roi_heads.mask_predictor = MaskRCNNPredictor(in_features_mask, hidden_layer, num_classes)
    return model
</code></pre>
<p>So far I tried to make only relevant changes, which I could think of and find suitable. It would be great if someone could point out where in the code the error might come from. thank you in advance…</p>

ptrblck: <p>Your code works fine if the previously mentioned class index ranges are used:</p>
<pre><code class="lang-python"># For training
images, boxes = torch.rand(4, 3, 600, 1200), torch.rand(4, 11, 4)
boxes[:, :, 2:4] = boxes[:, :, 0:2] + boxes[:, :, 2:4]
labels = torch.randint(0, 3, (4, 11))
images = list(image for image in images)
targets = []
for i in range(len(images)):
    d = {}
    d['boxes'] = boxes[i]
    d['labels'] = labels[i]
    targets.append(d)
output = model(images, targets)
</code></pre>
<p>Did you check the target class indices and compared them to the logit output shape of your model?</p>