【问题标题】:"Model not quantized" after post-training quantization depends on model structure?训练后量化后的“模型未量化”取决于模型结构?
【发布时间】:2020-07-09 05:31:37
【问题描述】:

似乎训练后量化适用于某些模型结构,而不适用于其他模型结构。例如,当我使用

运行我的代码时
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10)
])

# Train the digit classification model
model.compile(optimizer='adam',
              loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

model.fit(train_images, train_labels, epochs=2)

训练后量化为

converter = tf.lite.TFLiteConverter.from_keras_model(model)
# This enables quantization
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_types = [tf.int8]
# This ensures that if any ops can't be quantized, the converter throws an error
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
# These set the input and output tensors to uint8 (added in r2.3)
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
# And this sets the representative dataset so we can quantize the activations
converter.representative_dataset = representative_data_gen


tflite_model = converter.convert()

with open('my_mnist_quant.tflite', 'wb') as f:
    f.write(tflite_model)
    

! edgetpu_compiler my_mnist_quant.tflite 命令运行良好,并创建了一个性能与我在服务器上训练的模型相当的 tflite 模型。

但是,当我将模型更改为

model = keras.Sequential([
  keras.layers.InputLayer(input_shape=(28, 28)),
  keras.layers.Reshape(target_shape=(28, 28, 1)),
  keras.layers.Conv2D(64, kernel_size=3, activation='relu', input_shape=(28,28,1)),
  keras.layers.Flatten(),
  keras.layers.Dense(10, activation='softmax')
])

我所做的只是为我的模型添加一个卷积层和一些重塑层。然而,当我用这个模型运行量化时,一切都运行良好,直到我尝试用 edgetpu_compiler 编译它。在这种情况下,edgetpu_compiler 抱怨说我的模型没有被量化,即使我运行的代码与第一个模型完全相同。

谁能向我解释为什么会发生这种错误?结构不同的模型不能量化吗?

【问题讨论】:

标签: python tensorflow keras tpu google-coral


【解决方案1】:

如果您使用的是 tf-nightly 或更新版本,则 MLIR 转换器可能已打开,编译器尚不支持。如果您尝试通过添加以下内容将其关闭,这可能会导致一些奇怪的错误:

converter.experimental_new_converter = False

这可能只是您的问题的原因!

【讨论】:

  • 感谢它现在完美运行!我想我将不得不等到 tf nightly 推出一种编译卷积的方法。
  • 我在这个问题上花了一天多的时间。我找不到言语来表达我对您的回答的感激之情。 [幸福噪音]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-21
  • 1970-01-01
  • 2019-03-20
  • 2021-07-19
  • 1970-01-01
  • 2019-10-14
  • 1970-01-01
相关资源
最近更新 更多