ubsuny / g2-coral

MIT License
0 stars 1 forks source link

quantization aware training #34

Open CarlatBuffalo opened 3 years ago

CarlatBuffalo commented 3 years ago

The quantization aware training fails because tf lite does not support some layers, such as conv1d, maxpool1d...

# annotate that only the Dense layers should be quantized.
def apply_quantization(layer):
    if isinstance(layer, tf.keras.layers.Dense):
        return tfmot.quantization.keras.quantize_annotate_layer(layer)
    elif isinstance(layer, tf.keras.layers.Conv1D):
        return tfmot.quantization.keras.quantize_annotate_layer(layer)
    elif isinstance(layer, tf.keras.layers.MaxPool1D):
        return tfmot.quantization.keras.quantize_annotate_layer(layer)
    return layer

annotated_model = tf.keras.models.clone_model(
    model = base_model,
    clone_function= apply_quantization )
# make the quantization aware
qaware_model = tfmot.quantization.keras.quantize_apply(base_model)

Error message:

RuntimeError: Layer conv1d_4:<class 'tensorflow.python.keras.layers.convolutional.Conv1D'> is not supported. You can quantize this layer by passing a `tfmot.quantization.keras.QuantizeConfig` instance to the `quantize_annotate_layer` API.
CarlatBuffalo commented 3 years ago

One solution is to use post-training quantization which I'm trying now, tho it'll affect the accuracy of the model.
Another possible way out is to adjust data type in the model to be integers, but then what about all parameters (weights, activations/inputs)?