yolov5 Error: RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place
Solution:
In model/yolo.py file
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)
Add with torch.no_grad(): as follows
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)
——> The root cause is to add:
with torch.no_grad():
Read More:
- [Solved] RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation
- Autograd error in Python: runtimeerror: grad can be implicitly created only for scalar outputs
- [Solved] Pytorch Tensor to numpy error: RuntimeError: Can‘t call numpy() on Tensor that requires grad.报错
- [Solved] Using summary to View network parameters Error: RuntimeError: Input type (torch.cuda.FloatTensor)
- pytorch DDP Accelerate Error: [W reducer.cpp:362] Warning: Grad strides do not match bucket view strides.
- [Solved] RuntimeError: No application found. Either work inside a view function or push an application contex
- RuntimeError: Invalid DISPLAY variable [How to Solve]
- [Solved] RuntimeError: DefaultCPUAllocator: not enough memory: you tried to allocate 1105920 bytes.
- RuntimeError: CUDA error: an illegal memory access was encountered
- [Solved] RuntimeError: Numpy is not available (Associated Torch or Tensorflow)
- Here is the difference and connection of Torch. View (), Transpose (), and Permute ()
- [Solved] Pytorch Error: RuntimeError: Error(s) in loading state_dict for Network: size mismatch
- [Solved] RuntimeError: one of the variables needed for gradient computation has been modified by an inplace
- [Solved] error: when using the property decorator in Python, an error occurs: typeerror: descriptor ‘setter’ requires a ‘property’ object but
- [Solved] RuntimeError: Error(s) in loading state_dict for BertForTokenClassification
- [Pytorch Error Solution] Pytorch distributed RuntimeError: Address already in use
- [Solved] yolo Error: IndexError: invalid index to scalar variable.
- [Solved] pytorch loss.backward() Error: RuntimeError: Function AddBackward0 returned an invalid gradient at index 1…
- [Solved] RuntimeError: Error(s) in loading state_dict for Net:
- [Solved] RuntimeError: function ALSQPlusBackward returned a gradient different than None at position 3, but t