Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QBatchNormalization with scale=False and model_save_quantized_weights #83

Closed
lattuada-st opened this issue Jan 21, 2022 · 5 comments
Closed
Assignees

Comments

@lattuada-st
Copy link

When model_save_quantized_weights is called on a model including a QBatchNormalization with scale=False it seems that the wrong quantizers are used.
QBatchNormalization.get_quantizers() returns a list with gamma_quantizer as first element even when there is no gamma, resulting in a disalignment between quantizers and weights in this point

for quantizer, weight in zip(qs, ws):

@danielemoro danielemoro self-assigned this Jan 21, 2022
@danielemoro
Copy link
Collaborator

Hello. Thank you for reporting this.

Can you please provide a code example where I can reproduce the problem?

@lattuada-st
Copy link
Author

This is an example
Number of weights is 3 while number of quantizers is 4. First quantizer (i.e., gamma_quantizer) is used to quantize first weight (i.e., beta)

#!/usr/bin/env python3
import qkeras
import tensorflow.keras as keras

input_layer = keras.layers.Input([19, 28, 16])
intermediate = qkeras.QBatchNormalization(
    scale=False
)(input_layer)
model = keras.Model(input_layer, intermediate)
print('Number of weights: ' + str(len(model.layers[1].get_weights())))
print('Number of quantizers: ' + str(len(model.layers[1].get_quantizers())))
qkeras.utils.model_save_quantized_weights(model)

@danielemoro
Copy link
Collaborator

So if I understand correctly, the issue is caused by this line:

for quantizer, weight in zip(qs, ws):

for quantizer, weight in zip(qs, ws):

If len(qs) != len(ws), then the wrong quantizer will be used to quantize each weight.

There may be several solutions to this, but I will need some time to investigate

@lattuada-st
Copy link
Author

Yes, the issue is there

@danielemoro
Copy link
Collaborator

We have determined that to fix this issue will take quite a bit of refactoring work, and so it will take more time.

In the meantime, the workaround is to use folded batch normalization. This method allows you to integrate your batch normalization operations into your convolution layers, thereby avoiding batch norm layers altogether. This is the method we currently use in our team.

We have good support for this in QKeras if you switch to using the QConv2DBatchnorm layers. Then you can use the convert_folded_model_to_normal_seq function to map a model full of QConv2DBatchnorm layers to an equivalent model with just QConv2D layers. See more info here: bc61890

Let me know if this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants