ultralytics / ultralytics

Ultralytics YOLO11 🚀
https://docs.ultralytics.com
GNU Affero General Public License v3.0
32.53k stars 6.26k forks source link

yolov8 ImportError: cannot import name 'YOLO' #16011

Open mcyhaha opened 2 months ago

mcyhaha commented 2 months ago

Search before asking

Question

C:\Users\mcy\anaconda3\envs\face_env\python.exe D:/dlcode/STF-YOLO-main/ultralytics/models/yolo/pose/train.py Traceback (most recent call last): File "D:\dlcode\STF-YOLO-main\ultralytics\models\yolo\pose\train.py", line 5, in from ultralytics.models import yolo File "D:\dlcode\STF-YOLO-main\ultralytics__init.py", line 5, in from ultralytics.models import RTDETR, SAM, YOLO File "D:\dlcode\STF-YOLO-main\ultralytics\models\init.py", line 3, in from .rtdetr import RTDETR File "D:\dlcode\STF-YOLO-main\ultralytics\models\rtdetr\init.py", line 3, in from .model import RTDETR File "D:\dlcode\STF-YOLO-main\ultralytics\models\rtdetr\model.py", line 9, in from .train import RTDETRTrainer File "D:\dlcode\STF-YOLO-main\ultralytics\models\rtdetr\train.py", line 7, in from ultralytics.models.yolo.detect import DetectionTrainer File "D:\dlcode\STF-YOLO-main\ultralytics\models\yolo\init.py", line 3, in from ultralytics.models.yolo import classify, detect, pose, segment File "D:\dlcode\STF-YOLO-main\ultralytics\models\yolo\pose\init.py", line 4, in from .train import PoseTrainer File "D:\dlcode\STF-YOLO-main\ultralytics\models\yolo\pose\train.py", line 74, in from ultralytics import YOLO ImportError: cannot import name 'YOLO' from partially initialized module 'ultralytics' (most likely due to a circular import) (D:\dlcode\STF-YOLO-main\ultralytics\init__.py)

进程已结束,退出代码1

Additional

How is this supposed to work? It's been bothering me for so long

Y-T-G commented 2 months ago

It seems like you modified the code and broke it. This is not there in the official ultralytics:

File "D:\dlcode\STF-YOLO-main\ultralytics\models\yolo\pose\train.py", line 74, in from ultralytics import YOLO

There's no from ultralytics import YOLO at that line.

mcyhaha commented 2 months ago

I added this myself. Will deleting this sentence fix it?

mcyhaha commented 2 months ago

C:\Users\mcy\anaconda3\envs\face_env\python.exe D:/dlcode/STF-YOLO-main/ultralytics/models/yolo/pose/train.py Traceback (most recent call last): File "D:\dlcode\STF-YOLO-main\ultralytics\models\yolo\pose\train.py", line 5, in from ultralytics import YOLO File "D:\dlcode\STF-YOLO-main\ultralytics__init.py", line 5, in from ultralytics.models import RTDETR, SAM, YOLO File "D:\dlcode\STF-YOLO-main\ultralytics\models\init.py", line 3, in from .rtdetr import RTDETR File "D:\dlcode\STF-YOLO-main\ultralytics\models\rtdetr\init.py", line 3, in from .model import RTDETR File "D:\dlcode\STF-YOLO-main\ultralytics\models\rtdetr\model.py", line 9, in from .train import RTDETRTrainer File "D:\dlcode\STF-YOLO-main\ultralytics\models\rtdetr\train.py", line 7, in from ultralytics.models.yolo.detect import DetectionTrainer File "D:\dlcode\STF-YOLO-main\ultralytics\models\yolo\init.py", line 3, in from ultralytics.models.yolo import classify, detect, pose, segment File "D:\dlcode\STF-YOLO-main\ultralytics\models\yolo\pose\init.py", line 4, in from .train import PoseTrainer File "D:\dlcode\STF-YOLO-main\ultralytics\models\yolo\pose\train.py", line 5, in from ultralytics import YOLO ImportError: cannot import name 'YOLO' from partially initialized module 'ultralytics' (most likely due to a circular import) (D:\dlcode\STF-YOLO-main\ultralytics\init__.py)

进程已结束,退出代码1 I deleted 74 lines of code, and there are errors in other lines How can this be solved?

Y-T-G commented 2 months ago

Why did you add that code there? It's broken. You still have the code in line 5.

mcyhaha commented 2 months ago

from ultralytics import YOLO This line of code, if not added, YOLO reports an error

glenn-jocher commented 2 months ago

It seems like there's a circular import issue. Ensure you're using the latest version of the package and check your import structure to avoid conflicts.

mcyhaha commented 2 months ago

Traceback (most recent call last): File "D:\dlcode\ultralytics-main\ultralytics-main\ultralytics\models\yolo\pose\train.py", line 85, in model = YOLO('D:\dlcode\ultralytics-main\ultralytics-main\ultralytics\cfg\models\v8\yolov8n-ghost-pose.yaml').load('D:\dlcode\ultralytics-main\yolov8n-pose.pt') # 从YAML构建并传输权重 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\mcy\anaconda3\envs\face_env\Lib\site-packages\ultralytics\models\yolo\model.py", line 23, in init super().init(model=model, task=task, verbose=verbose) File "C:\Users\mcy\anaconda3\envs\face_env\Lib\site-packages\ultralytics\engine\model.py", line 143, in init self._new(model, task=task, verbose=verbose) File "C:\Users\mcy\anaconda3\envs\face_env\Lib\site-packages\ultralytics\engine\model.py", line 263, in _new self.model = (model or self._smart_load("model"))(cfg_dict, verbose=verbose and RANK == -1) # build model ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\mcy\anaconda3\envs\face_env\Lib\site-packages\ultralytics\nn\tasks.py", line 423, in init super().init(cfg=cfg, ch=ch, nc=nc, verbose=verbose) File "C:\Users\mcy\anaconda3\envs\face_env\Lib\site-packages\ultralytics\nn\tasks.py", line 313, in init self.model, self.save = parse_model(deepcopy(self.yaml), ch=ch, verbose=verbose) # model, savelist ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\mcy\anaconda3\envs\face_env\Lib\site-packages\ultralytics\nn\tasks.py", line 906, in parse_model m = getattr(torch.nn, m[3:]) if "nn." in m else globals()[m] # get module


KeyError: 'pose'

Hello, I am now using the new version, after adding ghost module running this error, how to solve it?
mcyhaha commented 2 months ago

C:\Users\mcy\anaconda3\envs\face_env\python.exe D:/yolov8_pose/train_yolo.py Traceback (most recent call last): File "D:\yolov8_pose\train_yolo.py", line 5, in model = YOLO("D:\yolov8_pose\ultralytics\cfg\models\v8\yolov8n-ghost-pose.yaml") # 权重的地址 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\yolov8_pose\ultralytics\models\yolo\model.py", line 23, in init super().init(model=model, task=task, verbose=verbose) File "D:\yolov8_pose\ultralytics\engine\model.py", line 149, in init self._new(model, task=task, verbose=verbose) File "D:\yolov8_pose\ultralytics\engine\model.py", line 218, in _new self.model = (model or self._smart_load("model"))(cfg_dict, verbose=verbose and RANK == -1) # build model ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\yolov8_pose\ultralytics\nn\tasks.py", line 418, in init super().init(cfg=cfg, ch=ch, nc=nc, verbose=verbose) File "D:\yolov8_pose\ultralytics\nn\tasks.py", line 314, in init self.model, self.save = parse_model(deepcopy(self.yaml), ch=ch, verbose=verbose) # model, savelist ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\yolov8_pose\ultralytics\nn\tasks.py", line 903, in parse_model m = getattr(torch.nn, m[3:]) if "nn." in m else globals()[m] # get module


KeyError: 'pose'

 how to solve it?
glenn-jocher commented 2 months ago

It seems like there's a KeyError related to the 'pose' module. Please ensure your custom module is correctly defined and integrated into the codebase. Also, verify that you're using the latest version of the Ultralytics package. If the issue persists, consider checking the module's implementation for any discrepancies.