学无先后,达者为师

网站首页 编程语言 正文

RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place

作者:别出BUG求求了 更新时间: 2023-11-17 编程语言

yolov5报错:RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place

解决办法:
在model/yolo.py文件

        for mi, s in zip(m.m, m.stride):  # from
            b = mi.bias.view(m.na, -1)  # conv.bias(255) to (3,85)
            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)

添加with torch.no_grad():如下

        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)

——> 根本原因就是在报错的语句前添加:

with torch.no_grad()

原文链接:https://blog.csdn.net/weixin_39589455/article/details/126808770

  • 上一篇:没有了
  • 下一篇:没有了
栏目分类
最近更新