-
-
Notifications
You must be signed in to change notification settings - Fork 16.5k
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
AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor' #6948
Comments
👋 Hello @JakobStadlhuber, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution. If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you. If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available. For business inquiries or professional support requests please visit https://ultralytics.com or email [email protected]. RequirementsPython>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started: git clone https://github.com/ultralytics/yolov5 # clone
cd yolov5
pip install -r requirements.txt # install EnvironmentsYOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):
StatusIf this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), validation (val.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit. |
@JakobStadlhuber good news 😃! This is fixed ✅ in yesterday's PyTorch 1.11.0 compatibility PR #6932 To receive this update:
|
@JakobStadlhuber BTW you can see in your post that your environment is not current master, it was last updated at the end of January: YOLOv5 🚀 2022-1-31 torch 1.11.0+cu102 CPU |
Yes, I also encountered the same problem, but do not know what the reason is, my cuda is version 11.5, I followed the link on the official website to download the cuda 11.5 version recommended pytorch, but reported the error, and then I downloaded the cuda 11.3 version recommended, but the same error |
@yourdaddaddadd I've already posted the solution in #6948 (comment) |
I've encountered the same issue, but on a trained model that I trained several months ago. What's the best way to deal with this? |
@stephenfujiwara I would retrain using latest PyTorch and YOLOv5. |
I was going to do that. Actually |
When will the pip release be updated with this fix please? |
detect: weights=['runs/train/exp11/weights/last.pt'], source=data/images/, data=data\coco128.yaml, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs\detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False Fusing layers... Got the same error any help will be appreocated |
@Arsalan66 👋 hi, thanks for letting us know about this possible problem with YOLOv5 🚀. It looks like you are using the latest version of torch, so the error might be due to an out of date model. We've created a few short guidelines below to help users provide what we need in order to start investigating a possible problem. How to create a Minimal, Reproducible ExampleWhen asking a question, people will be better able to provide help if you provide code that they can easily understand and use to reproduce the problem. This is referred to by community members as creating a minimum reproducible example. Your code that reproduces the problem should be:
For Ultralytics to provide assistance your code should also be:
If you believe your problem meets all the above criteria, please close this issue and raise a new one using the 🐛 Bug Report template with a minimum reproducible example to help us better understand and diagnose your problem. Thank you! 😃 |
@Arsalan66 As a work around you can edit your upsampling.py file: "E:\condaaa\lib\site-packages\torch\nn\modules\upsampling.py" If you comment out the "recompute_scale_factor=self.recompute_scale_factor" parameter everything seems to work fine. |
thank you so much let me retry and get back to you. |
For anyone looking for the solution , modify your upsampling.py in E:\condaaa\Lib\site-packages\torch\nn\modules (conda files) , according to the function above. |
Thanks it works |
i fixed with #6948 (comment), thanks. |
1 similar comment
i fixed with #6948 (comment), thanks. |
I used an earlier version of PyTorch and torchvision - probably the worst solution, but for those who need a quick and dirty solution without having to upgrade the repo:
|
I used the below pytorch and torchvision version and it is working well. This one is not working for me #6948 (comment).
|
how can i fix this in colab? |
try the solution I or @dsabarinathan have posted |
… adjust for python 3.8. See: ultralytics/yolov5#6948
Greet works! |
it's work. Thank's you very much |
I solved this problem without modifying
I hope this will help someone. |
It did, thank you!!! |
Hi, can anyone explain why this happens? I mean, PyTorch provides that line on purpose, right? |
can you please tell me where should we add that? |
File "C:\Users\chandu\AppData\Roaming\Python\Python310\site-packages\torch\nn\modules\module.py", line 1185, in getattr
AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor' |
where we need to keep this? |
maybe models/yolo.py, it works. def _forward_once(self, x, profile=False, visualize=False):
y, dt = [], [] # outputs
for m in self.model:
if m.f != -1: # if not from previous layer
x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f] # from earlier layers
if profile:
self._profile_one_layer(m, x, dt)
if isinstance(m, nn.Upsample):
m.recompute_scale_factor = None
x = m(x) # run
y.append(x if m.i in self.save else None) # save output
if visualize:
feature_visualization(x, m.type, m.i, save_dir=visualize)
return x |
model Summary: 224 layers, 7266973 parameters, 0 gradients, 0.7 GFLOPs
after modifying models/yolo.py again I getting error like this?? |
@NaragintiChanduPriya maybe like this example # load yolo model
model = torch.hub.load("ultralytics/yolov5", "yolov5s")
# model = Model(opt.cfg).to(device)
# error fix code
for m in model.modules():
if isinstance(m, nn.Upsample):
m.recompute_scale_factor = None
# model inference
results = model(img) or you can put fix code in But your last error doesn't seem to be related to the upsample issue. |
@NaragintiChanduPriya the error message One possible reason for this could be that the number of classes predicted by your model is different from the number of classes for which you have defined names. Make sure that the length of your import torch
model = torch.hub.load("ultralytics/yolov5", "yolov5s")
stride = model.model[-1].stride
num_classes = int(stride ** 2 * model.model[-1].out_channels)
print(num_classes) This will print the number of classes predicted by your model based on the configuration of the last layer. Once you know how many classes your model is predicting, make sure that your I hope this helps! Let me know if you have any other questions. |
thx, it work |
@alps-sun hi and glad to hear that the suggested solution worked for you! If you have any other issues or questions in the future, please don't hesitate to ask. Have a great day! |
modify like this to models/yolo.py def _forward_once(self, x, profile=False, visualize=False):
y, dt = [], [] # outputs
for m in self.model:
if m.f != -1: # if not from previous layer
x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f] # from earlier layers
if profile:
self._profile_one_layer(m, x, dt)
#this is where u have to update
if isinstance(m, nn.Upsample):
m.recompute_scale_factor = None
####
x = m(x) # run
y.append(x if m.i in self.save else None) # save output
if visualize:
feature_visualization(x, m.type, m.i, save_dir=visualize)
return x |
@JAYANTH-MOHAN Thanks for sharing the modification. It looks like you've added the suggested code snippet in the right place within the If you have any more questions or need further assistance, feel free to ask. Good luck with your YOLOv5 project! |
best method |
@For0 While directly modifying the Instead, it's better to handle the issue within your own codebase. If you encounter the Remember, if you're modifying the library files, you should keep track of these changes and be aware that they might be overwritten when you update PyTorch. It's always best to keep your environment as standard as possible to avoid compatibility issues. If you need further assistance, feel free to ask. Happy coding! |
Thank you. This solution works for me. |
@zohaibmohammad i'm glad to hear the solution worked for you! Just a friendly reminder, while modifying library files directly can be a quick fix, it's generally safer to adjust your code to avoid compatibility issues in the future or consider alternative solutions within your project scope. Happy coding! 😊👍 |
Search before asking
YOLOv5 Component
Other
Bug
I just have run
pip install -r yolov5/requirements.txt
again and now I get this error when running my main class.Fusing layers... Model Summary: 232 layers, 7278882 parameters, 0 gradients Adding AutoShape... Traceback (most recent call last): File "/mnt/space/work/thesis/main.py", line 177, in <module> results = model(resized_img, size=img_size) File "/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py", line 1110, in _call_impl return forward_call(*input, **kwargs) File "/usr/local/lib/python3.8/dist-packages/torch/autograd/grad_mode.py", line 27, in decorate_context return func(*args, **kwargs) File "/root/.cache/torch/hub/ultralytics_yolov5_master/models/common.py", line 540, in forward y = self.model(x, augment, profile) # forward File "/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py", line 1110, in _call_impl return forward_call(*input, **kwargs) File "/root/.cache/torch/hub/ultralytics_yolov5_master/models/common.py", line 399, in forward y = self.model(im) if self.jit else self.model(im, augment=augment, visualize=visualize) File "/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py", line 1110, in _call_impl return forward_call(*input, **kwargs) File "/root/.cache/torch/hub/ultralytics_yolov5_master/models/yolo.py", line 126, in forward return self._forward_once(x, profile, visualize) # single-scale inference, train File "/root/.cache/torch/hub/ultralytics_yolov5_master/models/yolo.py", line 149, in _forward_once x = m(x) # run File "/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py", line 1110, in _call_impl return forward_call(*input, **kwargs) File "/usr/local/lib/python3.8/dist-packages/torch/nn/modules/upsampling.py", line 154, in forward recompute_scale_factor=self.recompute_scale_factor) File "/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py", line 1185, in __getattr__ raise AttributeError("'{}' object has no attribute '{}'".format( AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor'
Environment
YOLOv5 🚀 2022-1-31 torch 1.11.0+cu102 CPU
Minimal Reproducible Example
Clone current master (11.03.2022)
pip install -r yolov5/requirements.txt
Additional
No response
Are you willing to submit a PR?
The text was updated successfully, but these errors were encountered: