Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NotImplementedError: cannot instantiate 'PosixPath' on your system #10240

Closed
2 tasks done
VYRION-Ai opened this issue Nov 21, 2022 · 19 comments · May be fixed by #12785
Closed
2 tasks done

NotImplementedError: cannot instantiate 'PosixPath' on your system #10240

VYRION-Ai opened this issue Nov 21, 2022 · 19 comments · May be fixed by #12785
Labels
bug Something isn't working

Comments

@VYRION-Ai
Copy link

Search before asking

  • I have searched the YOLOv5 issues and found no similar bug report.

YOLOv5 Component

No response

Bug

i train classification and when i test it i got this error

raise NotImplementedError("cannot instantiate %r on your system" NotImplementedError: cannot instantiate 'PosixPath' on your system

but when i use yolov5n-cls.pt no error happened

Environment

No response

Minimal Reproducible Example

No response

Additional

No response

Are you willing to submit a PR?

  • Yes I'd like to help by submitting a PR!
@VYRION-Ai VYRION-Ai added the bug Something isn't working label Nov 21, 2022
@glenn-jocher
Copy link
Member

glenn-jocher commented Nov 21, 2022

@totoadel 👋 hi, thanks for letting us know about this possible problem with YOLOv5 🚀. We've created a few short guidelines below to help users provide what we need in order to start investigating a possible problem.

How to create a Minimal, Reproducible Example

When asking a question, people will be better able to provide help if you provide code that they can easily understand and use to reproduce the problem. This is referred to by community members as creating a minimum reproducible example. Your code that reproduces the problem should be:

  • Minimal – Use as little code as possible to produce the problem
  • Complete – Provide all parts someone else needs to reproduce the problem
  • Reproducible – Test the code you're about to provide to make sure it reproduces the problem

For Ultralytics to provide assistance your code should also be:

  • Current – Verify that your code is up-to-date with GitHub master, and if necessary git pull or git clone a new copy to ensure your problem has not already been solved in master.
  • Unmodified – Your problem must be reproducible using official YOLOv5 code without changes. Ultralytics does not provide support for custom code ⚠️.

If you believe your problem meets all the above criteria, please close this issue and raise a new one using the 🐛 Bug Report template with a minimum reproducible example to help us better understand and diagnose your problem.

Thank you! 😃

@V4A001
Copy link

V4A001 commented Jan 6, 2023

Suddenly, I run into exact the same situation. No idea how to get fore or backward with it. Did you get out of this situation?

`
python segment/predict.py --device cpu --weights best.pt --source data/images/bus.jpg
segment\predict: weights=['best.pt'], source=data/images/bus.jpg, data=data\coco128.yaml, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=cpu, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs\predict-seg, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False, vid_stride=1, retina_masks=False
YOLOv5 v7.0-56-gc0ca1d2 Python-3.10.8 torch-1.13.1+cpu CPU

Traceback (most recent call last):
File "D:\myfolders\yolov5\segment\predict.py", line 284, in
main(opt)
File "D:\myfolders\yolov5\segment\predict.py", line 279, in main
run(**vars(opt))
File "C:\Users\me\anaconda3\envs\v4a004\lib\site-packages\torch\autograd\grad_mode.py", line 27, in decorate_context
return func(*args, **kwargs)
File "D:\myfolders\yolov5\segment\predict.py", line 101, in run
model = DetectMultiBackend(weights, device=device, dnn=dnn, data=data, fp16=half)
File "D:\myfolders\yolov5\models\common.py", line 345, in init
model = attempt_load(weights if isinstance(weights, list) else w, device=device, inplace=True, fuse=fuse)
File "D:\myfolders\yolov5\models\experimental.py", line 79, in attempt_load
ckpt = torch.load(attempt_download(w), map_location='cpu') # load
File "C:\Users\me\anaconda3\envs\v4a004\lib\site-packages\torch\serialization.py", line 789, in load
return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
File "C:\Users\me\anaconda3\envs\v4a004\lib\site-packages\torch\serialization.py", line 1131, in _load
result = unpickler.load()
File "C:\Users\me\anaconda3\envs\v4a004\lib\pathlib.py", line 962, in new
raise NotImplementedError("cannot instantiate %r on your system"
NotImplementedError: cannot instantiate 'PosixPath' on your system
`

@akashAD98
Copy link
Contributor

@V4A001 @glenn-jocher im also facing same issue with classification, on my local system using on cpu

@danielsoy
Copy link

danielsoy commented Feb 1, 2023

me too.
I can predict.py in my local PC, only with a model trained locally.
But got the same error, trying to predict locally, with best.pt or last.pt trained in Colab.
The model trained in colab, predicts well in colab.

@ZYM66
Copy link

ZYM66 commented Feb 2, 2023

I got the same problem.
I train my model using Google Colab and I predict it on my windows computer, it didn't work, and raised this exception.

@ljubantomic01
Copy link

I just added:

import pathlib
temp = pathlib.PosixPath
pathlib.PosixPath = pathlib.WindowsPath

...in my Jupyter Notebook and it works now.

This error ocurred when I wanted to load classification model with torch.hub.load.

@glenn-jocher
Copy link
Member

@ljubanradisin @V4A001 Thank you for bringing this issue to our attention and sharing your experience.

It seems that the issue you are facing is related to the PosixPath class in the pathlib module. The error message suggests that PosixPath cannot be instantiated on your system.

Based on your feedback, it appears that substituting pathlib.PosixPath with pathlib.WindowsPath in your code resolves the issue. This modification allows the code to execute successfully on Windows platforms.

We appreciate you sharing the workaround you found. This information will be helpful to other users encountering the same problem when working with classification models and using torch.hub.load() function.

Thank you for your contributions, and please don't hesitate to reach out if you have any further questions or issues.

Best regards,
Glenn Jocher

@falahfin
Copy link

permission to add for those who are new to the world of coding like me, don't forget to add the import path if the code like the one above given by ljubantomic01 if you has a problem say "NameError: name 'Path' is not defined."

import pathlib
from pathlib import Path
pathlib.PosixPath = pathlib.WindowsPath

@glenn-jocher
Copy link
Member

@falahfin Absolutely, thank you for pointing that out! It's important to ensure that all necessary modules and classes are imported when modifying code to prevent NameError issues. Your addition of the import statement for Path from pathlib is essential for the code to recognize the Path class.

For anyone else encountering similar issues, make sure to include the appropriate import statements in your code:

import pathlib
from pathlib import Path
pathlib.PosixPath = pathlib.WindowsPath

This will help ensure that your code runs smoothly on Windows systems when dealing with file paths.

If you have any more questions or need further assistance, feel free to ask. Happy coding! 😊

@devendew
Copy link

devendew commented Feb 5, 2024

The above solutions are not working for me in Windows

@glenn-jocher
Copy link
Member

@devendew i'm sorry to hear that the workaround isn't working for you. This issue typically arises when a model trained on a Unix-like system (such as Linux or macOS) is loaded on a Windows system. The PosixPath object, which is used for Unix-style paths, is not natively compatible with Windows.

Here's a step-by-step approach to try and resolve the issue:

  1. Update Your Code: Ensure that you've placed the workaround code at the very beginning of your script or notebook, before importing any other modules that might use pathlib.

    import pathlib
    pathlib.PosixPath = pathlib.WindowsPath
  2. Update YOLOv5: Make sure you have the latest version of YOLOv5 from the GitHub repository. Sometimes these issues are resolved in newer releases.

  3. Check Your Environment: Verify that your Python environment is set up correctly and that you're using compatible versions of Python and PyTorch.

  4. Use Absolute Paths: When specifying paths in your code, use absolute paths and ensure they are formatted correctly for Windows (using backslashes \ or double backslashes \\).

  5. Re-train if Necessary: If the model was trained on a Unix-like system, consider re-training it on your Windows system if possible. This can sometimes circumvent compatibility issues.

  6. Manual Path Conversion: As a last resort, you can manually convert PosixPath objects to WindowsPath objects in the code where the error occurs. This would require identifying where in the code the PosixPath is instantiated and replacing it with a WindowsPath.

If you've tried all of the above and the issue persists, it might be helpful to provide more details about the error and your environment. This would include the exact error message, the code snippet where the error occurs, and details about your Python and PyTorch versions. With more information, the community may be able to offer more targeted assistance.

@MaxErikssons
Copy link

@devendew
I encountered the NotImplementedError: cannot instantiate 'PosixPath' on your system error when trying to run inference with a model trained on Kaggle (a Linux environment) on my Windows machine. The workaround suggested by @ljubantomic01 works for me by placing:

import pathlib
temp = pathlib.PosixPath
pathlib.PosixPath = pathlib.WindowsPath

at the beginning of the detect.py file.

@sajjadalik442
Copy link

@devendew thanks adding

import pathlib temp = pathlib.PosixPath pathlib.PosixPath = pathlib.WindowsPath

to detect.py helped

@rochellemadulara
Copy link

rochellemadulara commented Mar 30, 2024

@glenn-jocher ljubantomic01 Hi, thank you for solving this problem. However, I run some problem. Btw, I am doing multiprocessing for yolvo5. My code is running already when I added the import pathlib pathlib.PosixPath = pathlib.WindowsPath
but the object detection is not working. I tried to run the model that I trained locally the object detection works fine, but then if I run the trained model from Google colab the detection is not working, only the video is playing.

@glenn-jocher
Copy link
Member

@rochellemadulara, it's great to hear that the initial issue with pathlib has been resolved by adapting the PosixPath to WindowsPath for Windows compatibility! Regarding the problem with object detection not working with the model trained on Google Colab, it sounds like there might be differences in the model's environment or dependencies between your local setup and Colab.

A few quick suggestions:

  • Ensure Compatibility: Verify that the versions of YOLOv5 and its dependencies (like PyTorch) are the same in your local environment as they were in Google Colab when you trained the model.
  • Check Model Path: Ensure the path to the model file is correctly specified in your code. It might be getting overlooked due to path differences between systems.
  • Inspect Model Outputs: Check if the model is producing any detections at all by printing the outputs before they are supposed to be displayed or used for further processing.

This minimal change in debugging approach could provide insights into whether the issue is with the detection process itself or with the display of the detections.

@floresdwm
Copy link

I just added:

import pathlib temp = pathlib.PosixPath pathlib.PosixPath = pathlib.WindowsPath

...in my Jupyter Notebook and it works now.

This error ocurred when I wanted to load classification model with torch.hub.load.

Thanks a lot, works for me!

Just for future references:

If you have a windows to solve use:
from pathlib import Path
import pathlib
temp = pathlib.PosixPath
pathlib.PosixPath = pathlib.WindowsPath

If you have linux (or deploying for linux) use:
from pathlib import Path
import pathlib
temp = pathlib.PosixPath
pathlib.WindowsPath = pathlib.PosixPath

@glenn-jocher
Copy link
Member

@floresdwm i'm glad to hear that the workaround resolved your issue! Your contribution in sharing the solution for both Windows and Linux users is greatly appreciated by the community. 😊

Just a quick addition:

  • For Windows users:
from pathlib import Path
import pathlib
temp = pathlib.PosixPath
pathlib.PosixPath = pathlib.WindowsPath
  • For Linux users (or deploying for Linux):
from pathlib import Path
import pathlib
temp = pathlib.WindowsPath
pathlib.WindowsPath = pathlib.PosixPath

Happy coding and don't hesitate to reach out if you face any more issues!

@linaMallek
Copy link

This didn't work for me

@glenn-jocher
Copy link
Member

I'm sorry to hear that the solution didn't work for you. Could you please ensure that the snippet is placed correctly at the beginning of your script, and check if any other imports might interfere with this workaround? If the issue persists, feel free to provide more details about your environment and error outputs for further assistance. 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet