Pytorch-yolov3: a batch of background images causes error during calculating cls loss

Created on 11 Apr 2019  ·  7Comments  ·  Source: eriklindernoren/PyTorch-YOLOv3

Got error if batch images don't contain any ground truth object. (all of them are background.)

mask is an empty tensor, which leads to empty tcls and torch.argmax error.

In models.py
202 loss_cls = (1 / nB) * self.ce_loss(pred_cls[mask], torch.argmax(tcls[mask], 1))

==========================
Traceback (most recent call last):
File "/pytroch-yolo/yolo/train/train.py", line 71, in
loss = model(images, targets)
File "/usr/local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in __call__
result = self.forward(input, *kwargs)
File "/pytroch-yolo/PyTorch-YOLOv3/models.py", line 259, in forward
x, losses = module0
File "/usr/local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in __call__
result = self.forward(
input, **kwargs)
File "/pytroch-yolo/PyTorch-YOLOv3/models.py", line 202, in forward
loss_cls = (1 / nB) * self.ce_loss(pred_cls[mask], torch.argmax(tcls[mask], 1))
File "/usr/local/lib/python3.6/site-packages/torch/functional.py", line 544, in argmax
return torch._argmax(input, dim, keepdim)
RuntimeError: cannot perform reduction function max on tensor with no elements because the operation does not have an identity

Most helpful comment

File "/home/user112/PyTorch-YOLOv3/utils/utils.py", line 294, in build_targets
best_ious, best_n = ious.max(0)
RuntimeError: cannot perform reduction function max on tensor with no elements because the operation does not have an identity

similar exception with empty images, is there anyone who solved this?

All 7 comments

@IreneLian Could you pull the latest version and try now?

Still gives the same error. Is there any way to represent training images with no labels( bounding box that is just background)?

Still gives the same error. Is there any way to represent training images with no labels( bounding box that is just background)?

When I added some images without objects and with empty label files, I got this exception, too.
What's the correct way to add empty files to the custom training set?

File "/home/user112/PyTorch-YOLOv3/utils/utils.py", line 294, in build_targets
best_ious, best_n = ious.max(0)
RuntimeError: cannot perform reduction function max on tensor with no elements because the operation does not have an identity

similar exception with empty images, is there anyone who solved this?

You might have to write your own implementation that uses the negative samples. Conventionally, object detection is trained on positive samples only

Duplicate of #339

I've fixed this
解决办法是:如果你的错误是在 best_ious, best_n = ious.max(0) 中出现的,可以考虑将 augmentations.py 文件夹中的 DefaultAug(),这一行注释掉,因为自定义的 iaa.Affine 中 translate_percent 参数会导致输出的bb_targets 变为空值

Was this page helpful?
0 / 5 - 0 ratings