【问题标题】:Tensorflow cannot quantize reshape functionTensorflow 无法量化重塑函数
【发布时间】:2020-09-04 08:21:18
【问题描述】:

我将训练我的模型量化意识。但是,当我使用它时,tensorflow_model_optimization 无法量化 tf.reshape 函数,并引发错误。

  1. 张量流版本:'2.4.0-dev20200903'
  2. python 版本:3.6.9

代码:

import os
os.environ['CUDA_VISIBLE_DEVICES'] = '3'
from tensorflow.keras.applications import VGG16
import tensorflow_model_optimization as tfmot
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
quantize_model = tfmot.quantization.keras.quantize_model
inputs = keras.Input(shape=(784,))
# img_inputs = keras.Input(shape=(32, 32, 3))

dense = layers.Dense(64, activation="relu")
x = dense(inputs)
x = layers.Dense(64, activation="relu")(x)
outputs = layers.Dense(10)(x)
outputs = tf.reshape(outputs, [-1, 2, 5])
model = keras.Model(inputs=inputs, outputs=outputs, name="mnist_model")

# keras.utils.plot_model(model, "my_first_model.png")


q_aware_model = quantize_model(model)

和输出:

Traceback (most recent call last):

  File "<ipython-input-39-af601b78c010>", line 14, in <module>
    q_aware_model = quantize_model(model)

  File "/home/essys/.local/lib/python3.6/site-packages/tensorflow_model_optimization/python/core/quantization/keras/quantize.py", line 137, in quantize_model
    annotated_model = quantize_annotate_model(to_quantize)

  File "/home/essys/.local/lib/python3.6/site-packages/tensorflow_model_optimization/python/core/quantization/keras/quantize.py", line 210, in quantize_annotate_model
    to_annotate, input_tensors=None, clone_function=_add_quant_wrapper)
...

  File "/home/essys/anaconda3/envs/tf_gpu/lib/python3.6/site-packages/tensorflow/python/autograph/impl/api.py", line 667, in wrapper
    raise e.ag_error_metadata.to_exception(e)

TypeError: in user code:


    TypeError: tf__call() got an unexpected keyword argument 'shape'

如果有人知道,请帮忙?

【问题讨论】:

  • 通过使用您的代码,我得到了不同的 Traceback :RuntimeError: Layer tf_op_layer_Reshape:&lt;class 'tensorflow.python.keras.engine.base_layer.TensorFlowOpLayer'&gt; is not supported. You can quantize this layer by passing a `tfmot.quantization.keras.QuantizeConfig` instance to the `quantize_annotate_layer` API.
  • 在旧版本中不支持 tf.reshape 但在 tf_nightly 版本中支持。
  • 那么你可能想在 Github 上打开一个问题。
  • 是的,我想我应该这样做。谢谢。

标签: tensorflow deep-learning quantization-aware-training


【解决方案1】:

背后的原因是因为您的层目前还不支持 QAT。如果你想量化它,你必须通过 quantize_annotate_layer 自己编写你的量化并通过 quantize_scope 并通过 quantize_apply 应用到你的模型,如下所述:https://www.tensorflow.org/model_optimization/guide/quantization/training_comprehensive_guide?hl=en#quantize_custom_keras_layer

我在here中创建了一个batch_norm_layer作为例子

Tensorflow 2.x 对于 QAT 层不完整,请考虑使用 tf1.x,在操作符后添加 FakeQuant。

【讨论】:

    猜你喜欢
    • 2016-10-18
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-13
    • 2016-03-12
    • 1970-01-01
    • 2016-02-15
    相关资源
    最近更新 更多