加载pickle文件报错ModuleNotFoundError: No module named numpy.core.multiarray

博客讨论了在不同操作系统之间使用pickle文件遇到的ModuleNotFoundError问题,特别是在Windows和Linux之间。错误可能是由于文件在不同系统下以文本模式存储导致的不兼容。解决方法包括在源系统上解析并重新保存pickle文件,确保使用'wb'模式,并考虑Python版本差异。此外,Python2和Python3之间的pickle文件兼容性也是需要注意的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

welcome to my blog

3种可能的错误原因
  1. 看看有没有安装numpy, 导入numpy看看会不会报错, import numpy as np
  2. 读取文件的模式’r’和’rb’分别试一下
  3. 升级numpy, pip install --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple numpy,很多人都是升级numpy后解决的问题, 但是我尝试了各种版本的numpy都不行, 一直报这个错误, 最终在这个提示下终于发现了问题, 希望能够帮助大家
错误原因4: 别人在windows下使用pickle.dump(obj, 'filename', 'w')生成的pickle文件, 我在linux下读取这个文件

'w’这种模式下, python将数据以文本的形式进行存储, windows下涉及文本的符号和linux的并不完全对应, 所以在linux下使用python的pickle模块解析该pickle文件会报错. 我突然想起来去年有位大佬跟我说使用pickle可能会产生问题, 因为会跟系统环境有关, 当时我还不理解… 原来是这么一回事, 踩过坑之后就能记住了

最简单的解决办法

在windows下使用pickle解析该文件, 再以’wb’模式生成pickle文件, 代码如下

# 在windows下使用pickle解析该文件
with open('train_id.pickle','r') as f:
    string = f.read()
    b = bytes(string,'ascii')
    res = pickle.loads(b)
    
# 再以'wb'模式生成pickle文件
with open('train_id2.pickle','wb') as f:
	pickle.dump(res, f)

# 至此问题已解决    
# 以'rb'模式读取新的pickle文件即可
with open('train_id2.pickle','rb') as f:
	res = pickle.load(f)
突如其来的新问题及解决办法

有个pickle文件在windows下也无法解析, 进一步了解到, 即使在windows下使用’wb’模式生成pickle文件, 在另一台windows下可不一定能够解析!
根本原因: 两台windows上的python版本不同, 一台用的python2, 另一台用的python3
python2和python3在生成pickle时也有差距, 换句话说, python2使用’wb’模式生成的pickle文件, 使用python3不一定能够解析
解决办法:安装python2, 安装numpy, 重新解析该pickle文件, 根据数据的类型, 以普通的方式写到硬盘上, 不用pickle了

所以说, python版本的不同, 系统的不同都会给pickle的使用带来麻烦…

C:\Users\asus\.conda\envs\yolov8\python.exe "C:\Users\asus\Desktop\yolov8(1)\yolov8-20231031\yolov8-main\autolabel.py" C:\Users\asus\.conda\envs\yolov8\lib\site-packages\timm\models\layers\__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", FutureWarning) C:\Users\asus\Desktop\yolov8(1)\yolov8-20231031\yolov8-main\ultralytics\nn\extra_modules\ops_dcnv3\functions\dcnv3_func.py:26: FutureWarning: `torch.cuda.amp.custom_fwd(args...)` is deprecated. Please use `torch.amp.custom_fwd(args..., device_type=&#39;cuda&#39;)` instead. def forward( C:\Users\asus\Desktop\yolov8(1)\yolov8-20231031\yolov8-main\ultralytics\nn\extra_modules\ops_dcnv3\functions\dcnv3_func.py:62: FutureWarning: `torch.cuda.amp.custom_bwd(args...)` is deprecated. Please use `torch.amp.custom_bwd(args..., device_type=&#39;cuda&#39;)` instead. def backward(ctx, grad_output): C:\Users\asus\.conda\envs\yolov8\lib\site-packages\timm\models\helpers.py:7: FutureWarning: Importing from timm.models.helpers is deprecated, please import via timm.models warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.models", FutureWarning) C:\Users\asus\.conda\envs\yolov8\lib\site-packages\timm\models\registry.py:4: FutureWarning: Importing from timm.models.registry is deprecated, please import via timm.models warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.models", FutureWarning) ============================================================ YOLOv8 高分辨率小目标检测系统 ============================================================ 优化特性: 1. 支持4K(3840x2160)分辨率采集 2. 专用高分辨率存储目录 3. SAHI切片技术增强小目标检测 4. 智能跳帧平衡性能与质量 ------------------------------------------------------------ ✅ 创建目录: dataset\images ✅ 创建目录: dataset\labels ✅ 创建目录: dataset\log ✅ 创建目录: dataset\highres C:\Users\asus\Desktop\yolov8(1)\yolov8-20231031\yolov8-main\ultralytics\nn\tasks.py:625: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don&#39;t have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature. return torch.load(file, map_location=&#39;cpu&#39;), file # load WARNING best.pt appears to require &#39;numpy._core&#39;, which is not in ultralytics requirements. AutoInstall will run now for &#39;numpy._core&#39; but this feature will be removed in the future. Recommend fixes are to train a new model using the latest &#39;ultralytics&#39; package or to run a command with an official YOLOv8 model, i.e. &#39;yolo predict model=yolov8n.pt&#39; C:\Users\asus\Desktop\yolov8(1)\yolov8-20231031\yolov8-main\ultralytics\nn\tasks.py:641: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don&#39;t have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature. return torch.load(file, map_location=&#39;cpu&#39;), file # load Traceback (most recent call last): File "C:\Users\asus\Desktop\yolov8(1)\yolov8-20231031\yolov8-main\ultralytics\nn\tasks.py", line 625, in torch_safe_load return torch.load(file, map_location=&#39;cpu&#39;), file # load File "C:\Users\asus\.conda\envs\yolov8\lib\site-packages\torch\serialization.py", line 1097, in load return _load( File "C:\Users\asus\.conda\envs\yolov8\lib\site-packages\torch\serialization.py", line 1525, in _load result = unpickler.load() File "C:\Users\asus\.conda\envs\yolov8\lib\site-packages\torch\serialization.py", line 1515, in find_class return super().find_class(mod_name, name) ModuleNotFoundError: No module named &#39;numpy._core&#39; During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\asus\Desktop\yolov8(1)\yolov8-20231031\yolov8-main\autolabel.py", line 169, in <module> realtime_detection_and_capture( File "C:\Users\asus\Desktop\yolov8(1)\yolov8-20231031\yolov8-main\autolabel.py", line 82, in realtime_detection_and_capture model = YOLO(model_path) File "C:\Users\asus\Desktop\yolov8(1)\yolov8-20231031\yolov8-main\ultralytics\engine\model.py", line 98, in __init__ self._load(model, task) File "C:\Users\asus\Desktop\yolov8(1)\yolov8-20231031\yolov8-main\ultralytics\engine\model.py", line 150, in _load self.model, self.ckpt = attempt_load_one_weight(weights) File "C:\Users\asus\Desktop\yolov8(1)\yolov8-20231031\yolov8-main\ultralytics\nn\tasks.py", line 687, in attempt_load_one_weight ckpt, weight = torch_safe_load(weight) # load ckpt File "C:\Users\asus\Desktop\yolov8(1)\yolov8-20231031\yolov8-main\ultralytics\nn\tasks.py", line 641, in torch_safe_load return torch.load(file, map_location=&#39;cpu&#39;), file # load File "C:\Users\asus\.conda\envs\yolov8\lib\site-packages\torch\serialization.py", line 1097, in load return _load( File "C:\Users\asus\.conda\envs\yolov8\lib\site-packages\torch\serialization.py", line 1525, in _load result = unpickler.load() File "C:\Users\asus\.conda\envs\yolov8\lib\site-packages\torch\serialization.py", line 1515, in find_class return super().find_class(mod_name, name) ModuleNotFoundError: No module named &#39;numpy._core&#39; 进程已结束,退出代码1
最新发布
08-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值