简体   繁体   English

ValueError:未知层:AnchorBoxes 量化 tensorflow

[英]ValueError: Unknown layer: AnchorBoxes quantization tensorflow

I am applying quantization to a SSD model.我正在对 SSD model 应用量化。 The gist is attached.附上要点。 There is a custom object called "AnchorBoxes" which is added while loading the model.加载 model 时添加了一个名为“AnchorBoxes”的自定义 object。 This works fine when I don't do quantization.当我不进行量化时,这很好用。 But when I apply quantization, this custom object is not recognized.但是当我应用量化时,无法识别此自定义 object。

I tried a work around.我尝试了解决方法。

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)

I commented this line quant_aware_model = tfmot.quantization.keras.quantize_apply(annotated_model) in the above code as it was throwing the error ValueError: Unknown layer: AnchorBoxes我在上面的代码中注释了这一行quant_aware_model = tfmot.quantization.keras.quantize_apply(annotated_model)因为它抛出错误ValueError: Unknown layer: AnchorBoxes

Instead I saved the model after applying quantization to the Conv2D layers as below相反,我在对 Conv2D 层应用量化后保存了 model,如下所示

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()

Then I loaded the model hoping that the loaded quantized model as below will have the custom_objects attached to it.然后我加载了 model 希望加载的量化 model 如下所示将附加 custom_objects。

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})

Finally I applied the quantize_apply to the new loaded_model which has quantized layers.最后,我将quantize_apply应用于具有量化层的新loaded_model模型。

quant_aware_model = tfmot.quantization.keras.quantize_apply(loaded_model)

which again resulted in the same error这再次导致了同样的错误

ValueError: Unknown layer: AnchorBoxes

System information系统信息

TensorFlow version (installed from source or binary): TF 2.0.0 TensorFlow 版本(从源代码或二进制安装):TF 2.0.0

TensorFlow Model Optimization version (installed from source or binary): 0.5.0 TensorFlow Model 优化版本(从源代码或二进制安装):0.5.0

Describe the expected behavior描述预期的行为
When I run quantize_apply(model), the model should become quantization aware当我运行 quantize_apply(model) 时,model 应该可以感知量化

Describe the current behavior描述当前行为
Throwing an error on the custom objects在自定义对象上引发错误

Code to reproduce the issue重现问题的代码
gist 要旨

The issue was fixed after passing the custom layer like this AnchorBoxes': AnchorBoxes in the below code.在通过以下代码中的AnchorBoxes': AnchorBoxes之类的自定义层后,该问题已得到解决。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM