PyTorch_YOLOv4 icon indicating copy to clipboard operation
PyTorch_YOLOv4 copied to clipboard

How to solve this error? RuntimeError: indices should be either on cpu or on the same device as the indexed tensor (cpu)

Open MheadHero opened this issue 2 years ago • 8 comments

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)

MheadHero avatar Feb 16 '23 16:02 MheadHero

i met same problem

nanhai78 avatar Feb 17 '23 06:02 nanhai78

i met same problem

lets try find some expert to help us out!

MheadHero avatar Feb 17 '23 07:02 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 comment indices.append((b, a, gj.clamp_(0, gain[3] - 1), gi.clamp_(0, gain[2] - 1))) # image, anchor, grid indices

This may solve the problem. Thank you.

MheadHero avatar Feb 20 '23 15:02 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 comment indices.append((b, a, gj.clamp_(0, gain[3] - 1), gi.clamp_(0, gain[2] - 1))) # image, anchor, grid indices

This may solve the problem. Thank you. thank you very much

nanhai78 avatar Feb 26 '23 09:02 nanhai78

Hi, could you please provide the pre-training weights for YOLOv4pacsp-x-mish? Thank you very much! @nanhai78 @MheadHero

lyl-9 avatar Apr 22 '23 03:04 lyl-9

sorry, i don't have pre-trainning weights for ms COCO

------------------ 原始邮件 ------------------ 发件人: @.>; 发送时间: 2023年4月22日(星期六) 中午11:16 收件人: @.>; 抄送: @.>; @.>; 主题: Re: [WongKinYiu/PyTorch_YOLOv4] How to solve this error? RuntimeError: indices should be either on cpu or on the same device as the indexed tensor (cpu) (Issue #424)

Hi, could you please provide the pre-training weights for YOLOv4pacsp-x-mish? Thank you very much! @nanhai78 @MheadHero

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

nanhai78 avatar Apr 22 '23 03:04 nanhai78

Do you have the pre-training weight of voc? @nanhai78

lyl-9 avatar Apr 22 '23 03:04 lyl-9

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 comment indices.append((b, a, gj.clamp_(0, gain[3] - 1), gi.clamp_(0, gain[2] - 1))) # image, anchor, grid indices

This may solve the problem. Thank you.

Thx a lot.

Qilingwan avatar Jul 31 '24 12:07 Qilingwan