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

I want to get feature map,but the --visualize argument have some problem 'AttributeError: 'tuple' object has no attribute 'shape'' #12457

Closed
2 tasks done
tiancailin opened this issue Dec 2, 2023 · 5 comments
Labels
bug Something isn't working Stale

Comments

@tiancailin
Copy link
Contributor

tiancailin commented Dec 2, 2023

Search before asking

  • I have searched the YOLOv5 issues and found no similar bug report.

YOLOv5 Component

Detection

Bug

f:\myWorkDir\yolov5>python segment\predict.py --weights 'myFinished .pt' --source 'myTestImgDir' --data '.\data\myOwnData.yaml' --view-img --visualize --save-txt

segment\predict: weights=['myFinished .pt'], source='myTestImgDir', data='.\data\myOwnData.yaml', imgsz=[640, 640], other default settings , and etc...

YOLOv5 v7.0-218-g9e97ac3 Python-3.10.9 torch-2.0.1+cu118 CUDA:0 (NVIDIA GeForce RTX 3080 Ti, 12287MiB)

Fusing layers...
Model summary: 165 layers, 1879750 parameters, 0 gradients, 6.7 GFLOPs
Saving runs\predict-seg\exp\13_03\stage0_Conv_features.png... (16/16) (<- look ,it works!)

....and continue worked 22 steps, could saved feature maps on disk...

Saving runs\predict-seg\exp\13_03\stage23_C3_features.png... (32/256) (<-problem after this step!)

Traceback (most recent call last):
File "myWorkDir\yolov5\segment\predict.py", line 285, in
main(opt)
File "myWorkDir\yolov5\segment\predict.py", line 280, in main
run(**vars(opt))
File "D:\Programs\Python\Python310\lib\site-packages\torch\utils_contextlib.py", line 115, in decorate_context
return func(*args, **kwargs)
File "myWorkDir\yolov5\segment\predict.py", line 132, in run
pred, proto = model(im, augment=augment, visualize=visualize)[:2]
File "D:\Programs\Python\Python310\lib\site-packages\torch\nn\modules\module.py", line 1501, in _call_impl
return forward_call(*args, **kwargs)
File "myWorkDir\yolov5\models\common.py", line 527, in forward
y = self.model(im, augment=augment, visualize=visualize) if augment or visualize else self.model(im)
File "D:\Programs\Python\Python310\lib\site-packages\torch\nn\modules\module.py", line 1501, in _call_impl
return forward_call(*args, **kwargs)
File "myWorkDir\yolov5\models\yolo.py", line 209, in forward
return self._forward_once(x, profile, visualize) # single-scale inference, train
File "myWorkDir\yolov5\models\yolo.py", line 124, in _forward_once
feature_visualization(x, m.type, m.i, save_dir=visualize)
File "myWorkDir\yolov5\utils\plots.py", line 63, in feature_visualization
batch, channels, height, width = x.shape # batch, channels, height, width
AttributeError: 'tuple' object has no attribute 'shape'

Environment

YOLOv5 v7.0-218-g9e97ac3
OS:Win10
Python:3.10.9
Pytorch:2.0.1+cu118(stable version)
GPU:3080Ti

Minimal Reproducible Example

Tried add print(x) and print(type(x)) after

batch, channels, height, width = x.shape # batch, channels, height, width

In the correct stetp
x =

tensor([[[[-0.13114, -0.17713, -0.19639,  ..., -0.20970, -0.22123, -0.21276],
          [-0.09691, -0.10795, -0.13889,  ..., -0.16266, -0.18692, -0.16942],
          [-0.10676, -0.07252, -0.09570,  ..., -0.15805, -0.17762, -0.16524],
          ...,          
          ...,
          [ 0.47142,  0.43590,  0.56596,  ...,  0.54877,  0.55914,  0.57092],
          [ 0.56170,  0.55984,  0.69695,  ...,  0.49471,  0.45835,  0.52159],
          [ 0.55527,  0.58349,  0.59248,  ...,  0.60307,  0.57910,  0.52247]]]], device='cuda:0')

and
x is a <class 'torch.Tensor'>

but in the incorrect step
x =

(tensor([[[ 7.34531e+00,  4.12756e+00,  1.39885e+01,  ...,  8.70913e-01,  5.32039e-01, -6.95609e-01],
         [ 1.27820e+01,  4.66258e+00,  2.52944e+01,  ...,  8.45581e-01,  4.91349e-01, -5.03686e-01],
         [ 1.66268e+01,  4.17587e+00,  2.91948e+01,  ...,  7.15675e-01,  6.96999e-01, -4.41090e-01],
         ...,
         [ 5.61341e+02,  4.30326e+02,  4.39551e+02,  ..., -1.56673e-01,  4.24248e-02, -4.17730e-01],
         [ 5.94560e+02,  4.29557e+02,  4.54697e+02,  ..., -9.76457e-02,  4.35937e-02, -3.96623e-01],
         [ 6.25151e+02,  4.30644e+02,  4.31965e+02,  ..., -1.92059e-01, -4.21485e-02, -3.72733e-01]]], device='cuda:0'),
 tensor([[[[ 1.18412e-01,  2.63690e-01,  3.27828e-01,  ...,  3.54180e-01,  3.29648e-01,  4.16207e-01],
          [ 2.20219e-01,  3.44874e-01,  4.20535e-01,  ...,  5.56718e-01,  5.52135e-01,  4.51106e-01],
          [ 3.57559e-01,  4.00981e-01,  4.53153e-01,  ...,  3.58718e-01,  3.89871e-01,  8.12342e-02],
          ...,
          [ 1.28279e-01,  2.99863e-01,  3.90491e-01,  ...,  7.77177e-02,  1.06266e-01,  1.06339e-01],
          [ 5.29424e-01,  7.73965e-01,  7.34390e-01,  ...,  5.42718e-01,  5.29335e-01,  3.64861e-01],
          [ 5.19088e-01,  6.43992e-01,  5.91782e-01,  ...,  5.98303e-01,  6.68231e-01,  5.50341e-01]],
           ...,
           [ 8.38901e-02, -1.04690e-01,  1.71522e-01,  ..., -1.56673e-01,  4.24248e-02, -4.17730e-01],
           [ 1.60364e-01, -1.52996e-01,  2.08947e-01,  ..., -9.76457e-02,  4.35937e-02, -3.96623e-01],
           [ 7.19955e-02, -8.48306e-02,  1.52580e-01,  ..., -1.92059e-01, -4.21485e-02, -3.72733e-01]]]]], device='cuda:0')])

and x is a <class 'tuple'>

looks like x changed from a single <class 'torch.Size'> to a tuple consisting of multiple <class 'torch.Size'> elements.

so will report AttributeError: 'tuple' object has no attribute 'shape'

looks like,this x is form this function's return?
https://github.com/ultralytics/yolov5/blob/3f02fdee1d8f1a6cf18a24be3438096466367d9f/models/yolo.py#L114C12-L114C12

Additional

I am not sure the problem came from yolo or pytorch 2.01,because I noticed #3902 , the --visualize should be worked fine after this issue
And I try to change weights file to official yolov5n-seg.pt,report the same report

Are you willing to submit a PR?

  • Yes I'd like to help by submitting a PR!
@tiancailin tiancailin added the bug Something isn't working label Dec 2, 2023
Copy link
Contributor

github-actions bot commented Dec 2, 2023

👋 Hello @tiancailin, 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 a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Requirements

Python>=3.8.0 with all requirements.txt installed including PyTorch>=1.8. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

YOLOv5 CI

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training, validation, inference, export and benchmarks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

Introducing YOLOv8 🚀

We're excited to announce the launch of our latest state-of-the-art (SOTA) object detection model for 2023 - YOLOv8 🚀!

Designed to be fast, accurate, and easy to use, YOLOv8 is an ideal choice for a wide range of object detection, image segmentation and image classification tasks. With YOLOv8, you'll be able to quickly and accurately detect objects in real-time, streamline your workflows, and achieve new levels of accuracy in your projects.

Check out our YOLOv8 Docs for details and get started with:

pip install ultralytics

@glenn-jocher
Copy link
Member

@tiancailin hi there! Thanks for reaching out and for the detailed report. It appears that the issue is related to the visualize argument causing a change of x from a single torch tensor to a tuple. We'll investigate this further and ensure a resolution for this behavior. Your feedback is invaluable, and we'll do our best to address this promptly. Feel free to follow the progress in the repository. Thank you for your patience and understanding!

tiancailin added a commit to tiancailin/yolov5_tclfork that referenced this issue Dec 2, 2023
When run 'python.exe segment/predict.py --visualize' will throw AttributeError: 'tuple' object has no attribute 'shape'
@tiancailin
Copy link
Contributor Author

@glenn-jocher
I found that this is a minor issue, and I overreacted with this problem.
In fact, it only need a simple modification to the if statement
from if 'Detect' not in module_type: https://github.com/ultralytics/yolov5/blob/3f02fdee1d8f1a6cf18a24be3438096466367d9f/utils/plots.py#L62C14-L62C14
to if ('Detect' not in module_type) and ('Segment' not in module_type): # 'Detect' for Object Detect task,'Segment' for Segment task
Because, in Segment task ,the last layer m.type = models.yolo.Segment,and the original Detect task the last layer m.type = models.yolo.Detect
I have submitted a pull request at #12459

@glenn-jocher
Copy link
Member

@tiancailin Thank you for identifying and addressing the issue! Your contribution is greatly appreciated. We'll review your pull request and continue the discussion there. Your input is valuable, and we're grateful for your efforts in improving the project. Keep up the great work!

Copy link
Contributor

github-actions bot commented Jan 2, 2024

👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLO 🚀 and Vision AI ⭐

@github-actions github-actions bot added the Stale label Jan 2, 2024
glenn-jocher added a commit that referenced this issue Jan 3, 2024
* Fix bug in #12457.
When run 'python.exe segment/predict.py --visualize' will throw AttributeError: 'tuple' object has no attribute 'shape'

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
pleb631 pushed a commit to pleb631/yolov5 that referenced this issue Jan 6, 2024
* Fix bug in ultralytics#12457.
When run 'python.exe segment/predict.py --visualize' will throw AttributeError: 'tuple' object has no attribute 'shape'

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Stale
Projects
None yet
Development

No branches or pull requests

2 participants