THU-MIG / yolov10

YOLOv10: Real-Time End-to-End Object Detection
https://arxiv.org/abs/2405.14458
GNU Affero General Public License v3.0
9.43k stars 889 forks source link

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

Closed Teymoor-Ali closed 3 months ago

Teymoor-Ali commented 3 months ago

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 predictcli 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 commented 3 months ago

You can use the Python API.

from ultralytics import YOLOv10
model = YOLOv10("best.pt")
model.predict(...)
cjxxu commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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:

https://github.com/THU-MIG/yolov10/blob/8d9f45a9557f25dbf75e3c9c95ac86bb8b97ba7c/ultralytics/cfg/__init__.py#L552

Teymoor-Ali commented 3 months ago

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 commented 3 months ago

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\ultralyticsinit.py)

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

Teymoor-Ali commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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

chriszxk commented 3 months ago

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 commented 3 months ago

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

yizhixiaochenzi commented 3 months ago

您可以使用 Python API。

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

他会保存在哪

yaosx425 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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:

https://github.com/THU-MIG/yolov10/blob/8d9f45a9557f25dbf75e3c9c95ac86bb8b97ba7c/ultralytics/cfg/__init__.py#L552

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

jameslahm commented 3 months ago

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

jameslahm commented 3 months ago

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