【已解决】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')
相关推荐
const_qiu4 分钟前
第二章 2.4:搭建 Monorepo 骨架 + 共享包 @autix/contracts
java·人工智能·arcgis
喵本喵叁肆4 分钟前
diffprivlib 中文实战教程 · 第8讲:实战——从零改造一个 DP 模型
python·neo4j·差分隐私
luoganttcc6 分钟前
AI 编译器中的 Tiling Shape 推导与选择
人工智能
Smoothcloud润云9 分钟前
PyTorch推理输入长度分桶与Padding优化实战:减少无效计算
人工智能·pytorch·python·深度学习·gpu算力
橙色云-智橙协同研发12 分钟前
汽车零部件行业:如何应对主机厂的严苛审核?
大数据·运维·服务器·人工智能·安全
人邮异步社区14 分钟前
C语言好学,还是Python好学?
c语言·开发语言·python
微三云云仔16 分钟前
2026年10月微三云 GEO 优化技术拆解:AI引用机制、结构化部署与服务商评估框架
人工智能·深度学习·geo系统·微三云
努力变大白20 分钟前
把“会说话的模型“变成“会干活的 Agent“
大数据·人工智能·机器学习
救救孩子把23 分钟前
AI面试八股文-03_3-RAG 系统面试题
人工智能·redis·面试·agent·rag
zx_7414848123 分钟前
YOLOv3 核心原理详解:Darknet-53 架构、多尺度预测与 FPN 特征融合
算法·yolo·目标检测·yolov3