xxx.pt is a zip archive(did you mean to use torch.jit.load()?)“

该博客介绍如何使用PyTorch在1.6及以上版本下,批量加载并转换U2NETPP模型为非zip格式,便于进一步操作。通过循环遍历目录下的模型文件,实现状态字典的加载和保存,保持模型结构完整。

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

```
import torch import os from model import U2NETPP net = U2NETPP(3,1) #网络实例化 model_dir = 'E:\\model_pp\\' model_dir2 = 'E:\\model_pp\\after\\' model_path = [model_dir + f for f in os.listdir(model_dir) if f.endswith('pth')] for file in model_path: if not os.path.exists(model_dir2): os.makedirs(model_dir2, exist_ok=True) print(file) img_name = file.split(os.sep)[-1] aaa = img_name.split(".") bbb = aaa[0:-1] imidx = bbb[0] state_dict = torch.load(file, map_location='cpu') net.load_state_dict(state_dict) torch.save(net.state_dict(),model_dir2+'u2netpp'+imidx+'.pth', _use_new_zipfile_serialization=False)
```

使用torch1.6及以上版本运行,重新加载保存模型即可

循环将路径下的模型文件全部转换为非zip

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值