yolov5报错:RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place
程序员文章站
2022-03-04 14:24:21
...
在执行下面代码时
python train.py --img 640 --batch 16 --epochs 5 --data ./data/coco128.yaml --cfg ./models/yolov5s.yaml --weights ''
我遇到了此报错
Traceback (most recent call last):
File "train.py", line 456, in <module>
train(hyp, opt, device, tb_writer)
File "train.py", line 77, in train
model = Model(opt.cfg, ch=3, nc=nc).to(device) # create
File "C:\Users\it possible\Documents\yolov5Project\yolov5-master\models\yolo.py", line 87, in __init__
self._initialize_biases() # only run once
File "C:\Users\it possible\Documents\yolov5Project\yolov5-master\models\yolo.py", line 145, in _initialize_biases
b[:, 4] += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image)
RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation.
解决方法
找到在File "C:\Users\it possible\Documents\yolov5Project\yolov5-master\models\yolo.py"的line 145
将代码修改为:
def _initialize_biases(self, cf=None): # initialize biases into Detect(), cf is class frequency
# cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1.
m = self.model[-1] # Detect() module
for mi, s in zip(m.m, m.stride): # from
b = mi.bias.view(m.na, -1) # conv.bias(255) to (3,85)
with torch.no_grad():
b[:, 4] += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image)
b[:, 5:] += math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum()) # cls
mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True)
再次测试,bug修复
下一篇: 将一个文件夹的内容拷贝到另一个文件夹中
推荐阅读
-
RuntimeError: a leaf Variable that requires grad has been used in an in-place operation
-
RuntimeError: a leaf Variable that requires grad has been used in an in-place operation.
-
RuntimeError: a leaf Variable that requires grad has been used in an in-place operation.a += torch.o
-
yolov5报错:RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place
-
RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation.