tensorflow_quantization.CustomQDQInsertionCase

class tensorflow_quantization.CustomQDQInsertionCase[source]

This class helps user to programatically decide toolkit behavior to quantize specific layers. Based on the output of this class 'case' function, toolkit deviates from its standard behavior.

case(keras_model: tf.keras.Model, qspec: QuantizationSpec) QuantizationSpec[source]

This function is called internally by the framework. Given keras model is passed as an argument and object of QuantizationSpec class is expcted in return. Returned QuantzaionSpec class object should contain information about the layers that needs to be treated specially/differently from default framework behavior.

Parameters
  • keras_model (tf.keras.Model) -- Keras functional or sequentail model

  • qspec (QuantizationSpec) -- User passed QuantizationSpec object. It is important to note that

  • new special qdq might or might not use quantizations specs user has provided.

Returns

A new QuantizationSpec object.

Example

class EfficientNetQDQCase(CustomQDQInsertionCase):
 def __init__(self) -> None:
     super().__init__()

 def info(self):
     return "In Multiply operation quantize inputs at index 0 and 1."

 def case(self, keras_model: 'tf.keras.Model', qspec: 'QuantizationSpec') -> 'QuantizationSpec':
     se_block_qspec_object = QuantizationSpec()
     for layer in keras_model.layers:
         if isinstance(layer, tf.keras.layers.Multiply):
             se_block_qspec_object.add(layer.name, quantize_input=True, quantize_weight=False, quantization_index=[0, 1])
     return se_block_qspec_object