首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >训练后量化后的“模型未量化”取决于模型结构?

训练后量化后的“模型未量化”取决于模型结构?
EN

Stack Overflow用户
提问于 2020-07-09 13:31:37
回答 1查看 352关注 0票数 0

似乎训练后量化对某些模型结构有效,而对其他模型结构无效。例如,当我运行我的代码时

代码语言:javascript
运行
复制
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)

和训练后的量化为

代码语言:javascript
运行
复制
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模型,该模型的性能与我在服务器上训练的模型相当。

但是,当我将模型更改为

代码语言:javascript
运行
复制
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抱怨并说我的模型没有量化,即使我运行的代码与第一个模型完全相同。

有人能给我解释一下为什么会发生这种错误吗?当一个模型是不同的结构时,它不能被量化吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-11 06:34:27

如果您使用的是tf-nightly或更新的版本,则MLIR转换器可能处于打开状态,而编译器还不支持此功能。这可能会导致一些奇怪的错误,如果您尝试通过添加以下命令将其关闭:

代码语言:javascript
运行
复制
converter.experimental_new_converter = False

这可能就是你的问题的原因!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62808038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档