ultralytics / yolov5

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

Exception: cannot instantiate 'WindowsPath' on your system. Cache may be out of date #12911

Closed Codefreediver closed 5 months ago

Codefreediver commented 7 months ago

Search before asking

Question

Hello friends,

I'm having problem for the inference stage of this Kaggle competition: https://www.kaggle.com/competitions/tensorflow-great-barrier-reef

I trained my model using the following code:

FOLD`      = 1 
DIM       = 2400
MODEL     = 'yolov5n'
BATCH     = 4
EPOCHS    = 10
OPTMIZER  = 'Adam'

!python train.py" --img {DIM}\
--batch {BATCH}\
--epochs {EPOCHS}\
--optimizer {OPTMIZER}\
--data "D:\Kaggle\cots\gbr.yaml"\
--hyp "D:\Kaggle\cots\hyp.yaml"\
--weights {MODEL}.pt\

I download the pretrain weights "best.pt" from Wandb, than I uploaded it to my Kaggle notebook dataset. I tried to load the pretrain model for inference, but I'm really having a hard time:

import torch
ROOT_DIR  = '/kaggle/input/tensorflow-great-barrier-reef/'

CKPT_PATH = '/kaggle/input/tfeqimg-2400/Final.pt' 
IMG_SIZE  = 2400
CONF      = 0.25
IOU       = 0.40
AUGMENT   = True
model = torch.hub.load('/kaggle/input/yolov7-0/yolov5-7.0', 'custom', source='local',force_reload = True , path=CKPT_PATH)`

And the error is:

`YOLOv5 πŸš€ 2024-4-12 Python-3.10.13 torch-2.1.2+cpu CPU

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
File /kaggle/input/yolov7-0/yolov5-7.0/hubconf.py:49, in _create(name, pretrained, channels, classes, autoshape, verbose, device)
     48 try:
---> 49     model = DetectMultiBackend(path, device=device, fuse=autoshape)  # detection model
     50     if autoshape:

File /kaggle/input/yolov7-0/yolov5-7.0/models/common.py:345, in DetectMultiBackend.__init__(self, weights, device, dnn, data, fp16, fuse)
    344 if pt:  # PyTorch
--> 345     model = attempt_load(weights if isinstance(weights, list) else w, device=device, inplace=True, fuse=fuse)
    346     stride = max(int(model.stride.max()), 32)  # model stride

File /kaggle/input/yolov7-0/yolov5-7.0/models/experimental.py:79, in attempt_load(weights, device, inplace, fuse)
     78 for w in weights if isinstance(weights, list) else [weights]:
---> 79     ckpt = torch.load(attempt_download(w), map_location='cpu')  # load
     80     ckpt = (ckpt.get('ema') or ckpt['model']).to(device).float()  # FP32 model

File /opt/conda/lib/python3.10/site-packages/torch/serialization.py:1014, in load(f, map_location, pickle_module, weights_only, mmap, **pickle_load_args)
   1013                 raise pickle.UnpicklingError(UNSAFE_MESSAGE + str(e)) from None
-> 1014         return _load(opened_zipfile,
   1015                      map_location,
   1016                      pickle_module,
   1017                      overall_storage=overall_storage,
   1018                      **pickle_load_args)
   1019 if mmap:

File /opt/conda/lib/python3.10/site-packages/torch/serialization.py:1422, in _load(zip_file, map_location, pickle_module, pickle_file, overall_storage, **pickle_load_args)
   1421 unpickler.persistent_load = persistent_load
-> 1422 result = unpickler.load()
   1424 torch._utils._validate_loaded_sparse_tensors()

File /opt/conda/lib/python3.10/pathlib.py:962, in Path.__new__(cls, *args, **kwargs)
    961 if not self._flavour.is_supported:
--> 962     raise NotImplementedError("cannot instantiate %r on your system"
    963                               % (cls.__name__,))
    964 return self

NotImplementedError: cannot instantiate 'WindowsPath' on your system

During handling of the above exception, another exception occurred:

NotImplementedError                       Traceback (most recent call last)
File /kaggle/input/yolov7-0/yolov5-7.0/hubconf.py:60, in _create(name, pretrained, channels, classes, autoshape, verbose, device)
     59     except Exception:
---> 60         model = attempt_load(path, device=device, fuse=False)  # arbitrary model
     61 else:

File /kaggle/input/yolov7-0/yolov5-7.0/models/experimental.py:79, in attempt_load(weights, device, inplace, fuse)
     78 for w in weights if isinstance(weights, list) else [weights]:
---> 79     ckpt = torch.load(attempt_download(w), map_location='cpu')  # load
     80     ckpt = (ckpt.get('ema') or ckpt['model']).to(device).float()  # FP32 model

File /opt/conda/lib/python3.10/site-packages/torch/serialization.py:1014, in load(f, map_location, pickle_module, weights_only, mmap, **pickle_load_args)
   1013                 raise pickle.UnpicklingError(UNSAFE_MESSAGE + str(e)) from None
-> 1014         return _load(opened_zipfile,
   1015                      map_location,
   1016                      pickle_module,
   1017                      overall_storage=overall_storage,
   1018                      **pickle_load_args)
   1019 if mmap:

File /opt/conda/lib/python3.10/site-packages/torch/serialization.py:1422, in _load(zip_file, map_location, pickle_module, pickle_file, overall_storage, **pickle_load_args)
   1421 unpickler.persistent_load = persistent_load
-> 1422 result = unpickler.load()
   1424 torch._utils._validate_loaded_sparse_tensors()

File /opt/conda/lib/python3.10/pathlib.py:962, in Path.__new__(cls, *args, **kwargs)
    961 if not self._flavour.is_supported:
--> 962     raise NotImplementedError("cannot instantiate %r on your system"
    963                               % (cls.__name__,))
    964 return self

NotImplementedError: cannot instantiate 'WindowsPath' on your system

The above exception was the direct cause of the following exception:

Exception                                 Traceback (most recent call last)
Cell In[6], line 3
      1 import torch
----> 3 model = torch.hub.load('/kaggle/input/yolov7-0/yolov5-7.0', 'custom', source='local',force_reload = True , path=CKPT_PATH)

File /opt/conda/lib/python3.10/site-packages/torch/hub.py:566, in load(repo_or_dir, model, source, trust_repo, force_reload, verbose, skip_validation, *args, **kwargs)
    562 if source == 'github':
    563     repo_or_dir = _get_cache_or_reload(repo_or_dir, force_reload, trust_repo, "load",
    564                                        verbose=verbose, skip_validation=skip_validation)
--> 566 model = _load_local(repo_or_dir, model, *args, **kwargs)
    567 return model

File /opt/conda/lib/python3.10/site-packages/torch/hub.py:595, in _load_local(hubconf_dir, model, *args, **kwargs)
    592     hub_module = _import_module(MODULE_HUBCONF, hubconf_path)
    594     entry = _load_entry_from_hubconf(hub_module, model)
--> 595     model = entry(*args, **kwargs)
    597 return model

File /kaggle/input/yolov7-0/yolov5-7.0/hubconf.py:83, in custom(path, autoshape, _verbose, device)
     81 def custom(path='path/to/model.pt', autoshape=True, _verbose=True, device=None):
     82     # YOLOv5 custom or local model
---> 83     return _create(path, autoshape=autoshape, verbose=_verbose, device=device)

File /kaggle/input/yolov7-0/yolov5-7.0/hubconf.py:78, in _create(name, pretrained, channels, classes, autoshape, verbose, device)
     76 help_url = '[https://github.com/ultralytics/yolov5/issues/36](https://github.com/ultralytics/yolov5/issues/36%3C/span%3E%3Cspan) style="color:rgb(175,0,0)">'
     77 s = f'{e}. Cache may be out of date, try `force_reload=True` or see {help_url} for help.'
---> 78 raise Exception(s) from e

Exception: cannot instantiate 'WindowsPath' on your system. Cache may be out of date, try `force_reload=True` or see https://github.com/ultralytics/yolov5/issues/36 for help.

As a newbie to both Kaggle and Yolo, I tried many solutions but failed. Please help me, thank you.

Additional

No response

github-actions[bot] commented 7 months ago

πŸ‘‹ Hello @Codefreediver, thank you for your interest in YOLOv5 πŸš€! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a πŸ› Bug Report, please provide a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Requirements

Python>=3.8.0 with all requirements.txt installed including PyTorch>=1.8. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

YOLOv5 CI

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training, validation, inference, export and benchmarks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

Introducing YOLOv8 πŸš€

We're excited to announce the launch of our latest state-of-the-art (SOTA) object detection model for 2023 - YOLOv8 πŸš€!

Designed to be fast, accurate, and easy to use, YOLOv8 is an ideal choice for a wide range of object detection, image segmentation and image classification tasks. With YOLOv8, you'll be able to quickly and accurately detect objects in real-time, streamline your workflows, and achieve new levels of accuracy in your projects.

Check out our YOLOv8 Docs for details and get started with:

pip install ultralytics
glenn-jocher commented 7 months ago

Hello! Thanks for reaching out. 😊 It seems the issue you're encountering is related to the path handling on different operating systems, specifically when loading your model weights. This is a common snag when paths formatted for Windows systems (using WindowsPath) are used in environments that do not support this path type, such as Linux-based systems often used in Kaggle notebooks.

The error message NotImplementedError: cannot instantiate 'WindowsPath' on your system suggests that the path object specific to Windows is not recognized on the Kaggle platform. As a solution, make sure your paths are defined in a way that's compatible with Unix-like systems, which typically use forward slashes (/) for paths. Also, ensure that your file paths are correctly pointing to the location where your weights are stored on Kaggle.

Here's a slight adjustment to your code where you load the model:

import torch

# Ensure the path is correctly specified for Unix-like systems
CKPT_PATH = '/kaggle/input/tfeqimg-2400/Final.pt'

# Loading the model
model = torch.hub.load('kaggle/input/yolov7-0/yolov5-7.0', 'custom', source='local', path=CKPT_PATH, force_reload=True)

Please double-check that the path '/kaggle/input/tfeqimg-2400/Final.pt' actually leads to your weights file in the Kaggle environment.

If you continue to have issues or face any other questions, feel free to ask. The YOLO community and the Ultralytics team are always here to help. Happy coding! πŸš€

mgnavarrete commented 6 months ago

I encountered the error cannot instantiate 'WindowsPath' on your system, and none of the suggested solutions helped.

To provide some context, I was trying to run a custom YOLOv5 model on CVAT or simply on my WSL, but every time I attempted it, I got the same error.

I tried various solutions, but none worked. Eventually, I realized that I had trained my model on a Windows system using Windows CMD. This caused the error to persist when I tried to run the model on Linux, Ubuntu, or WSL on Windows.

What I ended up doing was retraining the model, this time using an Ubuntu system. I used Google Colab for training, then tested the model on Ubuntu and CVAT, and it worked fine.

I created a repository with the Jupyter notebook and the data that I used to train the model on Colab.

I hope this helps anyone encountering a similar problem, or perhaps it could fix the 'PosixPath' error to.

glenn-jocher commented 6 months ago

Hi there! 🌟 Thank you for sharing your solution with the community. It's insightful and helpful!

Your observation correctly highlights a common issue when switching between Windows and Linux-based environments, like WSL or Ubuntu. The path handling between these systems can indeed cause the cannot instantiate 'WindowsPath' on your system error due to differences in their file system path formats.

For those encountering similar issues, it's a good practice to ensure compatibility by using platform-agnostic paths within your code. Python's Pathlib library can be particularly useful for this, as it automatically adapts paths according to the operating system. Here's a quick example:

from pathlib import Path

# Create a path that's compatible with your OS
model_path = Path("/path/to/your/model")

# Now you can use model_path in a way that's OS-independent

This approach can help avoid such errors without needing to switch your development environment or retrain your models on a different OS.

Again, thanks for sharing your workaround. Happy coding, and keep contributing to the community! πŸš€

github-actions[bot] commented 5 months ago

πŸ‘‹ Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLO πŸš€ and Vision AI ⭐

onetwo1 commented 3 months ago

This can run successfully!!

plt = platform.system()
if plt == 'Windows':
    pathlib.PosixPath = pathlib.WindowsPath
else:
    pathlib.WindowsPath = pathlib.PosixPath
model = DetectMultiBackend("xxx.pt")
glenn-jocher commented 3 months ago

Hello! 😊

Thank you for sharing your solution. It's great to hear that you found a workaround to handle the path compatibility issue between Windows and Linux systems. Your approach of conditionally setting the pathlib path types based on the operating system is clever and can indeed help in many scenarios.

For others who might encounter similar issues, here's a slightly refined version of your code snippet that ensures compatibility across different platforms:

import platform
import pathlib

# Check the operating system and set the appropriate path type
if platform.system() == 'Windows':
    pathlib.PosixPath = pathlib.WindowsPath
else:
    pathlib.WindowsPath = pathlib.PosixPath

# Load the model
model = DetectMultiBackend("xxx.pt")

This snippet dynamically adjusts the path handling based on the detected operating system, which can be very useful when working in cross-platform environments.

If you encounter any further issues or have additional questions, please ensure you are using the latest version of YOLOv5 and its dependencies. The community and the Ultralytics team are always here to help. Keep up the great work, and happy coding! πŸš€