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

AutoML Vision Edge导出的saved_model无法在TensorFlow 2.0中正常加载

解决AutoML Vision Edge导出的SavedModel在TensorFlow 2.x中加载后无summary方法的问题

我之前也碰到过类似的问题,这是因为AutoML Vision Edge导出的SavedModel并不是标准的Keras模型格式,而是TensorFlow原生的SavedModel(基于AutoTrackable架构),所以用tf.keras.models.load_model加载后,得到的对象并没有Keras模型特有的summary()、predict()等方法,这就是你遇到AttributeError的核心原因。

下面是具体的解决步骤和代码示例:

1. 正确加载原生SavedModel

放弃使用Keras的load_model,改用TensorFlow原生的tf.saved_model.load方法加载模型:

import tensorflow as tf
import cv2
import numpy as np

# 加载AutoML导出的SavedModel目录
loaded_model = tf.saved_model.load('saved_model')
# 获取模型的推理签名(AutoML Vision Edge的分类模型默认签名是serving_default)
inference_fn = loaded_model.signatures['serving_default']

2. 执行图像分类推理

加载模型后,你需要按照AutoML Vision Edge的输入要求处理图像,然后调用签名函数完成推理:

# 读取并预处理图像(示例)
# 注意:输入尺寸要和你训练模型时设置的一致,格式为RGB
img = cv2.imread('your_test_image.jpg')
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_resized = cv2.resize(img_rgb, (224, 224))  # 替换为你的模型输入尺寸

# 转换为模型要求的张量格式(uint8类型,增加batch维度)
input_tensor = tf.convert_to_tensor(img_resized, dtype=tf.uint8)
input_tensor = tf.expand_dims(input_tensor, axis=0)  # 形状变为(1, height, width, 3)

# 执行推理
output_dict = inference_fn(input_tensor)

# 解析输出结果
# AutoML分类模型的输出通常是一个名为'Predictions'的张量,包含各类别的置信度
predictions = output_dict['Predictions'].numpy()[0]
top_class_index = np.argmax(predictions)
top_confidence = predictions[top_class_index]

print(f"最高置信度类别索引:{top_class_index},置信度:{top_confidence:.4f}")

3. (可选)尝试转换为Keras模型(不保证兼容)

如果你确实需要使用Keras的API,可以尝试用tf.keras.models.model_from_saved_model转换,但由于AutoML导出的模型结构特殊性,可能会出现兼容性问题:

try:
    keras_model = tf.keras.models.model_from_saved_model('saved_model')
    keras_model.summary()
    # 转换成功后可以用keras_model.predict()进行推理
except Exception as e:
    print(f"转换为Keras模型失败:{str(e)}")
    # 此时建议还是用原生SavedModel的方式进行推理

总结一下:AutoML Vision Edge导出的SavedModel是TensorFlow原生格式,而非Keras格式,所以不能直接用Keras的加载方法获取Keras模型对象,改用原生加载方式并通过签名函数推理即可解决问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:19:36