不能pickle的OrderedDict.keys()对象(Py2-bt-py3)

本文探讨了从Python 2.7到Python 3.6迁移过程中使用Django-Redis时出现的TypeError: can't pickle odict_keys objects错误。详细分析了Python 3中OrderedDict.keys()的变化,并提供了相应的解决方案。

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

一则来自django-redis的报错:TypeError: can't pickle odict_keys objects

迁移到Python3时遇到的一个问题,Py3中的OrderedDict.keys()返回的是一个对象:odict_keys。可以迭代的对象,但是不支持index操作,和切片操作。并且不能pickle。所以我就遇到这个坑了。不过它多了一个更新keys的功能,可以看最后的参考链接。

代码如下:


python2.7和Python3.6下分别运行:


参考:

- from the5fire.com 

文章转自:https://www.the5fire.com/


2025-06-04 16:58:12,168 - Grad-CAM-Mask - INFO - 🚀 开始Grad-CAM处理流程... 2025-06-04 16:58:12,169 - Grad-CAM-Mask - INFO - ⚡ 使用设备: cuda 2025-06-04 16:58:12,323 - Grad-CAM-Mask - INFO - 🎯 正在初始化模型... 2025-06-04 16:58:13,837 - Grad-CAM-Mask - INFO - ⚙️ 正在加载模型权重... C:\Users\53145\AppData\Local\Temp\ipykernel_8884\1629840245.py:208: 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't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature. state_dict = torch.load(weights_path, map_location=device) 2025-06-04 16:58:14,519 - Grad-CAM-Mask - INFO - 💾 从 D:/monaisj/3/50_0.7941_0.7.pth加载权重 2025-06-04 16:58:14,545 - Grad-CAM-Mask - INFO - ✅ 权重加载成功 2025-06-04 16:58:14,550 - Grad-CAM-Mask - INFO - 📊 加载了 324/324参数 2025-06-04 16:58:14,552 - Grad-CAM-Mask - INFO - 🖼️ 正在预处理图像数据... 2025-06-04 16:58:14,554 - Grad-CAM-Mask - ERROR - ❌ 预处理失败: required package `itk` is not installed or the version doesn't match requirement. 2025-06-04 16:58:14,554 - Grad-CAM-Mask - ERROR - ❌ 处理失败 什么处理失败?为什么失败?
06-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值