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

AttributeError: 'dict' object has no attribute 'shape' #46

Closed
Teymoor-Ali opened this issue May 25, 2024 · 19 comments
Closed

AttributeError: 'dict' object has no attribute 'shape' #46

Teymoor-Ali opened this issue May 25, 2024 · 19 comments

Comments

@Teymoor-Ali
Copy link

Hi so i trained my model and im trying to run inference,

Command used "!yolo predict model="I:/Github/test/runs/detect/train/weights/best.pt" source="03_001_130524.png"

i get this error
File "c:\Users\Teymo.conda\envs\YOLO10\lib\runpy.py", line 197, in _run_module_as_main
return run_code(code, main_globals, None,
File "c:\Users\Teymo.conda\envs\YOLO10\lib\runpy.py", line 87, in run_code
exec(code, run_globals)
File "C:\Users\Teymo.conda\envs\YOLO10\Scripts\yolo.exe_main
.py", line 7, in
sys.exit(entrypoint())
File "I:\Github\test\yolov10-main\yolov10-main\ultralytics\cfg_init
.py", line 587, in entrypoint
getattr(model, mode)(**overrides) # default args from model
File "I:\Github\test\yolov10-main\yolov10-main\ultralytics\engine\model.py", line 441, in predict
return self.predictor.predict_cli(source=source) if is_cli else self.predictor(source=source, stream=stream)
File "I:\Github\test\yolov10-main\yolov10-main\ultralytics\engine\predictor.py", line 177, in predict_cli
for _ in gen: # noqa, running CLI inference without accumulating any outputs (do not modify)
File "c:\Users\Teymo.conda\envs\YOLO10\lib\site-packages\torch\utils_contextlib.py", line 35, in generator_context
response = gen.send(None)
File "I:\Github\test\yolov10-main\yolov10-main\ultralytics\engine\predictor.py", line 255, in stream_inference
self.results = self.postprocess(preds, im, im0s)
File "I:\Github\test\yolov10-main\yolov10-main\ultralytics\models\yolo\detect\predict.py", line 25, in postprocess
preds = ops.non_max_suppression(
File "I:\Github\test\yolov10-main\yolov10-main\ultralytics\utils\ops.py", line 216, in non_max_suppression
bs = prediction.shape[0] # batch size
AttributeError: 'dict' object has no attribute 'shape'

@Y-T-G
Copy link

Y-T-G commented May 25, 2024

You can use the Python API.

from ultralytics import YOLOv10
model = YOLOv10("best.pt")
model.predict(...)

@cjxxu
Copy link

cjxxu commented May 25, 2024

add ‘prediction = prediction['one2many'][0]’ to File "I:\Github\test\yolov10-main\yolov10-main\ultralytics\utils\ops.py", line 216, in non_max_suppression
bs = prediction.shape[0] # batch size

final code:

prediction = prediction['one2many'][0]
bs = prediction.shape[0]

@Y-T-G
Copy link

Y-T-G commented May 25, 2024

add ‘prediction = prediction['one2many'][0]’ to File "I:\Github\test\yolov10-main\yolov10-main\ultralytics\utils\ops.py", line 216, in non_max_suppression bs = prediction.shape[0] # batch size

final code:

prediction = prediction['one2many'][0]
bs = prediction.shape[0]

You shouldn't do this because NMS shouldn't be used for post-processing at all. Otherwise it defeats the purpose.

@Y-T-G
Copy link

Y-T-G commented May 25, 2024

You can also rename the best.pt file to yolov10.pt to fix the CLI tool because the code decides based on that whether it's YOLOv10 or not:

elif "yolov10" in stem:

@Teymoor-Ali
Copy link
Author

from ultralytics import YOLOv10

Just to double check, I installed this Repo, unless theres another repo thats not the main ultralytics package thats contains YOLOv10? as far as i know they havent merges the yolo10 code into their main.

ImportError: cannot import name 'YOLOv10' from 'ultralytics' (c:\Users\Teymo\AppData\Local\Programs\Python\Python311\Lib\site-packages\ultralytics_init_.py)

@Y-T-G
Copy link

Y-T-G commented May 26, 2024

from ultralytics import YOLOv10

Just to double check, I installed this Repo, unless theres another repo thats not the main ultralytics package thats contains YOLOv10? as far as i know they havent merges the yolo10 code into their main.

ImportError: cannot import name 'YOLOv10' from 'ultralytics' (c:\Users\Teymo\AppData\Local\Programs\Python\Python311\Lib\site-packages\ultralytics__init__.py)

How did you install it? You should use pip install -e . and be running the import from inside the repo directory.

@Teymoor-Ali
Copy link
Author

Yes, I have done pip install, I assume I have to switch to the directory for it to import.

I have been using juypter notebooks on visual studio.

@Y-T-G
Copy link

Y-T-G commented May 26, 2024

Yes, I have done pip install, I assume I have to switch to the directory for it to import.

I have been using juypter notebooks on visual studio.

Yeah. You have to cd into that directory.

In Jupyter, you do %cd /path/ to change directory.

@Teymoor-Ali
Copy link
Author

Apologies, didnt realise that the conda environment with the YOLOV10 installation was not selected. I got it working now, Thanks.

@chriszxk
Copy link

You can use the Python API.

from ultralytics import YOLOv10
model = YOLOv10("best.pt")
model.predict(...)

I used the code to predict, but do not know where the prediction results save?

@jameslahm
Copy link
Collaborator

Could you please try to pass save=True into model.predict()?

@yizhixiaochenzi
Copy link

您可以使用 Python API。

from ultralytics import YOLOv10
model = YOLOv10("best.pt")
model.predict(...)

他会保存在哪

@yaosx425
Copy link

You can use the Python API.

from ultralytics import YOLOv10
model = YOLOv10("best.pt")
model.predict(...)

I used the code to predict, but do not know where the prediction results save?

Hello, have you found a way to save the results?

@jameslahm
Copy link
Collaborator

Could you please try to add save=True in model.predict(...)?

from ultralytics import YOLOv10
model = YOLOv10("yolov10n.pt")
model.predict(save=True)

And the results will be saved in runs/detect/predict by default.

WARNING ⚠️ 'source' is missing. Using 'source=ultralytics/assets'.

image 1/2 ultralytics/assets/bus.jpg: 640x480 4 persons, 1 bus, 83.2ms
image 2/2 ultralytics/assets/zidane.jpg: 384x640 2 persons, 82.5ms
Speed: 2.3ms preprocess, 82.8ms inference, 1.5ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs/detect/predict2

@yaosx425
Copy link

Could you please try to add save=True in model.predict(...)?

from ultralytics import YOLOv10
model = YOLOv10("yolov10n.pt")
model.predict(save=True)

And the results will be saved in runs/detect/predict by default.

WARNING ⚠️ 'source' is missing. Using 'source=ultralytics/assets'.

image 1/2 ultralytics/assets/bus.jpg: 640x480 4 persons, 1 bus, 83.2ms
image 2/2 ultralytics/assets/zidane.jpg: 384x640 2 persons, 82.5ms
Speed: 2.3ms preprocess, 82.8ms inference, 1.5ms postprocess per image at shape (1, 3, 384, 640)
Results saved to runs/detect/predict2

Thanks for answering! It worked!

@wint11
Copy link

wint11 commented Jun 3, 2024

I want to do object tracking for the video. The python code is
import cv2
from ultralytics import YOLO
model = YOLO('/data/anonym1/project-office/ultralytics-main/detect/train2/weights/best.pt')
video_path = "/data/anonym1/Users/ghm_data/yolov9-main/yolov9-main/Manhole_image/test/Drone_aerial_video_3.mp4"
cap = cv2.VideoCapture(video_path)
while cap.isOpened():
success, frame = cap.read()
if success:
results = model.track(frame, persist=True, save=True)
print(results[0].boxes)
annotated_frame = results[0].plot()
cv2.imshow("YOLOv10 Tracking", annotated_frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
break
cap.release()
cv2.destroyAllWindows()

But there is an error, the error code is:
Traceback (most recent call last):
File "/data/anonym1/Users/ghm_data/yolov10-main/tracker.py", line 18, in
results = model.track(frame, persist=True, save=True)
File "/data/anonym1/Users/ghm_data/yolov10-main/ultralytics/engine/model.py", line 481, in track
return self.predict(source=source, stream=stream, **kwargs)
File "/data/anonym1/Users/ghm_data/yolov10-main/ultralytics/engine/model.py", line 441, in predict
return self.predictor.predict_cli(source=source) if is_cli else self.predictor(source=source, stream=stream)
File "/data/anonym1/Users/ghm_data/yolov10-main/ultralytics/engine/predictor.py", line 168, in call
return list(self.stream_inference(source, model, *args, **kwargs)) # merge list of Result into one
The File "/ data/anonym1 / anaconda3 / envs/yolov10 / lib/python3.9 / site - packages/torch/utils / _contextlib py", line 35, in generator_context
response = gen.send(None)
File "/data/anonym1/Users/ghm_data/yolov10-main/ultralytics/engine/predictor.py", line 255, in stream_inference
self.results = self.postprocess(preds, im, im0s)
File "/data/anonym1/Users/ghm_data/yolov10-main/ultralytics/models/yolo/detect/predict.py", line 25, in postprocess
preds = ops.non_max_suppression(
File "/data/anonym1/Users/ghm_data/yolov10-main/ultralytics/utils/ops.py", line 216, in non_max_suppression
bs = prediction.shape[0] # batch size
AttributeError: 'dict' object has no attribute 'shape'

@JaySonWu-star
Copy link

You can also rename the best.pt file to yolov10.pt to fix the CLI tool because the code decides based on that whether it's YOLOv10 or not:

elif "yolov10" in stem:

The author seems to ignore this issue. This is a very simple and effective method

@jameslahm jameslahm unpinned this issue Jun 4, 2024
@jameslahm
Copy link
Collaborator

jameslahm commented Jun 4, 2024

@wint11 Could you please try to replace YOLO with YOLOv10? Thanks!

@jameslahm
Copy link
Collaborator

@JaySonWu-star Thanks! We just made the YOLOv10 by default in cli. Could you please update the codebase and try again? Thank you!

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

9 participants