Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Predict 报错ValueError: Cannot assign non-leaf Tensor to parameter 'weight'. Model parameters must be created explicitly. To express 'weight' as a function of another Tensor, compute the value in the forward() method. #58

Open
KeciaHH opened this issue Apr 15, 2024 · 2 comments

Comments

@KeciaHH
Copy link

KeciaHH commented Apr 15, 2024

File "/home/hhee/Desktop/yolov8-pytorch/predict.py", line 91, in
r_image = yolo.detect_image(image, crop = crop, count=count)
File "/home/hhee/Desktop/yolov8-pytorch/yolo.py", line 144, in detect_image
outputs = self.net(images)
File "/home/hhee/anaconda3/envs/kws/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
return forward_call(*args, **kwargs)
File "/home/hhee/anaconda3/envs/kws/lib/python3.9/site-packages/torch/nn/parallel/data_parallel.py", line 170, in forward
replicas = self.replicate(self.module, self.device_ids[:len(inputs)])
File "/home/hhee/anaconda3/envs/kws/lib/python3.9/site-packages/torch/nn/parallel/data_parallel.py", line 175, in replicate
return replicate(module, device_ids, not torch.is_grad_enabled())
File "/home/hhee/anaconda3/envs/kws/lib/python3.9/site-packages/torch/nn/parallel/replicate.py", line 148, in replicate
setattr(replica, key, param)
File "/home/hhee/anaconda3/envs/kws/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1632, in setattr
self.register_parameter(name, value)
File "/home/hhee/anaconda3/envs/kws/lib/python3.9/site-packages/torch/nn/modules/module.py", line 573, in register_parameter
raise ValueError(
ValueError: Cannot assign non-leaf Tensor to parameter 'weight'. Model parameters must be created explicitly. To express 'weight' as a function of another Tensor, compute the value in the forward() method.
加载权重成功了,infer报错

@ailongxia
Copy link

将nets\yolo.py下fusedconv.weight.copy_(torch.mm(w_bn, w_conv).view(fusedconv.weight.shape))改成fusedconv.weight.data = torch.mm(w_bn, w_conv).view(fusedconv.weight.shape);fusedconv.bias.copy_(torch.mm(w_bn, b_conv.reshape(-1, 1)).reshape(-1) + b_bn)改成fusedconv.bias.data = torch.mm(w_bn, b_conv.reshape(-1, 1)).reshape(-1) + b_bn即可

@Weichy9218
Copy link

Weichy9218 commented Sep 26, 2024

Thanks a lot. I used ailongxia's solution to solve the same issue. Could you tell me the reason why it can deal with it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants