You need to enable JavaScript to run this app. 已训练的Yolo-V3模型如何转换为可用的TensorFlow Lite模型?
优惠活动
大模型
产品
解决方案
定价
更多

已训练的Yolo-V3模型如何转换为可用的TensorFlow Lite模型?

YOLOv3门检测模型转TensorFlow Lite问题解决方案

1. TXT标注批量转CSV/XML格式(无需手动重标)

  • 首先确认你的TXT为YOLO标准标注格式:单张图片对应同名TXT文件,每行内容为类别id 归一化中心点x 归一化中心点y 归一化宽度 归一化高度
  • 直接通过脚本批量完成转换,千级样本转换耗时不超过1分钟:
    • 转CSV格式:读取对应图片的宽高,将归一化坐标还原为绝对坐标,按标注要求的字段逐行写入即可,核心代码示例如下:
# YOLO TXT转VOC格式CSV核心代码
import os
import cv2
import pandas as pd

# 按实际情况修改以下路径和类别映射
class_mapping = {0: "door"}
image_folder = "./images"
yolo_txt_folder = "./labels"
output_csv_path = "./annotation.csv"

annotation_rows = []
for txt_file in os.listdir(yolo_txt_folder):
    if not txt_file.endswith(".txt"):
        continue
    # 匹配对应图片
    image_name = txt_file.replace(".txt", ".jpg")
    image_path = os.path.join(image_folder, image_name)
    if not os.path.exists(image_path):
        continue
    img_height, img_width = cv2.imread(image_path).shape[:2]
    # 读取TXT标注
    with open(os.path.join(yolo_txt_folder, txt_file), "r", encoding="utf-8") as f:
        for line in f.readlines():
            line = line.strip()
            if not line:
                continue
            class_id, x_center, y_center, bbox_width, bbox_height = map(float, line.split())
            # 还原绝对坐标
            x_min = int((x_center - bbox_width / 2) * img_width)
            y_min = int((y_center - bbox_height / 2) * img_height)
            x_max = int((x_center + bbox_width / 2) * img_width)
            y_max = int((y_center + bbox_height / 2) * img_height)
            annotation_rows.append([
                image_name, img_width, img_height,
                class_mapping[int(class_id)],
                x_min, y_min, x_max, y_max
            ])
# 写入CSV
pd.DataFrame(
    annotation_rows,
    columns=["filename", "width", "height", "class", "xmin", "ymin", "xmax", "ymax"]
).to_csv(output_csv_path, index=False, encoding="utf-8")
  • 转Pascal VOC XML格式:逻辑和转CSV一致,仅需将每幅图的标注信息按XML标签格式写入对应文件即可。
  • 转换完成后随机抽取3-5个样本对比原TXT和转换后文件的坐标、类别是否匹配,避免映射错误。

2. 转换后TFLite模型无法使用排查方案

  • 先验证原始YOLOv3权重(.weights或.h5格式)的推理结果正常,排除原模型本身的精度问题。
  • 调整转换配置:通用转换教程默认适配COCO 80类的输出配置,你的门检测为自定义类别,需手动修改转换脚本的类别数、输出节点配置,不要直接套用教程默认参数。
  • 统一预处理逻辑:TFLite推理时的图片缩放比例、归一化参数、通道顺序(RGB/BGR)必须和训练YOLOv3时的预处理完全一致,常见错误为训练时使用0-255范围的BGR输入,推理时误使用0-1范围的RGB输入。
  • 补充后处理逻辑:转换后的TFLite输出为原始特征图,需自行添加边界框解码、NMS过滤、坐标还原的后处理逻辑,不可直接将输出值作为检测结果使用。
  • 量化问题排查:如果转换的是INT8量化模型,优先转换FP32非量化模型测试,确认转换流程正常后再做量化操作,避免量化误差导致模型失效。

内容的提问来源于stack exchange,提问作者newbie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 04:39:02