当前位置: 首页 > 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.xdnf.cn/news/1179055.html

相关文章:

  • C++实现精确延时的方法
  • 鸿蒙平台运行Lua脚本
  • 论文阅读:《无约束多目标优化的遗传算法,群体和进化计算》
  • 【Word Press进阶】自定义区块的行为与样式
  • Linux(centos7)安装 docker + ollama+ deepseek-r1:7b + Open WebUI(内含一键安装脚本)
  • Terraform与Ansible的关系
  • MCNN-BiLSTM-Attention分类预测模型等!
  • 行为型模式-协作与交互机制
  • fabric搭建基础的测试网络
  • 时序数据库IoTDB的核心功能特性
  • 重构数据库未来:金仓数据库,抢占 AI 原生时代先机
  • Java 大视界 -- Java 大数据在智能教育自适应学习路径规划与学习效果强化中的应用(362)
  • [数据结构]#7 哈希表
  • 造成服务器内存不足的原因有什么
  • Lua(垃圾回收)
  • 跨境支付入门~国际支付结算(电商篇)
  • Leetcode—1035. 不相交的线【中等】
  • 深度解析:在Odoo 18中基于原生Owl框架为PWA定制功能丰富的底部导航栏
  • 磁性材料如何破解服务器电源高频损耗难题?
  • Vue2——5
  • Linux系统编程——网络
  • 【物联网】基于树莓派的物联网开发【16】——树莓派GPIO控制LED灯实验
  • 使用 eBPF 实时捕获 TCP 重传告警:精准定位网络抖动问题
  • 亚马逊云科技:引领云计算新时代,开启无限可能
  • OSPF多区域介绍
  • Android Telephony UrspRule 介绍
  • Java设计模式-适配器模式
  • Docker4-容器化企业级应用
  • 不同头会关注输入序列中不同的部分和不同维度所蕴含的信息,这里的头和嵌入维度不是对应的,仅仅是概念上的吗?
  • 调节广告adload的算法:Contextual Bandits、多臂老虎机 Policy Gradient、Q-learning