Using FasterRCNN model for object detection ( copy_if failed to synchronize: device-side assert triggered)

username: juanmed
cooked: <p>Hi,</p>
<p>I am a beginner in Pytorch. I am currently trying to use the recently released (torchvision 0.3) models for object detection.</p>
<p>I have read the examples <a href="https://colab.research.google.com/github/pytorch/vision/blob/temp-tutorial/tutorials/torchvision_finetuning_instance_segmentation.ipynb#scrollTo=at-h4OWK0aoc" rel="nofollow noopener">here</a> and <a href="https://github.com/pytorch/vision/blob/v0.3.0/references/detection/train.py" rel="nofollow noopener">here</a>, 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.</p>
<p>The error I constantly get is:</p>
<pre><code class="lang-auto">/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 &gt;= 0 &amp;&amp; t &lt; 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 &gt;= 0 &amp;&amp; t &lt; 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 &gt;= 0 &amp;&amp; t &lt; 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 &gt;= 0 &amp;&amp; t &lt; n_classes` failed.
Traceback (most recent call last):
  File "detector_totales.py", line 222, in &lt;module&gt;
    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 &gt; 0).squeeze(1)
RuntimeError: copy_if failed to synchronize: device-side assert triggered
</code></pre>
<p>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:</p>
<pre><code class="lang-auto">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
</code></pre>
<p>and also like this:</p>
<pre><code class="lang-auto">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)
</code></pre>
<p>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.</p>
<p>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.</p>

username: juanmed
cooked: <p>I was able to get a little bit further. It seems the problem did was with the classes definition. Originally the classes did not start in 0 and were not sequential. I defined them from 0 and sequential and the previous error seems to be resolved.</p>
<p>However, after trying to run I got a CUDA out of memory error even in the on an NVIDIA RTX 24Gb GPU, which seems very strange since the model is based on mobilenet:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "detector_totales.py", line 230, in &lt;module&gt;
    main()
  File "detector_totales.py", line 166, in main
    model = model.to(device)
  File "/home/jfmy/Repositories/eleccionesGT2019/.tse/lib/python3.6/site-packages/torch/nn/modules/module.py", line 386, in to
    return self._apply(convert)
  File "/home/jfmy/Repositories/eleccionesGT2019/.tse/lib/python3.6/site-packages/torch/nn/modules/module.py", line 193, in _apply
    module._apply(fn)
  File "/home/jfmy/Repositories/eleccionesGT2019/.tse/lib/python3.6/site-packages/torch/nn/modules/module.py", line 193, in _apply
    module._apply(fn)
  File "/home/jfmy/Repositories/eleccionesGT2019/.tse/lib/python3.6/site-packages/torch/nn/modules/module.py", line 193, in _apply
    module._apply(fn)
  File "/home/jfmy/Repositories/eleccionesGT2019/.tse/lib/python3.6/site-packages/torch/nn/modules/module.py", line 199, in _apply
    param.data = fn(param.data)
  File "/home/jfmy/Repositories/eleccionesGT2019/.tse/lib/python3.6/site-packages/torch/nn/modules/module.py", line 384, in convert
    return t.to(device, dtype if t.is_floating_point() else None, non_blocking)
RuntimeError: CUDA out of memory. Tried to allocate 246.00 MiB (GPU 0; 23.62 GiB total capacity; 65.31 MiB already
allocated; 60.75 MiB free; 20.69 MiB cached)
</code></pre>
<p>After trying to run on CPU, the model does start training and there is some output but a new error, which I do not understand, appears:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "detector_totales.py", line 230, in &lt;module&gt;
    main()
  File "detector_totales.py", line 196, in main
    evaluate(model, test_loader, device=device)
  File "/home/fer/git_clone/eleccionesGT2019/.tse/lib/python3.5/site-packages/torch/autograd/grad_mode.py", line 43, in decorate_no_grad
    return func(*args, **kwargs)
  File "/home/fer/git_clone/eleccionesGT2019/engine.py", line 78, in evaluate
    coco = get_coco_api_from_dataset(data_loader.dataset)
  File "/home/fer/git_clone/eleccionesGT2019/coco_utils.py", line 205, in get_coco_api_from_dataset
    return convert_to_coco_api(dataset)
  File "/home/fer/git_clone/eleccionesGT2019/coco_utils.py", line 155, in convert_to_coco_api
    image_id = targets["image_id"].item()
KeyError: 'image_id'
</code></pre>
<p>Any help on both errors will be appreciated.</p>

username: juanmed
cooked: <p>I was able to solve the 2nd issue related to a key ‘image_id’ not available. It was my error and I noticed after double reading the example <a href="https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html" rel="nofollow noopener">here</a>. I only added the ‘boxes’ and ‘labels’ keys to the target dictionary but the keys ‘image_id’, ‘area’ and ‘iscrowd’ are also necessary. I created them by following the same example.</p>
<p>Any help on the CUDA Out of memory error will be welcomed.</p>

username: juanmed
cooked: <p>After running  training  for 100 epochs, all IOU results are 0.0 and -1.0 which seems quite strange. I’ve got a learning rate = 0.005, momentum = 0.5 and weight_decay = 0.005, which, after reading some posts related to such results (AP = 0.0) might solve such results.  Could anything in the model definition be leading to these results? Any comments will be appreciated.</p>
<pre><code class="lang-auto">Test:  [0/1]  eta: 0:00:27  model_time: 26.7939 (26.7939)  evaluator_time: 0.0214 (0.0214)  time: 27.9693  data: 1.1538  max mem: 0
Test: Total time: 0:00:28 (28.1102 s / it)
Averaged stats: model_time: 26.7939 (26.7939)  evaluator_time: 0.0214 (0.0214)
Accumulating evaluation results...
DONE (t=0.06s).
IoU metric: bbox
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.000
 Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.000
 Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.000
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.000
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.000
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.000
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.000
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.000
</code></pre>

username: juanmed
cooked: <p>I think I might have some clues on what is the problem related to the CUDA Out of Memory error. My dataset has several bounding boxes (&gt;20)  per image and according to <a href="https://github.com/facebookresearch/maskrcnn-benchmark/issues/120" rel="nofollow noopener">this</a> and<a href="https://github.com/facebookresearch/maskrcnn-benchmark/issues/18" rel="nofollow noopener">this</a>, calculation of IOU in the GPU is memory expensive.</p>
<p>In those issues, they suggest reducing batch size. So I though my batch size of 2 was low enough, but after I changed it to 1, the training process did started and has not stopped for 70 epochs. I wanted to implement the<a href="https://github.com/facebookresearch/maskrcnn-benchmark/issues/120#issuecomment-438674823" rel="nofollow noopener">suggestion in the thread</a> on moving IOU calculation to CPU (which can severely slow down training).</p>
<p>Do anyone knows where the IOU calculation code is performed for the detection models? Any comments will be appreciated.</p>

username: juanmed
cooked: <p>I was able to overcome the issue with CUDA Out of memory error. I did not solve it since I have not yet tried moving IOU calculation to CPU but at least now I can train with batch_size &gt; 1.</p>
<p>In the end I simple reduced the number of anchors created from 5 to 3, and reduced the size of the input images. This allowed me to increase the batch size from 1 to 8.</p>
<p>While doing this, I needed to resize both input images and targets (bounding box, area). Luckily, the detection models released with torchvision 0.3 already include resizing transforms that take care of both images and targets. Please take a look at <a href="https://github.com/pytorch/vision/issues/329#issuecomment-504975409" rel="nofollow noopener">this thread</a>.</p>
<p>I will close this issue now.</p>

username: Pcamellon
cooked: <aside class="quote no-group" data-username="juanmed" data-post="1" data-topic="48656">
<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/j/5f8ce5/48.png" class="avatar"> juanmed:</div>
<blockquote>
<p>I only want to do object detection</p>
</blockquote>
</aside>
<p>Hi! The same for me. Can you share your code or point me to a good tutorial?</p>