24
24
from tensorflow_model_optimization .python .core .quantization .keras import quantize_aware_activation
25
25
from tensorflow_model_optimization .python .core .quantization .keras import quantize_layer
26
26
from tensorflow_model_optimization .python .core .quantization .keras import quantizers
27
+ from tensorflow_model_optimization .python .core .quantization .keras import utils as quantize_utils
27
28
from tensorflow_model_optimization .python .core .quantization .keras .default_8bit import default_8bit_quantize_configs
28
29
from tensorflow_model_optimization .python .core .quantization .keras .default_8bit import default_8bit_quantize_registry
29
30
from tensorflow_model_optimization .python .core .quantization .keras .graph_transformations import transforms
@@ -67,13 +68,17 @@ def _get_params(conv_layer, bn_layer, relu_layer=None):
67
68
list (conv_layer ['config' ].items ()) + list (bn_layer ['config' ].items ()))
68
69
69
70
if relu_layer is not None :
70
- params ['post_activation' ] = keras .layers .deserialize (relu_layer )
71
+ params ['post_activation' ] = quantize_utils .deserialize_layer (
72
+ relu_layer , use_legacy_format = True
73
+ )
71
74
72
75
return params
73
76
74
77
75
78
def _get_layer_node (fused_layer , weights ):
76
- layer_config = keras .layers .serialize (fused_layer )
79
+ layer_config = quantize_utils .serialize_layer (
80
+ fused_layer , use_legacy_format = True
81
+ )
77
82
layer_config ['name' ] = layer_config ['config' ]['name' ]
78
83
# This config tracks which layers get quantized, and whether they have a
79
84
# custom QuantizeConfig.
@@ -118,7 +123,10 @@ def _replace(self, bn_layer_node, conv_layer_node):
118
123
return bn_layer_node
119
124
120
125
conv_layer_node .layer ['config' ]['activation' ] = (
121
- keras .activations .serialize (quantize_aware_activation .NoOpActivation ()))
126
+ quantize_utils .serialize_activation (
127
+ quantize_aware_activation .NoOpActivation (), use_legacy_format = True
128
+ )
129
+ )
122
130
bn_layer_node .metadata ['quantize_config' ] = (
123
131
default_8bit_quantize_configs .Default8BitOutputQuantizeConfig ())
124
132
@@ -180,7 +188,10 @@ def _replace(self, relu_layer_node, bn_layer_node, conv_layer_node):
180
188
return relu_layer_node
181
189
182
190
conv_layer_node .layer ['config' ]['activation' ] = (
183
- keras .activations .serialize (quantize_aware_activation .NoOpActivation ()))
191
+ quantize_utils .serialize_activation (
192
+ quantize_aware_activation .NoOpActivation (), use_legacy_format = True
193
+ )
194
+ )
184
195
bn_layer_node .metadata ['quantize_config' ] = (
185
196
default_8bit_quantize_configs .NoOpQuantizeConfig ())
186
197
@@ -261,7 +272,10 @@ def _replace(self, bn_layer_node, dense_layer_node):
261
272
return bn_layer_node
262
273
263
274
dense_layer_node .layer ['config' ]['activation' ] = (
264
- keras .activations .serialize (quantize_aware_activation .NoOpActivation ()))
275
+ quantize_utils .serialize_activation (
276
+ quantize_aware_activation .NoOpActivation (), use_legacy_format = True
277
+ )
278
+ )
265
279
bn_layer_node .metadata ['quantize_config' ] = (
266
280
default_8bit_quantize_configs .Default8BitOutputQuantizeConfig ())
267
281
@@ -297,7 +311,10 @@ def _replace(self, relu_layer_node, bn_layer_node, dense_layer_node):
297
311
return relu_layer_node
298
312
299
313
dense_layer_node .layer ['config' ]['activation' ] = (
300
- keras .activations .serialize (quantize_aware_activation .NoOpActivation ()))
314
+ quantize_utils .serialize_activation (
315
+ quantize_aware_activation .NoOpActivation (), use_legacy_format = True
316
+ )
317
+ )
301
318
bn_layer_node .metadata ['quantize_config' ] = (
302
319
default_8bit_quantize_configs .NoOpQuantizeConfig ())
303
320
@@ -408,7 +425,9 @@ def replacement(self, match_layer):
408
425
else :
409
426
spatial_dim = 2
410
427
411
- sepconv2d_layer_config = keras .layers .serialize (sepconv2d_layer )
428
+ sepconv2d_layer_config = quantize_utils .serialize_layer (
429
+ sepconv2d_layer , use_legacy_format = True
430
+ )
412
431
sepconv2d_layer_config ['name' ] = sepconv2d_layer .name
413
432
414
433
# Needed to ensure these new layers are considered for quantization.
@@ -420,15 +439,19 @@ def replacement(self, match_layer):
420
439
expand_layer = tf .keras .layers .Lambda (
421
440
lambda x : tf .expand_dims (x , spatial_dim ),
422
441
name = self ._get_name ('sepconv1d_expand' ))
423
- expand_layer_config = keras .layers .serialize (expand_layer )
442
+ expand_layer_config = quantize_utils .serialize_layer (
443
+ expand_layer , use_legacy_format = True
444
+ )
424
445
expand_layer_config ['name' ] = expand_layer .name
425
446
expand_layer_metadata = {
426
447
'quantize_config' : default_8bit_quantize_configs .NoOpQuantizeConfig ()}
427
448
428
449
squeeze_layer = tf .keras .layers .Lambda (
429
450
lambda x : tf .squeeze (x , [spatial_dim ]),
430
451
name = self ._get_name ('sepconv1d_squeeze' ))
431
- squeeze_layer_config = keras .layers .serialize (squeeze_layer )
452
+ squeeze_layer_config = quantize_utils .serialize_layer (
453
+ squeeze_layer , use_legacy_format = True
454
+ )
432
455
squeeze_layer_config ['name' ] = squeeze_layer .name
433
456
squeeze_layer_metadata = {
434
457
'quantize_config' : default_8bit_quantize_configs .NoOpQuantizeConfig ()}
@@ -493,7 +516,9 @@ def replacement(self, match_layer):
493
516
)
494
517
dconv_weights = collections .OrderedDict ()
495
518
dconv_weights ['depthwise_kernel:0' ] = sepconv_weights [0 ]
496
- dconv_layer_config = keras .layers .serialize (dconv_layer )
519
+ dconv_layer_config = quantize_utils .serialize_layer (
520
+ dconv_layer , use_legacy_format = True
521
+ )
497
522
dconv_layer_config ['name' ] = dconv_layer .name
498
523
# Needed to ensure these new layers are considered for quantization.
499
524
dconv_metadata = {'quantize_config' : None }
@@ -521,7 +546,9 @@ def replacement(self, match_layer):
521
546
conv_weights ['kernel:0' ] = sepconv_weights [1 ]
522
547
if sepconv_layer ['config' ]['use_bias' ]:
523
548
conv_weights ['bias:0' ] = sepconv_weights [2 ]
524
- conv_layer_config = keras .layers .serialize (conv_layer )
549
+ conv_layer_config = quantize_utils .serialize_layer (
550
+ conv_layer , use_legacy_format = True
551
+ )
525
552
conv_layer_config ['name' ] = conv_layer .name
526
553
# Needed to ensure these new layers are considered for quantization.
527
554
conv_metadata = {'quantize_config' : None }
@@ -588,7 +615,9 @@ def replacement(self, match_layer):
588
615
quant_layer = quantize_layer .QuantizeLayer (
589
616
quantizers .AllValuesQuantizer (
590
617
num_bits = 8 , per_axis = False , symmetric = False , narrow_range = False ))
591
- layer_config = keras .layers .serialize (quant_layer )
618
+ layer_config = quantize_utils .serialize_layer (
619
+ quant_layer , use_legacy_format = True
620
+ )
592
621
layer_config ['name' ] = quant_layer .name
593
622
594
623
quant_layer_node = LayerNode (
0 commit comments