【发布时间】: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 抱怨说我的模型没有被量化,即使我运行的代码与第一个模型完全相同。
谁能向我解释为什么会发生这种错误?结构不同的模型不能量化吗?
【问题讨论】:
-
问题可能与扁平化有关。这个链接应该有帮助:coral.ai/docs/edgetpu/models-intro/#model-requirements
-
你用的是什么 tensorflow 版本?
标签: python tensorflow keras tpu google-coral