google / qkeras

QKeras: a quantization deep learning library for Tensorflow Keras
Apache License 2.0
533 stars 102 forks source link

Unable to quantize QDepthwiseConv2DBatchnorm() and Conv2DBatchnorm() #87

Closed stbunda closed 2 years ago

stbunda commented 2 years ago

When model_save_quantized_weights is called on a model including a QDepthwiseConv2DBatchnorm() and Conv2DBatchnorm() I get the notification: depthwise_3 conv and batchnorm weights cannot be seperately quantized because they will be folded before quantization. As expected from this comment the returned weights are not quantized.

The layer looks something like this: x = QDepthwiseConv2DBatchnorm(kernel_size=kernel, strides=stride, padding='same', use_bias=False, depthwise_initializer=initializers.HeNormal(seed=2), depthwise_quantizer=quantized_bits(bits=4, integer=0, alpha=1), name='depthwise_3' )(x)

The print_qstats() returns the following: Weight profiling: depthwise_3_weights : 25 (4-bit unit) depthwise_3_bias : 1 (32-bit unit)

Hopefully, you can point out what I did wrong, or what I should change. Many thanks in advance!

stbunda commented 2 years ago

After some digging I found bn_folding_utils.unfold_model() such that: qmodel_unfolded = bn_folding_utils.unfold_model(qmodel) model_save_quantized_weights(qmodel_unfolded) This seems to do the trick!