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

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

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

python 复制代码
import sys  # noqa: F401
import argparse
import os
import struct
import torch


def 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.type


pt_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')
相关推荐
IT231014 小时前
国产OpenClaw产品崛起:博云BoClaw如何破解AI智能体的「安全与自主」双命题
人工智能·安全
小北的AI科技分享14 小时前
API管理的五种路径:五款工具的功能侧写与数据支撑
大数据·人工智能·api管理
m0_5967490914 小时前
Vue.js计算属性computed依赖追踪与副作用函数effect关联机制
jvm·数据库·python
展示猪肝14 小时前
Vue2 + FastAPI + Dify 实现 AI 医疗预检分诊助手:从问诊追问到医生审核闭环
人工智能·vue·fastapi·dify
容器魔方14 小时前
“驾驭工程”下一跳?JiuwenClaw AgentTeam开启“协同工程”全新范式
人工智能·云原生·容器·架构·开源
夕小瑶14 小时前
Codex上线手机端啦!免费用户可用
人工智能
极客老王说Agent14 小时前
2026供应商寻源新范式:实在Agent供应商寻源智能助理核心功能与落地案例深度解析
人工智能·ai·chatgpt
科智咨询14 小时前
2026 AI智能体落地纪实:谁在用?用在哪?
大数据·人工智能·科技·aigc
LedgerNinja14 小时前
Auvera Chain 宣布 AI 驱动的 Layer 2 网络测试网正式上线
人工智能
神明93114 小时前
Golang testing怎么写单元测试_Golang单元测试教程【经典】
jvm·数据库·python