当前位置: 首页 > 工具软件 > Viewa > 使用案例 >

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

王季萌
2023-12-01

跑yolov5的代码时,pytorch遇到<>RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation.的报错

在报错的这一块增加 with torch.no_grad():

  def _initialize_biases(self, cf=None):  # initialize biases into Detect(), cf is class frequency
      # https://arxiv.org/abs/1708.02002 section 3.3
      # 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)
 类似资料:

相关阅读

相关文章

相关问答