pytorch加载模型时出现.....xxx.pth is a zip archive (did you mean to use torch.jit.load()?)

这是因为pytorch1.6以后默认把模型存储为压缩文件,而老版本不是压缩文件,因此识别不了
解决方法:可以在新版本中将模型加载后,再存储为非压缩文件,再用老版本加载:

    def save(self, apath, epoch, is_best=False):
        target = self.get_model()
        torch.save(
            target.state_dict(), 
            os.path.join(apath, 'model', 'model_latest.pt'),
            _use_new_zipfile_serialization=False
        )
        if is_best:
            torch.save(
                target.state_dict(),
                os.path.join(apath, 'model', 'model_best.pt'),
                _use_new_zipfile_serialization=False
            )
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
torch.jit.load()是PyTorch中的一个函数,用于加载经过torch.jit.save()保存的模型。该函数接受一个路径参数,指定保存模型的文件路径,并返回加载后的模型对象。通常在加载模型后,需要通过调用model.eval()方法将模型设置为评估模式。。 该函数的使用有三种方法,可以根据具体情况选择: 1. 使用默认的CPU设备加载模型:model = torch.jit.load(path)。 2. 在GPU上加载模型:设定特定的设备,如loc = torch.device('cuda:0'),然后使用map_location参数指定加载设备: model = torch.jit.load(path, map_location=loc*** 当使用torch.load()加载模型参数,如果出现类似"xxx.pt is a zip archive(did you mean to use torch.jit.load()?)"的错误提示,可能是因为误用了torch.load()函数,应该使用torch.jit.load()来加载模型。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [torch.jit保存,加载模型](https://blog.csdn.net/jacke121/article/details/115539086)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [xxx.pth或者xxx.pt is a zip archive(did you mean to use torch.jit.load()?问题解决](https://blog.csdn.net/dgxl22/article/details/120768772)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值