You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

YOLOv8 .pt可检测4条掌纹,转.tflite后Android仅检测3条的问题排查

掌纹检测模型YOLOv8转TFLite后检测数量减少问题

问题现象

基于Ultralytics YOLOv8训练的掌纹检测模型,在Python中使用.pt模型测试可正确检测全部4条掌纹,但转换为.tflite格式后在Android Studio(Kotlin)中测试仅能检测出3条掌纹。

训练与转换情况

  • 训练:使用Ultralytics YOLOv8完成训练,.pt模型在Python测试中表现正常。
  • 转换后输出:TFLite模型输出张量形状为[1, 8, 8400],包含4个边界框坐标(x1, y1, x2, y2)、1个目标置信度分数、3个类别概率值(对应3类掌纹)。

已尝试操作

训练脚本

from ultralytics import YOLO

# Load the YOLOv8 keypoint detection model
model_path = ""  # Update with the correct path
model = YOLO(model_path)  # Load the custom model

# Train the model on GPU
model.train(
    data="",  # Correct dataset path
    epochs=10,          # Number of training epochs
    batch=8,            # Reduce batch size if low memory
    imgsz=640,          # Image size
    device="cuda"       # Use GPU for training
)

results = model.val(device="cuda")

# Export the model to ONNX
model.export(format="onnx")

.pt模型测试脚本

from ultralytics import YOLO

model = YOLO("model_path/model_name")
results = model.predict(source="your_image_path", save=True, imgsz=640)
results[0].show()
# This shows the image with detections

疑问

  1. 为何.tflite模型仅能检测3条掌纹,而.pt模型可正确检测全部4条?
  2. 是否与输出张量形状[1,8,8400]有关?
  3. 如何解决该问题以实现Android端全掌纹检测?

问题原因分析与解决方法

1. 核心原因排查

输出张量形状[1,8,8400]是符合预期的(4框坐标+1置信度+3类别=8个通道),问题大概率出在Android端与Python端的后处理/预处理逻辑不一致,或是模型转换过程的精度损失。

2. 具体解决步骤

步骤1:对齐后处理参数

Python端YOLOv8的predict()方法默认参数为conf=0.25、iou=0.7、max_det=300,Android端需完全对齐这些参数:

  • 把置信度阈值设为0.25,避免过滤掉低置信度的掌纹检测结果
  • 将NMS(非极大值抑制)的IOU阈值设为0.7,防止误过滤重叠掌纹框
  • 确保最大检测数设置足够大(比如300),避免因上限限制漏检
  • 检查Android端代码是否遗漏了某个类别的判断逻辑

步骤2:优化模型转换流程

跳过ONNX中间步骤,直接从.pt转TFLite,同时关闭量化避免精度损失:

from ultralytics import YOLO

model = YOLO("your_trained_model.pt")
# 导出FP32精度的TFLite模型
model.export(format="tflite", int8=False, fp16=False)

步骤3:对齐图像预处理逻辑

Android端需实现和Python端完全一致的预处理:

  • 使用letterbox缩放:保持图像宽高比,将长边缩放到640,短边补黑边,记录缩放比例和偏移量用于后续坐标转换
  • 归一化:将像素值从[0,255]转换为[0,1]
  • 通道顺序:确保输入为RGB格式(YOLOv8默认输入为RGB)

步骤4:验证TFLite模型输出

在Python中直接加载TFLite模型测试,确认模型本身是否能输出4条掌纹的检测结果:

import tensorflow as tf
import numpy as np
from PIL import Image

interpreter = tf.lite.Interpreter(model_path="your_model.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# 预处理图像(和YOLOv8一致)
img = Image.open("test_image.jpg").resize((640,640))
input_data = np.array(img, dtype=np.float32) / 255.0
input_data = np.expand_dims(input_data, axis=0)

interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])

# 提取置信度列(第5个通道,索引4)
confs = output_data[0,4,:]
print("置信度大于0.25的检测框数量:", len(confs[confs>0.25]))

如果Python中TFLite模型能检测到4条掌纹,说明问题在Android端的代码逻辑;如果也只能检测3条,需重新调整转换参数或重新训练模型。


内容的提问来源于stack exchange,提问作者Fida Ur Rahman

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 03:15:14