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

TensorFlow量化感知训练报错:期望Model实例却得到Sequential对象

量化感知训练报错:Expected 'model' argument to be a 'Model' instance

我需要对模型进行量化感知训练,模型架构如下:

Model: "sequential_4"


Layer (type) Output Shape Param #

masking_4 (Masking) (None, 389, 64) 0


my_layer_5_4 (my_layer_5) (None, 389, 512) 12288


time_distributed_4 (TimeDistributed) (None, 389, 39) 20007

我参照tfmot.quantization.keras.QuantizeConfig编写代码,目标是让所有层都参与量化,代码如下:

import tensorflow_model_optimization as tfmot
from tensorflow_model_optimization.python.core.quantization.keras.default_8bit import default_8bit_quantize_configs
NoOpQuantizeConfig = default_8bit_quantize_configs.NoOpQuantizeConfig

class NoOpQuantizeConfig(tfmot.quantization.keras.QuantizeConfig):
    """QuantizeConfig which does not quantize any part of the layer."""
    def get_weights_and_quantizers(self, layer):
        return []
    def get_activations_and_quantizers(self, layer):
        return []
    def set_quantize_weights(self, layer, quantize_weights):
        pass
    def set_quantize_activations(self, layer, quantize_activations):
        pass
    def get_output_quantizers(self, layer):
        return []
    def get_config(self):
        return {}  

def apply_quantization(layer):
    if isinstance(layer, (tf.keras.layers.TimeDistributed, tf.keras.layers.Masking, tf.keras.layers.my_layer_5_4)):
        return tfmot.quantization.keras.quantize_annotate_layer(layer, quantize_config=NoOpQuantizeConfig())
    else:
        return tfmot.quantization.keras.quantize_annotate_layer(layer)

if __name__ == '__main__':
    model = load_model('./model.h5', custom_objects={'my_layer_5': my_layer_5})
    model.summary()
    annotated_model = tf.keras.models.clone_model(
        model,
        clone_function=apply_quantization,
    )
    with tf.keras.utils.custom_object_scope({"NoOpQuantizeConfig": NoOpQuantizeConfig}):
        q_aware_model = tfmot.quantization.keras.quantize_apply(annotated_model)
    q_aware_model.summary()

运行代码时触发以下错误:

ValueError: ('Expected 'model' argument to be a 'Model' instance, got ', <keras.engine.sequential.Sequential object at 0x7f234263dfd0>)

补充说明:使用的tensorflow-model-optimization版本为0.4.0。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 14:58:28