Azure Custom Vision导出模型本地推理结果不符及生产选型咨询
问题:Azure Custom Vision导出模型本地推理偏差及生产适用性疑问
一、导出模型本地推理检测框偏差问题
我使用Azure Custom Vision针对生产线草莓图像训练了一个自定义目标检测模型,并导出为两种格式用于本地推理:
- ONNX(model.onnx)
- TensorFlow Lite(model.tflite)
但发现,即使使用完全相同的图像,本地推理(ONNX和TFLite)的检测框会出现偏移、错误或缺失,与Azure门户上的准确检测结果存在明显差异。
起初我认为是归一化或置信度阈值问题,查阅资料调整预处理流程后,当前的预处理步骤如下:
- 将所有输入图像调整为320x320尺寸
- 将像素值归一化至[0, 1]区间
- 对ONNX模型使用通道优先格式(CHW)
- 相应调整检测框坐标
以下是我在Google Colab中使用的代码:
import cv2 import numpy as np import onnxruntime as ort import matplotlib.pyplot as plt onnx_model_path = "model.onnx" session = ort.InferenceSession(onnx_model_path) def get_model_output(image): input_name = session.get_inputs()[0].name output_names = [output.name for output in session.get_outputs()] # Resize to 320x320 (model-specific input size) image_resized = cv2.resize(image, (320, 320)) image_normalized = image_resized.astype(np.float32) / 255.0 # Convert HWC → CHW and add batch dimension image_input = np.expand_dims(np.transpose(image_normalized, (2, 0, 1)), axis=0) outputs = session.run(output_names, {input_name: image_input}) boxes = outputs[0][0] # (N, 4) in relative coordinates class_ids = outputs[1][0].astype(int) scores = outputs[2][0] return boxes, class_ids, scores def process_image(input_image_path): image = cv2.imread(input_image_path) original = image.copy() boxes, class_ids, scores = get_model_output(image) detection_count = 0 for i in range(len(boxes)): if scores[i] > 0.5: y1, x1, y2, x2 = boxes[i] x1_pixel = int(x1 * original.shape[1]) y1_pixel = int(y1 * original.shape[0]) x2_pixel = int(x2 * original.shape[1]) y2_pixel = int(y2 * original.shape[0]) cv2.rectangle(original, (x1_pixel, y1_pixel), (x2_pixel, y2_pixel), (0, 255, 0), 2) label = f"Class {class_ids[i]}: {scores[i]:.2f}" cv2.putText(original, label, (x1_pixel, y1_pixel - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) detection_count += 1 plt.imshow(cv2.cvtColor(original, cv2.COLOR_BGR2RGB)) plt.axis('off') plt.show() print(f"Total detections: {detection_count}") # Run input_image_path = "/content/imagen_fresas.png" process_image(input_image_path)
Azure门户预测结果

本地推理预测结果

二、附加疑问
我想了解:Azure Custom Vision模型是否适合实际生产中的目标检测场景?还是使用YOLOv5/YOLOv8等开源库训练自定义模型(可完全控制训练、锚框、置信度阈值及后处理)更好?希望了解Azure Custom Vision是更适合原型开发还是可可靠用于生产环境。
内容的提问来源于stack exchange,提问作者Br0k3nS0u1
相关产品推荐
相关产品推荐

