ultralytics / ultralytics

NEW - YOLOv8 🚀 in PyTorch > ONNX > OpenVINO > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
23.76k stars 4.74k forks source link

YOLOv8m-pose AttributeError: 'Pose' object has no attribute 'detect' #11691

Closed KKopilka closed 5 days ago

KKopilka commented 1 week ago

Search before asking

Question

Hello, I have trained a YOLOv8m-pose model and after running the model, I get an error: Traceback (most recent call last): File "D:\AI-Trainer\manual.py", line 140, in <module> main() File "D:\AI-Trainer\manual.py", line 54, in main results = model.predict(frame) ^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\ultralytics\engine\model.py", line 451, in predict return self.predictor.predict_cli(source=source) if is_cli else self.predictor(source=source, stream=stream) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\ultralytics\engine\predictor.py", line 168, in __call__ return list(self.stream_inference(source, model, *args, **kwargs)) # merge list of Result into one ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\torch\utils\_contextlib.py", line 35, in generator_context response = gen.send(None) ^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\ultralytics\engine\predictor.py", line 228, in stream_inference self.model.warmup(imgsz=(1 if self.model.pt or self.model.triton else self.dataset.bs, 3, *self.imgsz)) File "C:\Python311\Lib\site-packages\ultralytics\nn\autobackend.py", line 589, in warmup self.forward(im) # warmup ^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\ultralytics\nn\autobackend.py", line 423, in forward y = self.model(im, augment=augment, visualize=visualize, embed=embed) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\torch\nn\modules\module.py", line 1511, in _wrapped_call_impl return self._call_impl(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\torch\nn\modules\module.py", line 1520, in _call_impl return forward_call(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\ultralytics\nn\tasks.py", line 89, in forward return self.predict(x, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\ultralytics\nn\tasks.py", line 107, in predict return self._predict_once(x, profile, visualize, embed) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\ultralytics\nn\tasks.py", line 128, in _predict_once x = m(x) # run ^^^^ File "C:\Python311\Lib\site-packages\torch\nn\modules\module.py", line 1511, in _wrapped_call_impl return self._call_impl(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\torch\nn\modules\module.py", line 1520, in _call_impl return forward_call(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\ultralytics\nn\modules\head.py", line 165, in forward x = self.detect(self, x) ^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\torch\nn\modules\module.py", line 1688, in __getattr__ raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'") AttributeError: 'Pose' object has no attribute 'detect'

I ran the 'n' and 's' models and everything worked correctly. Can you please tell me what the problem might be? Why is the error appearing now? I also run the code in VS code, but everything works in Google Colab. My code looks like this:

`def main():

model = YOLO('models/yolo3/best.pt')
video_path = 'assets/left_side_cut.mp4'
# video_path = 'assets/biceps.mp4'
cap = cv2.VideoCapture(video_path)
count = 0
dirr = 1

validFrames = {}

while cap.isOpened():
    success, frame = cap.read()
    # h, w, _ = frame.shape
    # window_w = int(frame.shape[1] * w)
    # window_h = int(frame.shape[0] * h)
    # frame_size = int(cap.get(3)), int(cap.get(4))  # frame_w, frame_h
    # img_w, img_h = frame_size
    # cv2.resizeWindow("Video", window_w, window_h)
    frame = cv2.resize(frame, (800, 650), interpolation=cv2.INTER_AREA)

    if success:
        results = model(frame)
        # kps = results.xyxy[0]
        # kps = results[0].keypoints.data.cpu().numpy()[0]
        kps = results[0].keypoints.xy.cpu().numpy()[0]
        x, y, z = kps.T[:3]
        # x_img = x * img_w
        # y_img = y * img_h
        # pose_2d = np.column_stack((x_img, y_img))
        pose_3d = np.column_stack((x, y, z))
        if pose_3d.size <= 0:
            continue
        frame = draw_pose(
            image=frame,
            keypoints=pose_3d,
            disposition="coco",
            thickness=2,
        ) ...`

I want to add: I tried using predict(), but the error persists. `if success: results = model.predict(frame)

kps = results.xyxy[0]

        # kps = results[0].keypoints.data.cpu().numpy()[0]
        kps = results[0].keypoints.xy.cpu().numpy()[0]
        x, y, z = kps.T[:3]
        # x_img = x * img_w
        # y_img = y * img_h
        # pose_2d = np.column_stack((x_img, y_img))
        pose_3d = np.column_stack((x, y, z))

`

Traceback (most recent call last): File "D:\AI-Trainer\manual.py", line 140, in <module> main() File "D:\AI-Trainer\manual.py", line 54, in main results = model.predict(frame) ^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\ultralytics\engine\model.py", line 451, in predict return self.predictor.predict_cli(source=source) if is_cli else self.predictor(source=source, stream=stream) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\ultralytics\engine\predictor.py", line 168, in __call__ return list(self.stream_inference(source, model, *args, **kwargs)) # merge list of Result into one ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\torch\utils\_contextlib.py", line 35, in generator_context response = gen.send(None) ^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\ultralytics\engine\predictor.py", line 228, in stream_inference self.model.warmup(imgsz=(1 if self.model.pt or self.model.triton else self.dataset.bs, 3, *self.imgsz)) File "C:\Python311\Lib\site-packages\ultralytics\nn\autobackend.py", line 589, in warmup self.forward(im) # warmup ^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\ultralytics\nn\autobackend.py", line 423, in forward y = self.model(im, augment=augment, visualize=visualize, embed=embed) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\torch\nn\modules\module.py", line 1511, in _wrapped_call_impl return self._call_impl(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\torch\nn\modules\module.py", line 1520, in _call_impl return forward_call(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\ultralytics\nn\tasks.py", line 89, in forward return self.predict(x, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\ultralytics\nn\tasks.py", line 107, in predict return self._predict_once(x, profile, visualize, embed) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\ultralytics\nn\tasks.py", line 128, in _predict_once x = m(x) # run ^^^^ File "C:\Python311\Lib\site-packages\torch\nn\modules\module.py", line 1511, in _wrapped_call_impl return self._call_impl(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\torch\nn\modules\module.py", line 1520, in _call_impl return forward_call(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\ultralytics\nn\modules\head.py", line 165, in forward x = self.detect(self, x) ^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\torch\nn\modules\module.py", line 1688, in __getattr__ raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'") AttributeError: 'Pose' object has no attribute 'detect'

Thanks in advance!

Additional

No response

github-actions[bot] commented 1 week ago

👋 Hello @KKopilka, thank you for your interest in Ultralytics YOLOv8 🚀! We recommend a visit to the Docs for new users where you can find many Python and CLI usage examples and where many of the most common questions may already be answered.

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.

Join the vibrant Ultralytics Discord 🎧 community for real-time conversations and collaborations. This platform offers a perfect space to inquire, showcase your work, and connect with fellow Ultralytics users.

Install

Pip install the ultralytics package including all requirements in a Python>=3.8 environment with PyTorch>=1.8.

pip install ultralytics

Environments

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

Status

Ultralytics CI

If this badge is green, all Ultralytics CI tests are currently passing. CI tests verify correct operation of all YOLOv8 Modes and Tasks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

glenn-jocher commented 1 week ago

Hello! It looks like you encountered an AttributeError because the method you're trying to use isn't available in the Pose class of YOLOv8m-pose.

For the pose estimation model, you should use the .predict() method instead of .detect() which is typically used with detection models. From the snippets you've shown, it appears you're using .predict() correctly. However, to avoid any possible errors, ensure all your syntax matches exactly as it should be when employing the .predict() method.

Here's a simple usage example:

results = model.predict(frame)
keypoints = results[0].keypoints.xy.cpu().numpy()

Ensure your installation is updated and correctly configured, particularly if transitioning between different environments like VS Code and Google Colab that might have different package versions.

If issues persist, verifying paths and ensuring that your environment aligns with the requirements of the Ultralytics library could help pinpoint discrepancies. If still unresolved, please provide further details or the specific line that continues to raise errors.

Thank you for reaching out!

KKopilka commented 1 week ago

@glenn-jocher OK, I tried changing the code to yours, but the error persists:

results = model.predict(frame)
kps = results[0].keypoints.xy.cpu().numpy()
Traceback (most recent call last):
  File "D:\AI-Trainer\manual.py", line 156, in <module>
    main()
  File "D:\AI-Trainer\manual.py", line 59, in main
    results = model.predict(frame)
              ^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\ultralytics\engine\model.py", line 451, in predict
    return self.predictor.predict_cli(source=source) if is_cli else self.predictor(source=source, stream=stream)
                                                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\ultralytics\engine\predictor.py", line 168, in __call__
    return list(self.stream_inference(source, model, *args, **kwargs))  # merge list of Result into one
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\torch\utils\_contextlib.py", line 35, in generator_context
    response = gen.send(None)
               ^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\ultralytics\engine\predictor.py", line 228, in stream_inference
    self.model.warmup(imgsz=(1 if self.model.pt or self.model.triton else self.dataset.bs, 3, *self.imgsz))
  File "C:\Python311\Lib\site-packages\ultralytics\nn\autobackend.py", line 589, in warmup
    self.forward(im)  # warmup
    ^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\ultralytics\nn\autobackend.py", line 423, in forward
    y = self.model(im, augment=augment, visualize=visualize, embed=embed)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\torch\nn\modules\module.py", line 1511, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\torch\nn\modules\module.py", line 1520, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\ultralytics\nn\tasks.py", line 89, in forward
    return self.predict(x, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\ultralytics\nn\tasks.py", line 107, in predict
    return self._predict_once(x, profile, visualize, embed)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\ultralytics\nn\tasks.py", line 128, in _predict_once
    x = m(x)  # run
        ^^^^
  File "C:\Python311\Lib\site-packages\torch\nn\modules\module.py", line 1511, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\torch\nn\modules\module.py", line 1520, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\ultralytics\nn\modules\head.py", line 165, in forward
    x = self.detect(self, x)
        ^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\torch\nn\modules\module.py", line 1688, in __getattr__
    raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")
AttributeError: 'Pose' object has no attribute 'detect'
glenn-jocher commented 1 week ago

Hello! It looks like there's a mix-up with the methods in use. The error 'Pose' object has no attribute 'detect' suggests trying to access functionality that doesn't exist in the pose estimation model.

Here's how you should configure your predictor:

results = model.predict(frame)  # Correct method for prediction
keypoints = results[0].keypoints.xy.cpu().numpy()  # Extracting keypoints

Ensure your model instantiation (YOLO('path_to_model')) is correctly referenced to the model you intend to use and that it's appropriately configured for pose detection, not object detection.

If the problem persists and all setups look correct, it might help to reinstall the Ultralytics library to ensure there are no corrupted or mismatched files. 👍 Let us know how it goes!

oOvOio commented 2 days ago

This may be due to inconsistencies between the version of Ultralytics used for inference and the version of Ultralytics used for training

glenn-jocher commented 2 days ago

Hello! It sounds like you might be right about the version inconsistency. Please make sure that both the training and inference environments are using the same version of Ultralytics YOLO. You can check the version in your environment with:

pip show ultralytics

If they differ, consider updating to the latest version using:

pip install --upgrade ultralytics

This should help maintain compatibility across your processes. Let us know how it goes! 😊

KKopilka commented 2 days ago

@glenn-jocher @oOvOio Good afternoon! Yes, I already realized that the problem was in the version update ultralytics :) Thank u!

glenn-jocher commented 2 days ago

Hello! Great to hear that you figured out the issue with the version update! 🎉 If you have any more questions or run into any other issues, feel free to reach out. Happy coding with Ultralytics! 😊🚀