yolov5 icon indicating copy to clipboard operation
yolov5 copied to clipboard

How can I move and run the classification model out of YOLOV5 codebase?

Open AI-Passionner opened this issue 2 years ago • 9 comments

Search before asking

  • [X] I have searched the YOLOv5 issues and discussions and found no similar questions.

Question

It seems that loading the classification model can't leave out of the YOLOV5-Classifier code base. When loading the model, model = torch.load('path/to/best.pt', map_location=torch.device('cpu'))['model'].float(), it throws error like this """ module = self._system_import(name, *args, **kwargs) ModuleNotFoundError: No module named 'models' """ Once the folders of models and utils are copied to a new project directory, the loading works.

I am asking if there is a way I can load the classification model and use it as a stand-alone and for example, converting to TensorFlow like the object detection codebase.
As I know, the YOLOV5 object detection doesn't have this problem.

Thanks.

Additional

No response

AI-Passionner avatar Jul 30 '22 00:07 AI-Passionner

@AI-Passionner this is a good question. In general for any pytorch model you'll need to have the modules available in the workspace prior to loading. PyTorch Hub accomplishes this in the background by downloading and caching the repo in a separate directory.

To point PyTorch Hub to a specific branch in a repo, i.e. the classifier branch in the YOLOv5 repo you can try this, though the classifier should be merged into master as part of the upcoming v6.2 release in the next few weeks.

model = torch.hub.load('ultralytics/yolov5:classifier', 'custom', path='path/to/model.pt')  # local model

glenn-jocher avatar Jul 30 '22 15:07 glenn-jocher

@glenn-jocher Thank you. Let me try. I bet this is what I am looking for. Really appreciate your help. Tiger

AI-Passionner avatar Aug 01 '22 15:08 AI-Passionner

The classification model was trained as yolov5s. I copied the model file to the new project and. But it didn't work when loading the new model as your suggested scripts. model = torch.hub.load('ultralytics/yolov5:classifier', 'custom', path='weights/yolov5s_exp30/weights/best.pt')

I threw the error message like this. Adding force_reload=True didn't work. Deleting the Cache didn't work. Any suggestion?
Do I need to specify the custom in the script?

"""" Fusing layers... YOLOv5s summary: 117 layers, 4169250 parameters, 0 gradients, 10.4 GFLOPs Adding AutoShape... Traceback (most recent call last): File "C:\Users\tiger.cao/.cache\torch\hub\ultralytics_yolov5_classifier\hubconf.py", line 62, in _create return model.to(device) File "C:\Users\tiger.cao\anaconda3\envs\Yolov5\lib\site-packages\torch\nn\modules\module.py", line 927, in to return self._apply(convert) File "C:\Users\tiger.cao/.cache\torch\hub\ultralytics_yolov5_classifier\models\common.py", line 571, in _apply m.stride = fn(m.stride) File "C:\Users\tiger.cao\anaconda3\envs\Yolov5\lib\site-packages\torch\nn\modules\module.py", line 1207, in getattr raise AttributeError("'{}' object has no attribute '{}'".format( AttributeError: 'Classify' object has no attribute 'stride' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Users\tiger.cao\anaconda3\envs\Yolov5\lib\site-packages\IPython\core\interactiveshell.py", line 3398, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 1, in <cell line: 1> model = torch.hub.load('ultralytics/yolov5:classifier', 'custom', force_reload=True, path='weights/yolov5s_exp30/weights/best.pt') # local model File "C:\Users\tiger.cao\anaconda3\envs\Yolov5\lib\site-packages\torch\hub.py", line 540, in load model = _load_local(repo_or_dir, model, *args, **kwargs) File "C:\Users\tiger.cao\anaconda3\envs\Yolov5\lib\site-packages\torch\hub.py", line 569, in _load_local model = entry(*args, **kwargs) File "C:\Users\tiger.cao/.cache\torch\hub\ultralytics_yolov5_classifier\hubconf.py", line 72, in custom return _create(path, autoshape=autoshape, verbose=_verbose, device=device) File "C:\Users\tiger.cao/.cache\torch\hub\ultralytics_yolov5_classifier\hubconf.py", line 67, in _create raise Exception(s) from e Exception: 'Classify' object has no attribute 'stride'. Cache may be out of date, try force_reload=True or see https://docs.ultralytics.com/yolov5/tutorials/pytorch_hub_model_loading for help. """"

AI-Passionner avatar Aug 01 '22 15:08 AI-Passionner

@AI-Passionner got it. It looks like we need to add some classifier-specific code to hubconf.py to handle these models differently, which makes sense. I'll add a TODO for this.

glenn-jocher avatar Aug 01 '22 17:08 glenn-jocher

@glenn-jocher Thank you. Currently, I copied the folders models/ and utils/ with a little bit of cleaning to the new project. It works.

AI-Passionner avatar Aug 04 '22 17:08 AI-Passionner

@AI-Passionner I think this should work now for newly trained models of type models.yolo.ClassificationModel, (but not for older models of type models.yolo.Model).

I'll run some tests to verify today.

glenn-jocher avatar Aug 04 '22 17:08 glenn-jocher

@glenn-jocher Thank you. I will run the train again and have a try. Tiger

AI-Passionner avatar Aug 04 '22 18:08 AI-Passionner

@AI-Passionner I've confirmed PyTorch Hub loading works in a test just now on Google Colab. I uploaded a recent ImageNet trained YOLOv5m-cls model (about 75 top-1 accuracy after 90 epochs) and then requested it like this. This does not need a local git clone of YOLOv5. https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5m-cls.pt

import torch

model = torch.hub.load('ultralytics/yolov5:classifier', 'custom', 'yolov5m-cls.pt')  # or use local cls model
print(type(model))
Screen Shot 2022-08-04 at 9 11 47 PM

EDIT: Once we release 6.2 with built-in classification support then you no longer need to point torch.hub.load() to the classifier branch, i.e. you can use torch.hub.load('ultralytics/yolov5', ...)

glenn-jocher avatar Aug 04 '22 19:08 glenn-jocher

Great. Really appreciate your effort. YOLOV5 is the best for our object detection project so far, no matter its accuracy and speed.

AI-Passionner avatar Aug 05 '22 03:08 AI-Passionner

I am getting the same issue trying to inference using hub with classification models. Exception: 'Classify' object has no attribute 'stride'. Cache may be out of date, try force_reload=True or see https://docs.ultralytics.com/yolov5/tutorials/pytorch_hub_model_loading for help. classify/predict.py works fine though.

UygarUsta avatar Aug 18 '22 17:08 UygarUsta

@UygarUsta can you please submit a minimum reproducible example?

glenn-jocher avatar Aug 18 '22 18:08 glenn-jocher

@UygarUsta works for me.

Screenshot 2022-08-18 at 20 57 13

glenn-jocher avatar Aug 18 '22 18:08 glenn-jocher

@UygarUsta can you please submit a minimum reproducible example?

image

UygarUsta avatar Aug 18 '22 19:08 UygarUsta

I also tried it with ultralytics cache.No luck.

image

UygarUsta avatar Aug 18 '22 19:08 UygarUsta

@UygarUsta added a CI check for this use case and can now reproduce in https://github.com/ultralytics/yolov5/runs/7905622621?check_suite_focus=true, I'll investigate.

glenn-jocher avatar Aug 18 '22 19:08 glenn-jocher

@UygarUsta good news 😃! Your original issue may now be fixed ✅ in PR #9027. To receive this update:

  • Git – git pull from within your yolov5/ directory or git clone https://github.com/ultralytics/yolov5 again
  • PyTorch Hub – Force-reload model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True)
  • Notebooks – View updated notebooks Open In Colab Open In Kaggle
  • Docker – sudo docker pull ultralytics/yolov5:latest to update your image Docker Pulls

Thank you for spotting this issue and informing us of the problem. Please let us know if this update resolves the issue for you, and feel free to inform us of any other issues you discover or feature requests that come to mind. Happy trainings with YOLOv5 🚀!

glenn-jocher avatar Aug 18 '22 19:08 glenn-jocher

@UygarUsta good news 😃! Your original issue may now be fixed ✅ in PR #9027. To receive this update:

  • Git – git pull from within your yolov5/ directory or git clone https://github.com/ultralytics/yolov5 again
  • PyTorch Hub – Force-reload model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True)
  • Notebooks – View updated notebooks Open In Colab Open In Kaggle
  • Docker – sudo docker pull ultralytics/yolov5:latest to update your image Docker Pulls

Thank you for spotting this issue and informing us of the problem. Please let us know if this update resolves the issue for you, and feel free to inform us of any other issues you discover or feature requests that come to mind. Happy trainings with YOLOv5 🚀!

Unfortunately another error arises.

image

I also tried my luck with ultralytics cache.As always thank you for your hard work . image

UygarUsta avatar Aug 18 '22 19:08 UygarUsta

its not working

import torch

model = torch.hub.load('ultralytics/yolov5:classifier', 'custom', 'yolov5m-cls.pt')  # or use local cls model
print(type(model))

ValueError: Cannot find classifier in https://github.com/ultralytics/yolov5. If it's a commit from a forked repo, please call hub.load() with forked repo directly.

aiakash avatar Jun 22 '23 06:06 aiakash

Understood, @aiakash. The error may be due to the ultralytics/yolov5:classifier hub identifier not yet being available. This is now under investigation. Thank you for your patience.

glenn-jocher avatar Nov 14 '23 17:11 glenn-jocher