【已解决】cannot instantiate ‘PosixPath‘ on your system
·
问题描述
在使用预训练模型的时候,加载了官方提供的预训练模型出现以下问题:
File "D:\ImageClassification\BiFormer_Demo\train.py", line 196, in <module>
model_ft = biformer_tiny(pretrained=True)
File "D:\ImageClassification\BiFormer_Demo\models\biformer.py", line 333, in biformer_tiny
checkpoint = torch.hub.load_state_dict_from_url(url=url, map_location="cpu", check_hash=True, file_name=f"{model_key}.pth")
File "D:\Users\wh109\anaconda3\lib\site-packages\torch\hub.py", line 731, in load_state_dict_from_url
return torch.load(cached_file, map_location=map_location)
File "D:\Users\wh109\anaconda3\lib\site-packages\torch\serialization.py", line 712, in load
return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
File "D:\Users\wh109\anaconda3\lib\site-packages\torch\serialization.py", line 1049, in _load
result = unpickler.load()
File "D:\Users\wh109\anaconda3\lib\pathlib.py", line 1084, in __new__
raise NotImplementedError("cannot instantiate %r on your system"
NotImplementedError: cannot instantiate 'PosixPath' on your system
本地的电脑是Win11环境。

问题原因
通过在网上搜索同类的问题分析:应该是PosixPath不支持Window的路径。所以需要做修改,来适应Win11环境。
解决方法
点击报错的脚本pathlib.py,将下面的代码:
def __new__(cls, *args, **kwargs):
if cls is Path:
cls = WindowsPath if os.name == 'nt' else PosixPath
self = cls._from_parts(args, init=False)
if not self._flavour.is_supported:
raise NotImplementedError("cannot instantiate %r on your system"
% (cls.__name__,))
self._init()
return self

修改为:
#修改后
def __new__(cls, *args, **kwargs):
if cls is Path:
# cls = WindowsPath if os.name == 'nt' else PosixPath
cls = WindowsPath
self = cls._from_parts(args, init=False)
# Windows doesn't support PosixPath
if type(self) == PosixPath:
cls = WindowsPath
self = cls._from_parts(args, init=False)
if not self._flavour.is_supported:
raise NotImplementedError("cannot instantiate %r on your system"
% (cls.__name__,))
self._init()
return self

然后,就可以正常运行了。
总结
出现这个问题,主要是Win11的兼容问题。如果有条件还是使用Ubuntu系统。

专栏目录:神经网络精讲与实战
这篇文章,是对专栏的总目录,方便大家查看文章。这个专栏我计划整理一些经典常用的主干网络模型,对其进行讲解和实战。由浅入深,逐步增加深度,让大家更容易接受。
PDF版的文章和实战代码以及数据集,我会放到网盘上,大家在文章的末尾可以看到。
AlexNet
VGGNet
GoogLeNet
Inception V2——V4
第十六篇 Inception V2、Inception V3、Inception V4模型详解
ResNet
DenseNet

SE-ResNet

更多推荐




所有评论(0)