首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ValueError:未知层: AnchorBoxes量化流

ValueError:未知层: AnchorBoxes量化流
EN

Stack Overflow用户
提问于 2021-01-21 14:27:31
回答 1查看 175关注 0票数 0

我正在将量化应用于SSD模型。要点附上去了。有一个名为"AnchorBoxes“的定制对象,它是在加载模型时添加的。当我不做量化时,这很好。但是当我应用量化时,这个自定义对象是不被识别的。

我试过了一件事。

代码语言:javascript
复制
def apply_quantization_to_conv2D(layer):
  #print(layer)
  if isinstance(layer, tf.keras.layers.Conv2D):
    return tfmot.quantization.keras.quantize_annotate_layer(layer)
  return layer

# Use `tf.keras.models.clone_model` to apply `apply_quantization_to_dense` 
# to the layers of the model.
annotated_model = tf.keras.models.clone_model(
    model,
    clone_function=apply_quantization_to_conv2D,
)

#annotated_model.save('quantize_ready_model_20_01_Conv2D.h5', include_optimizer=True)
annotated_model.summary()
# Now that the Dense layers are annotated,
# `quantize_apply` actually makes the model quantization aware.



#quant_aware_model = tfmot.quantization.keras.quantize_apply(annotated_model)

我在上面的代码中注释了这一行quant_aware_model = tfmot.quantization.keras.quantize_apply(annotated_model),因为它抛出了错误ValueError: Unknown layer: AnchorBoxes

相反,我在将量化应用于Conv2D层之后保存了模型,如下所示

代码语言:javascript
复制
def apply_quantization_to_conv2D(layer):
  #print(layer)
  if isinstance(layer, tf.keras.layers.Conv2D):
    return tfmot.quantization.keras.quantize_annotate_layer(layer)
  return layer

# Use `tf.keras.models.clone_model` to apply `apply_quantization_to_dense` 
# to the layers of the model.
annotated_model = tf.keras.models.clone_model(
    model,
    clone_function=apply_quantization_to_conv2D,
)


annotated_model.summary()

annotated_model.save('quantize_ready_model_20_01_Conv2D_1.h5', include_optimizer=True)
# Now that the Dense layers are annotated,
# `quantize_apply` actually makes the model quantization aware.



#quant_aware_model = tfmot.quantization.keras.quantize_apply(annotated_model)
#quant_aware_model.compile(optimizer=adam, loss=ssd_loss.compute_loss)
#quant_aware_model.summary()

然后,我加载了模型,希望所加载的量化模型如下所示,并将custom_objects附加到模型上。

代码语言:javascript
复制
with tfmot.quantization.keras.quantize_scope():
    loaded_model = tf.keras.models.load_model('./quantize_ready_model_20_01_Conv2D_1.h5', custom_objects={'AnchorBoxes': AnchorBoxes})

最后,我将quantize_apply应用到新的具有量化层的loaded_model中。

代码语言:javascript
复制
quant_aware_model = tfmot.quantization.keras.quantize_apply(loaded_model)

这再次导致同样的错误

代码语言:javascript
复制
ValueError: Unknown layer: AnchorBoxes

系统信息

TensorFlow版本(从源代码或二进制安装):TF-2.0.0

TensorFlow模型优化版本(从源代码或二进制安装):0.5.0

描述了预期的行为

当我运行quantize_apply(模型)时,模型应该是量化感知的。

描述当前行为

在自定义对象上抛出错误

代码以复制问题

要旨

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-04 16:31:44

在下面的代码中传递像这个AnchorBoxes': AnchorBoxes这样的自定义层后,这个问题得到了修正。

代码语言:javascript
复制
with quantize_scope(
  {'DefaultDenseQuantizeConfig': DefaultDenseQuantizeConfig,
   'AnchorBoxes': AnchorBoxes}):
  # Use `quantize_apply` to actually make the model quantization aware.
  quant_aware_model = tfmot.quantization.keras.quantize_apply(annotated_model)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65829552

复制
相关文章

相似问题

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