ultralytics / yolov5

YOLOv5 ๐Ÿš€ in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
47.78k stars 15.73k forks source link

ModuleNotFoundError: No module named 'models.yolo' on custom trained weight. Is it a bug? #12999

Open dilwolf opened 3 weeks ago

dilwolf commented 3 weeks ago

Search before asking

Question

After training the Yolov5 model on a custom dataset with yolov5/train.py in the repository for multi-gpu and loss_weight purposes, I am able to do inference with yolov5/detect.py in the repository. However, when I use the fine tuned weight with short code for customization, model is returning ModuleNotFoundError:. Below is my short Python script and error logging:

inference.py:

from ultralytics import YOLO model = YOLO("models/best.pt") results = model.predict(source="videos/video.mp4", show=True, save=True)

Error>>>>>:

WARNING โš ๏ธ models/best.pt appears to require 'models.yolo', which is not in ultralytics requirements. AutoInstall will run now for 'models.yolo' but this feature will be removed in the future. Recommend fixes are to train a new model using the latest 'ultralytics' package or to run a command with an official YOLOv8 model, i.e. 'yolo predict model=yolov8n.pt' requirements: Ultralytics requirement ['models.yolo'] not found, attempting AutoUpdate... ERROR: Could not find a version that satisfies the requirement models.yolo (from versions: none) ERROR: No matching distribution found for models.yolo Retry 1/2 failed: Command 'pip install --no-cache "models.yolo" ' returned non-zero exit status 1. requirements: AutoUpdate success โœ… 2.7s, installed 1 package: ['models.yolo'] requirements: โš ๏ธ Restart runtime or rerun command for updates to take effect

Traceback (most recent call last): File "/home/imes-server2/anaconda3/envs/dilwolf/lib/python3.10/site-packages/ultralytics/nn/tasks.py", line 732, in torch_safe_load ckpt = torch.load(file, map_location="cpu") File "/home/user/anaconda3/envs/dilwolf/lib/python3.10/site-packages/torch/serialization.py", line 1025, in load return _load(opened_zipfile, File "/home/user/anaconda3/envs/dilwolf/lib/python3.10/site-packages/torch/serialization.py", line 1446, in _load result = unpickler.load() File "/home/user/anaconda3/envs/dilwolf/lib/python3.10/site-packages/torch/serialization.py", line 1439, in find_class return super().find_class(mod_name, name) ModuleNotFoundError: No module named 'models.yolo'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/home/user/Dilwolf/football_analysis/inference.py", line 3, in model = YOLO('models/best_weighted.pt') File "/home/user/anaconda3/envs/dilwolf/lib/python3.10/site-packages/ultralytics/models/yolo/model.py", line 23, in init super().init(model=model, task=task, verbose=verbose) File "/home/user/anaconda3/envs/dilwolf/lib/python3.10/site-packages/ultralytics/engine/model.py", line 151, in init self._load(model, task=task) File "/home/user/anaconda3/envs/dilwolf/lib/python3.10/site-packages/ultralytics/engine/model.py", line 240, in _load self.model, self.ckpt = attempt_load_one_weight(weights) File "/home/user/anaconda3/envs/dilwolf/lib/python3.10/site-packages/ultralytics/nn/tasks.py", line 806, in attempt_load_one_weight ckpt, weight = torch_safe_load(weight) # load ckpt File "/home/user/anaconda3/envs/dilwolf/lib/python3.10/site-packages/ultralytics/nn/tasks.py", line 752, in torch_safe_load ckpt = torch.load(file, map_location="cpu") File "/home/user/anaconda3/envs/dilwolf/lib/python3.10/site-packages/torch/serialization.py", line 1025, in load return _load(opened_zipfile, File "/home/user/anaconda3/envs/dilwolf/lib/python3.10/site-packages/torch/serialization.py", line 1446, in _load result = unpickler.load() File "/home/user/anaconda3/envs/dilwolf/lib/python3.10/site-packages/torch/serialization.py", line 1439, in find_class return super().find_class(mod_name, name) ModuleNotFoundError: No module named 'models.yolo'

Tried restarting runtime and other installations, did not work....

Additional

When I trained with !yolo task=detect mode=train model=yolov5x.pt data=data.yaml epochs=100 imgsz=640 command on jupyter notebook, I am able to run fine tuned weight with my inference.py. Current ultralystics==8.2.11 I also tried with little bit previous version of ultralytics but it did not help.

Furthermore, is it possible use mult-gpu, when I train with !yolo task=detect ..... on jupter notebook? I tried !yolo -m torch.distributed.run nproc_per_node=2 train.py batch=64 data=data.yaml --weights yolov5s.pt --device 0,1 but did not work?

Thanks in advance !!!

glenn-jocher commented 3 weeks ago

Hi there! ๐Ÿ‘‹ It looks like you're encountering issues related to model package dependencies. From the error logs, it appears that the models.yolo module is being mistakenly required, which shouldn't be the case with the standard setup.

Please ensure you are using the latest version of YOLOv5 from the Ultralytics GitHub repository. If you've trained your model with a custom setup or modified version of YOLO, make sure all your imports and custom modules are correctly aligned with the Ultralytics library structures.

For the multi-GPU training query, ensure youโ€™re using the latest updates of YOLOv5 and PyTorch with correct syntax:

python -m torch.distributed.run --nproc_per_node=2 train.py --img 640 --batch 32 --epochs 100 --data data.yaml --weights yolov5s.pt --device 0,1

If issues persist, try retracing your steps or consider training a fresh model with the official scripts to see if the issue is ongoing. For further detailed guidance, please check the docs.

dilwolf commented 3 weeks ago

Hey there! Thank you so much for your help and effort on this.

Today, I retrained my model again with fresh source code by cloning it after getting your response. I reinstalled requirements.txt in the official Yolov5 repository, which includes ultralytics==8.2.13. Again, it is returning the same error.

It may seem very childish, but being meticulous may solve our error. Let me briefly explain what I did again today.

I started cloning the official repo and fine tuned the pre-trained yolov5x model on my dataset. I trained using yolov5/train.py from yolov5 official repository. However, when I use the trained weight with my short inference.py script, it's giving ModuleNotFoundError. I am curious it should work normally with the YOLO module when I call from ultralytics import YOLO. Below is my inference script:

inference.py:

from ultralytics import YOLO model = YOLO("models/best.pt") results = model.predict(source="videos/video.mp4", show=True, save=True)

Any help or suggestion are really appreciated !!!

glenn-jocher commented 2 weeks ago

Hello! ๐Ÿ‘‹ Thank you for your detailed follow-up and the efforts youโ€™ve put into resolving this issue.

The error you're encountering is quite peculiar, especially after following all the correct steps. Just to ensure that we're on the exact same page, the YOLO class is intended to work directly with the pre-trained models provided or ones trained using Ultralytics official scripts without modification.

Given your situation, here's a simple thing you can try:

Here's a slightly modified script that makes sure the weights are being pointed to from the correct relative path:

from ultralytics import YOLO

# Assuming your 'best.pt' is in the 'models' directory relative to your script
model = YOLO("./models/best.pt")
results = model.predict(source="videos/video.mp4", show=True, save=True)

Additionally, if this continues to fail, could you check the version of YOLOv5 directly in your environment to make certain it aligns with your operations? Sometimes, discrepancies in versioning can lead to unexpected issues.

If the problem persists, feel free to consult our docs or respond here with any updates or further details. We'll find a solution together! ๐Ÿš€

dilwolf commented 2 weeks ago

Thank you so much for your help on this issue!

As I mentioned earlier, I am using yolov5/train.py by customizing cls_pw: [15, 0.1, 2] in data/hyps/hyp.scratch-low.yaml file for weighting loss for imbalanced dataset. I can easily use my fine-tuned weight with predict.py. However, I can not use my fine-tuned weight with YOLO class. The YOLO class just works fine with default pre-trained weight in the original repository.

Today, I tried with latest version ultralytics==8.2.16 but today it has got newtype of Error:

Traceback (most recent call last): File "/home/dilwolf/my_projects/yolov5/inference.py", line 5, in results = model.predict(source="input_videos/video.mp4", save=True) File "/home/dilwolf/anaconda3/envs/yolo/lib/python3.10/site-packages/ultralytics/engine/model.py", line 446, in predict self.predictor.setup_model(model=self.model, verbose=is_cli) File "/home/dilwolf/anaconda3/envs/yolo/lib/python3.10/site-packages/ultralytics/engine/predictor.py", line 297, in setup_model self.model = AutoBackend( File "/home/dilwolf/anaconda3/envs/yolo/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context return func(*args, **kwargs) File "/home/dilwolf/anaconda3/envs/yolo/lib/python3.10/site-packages/ultralytics/nn/autobackend.py", line 144, in init model = model.fuse(verbose=verbose) TypeError: BaseModel.fuse() got an unexpected keyword argument 'verbose'

I am not using any kind of verbose function.

inference.py:

from ultralytics import YOLO

# Assuming your 'best.pt' is in the 'models' directory relative to your script
model = YOLO("./models/best.pt")
results = model.predict(source="input_videos/video.mp4", save=True)
dilwolf commented 2 weeks ago

Thank you so much for your help on this issue!

As I mentioned earlier, I am using yolov5/train.py by customizing cls_pw: [15, 0.1, 2] in data/hyps/hyp.scratch-low.yaml file for weighting loss for imbalanced dataset. I can easily use my fine-tuned weight with predict.py. However, I can not use my fine-tuned weight with YOLO class. The YOLO class just works fine with default pre-trained weight in the original repository.

Today, I tried with latest version ultralytics==8.2.16 but today it has got newtype of Error:

Traceback (most recent call last): File "/home/dilwolf/my_projects/yolov5/inference.py", line 5, in results = model.predict(source="input_videos/video.mp4", save=True) File "/home/dilwolf/anaconda3/envs/yolo/lib/python3.10/site-packages/ultralytics/engine/model.py", line 446, in predict self.predictor.setup_model(model=self.model, verbose=is_cli) File "/home/dilwolf/anaconda3/envs/yolo/lib/python3.10/site-packages/ultralytics/engine/predictor.py", line 297, in setup_model self.model = AutoBackend( File "/home/dilwolf/anaconda3/envs/yolo/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context return func(*args, kwargs) File "/home/dilwolf/anaconda3/envs/yolo/lib/python3.10/site-packages/ultralytics/nn/autobackend.py", line 144, in init** model = model.fuse(verbose=verbose) TypeError: BaseModel.fuse() got an unexpected keyword argument 'verbose'

I am not using any kind of verbose function.

inference.py:

from ultralytics import YOLO

# Assuming your 'best.pt' is in the 'models' directory relative to your script
model = YOLO("./models/best.pt")
results = model.predict(source="input_videos/video.mp4", save=True)

I checked that I can not do this customization using yolo CLI. Because I can use fine-tuned weights using yolo CLI. Or is there a way to do the above weighted_loss custimization using YOLO CLI?

glenn-jocher commented 2 weeks ago

Hi there! ๐Ÿ‘‹

It looks like you're running into a fairly specific error after customizing and training your model. The error being thrown when using your custom-trained model with the YOLO class might indicate an incompatibility or an issue with how the custom weights were saved or loaded.

Since the issue arises specifically with customized training configurations and not with the default pre-trained weights, this suggests that the customization itself (i.e., weighting in the loss) might be modifying the model architecture or saved state in a way that isn't fully compatible with the current YOLO class handling.

Regarding the error with unexpected keyword argument 'verbose', it seems like there might have been changes in the Ultralytics library that are causing this issue. Our team continuously updates the library, so checking the compatibility of different versions might help.

A temporary solution would be to use predict.py as it works without issues. However, pointing out these discrepancies is crucial for us to update and fix potential bugs in future releases.

For now, unfortunately, there isn't a direct way to implement this specific weighted_loss customization using the YOLO CLI without diving into the code and modifying the requisite parts as you have done.

Thanks for bringing this to attention, and we appreciate your input as it helps improve the tool! Let us know if there are any further details or changes, and we'll try to assist further! ๐Ÿš€