How to get quantized weights from QAT model?
See original GitHub issueHi all. I’ve recently trained a keras implementation of ssd-keras. I’ve managed to run QAT training on the model and got desired the accuracy. I wanted to get the quantised weights from the QAT model saved as a H5 model. There’s no support or documentation regarding this in the tensorflow website. How can I get the quantised weights from the saved model after QAT? I tried converting it to TFLite but it is not converting due to a custom layer in model definition. So it would be helpful if I can get the quantised weights alone from the saved model. Here’s the code snippet for my QAT training. I am using TF 2.3.
`quantize_scope = tfmot.quantization.keras.quantize_scope
def apply_quantization_to_dense(layer): if ‘priorbox’ in layer.name: return layer
if isinstance(layer,tf.keras.layers.Concatenate) or isinstance(layer, tf.keras.layers.Reshape) or isinstance(layer,tf.keras.layers.Lambda): return layer
return tfmot.quantization.keras.quantize_annotate_layer(layer)
annotated_model = tf.keras.models.clone_model( model, clone_function=apply_quantization_to_dense, )
with quantize_scope({‘AnchorBoxes’: AnchorBoxes}): quant_aware_model = tfmot.quantization.keras.quantize_apply(annotated_model)
quant_aware_model.summary() quant_aware_model.compile(optimizer=adam, loss=ssd_loss.compute_loss) quant_aware_model.fit_generator(train_generator, epochs=424, steps_per_epoch=1000, callbacks=callbacks, validation_data=val_generator, validation_steps=100, initial_epoch=414)`
Issue Analytics
- State:
- Created 3 years ago
- Comments:10

Top Related StackOverflow Question
Assuming you have implemented the default 8 bit scheme
Where
dequantizeis specific to the quantizer based on the options there, for example fornum_bits = 8, per_axis = False, symmetric = True, narrow_range = False:Sorry, edited the last comment to point to the current release tag. As I mentioned, that folding is no longer done in TF. However, the calculation is essentially the same in the TFLite converter