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

TensorFlow手语分类推理模型报错:找不到匹配的SavedModel具体函数

ASL分类推理模型SavedModel调用报错修复方案

这种找不到匹配具体函数的问题,核心原因是自定义模型的计算图签名不明确,或者输入输出维度不匹配导致TensorFlow无法正确绑定推理函数。以下是具体修复步骤:

1. 给自定义模型的call方法固定输入签名

在自定义Inference_Model的call方法上加上@tf.function装饰器,并显式指定input_signature,明确输入的shape和数据类型,确保保存时生成正确的推理签名:

import tensorflow as tf

class Inference_Model(tf.keras.Model):
    def __init__(self):
        super().__init__()
        # 加载预训练模型并冻结参数
        self.feature_gen = tf.keras.models.load_model('path/to/feature_gen')
        self.feature_gen.trainable = False
        self.model = tf.keras.models.load_model('path/to/model')
        self.model.trainable = False

    # 根据实际推理场景定义输入签名,比如这里是(任意批量, 64,64,3)的RGB图像
    @tf.function(input_signature=[tf.TensorSpec(shape=(None, 64, 64, 3), dtype=tf.float32)])
    def call(self, inputs):
        features = self.feature_gen(inputs)
        predictions = self.model(features)
        return predictions

# 实例化模型后先跑一次前向传播,让TensorFlow追踪完整计算图
inference_model = Inference_Model()
dummy_input = tf.random.normal((1, 64, 64, 3))
_ = inference_model(dummy_input)
# 保存模型
inference_model.save('path/to/saved_inference_model')

2. 调用时严格匹配输入签名

加载保存的模型后,输入的shape必须和定义的签名一致(批量维度可以是任意大小,比如1、4、8等):

loaded_model = tf.keras.models.load_model('path/to/saved_inference_model')
# 示例输入:2张64x64的RGB图像
test_input = tf.random.normal((2, 64, 64, 3))
results = loaded_model(test_input)

3. 检查预加载模型的维度兼容性

确认feature_gen的输出shape和model的输入shape完全匹配,避免中间维度不兼容导致签名失效:

# 验证特征生成器的输出维度
dummy_feat_input = tf.random.normal((1, 64, 64, 3))
feat_output = inference_model.feature_gen(dummy_feat_input)
print(f"特征生成器输出shape: {feat_output.shape}")

# 验证分类模型的输入是否匹配上述输出
dummy_model_input = tf.random.normal(feat_output.shape)
model_output = inference_model.model(dummy_model_input)
print(f"分类模型输出shape: {model_output.shape}")

4. 用SavedModel工具验证签名

用TensorFlow自带的命令行工具检查保存的模型签名,确认存在可用的推理签名:

saved_model_cli show --dir path/to/saved_inference_model --tag_set serve --signature_def serving_default

如果输出里没有正确的输入输出规范,说明保存时签名未生成,回到步骤1重新定义。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 14:12:41