keras 从TensorFlow量化中排除重缩放层,同时保留稀疏性和聚类

p1tboqfb  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(101)

我正在按照this指南对我的模型stripped_clustered_model进行量化。不幸的是,我的模型包含一个无法量化的层(重缩放层)。为了解决这个问题,我使用 quantize_annotate_layer 来标记其他要量化的层。我通过调用以下代码来实现这一点:

def apply_quantization_to_non_rescaling(layer):
    if not isinstance(layer, tf.keras.layers.Rescaling):
        print('=> NOT Rescaling')
        return tfmot.quantization.keras.quantize_annotate_layer(layer, quantize_config=None)
    print('=> Rescaling')
    return layer

quant_aware_annotate_model = tf.keras.models.clone_model(
    stripped_clustered_model,
    clone_function=apply_quantization_to_non_rescaling,
)

pcqat_model = tfmot.quantization.keras.quantize_apply(
              quant_aware_annotate_model,
              tfmot.experimental.combine.Default8BitClusterPreserveQuantizeScheme(preserve_sparsity=True)
)

为了便于理解,我用 quantize_annotate_layer 标记所有要量化的层。稍后,我调用 quantize_apply 来实际执行此量化。但是,运行此代码会导致以下错误:

=> Rescaling
=> NOT Rescaling
=> NOT Rescaling
=> NOT Rescaling
=> NOT Rescaling
=> NOT Rescaling
=> NOT Rescaling
=> NOT Rescaling
=> NOT Rescaling
=> NOT Rescaling
Traceback (most recent call last):
  File "model_2.py", line 332, in <module>
    main()
  File "model_2.py", line 304, in main
    pcqat_model = tfmot.quantization.keras.quantize_apply(
  File "/usr/local/lib/python3.8/dist-packages/tensorflow_model_optimization/python/core/keras/metrics.py", line 74, in inner
    raise error
  File "/usr/local/lib/python3.8/dist-packages/tensorflow_model_optimization/python/core/keras/metrics.py", line 69, in inner
    results = func(*args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/tensorflow_model_optimization/python/core/quantization/keras/quantize.py", line 474, in quantize_apply
    return keras.models.clone_model(
  File "/home/user/.local/lib/python3.8/site-packages/keras/models.py", line 453, in clone_model
    return _clone_sequential_model(
  File "/home/user/.local/lib/python3.8/site-packages/keras/models.py", line 330, in _clone_sequential_model
    if isinstance(layer, InputLayer) else layer_fn(layer))
  File "/usr/local/lib/python3.8/dist-packages/tensorflow_model_optimization/python/core/quantization/keras/quantize.py", line 408, in _quantize
    full_quantize_config = quantize_registry.get_quantize_config(layer)
  File "/usr/local/lib/python3.8/dist-packages/tensorflow_model_optimization/python/core/quantization/keras/collaborative_optimizations/cluster_preserve/cluster_preserve_quantize_registry.py", line 293, in get_quantize_config
    quantize_config = (default_8bit_quantize_registry.
  File "/usr/local/lib/python3.8/dist-packages/tensorflow_model_optimization/python/core/quantization/keras/default_8bit/default_8bit_quantize_registry.py", line 272, in get_quantize_config
    raise ValueError(
ValueError: `get_quantize_config()` called on an unsupported layer <class 'keras.layers.preprocessing.image_preprocessing.Rescaling'>. Check if layer is supported by calling `supports()`. Alternatively, you can use `QuantizeConfig` to specify a behavior for your layer.

输出显示,除了第一层(即缩放层),所有层都被标记为量化。然而,下面的错误告诉我,缩放层也被用于量化。

如何从量化中排除重新缩放层?
**2022年4月22日更新:**无法通过使用

pcqat_model = tfmot.quantization.keras.quantize_apply(
    quant_aware_annotate_model
)

代替

pcqat_model = tfmot.quantization.keras.quantize_apply(
    quant_aware_annotate_model,
    tfmot.experimental.combine.Default8BitClusterPreserveQuantizeScheme(preserve_sparsity=True)
)

因为这将不能保持稀疏性和聚类。

u91tlkcl

u91tlkcl1#

因此,当传递另一个量化策略而不是默认策略时,会出现某种错误。如果你只使用pcqat_model = tfmot.quantization.keras.quantize_apply(quant_aware_annotate_model),它会工作。我尝试使用其他非实验性的量化策略,但它们也会引发一些错误。因此,如果你绝对设置了另一个策略而不是默认策略,这不会帮助你,但如果你只想使用量化,请使用默认策略。

相关问题