首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tensorflow量化感知训练

Tensorflow量化感知训练
EN

Stack Overflow用户
提问于 2021-01-08 18:03:07
回答 1查看 504关注 0票数 0

我想量化一个DenseNet模型。我使用的是Tensorflow 2.4。

代码语言:javascript
复制
import tensorflow_model_optimization as tfmot
model = tf.keras.applications.DenseNet121(include_top=True,weights=None,input_tensor=None,input_shape=None,pooling=None,classes=1000) 
quantize_model = tfmot.quantization.keras.quantize_model
model = quantize_model(model)

但我得到了以下信息:

'tensorflow.python.keras.layers.normalization_v2.BatchNormalization'>:不支持Layer conv2_block1__bn:quantize_annotate_layer接口传递一个tfmot.quantization.keras.QuantizeConfig实例来量化该层。

有没有办法让我做到这一点。我不能更改keras代码。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-29 03:39:48

在您的情况下,您需要单独量化BatchNormalization层。

如果您在此Quantization TF Guide中看到下面的示例代码片段,则可以使用DefaultDenseQuantizeConfig来处理此问题。希望这篇指南能帮助你解决这个问题。

代码语言:javascript
复制
quantize_annotate_layer = tfmot.quantization.keras.quantize_annotate_layer
quantize_annotate_model = tfmot.quantization.keras.quantize_annotate_model
quantize_scope = tfmot.quantization.keras.quantize_scope

class CustomLayer(tf.keras.layers.Dense):
  pass

model = quantize_annotate_model(tf.keras.Sequential([
   quantize_annotate_layer(CustomLayer(20, input_shape=(20,)), DefaultDenseQuantizeConfig()),
   tf.keras.layers.Flatten()
]))

# `quantize_apply` requires mentioning `DefaultDenseQuantizeConfig` with `quantize_scope`
# as well as the custom Keras layer.
with quantize_scope(
  {'DefaultDenseQuantizeConfig': DefaultDenseQuantizeConfig,
   'CustomLayer': CustomLayer}):
  # Use `quantize_apply` to actually make the model quantization aware.
  quant_aware_model = tfmot.quantization.keras.quantize_apply(model)

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

https://stackoverflow.com/questions/65626935

复制
相关文章

相似问题

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