Skip to content

Commit 256658f

Browse files
alanchiaotensorflower-gardener
authored andcommitted
Fix quantization tests on opensource and run tests on TF 2.X.
Tests that require conversion to TFLite don't pass in 1.XX and 2.XX yet. PiperOrigin-RevId: 285711327
1 parent ab96106 commit 256658f

File tree

12 files changed

+49
-24
lines changed

12 files changed

+49
-24
lines changed

pip_pkg.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ cd bazel-bin/pip_pkg.runfiles/tensorflow_model_optimization
4040
# specifies the output dir) to setup.py, e.g.,
4141
# ./pip_pkg /tmp/tensorflow_model_optimization_pkg --release
4242
# passes `--release` to setup.py.
43-
python setup.py bdist_wheel --universal ${@:2} --dist-dir="$DEST" # >/dev/null
43+
python3 setup.py bdist_wheel --universal ${@:2} --dist-dir="$DEST" # >/dev/null
4444

4545
set +x
4646
echo -e "\nBuild complete. Wheel files are in $DEST"

tensorflow_model_optimization/python/core/quantization/keras/BUILD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ py_test(
7373
visibility = ["//visibility:public"],
7474
deps = [
7575
":quantizers",
76+
# absl/testing:parameterized dep1,
7677
# numpy dep1,
7778
# tensorflow dep1,
7879
# python:dtypes tensorflow dep2,
@@ -147,6 +148,7 @@ py_test(
147148
deps = [
148149
":quantize_annotate",
149150
":quantize_provider",
151+
# numpy dep1,
150152
# tensorflow dep1,
151153
# python/keras tensorflow dep2,
152154
],
@@ -177,6 +179,7 @@ py_test(
177179
deps = [
178180
":quantize_aware_activation",
179181
":quantizers",
182+
# numpy dep1,
180183
# tensorflow dep1,
181184
# python/keras tensorflow dep2,
182185
],
@@ -209,6 +212,7 @@ py_test(
209212
deps = [
210213
":quantize_aware_activation",
211214
":quantize_wrapper",
215+
# absl/testing:parameterized dep1,
212216
# numpy dep1,
213217
# tensorflow dep1,
214218
# python/keras tensorflow dep2,
@@ -244,6 +248,7 @@ py_test(
244248
":quantize",
245249
":quantize_wrapper",
246250
# absl/testing:parameterized dep1,
251+
# numpy dep1,
247252
# tensorflow dep1,
248253
"//tensorflow_model_optimization/python/core/quantization/keras/tflite:tflite_quantize_registry",
249254
],
@@ -256,6 +261,7 @@ py_test(
256261
deps = [
257262
":quantize",
258263
":utils",
264+
# absl/testing:parameterized dep1,
259265
# numpy dep1,
260266
# tensorflow dep1,
261267
"//tensorflow_model_optimization/python/core/keras:test_utils",
@@ -271,6 +277,7 @@ py_test(
271277
deps = [
272278
":quantize",
273279
":utils",
280+
# absl/testing:parameterized dep1,
274281
# numpy dep1,
275282
# tensorflow dep1,
276283
"//tensorflow_model_optimization/python/core/keras:test_utils",

tensorflow_model_optimization/python/core/quantization/keras/graph_transformations/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ py_test(
5555
visibility = ["//visibility:public"],
5656
deps = [
5757
":model_transformer",
58+
# numpy dep1,
5859
# tensorflow dep1,
5960
# python/keras tensorflow dep2,
6061
],

tensorflow_model_optimization/python/core/quantization/keras/layers/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ py_test(
2929
deps = [
3030
":conv_batchnorm",
3131
":conv_batchnorm_test_utils",
32+
# numpy dep1,
3233
# tensorflow dep1,
3334
"//tensorflow_model_optimization/python/core/quantization/keras:quantize",
3435
"//tensorflow_model_optimization/python/core/quantization/keras:utils",

tensorflow_model_optimization/python/core/quantization/keras/layers/conv_batchnorm.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
from __future__ import absolute_import
1818
from __future__ import division
19-
from __future__ import google_type_annotations
2019
from __future__ import print_function
2120

2221
import tensorflow as tf

tensorflow_model_optimization/python/core/quantization/keras/layers/conv_batchnorm_test.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ def _get_asymmetric_quant_params(real_min, real_max, quant_min, quant_max):
7575
def _test_equal_tf_and_tflite_outputs(self,
7676
tf_model,
7777
is_tflite_quantized=False):
78-
_, keras_file = tempfile.mkstemp('.h5')
7978
_, tflite_file = tempfile.mkstemp('.tflite')
8079

8180
batched_input_shape = self._get_batched_input_shape()
@@ -115,10 +114,9 @@ def _test_equal_tf_and_tflite_outputs(self,
115114
tf_out = tf_model.predict(inp)
116115

117116
# TensorFlow Lite inference.
118-
tf.keras.models.save_model(tf_model, keras_file)
119117
with quantize.quantize_scope():
120118
utils.convert_keras_to_tflite(
121-
keras_file,
119+
tf_model,
122120
tflite_file,
123121
custom_objects={
124122
'_ConvBatchNorm2D': _ConvBatchNorm2D,

tensorflow_model_optimization/python/core/quantization/keras/quantize_functional_test.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
from absl.testing import parameterized
2424

25-
from tensorflow.python import keras
2625
from tensorflow.python.platform import test
2726

2827
from tensorflow_model_optimization.python.core.keras.testing import test_utils_mnist
@@ -54,13 +53,11 @@ def testQuantizesMnist(self):
5453

5554
self.assertGreater(quantized_model_accuracy, 0.6)
5655

57-
_, quantized_keras_file = tempfile.mkstemp('.h5')
5856
_, quantized_tflite_file = tempfile.mkstemp('.tflite')
5957

60-
keras.models.save_model(quantized_model, quantized_keras_file)
6158
with quantize.quantize_scope():
6259
test_utils.convert_keras_to_tflite(
63-
model_path=quantized_keras_file,
60+
model=quantized_model,
6461
output_path=quantized_tflite_file,
6562
is_quantized=True)
6663
quantized_model_tflite_accuracy = test_utils_mnist.eval_tflite(

tensorflow_model_optimization/python/core/quantization/keras/quantize_integration_test.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def testSerialization(self):
7272
loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
7373
quantized_model.fit(
7474
np.random.rand(20, 10),
75-
keras.utils.np_utils.to_categorical(
75+
tf.keras.utils.to_categorical(
7676
np.random.randint(5, size=(20, 1)), 5),
7777
batch_size=20)
7878

@@ -91,13 +91,10 @@ def testProductionModelConversionToTFLite(self):
9191
annotated = quantize_annotate(model)
9292
quantized_model = quantize_apply(annotated)
9393

94-
_, keras_file = tempfile.mkstemp('.h5')
9594
_, tflite_file = tempfile.mkstemp('.h5')
9695

97-
keras.models.save_model(quantized_model, keras_file)
98-
9996
with quantize.quantize_scope():
100-
utils.convert_keras_to_tflite(keras_file, tflite_file)
97+
utils.convert_keras_to_tflite(quantized_model, tflite_file)
10198

10299

103100
if __name__ == '__main__':

tensorflow_model_optimization/python/core/quantization/keras/quantize_wrapper_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def _get_quantized_weights(shape, dtype): # pylint: disable=unused-argument
116116
assert tuple(shape) == self.weights.shape
117117

118118
# Default values used in TFLiteRegistry.
119-
return tf.fake_quant_with_min_max_vars(
119+
return tf.quantization.fake_quant_with_min_max_vars(
120120
self.weights, -6.0, 6.0, num_bits=8, narrow_range=True)
121121

122122
layer = layer_type(kernel_initializer=_get_random_weights, **kwargs)
@@ -141,7 +141,7 @@ def _get_quantized_weights(shape, dtype): # pylint: disable=unused-argument
141141
inputs = np.random.rand(1, *input_shape)
142142
# `quantized_model` should apply FakeQuant. Explicitly applying to the
143143
# results of `model` to verify QuantizeWrapper works as expected.
144-
expected_output = tf.fake_quant_with_min_max_vars(
144+
expected_output = tf.quantization.fake_quant_with_min_max_vars(
145145
model.predict(inputs), -6.0, 6.0, num_bits=8, narrow_range=False)
146146
self.assertAllClose(expected_output, quantized_model.predict(inputs))
147147

@@ -157,7 +157,7 @@ def testQuantizesOutputsFromLayer(self):
157157
model = keras.Sequential([layers.ReLU()])
158158

159159
inputs = np.random.rand(1, 2, 1)
160-
expected_output = tf.fake_quant_with_min_max_vars(
160+
expected_output = tf.quantization.fake_quant_with_min_max_vars(
161161
model.predict(inputs), -6.0, 6.0, num_bits=8, narrow_range=False)
162162
self.assertAllClose(expected_output, quantized_model.predict(inputs))
163163

tensorflow_model_optimization/python/core/quantization/keras/tflite/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ py_test(
6262
visibility = ["//visibility:public"],
6363
deps = [
6464
":tflite_quantize_registry",
65+
# numpy dep1,
6566
# tensorflow dep1,
6667
# python/keras tensorflow dep2,
6768
],
@@ -92,6 +93,7 @@ py_test(
9293
visibility = ["//visibility:public"],
9394
deps = [
9495
":tflite_transforms",
96+
# numpy dep1,
9597
# python/keras tensorflow dep2,
9698
"//tensorflow_model_optimization/python/core/quantization/keras:quantize",
9799
"//tensorflow_model_optimization/python/core/quantization/keras/graph_transformations:model_transformer",

0 commit comments

Comments
 (0)