27
27
from tensorflow_model_optimization .python .core .quantization .keras import quantize_registry
28
28
from tensorflow_model_optimization .python .core .quantization .keras import quantizers
29
29
from tensorflow_model_optimization .python .core .quantization .keras .layers import conv_batchnorm
30
+ from tensorflow_model_optimization .python .core .quantization .keras .tflite import tflite_quantizers
30
31
31
32
QuantizeProvider = quantize_provider .QuantizeProvider
32
33
@@ -84,7 +85,6 @@ class TFLiteQuantizeRegistry(quantize_registry.QuantizeRegistry, _RNNHelper):
84
85
85
86
# Convolution Layers
86
87
_QuantizeInfo (layers .convolutional .Conv1D , ['kernel' ], ['activation' ]),
87
- _QuantizeInfo (layers .convolutional .Conv2D , ['kernel' ], ['activation' ]),
88
88
_QuantizeInfo (layers .convolutional .Conv3D , ['kernel' ], ['activation' ]),
89
89
# TODO(pulkitb): Verify Transpose layers.
90
90
_QuantizeInfo (layers .convolutional .Conv2DTranspose ,
@@ -94,8 +94,6 @@ class TFLiteQuantizeRegistry(quantize_registry.QuantizeRegistry, _RNNHelper):
94
94
_no_quantize (layers .convolutional .Cropping1D ),
95
95
_no_quantize (layers .convolutional .Cropping2D ),
96
96
_no_quantize (layers .convolutional .Cropping3D ),
97
- _QuantizeInfo (layers .convolutional .DepthwiseConv2D ,
98
- ['depthwise_kernel' ], ['activation' ]),
99
97
_no_quantize (layers .convolutional .UpSampling1D ),
100
98
_no_quantize (layers .convolutional .UpSampling2D ),
101
99
_no_quantize (layers .convolutional .UpSampling3D ),
@@ -172,6 +170,10 @@ def __init__(self):
172
170
# Hack for `Activation` layer. That is the only layer with a separate
173
171
# QuantizeProvider.
174
172
self ._layer_quantize_map [layers .Activation ] = ActivationQuantizeProvider ()
173
+ self ._layer_quantize_map [layers .Conv2D ] = ConvQuantizeProvider (
174
+ ['kernel' ], ['activation' ], False )
175
+ self ._layer_quantize_map [layers .DepthwiseConv2D ] = ConvQuantizeProvider (
176
+ ['depthwise_kernel' ], ['activation' ], False )
175
177
176
178
def _is_supported_layer (self , layer ):
177
179
return layer .__class__ in self ._layer_quantize_map
@@ -466,9 +468,20 @@ def get_config(self):
466
468
return {}
467
469
468
470
471
+ class ConvQuantizeProvider (TFLiteQuantizeProvider ):
472
+ """QuantizeProvider for Conv2D/DepthwiseConv2D layers."""
473
+
474
+ def __init__ (self , weight_attrs , activation_attrs , quantize_output ):
475
+ super (ConvQuantizeProvider , self ).__init__ (
476
+ weight_attrs , activation_attrs , quantize_output )
477
+
478
+ self .weight_quantizer = tflite_quantizers .ConvWeightsQuantizer ()
479
+
480
+
469
481
def _types_dict ():
470
482
return {
471
483
'TFLiteQuantizeProvider' : TFLiteQuantizeProvider ,
472
484
'TFLiteQuantizeProviderRNN' : TFLiteQuantizeProviderRNN ,
473
- 'ActivationQuantizeProvider' : ActivationQuantizeProvider
485
+ 'ActivationQuantizeProvider' : ActivationQuantizeProvider ,
486
+ 'ConvQuantizeProvider' : ConvQuantizeProvider
474
487
}
0 commit comments