0

This is WongKinYiu / PyTorch_YOLOv4 problem. I found many solutions of this problem in YOLOv7, and no solution in YOLOv4. Since, the community here is more active. Thus, I try to ask here also.

This is the link to WongKinYiu loss.py

Traceback (most recent call last):
File "/content/PyTorch_YOLOv4/train.py", line 537, in
train(hyp, opt, device, tb_writer, wandb)
File "/content/PyTorch_YOLOv4/train.py", line 288, in train
loss, loss_items = compute_loss(pred, targets.to(device), model) # loss scaled by batch_size
File "/content/PyTorch_YOLOv4/utils/loss.py", line 69, in compute_loss
tcls, tbox, indices, anchors = build_targets(p, targets, model) # targets
File "/content/PyTorch_YOLOv4/utils/loss.py", line 151, in build_targets
a, t = at[j], t.repeat(na, 1, 1)[j] # filter
RuntimeError: indices should be either on cpu or on the same device as the indexed tensor (cpu)
halfer
  • 19,824
  • 17
  • 99
  • 186
MheadHero
  • 103
  • 1
  • 1
  • 7
  • What I can see in this error message is that when you call `compute_loss()` function you explicitly send targets to device (whatever the device is) but when you call `build_targets()` you don't do the same. So this is how they may end up on different devices. – NotAName Feb 17 '23 at 05:45

1 Answers1

0

The Github issue has a solution from MheadHero:

I have found a solution.

Modify your loss.py

add at = at.to(targets.device) above a, t = at[j], t.repeat(na, 1, 1)[j] # filter

Then,

uncomment indices.append((b, a, gj, gi)) # image, anchor, grid indices and

comment indices.append((b, a, gj.clamp_(0, gain[3] - 1), gi.clamp_(0, gain[2] - 1))) # image, anchor, grid indices

Dragon
  • 2,017
  • 1
  • 19
  • 35
mia
  • 1
  • 1