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

跑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)

你可能感兴趣的:(报错记录,深度框架,Pytorch,pytorch,深度学习,python)