问题:
Traceback (most recent call last):
File “train.py”, line 544, in <module>
train(hyp, opt, device, tb_writer)
File “train.py”, line 305, in train
loss, loss_items = compute_loss(pred, targets.to(device)) # loss scaled by batch_size
File “/home/**/YOLOv5-Lite-master/utils/loss.py”, line 122, in call
b = b.long()
UnboundLocalError: local variable ‘b’ referenced before assignment
(1)小白训练一直出现报错,一开始是utils里面的datasets.py general.py里面的数据类型和版本对应不上,把这两个文件里面的np.int修改成np.int_
例如:bi = np.floor(np.arange(n) / batch_size).astype(np.int)修改成
bi = np.floor(np.arange(n) / batch_size).astype(np.int_)
(2)然后再次报错出现了loss.py中的数据类型问题,修改成红色部分
# Define
b, c = t[:, :2].long().T # image, class
gxy = t[:, 2:4] # grid xy
gwh = t[:, 4:6] # grid wh
gij = (gxy - offsets).to(torch.int64)
gi, gj = gij.T # grid xy indices
# Append
a = t[:, 6].long() # anchor indices
indices.append((b, a, gj.to(torch.int64).clamp(0, gain[3] - 1), gi.to(torch.int64).clamp(0, gain[2] - 1))) # image, anchor, grid indices
tbox.append(torch.cat((gxy - gij, gwh), 1)) # box
anch.append(anchors[a]) # anchors
tcls.append(c) # class
(3)最后出现了一个报错我无法解决,以我这种小白就在网上各种找资料,最后修改了
Traceback (most recent call last):
File “train.py”, line 544, in <module>
train(hyp, opt, device, tb_writer)
File “train.py”, line 305, in train
loss, loss_items = compute_loss(pred, targets.to(device)) # loss scaled by batch_size
File “/home/**/YOLOv5-Lite-master/utils/loss.py”, line 122, in call
b = b.long()
UnboundLocalError: local variable ‘b’ referenced before assignment
把loss.py中的这一行
gain = torch.ones(7, device=targets.device) 修改成
gain = torch.ones(7, device=targets.device).long()
问题解决是转载https://blog.csdn.net/weixin_41012767/article/details/125779972
虽然我也不太懂为什么是这么修改,但是最后真的跑通了,感谢大佬!