Hi,
I am a beginner in Pytorch. I am currently trying to use the recently released (torchvision 0.3) models for object detection.
I have read the examples here 6 and here 6, but I am unable to make it work. Specifically, the example do segmentation, and I only want to do object detection. I think that is the reason why I cannot adequately adapt the examples to my script.
The error I constantly get is:
/pytorch/aten/src/THCUNN/ClassNLLCriterion.cu:105: void cunn_ClassNLLCriterion_updateOutput_kernel(Dtype *, Dtype *, Dtype *, long *, Dtype *, int, int, int, int, long) [with Dtype = float, Acctype = float]: block: [0,0,0], thread: [28,0,0] Assertion `t >= 0 && t < n_classes` failed.
/pytorch/aten/src/THCUNN/ClassNLLCriterion.cu:105: void cunn_ClassNLLCriterion_updateOutput_kernel(Dtype *, Dtype *, Dtype *, long *, Dtype *, int, int, int, int, long) [with Dtype = float, Acctype = float]: block: [0,0,0], thread: [29,0,0] Assertion `t >= 0 && t < n_classes` failed.
/pytorch/aten/src/THCUNN/ClassNLLCriterion.cu:105: void cunn_ClassNLLCriterion_updateOutput_kernel(Dtype *, Dtype *, Dtype *, long *, Dtype *, int, int, int, int, long) [with Dtype = float, Acctype = float]: block: [0,0,0], thread: [30,0,0] Assertion `t >= 0 && t < n_classes` failed.
/pytorch/aten/src/THCUNN/ClassNLLCriterion.cu:105: void cunn_ClassNLLCriterion_updateOutput_kernel(Dtype *, Dtype *, Dtype *, long *, Dtype *, int, int, int, int, long) [with Dtype = float, Acctype = float]: block: [0,0,0], thread: [31,0,0] Assertion `t >= 0 && t < n_classes` failed.
Traceback (most recent call last):
  File "detector_totales.py", line 222, in <module>
    main()
  File "detector_totales.py", line 186, in main
    train_one_epoch(model, optimizer, train_loader, device, epoch, print_freq=1)
  File "/home/fer/git_clone/eleccionesGT2019/engine.py", line 30, in train_one_epoch
    loss_dict = model(images, targets)
  File "/home/fer/git_clone/eleccionesGT2019/.tse/lib/python3.5/site-packages/torch/nn/modules/module.py", line 493, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/fer/git_clone/eleccionesGT2019/.tse/lib/python3.5/site-packages/torchvision/models/detection/generalized_rcnn.py", line 52, in forward
    detections, detector_losses = self.roi_heads(features, proposals, images.image_sizes, targets)
  File "/home/fer/git_clone/eleccionesGT2019/.tse/lib/python3.5/site-packages/torch/nn/modules/module.py", line 493, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/fer/git_clone/eleccionesGT2019/.tse/lib/python3.5/site-packages/torchvision/models/detection/roi_heads.py", line 534, in forward
    class_logits, box_regression, labels, regression_targets)
  File "/home/fer/git_clone/eleccionesGT2019/.tse/lib/python3.5/site-packages/torchvision/models/detection/roi_heads.py", line 34, in fastrcnn_loss
    sampled_pos_inds_subset = torch.nonzero(labels > 0).squeeze(1)
RuntimeError: copy_if failed to synchronize: device-side assert triggered

I have tried creating the network following the documentation in h**ps://github.com/pytorch/vision/blob/master/torchvision/models/detection/faster_rcnn.py like this:
def get_mobilenet_model(num_classes):
    # Seguir ejemplo en 

    backbone = torchvision.models.mobilenet_v2(pretrained=True).features
    backbone.out_channels = 1280

    anchor_generator = AnchorGenerator(sizes=((32, 64, 128, 256, 512),), aspect_ratios=((0.5, 1.0, 2.0),))
    roi_pooler = torchvision.ops.MultiScaleRoIAlign(featmap_names=[0], output_size=7,sampling_ratio=2)

    model = FasterRCNN(backbone, num_classes=num_classes,rpn_anchor_generator=anchor_generator, box_roi_pool=roi_pooler)
    return model

and also like this:
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True) 
in_features = model.roi_heads.box_predictor.cls_score.in_features
model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)

I have in total 31 classes, but they do not start in 0 and are not sequential. I am not sure if this can be an issue.
Do anyone has any idea on what the issue could be? The full script is available in (h**ps://github.com/juanmed/eleccionesGT2019/blob/master/detector_totales.py).  I will appreciate any help.