当前位置: 首页 > news >正文

【已解决】YOLO11模型转wts时报错:PytorchStreamReader failed reading zip archive

问题:在把训练好的新YOLO11s模型转wts文件时报错,具体信息如下图(PytorchStreamReader failed reading zip archive: failed finding central directory)

使用

解决:新老版本pytorch之间的兼容问题,改动一下生成wts文件即可。代码帖在下面。

import sys  # noqa: F401
import argparse
import os
import struct
import torchdef parse_args():parser = argparse.ArgumentParser(description='Convert .pt file to .wts')parser.add_argument('-w', '--weights', required=True,help='Input weights (.pt) file path (required)')parser.add_argument('-o', '--output', help='Output (.wts) file path (optional)')parser.add_argument('-t', '--type', type=str, default='detect', choices=['detect', 'cls', 'seg', 'pose', 'obb'],help='determines the model is detection/classification')args = parser.parse_args()if not os.path.isfile(args.weights):raise SystemExit('Invalid input file')if not args.output:args.output = os.path.splitext(args.weights)[0] + '.wts'elif os.path.isdir(args.output):args.output = os.path.join(args.output,os.path.splitext(os.path.basename(args.weights))[0] + '.wts')return args.weights, args.output, args.typept_file, wts_file, m_type = parse_args()print(f'Generating .wts for {m_type} model')# Load model
print(f'Loading {pt_file}')# Initialize
device = 'cpu'# Load model
model = torch.load(pt_file, map_location=device, weights_only=False)  # Load FP32 weights
model = model['ema' if model.get('ema') else 'model'].float()if m_type in ['detect', 'seg', 'pose', 'obb']:anchor_grid = model.model[-1].anchors * model.model[-1].stride[..., None, None]delattr(model.model[-1], 'anchors')model.to(device).eval()with open(wts_file, 'w') as f:f.write('{}\n'.format(len(model.state_dict().keys())))for k, v in model.state_dict().items():vr = v.reshape(-1).cpu().numpy()f.write('{} {} '.format(k, len(vr)))for vv in vr:f.write(' ')f.write(struct.pack('>f', float(vv)).hex())f.write('\n')
http://www.dtcms.com/a/296394.html

相关文章:

  • PyTorch数据选取与索引详解:从入门到高效实践
  • es 和 lucene 的区别
  • 【REACT18.x】CRA+TS+ANTD5.X实现useImperativeHandle让父组件修改子组件的数据
  • R study notes[1]
  • linux入门 相关linux系统操作命令(二)--文件管理系统 ubuntu22.04
  • 二分查找-153-寻找旋转排序数组中的最小值-力扣(LeetCode)
  • unordered_map和unordered_set特性以及解决哈希冲突
  • Gemini拿下IMO2025金牌的提示词解析
  • Redis Lua脚本语法详解
  • Redis ①⑦-分布式锁
  • 跨模态理解的基石:非文本内容向量化方法全景解析
  • Lua协同程序(coroutine)
  • leetcode100.相同的树(递归练习题)
  • Xilinx-FPGA-PCIe-XDMA 驱动内核兼容性问题修复方案
  • 基于单片机睡眠质量/睡眠枕头设计
  • 1.1.2 建筑构造要求
  • 无人机正摄影像自动识别与矢量提取系统
  • 用phpEnv安装Thinkphp8.x出错调试全过程记录
  • C++ 中打开文件的多种方式及相关流类
  • matplotlib的详细知识点
  • k8s之ingress定义https访问方式
  • 【AI News | 20250723】每日AI进展
  • Windows11 本地安装docker Desktop 部署dify 拉取镜像报错
  • iOS Core Data 本地数据库 使用详解:从模型关系到数据操作
  • 技嘉z370主板开启vtx
  • 谈谈ArrayList与Vector的理解?
  • SpringBoot+AI+Web3实战指南
  • Python循环结构
  • 红黑树:高效平衡的终极指南
  • c语言学习(dyas10)