Remove run_v1_only from lite_test Python tests.

PiperOrigin-RevId: 261150274
This commit is contained in:
Nupur Garg 2019-08-01 10:39:04 -07:00 committed by TensorFlower Gardener
parent 2bc45ceb9e
commit 219e8d8d6f

View File

@ -101,14 +101,14 @@ class FromConstructor(TestModels):
self.assertTrue(converter._has_valid_tensors()) self.assertTrue(converter._has_valid_tensors())
@test_util.run_v1_only('Incompatible with 2.0.')
class FromSessionTest(TestModels, parameterized.TestCase): class FromSessionTest(TestModels, parameterized.TestCase):
def testFloat(self): def testFloat(self):
in_tensor = array_ops.placeholder( with ops.Graph().as_default():
shape=[1, 16, 16, 3], dtype=dtypes.float32) in_tensor = array_ops.placeholder(
out_tensor = in_tensor + in_tensor shape=[1, 16, 16, 3], dtype=dtypes.float32)
sess = session.Session() out_tensor = in_tensor + in_tensor
sess = session.Session()
# Convert model and ensure model is not None. # Convert model and ensure model is not None.
converter = lite.TFLiteConverter.from_session(sess, [in_tensor], converter = lite.TFLiteConverter.from_session(sess, [in_tensor],
@ -135,9 +135,10 @@ class FromSessionTest(TestModels, parameterized.TestCase):
self.assertEqual((0., 0.), output_details[0]['quantization']) self.assertEqual((0., 0.), output_details[0]['quantization'])
def testString(self): def testString(self):
in_tensor = array_ops.placeholder(shape=[4], dtype=dtypes.string) with ops.Graph().as_default():
out_tensor = array_ops.reshape(in_tensor, shape=[2, 2]) in_tensor = array_ops.placeholder(shape=[4], dtype=dtypes.string)
sess = session.Session() out_tensor = array_ops.reshape(in_tensor, shape=[2, 2])
sess = session.Session()
# Convert model and ensure model is not None. # Convert model and ensure model is not None.
converter = lite.TFLiteConverter.from_session(sess, [in_tensor], converter = lite.TFLiteConverter.from_session(sess, [in_tensor],
@ -164,13 +165,14 @@ class FromSessionTest(TestModels, parameterized.TestCase):
# interpreter API after support has been added. # interpreter API after support has been added.
def testQuantization(self): def testQuantization(self):
in_tensor_1 = array_ops.placeholder( with ops.Graph().as_default():
shape=[1, 16, 16, 3], dtype=dtypes.float32, name='inputA') in_tensor_1 = array_ops.placeholder(
in_tensor_2 = array_ops.placeholder( shape=[1, 16, 16, 3], dtype=dtypes.float32, name='inputA')
shape=[1, 16, 16, 3], dtype=dtypes.float32, name='inputB') in_tensor_2 = array_ops.placeholder(
out_tensor = array_ops.fake_quant_with_min_max_args( shape=[1, 16, 16, 3], dtype=dtypes.float32, name='inputB')
in_tensor_1 + in_tensor_2, min=0., max=1., name='output') out_tensor = array_ops.fake_quant_with_min_max_args(
sess = session.Session() in_tensor_1 + in_tensor_2, min=0., max=1., name='output')
sess = session.Session()
# Convert model and ensure model is not None. # Convert model and ensure model is not None.
converter = lite.TFLiteConverter.from_session(sess, converter = lite.TFLiteConverter.from_session(sess,
@ -210,13 +212,14 @@ class FromSessionTest(TestModels, parameterized.TestCase):
self.assertTrue(output_details[0]['quantization'][0] > 0) # scale self.assertTrue(output_details[0]['quantization'][0] > 0) # scale
def testQuantizationInvalid(self): def testQuantizationInvalid(self):
in_tensor_1 = array_ops.placeholder( with ops.Graph().as_default():
shape=[1, 16, 16, 3], dtype=dtypes.float32, name='inputA') in_tensor_1 = array_ops.placeholder(
in_tensor_2 = array_ops.placeholder( shape=[1, 16, 16, 3], dtype=dtypes.float32, name='inputA')
shape=[1, 16, 16, 3], dtype=dtypes.float32, name='inputB') in_tensor_2 = array_ops.placeholder(
out_tensor = array_ops.fake_quant_with_min_max_args( shape=[1, 16, 16, 3], dtype=dtypes.float32, name='inputB')
in_tensor_1 + in_tensor_2, min=0., max=1., name='output') out_tensor = array_ops.fake_quant_with_min_max_args(
sess = session.Session() in_tensor_1 + in_tensor_2, min=0., max=1., name='output')
sess = session.Session()
# Convert model and ensure model is not None. # Convert model and ensure model is not None.
converter = lite.TFLiteConverter.from_session(sess, converter = lite.TFLiteConverter.from_session(sess,
@ -232,11 +235,12 @@ class FromSessionTest(TestModels, parameterized.TestCase):
def testIntermediateInputArray(self): def testIntermediateInputArray(self):
"""Convert a model from an intermediate input array.""" """Convert a model from an intermediate input array."""
in_tensor_init = array_ops.placeholder( with ops.Graph().as_default():
shape=[1, 16, 16, 3], dtype=dtypes.float32) in_tensor_init = array_ops.placeholder(
in_tensor_final = in_tensor_init + in_tensor_init shape=[1, 16, 16, 3], dtype=dtypes.float32)
out_tensor = in_tensor_final + in_tensor_final in_tensor_final = in_tensor_init + in_tensor_init
sess = session.Session() out_tensor = in_tensor_final + in_tensor_final
sess = session.Session()
# Convert model and ensure model is not None. # Convert model and ensure model is not None.
converter = lite.TFLiteConverter.from_session(sess, [in_tensor_final], converter = lite.TFLiteConverter.from_session(sess, [in_tensor_final],
@ -263,9 +267,10 @@ class FromSessionTest(TestModels, parameterized.TestCase):
self.assertEqual((0., 0.), output_details[0]['quantization']) self.assertEqual((0., 0.), output_details[0]['quantization'])
def testSizeNoneInvalid(self): def testSizeNoneInvalid(self):
in_tensor = array_ops.placeholder(dtype=dtypes.float32) with ops.Graph().as_default():
out_tensor = in_tensor + in_tensor in_tensor = array_ops.placeholder(dtype=dtypes.float32)
sess = session.Session() out_tensor = in_tensor + in_tensor
sess = session.Session()
# Test None as shape. # Test None as shape.
converter = lite.TFLiteConverter.from_session(sess, [in_tensor], converter = lite.TFLiteConverter.from_session(sess, [in_tensor],
@ -277,9 +282,10 @@ class FromSessionTest(TestModels, parameterized.TestCase):
def testScalarValid(self): def testScalarValid(self):
# Construct a graph using a scalar (empty shape) input. # Construct a graph using a scalar (empty shape) input.
in_tensor = array_ops.placeholder(dtype=dtypes.float32, shape=[]) with ops.Graph().as_default():
out_tensor = in_tensor + in_tensor in_tensor = array_ops.placeholder(dtype=dtypes.float32, shape=[])
sess = session.Session() out_tensor = in_tensor + in_tensor
sess = session.Session()
# Test conversion with the scalar input shape. # Test conversion with the scalar input shape.
converter = lite.TFLiteConverter.from_session(sess, [in_tensor], converter = lite.TFLiteConverter.from_session(sess, [in_tensor],
@ -313,10 +319,11 @@ class FromSessionTest(TestModels, parameterized.TestCase):
self.assertTrue((expected_output == output_data).all()) self.assertTrue((expected_output == output_data).all())
def testSizeInvalid(self): def testSizeInvalid(self):
in_tensor = array_ops.placeholder( with ops.Graph().as_default():
shape=[1, None, 16, 3], dtype=dtypes.float32) in_tensor = array_ops.placeholder(
out_tensor = in_tensor + in_tensor shape=[1, None, 16, 3], dtype=dtypes.float32)
sess = session.Session() out_tensor = in_tensor + in_tensor
sess = session.Session()
# Test invalid shape. None after 1st dimension. # Test invalid shape. None after 1st dimension.
converter = lite.TFLiteConverter.from_session(sess, [in_tensor], converter = lite.TFLiteConverter.from_session(sess, [in_tensor],
@ -329,10 +336,11 @@ class FromSessionTest(TestModels, parameterized.TestCase):
str(error.exception)) str(error.exception))
def testBatchSizeValid(self): def testBatchSizeValid(self):
in_tensor = array_ops.placeholder( with ops.Graph().as_default():
shape=[None, 16, 16, 3], dtype=dtypes.float32) in_tensor = array_ops.placeholder(
out_tensor = in_tensor + in_tensor shape=[None, 16, 16, 3], dtype=dtypes.float32)
sess = session.Session() out_tensor = in_tensor + in_tensor
sess = session.Session()
# Convert model and ensure model is not None. # Convert model and ensure model is not None.
converter = lite.TFLiteConverter.from_session(sess, [in_tensor], converter = lite.TFLiteConverter.from_session(sess, [in_tensor],
@ -359,13 +367,14 @@ class FromSessionTest(TestModels, parameterized.TestCase):
self.assertEqual((0., 0.), output_details[0]['quantization']) self.assertEqual((0., 0.), output_details[0]['quantization'])
def testFreezeGraph(self): def testFreezeGraph(self):
in_tensor = array_ops.placeholder( with ops.Graph().as_default():
shape=[1, 16, 16, 3], dtype=dtypes.float32) in_tensor = array_ops.placeholder(
var = variable_scope.get_variable( shape=[1, 16, 16, 3], dtype=dtypes.float32)
'weights', shape=[1, 16, 16, 3], dtype=dtypes.float32) var = variable_scope.get_variable(
out_tensor = in_tensor + var 'weights', shape=[1, 16, 16, 3], dtype=dtypes.float32)
sess = session.Session() out_tensor = in_tensor + var
sess.run(_global_variables_initializer()) sess = session.Session()
sess.run(_global_variables_initializer())
# Convert model and ensure model is not None. # Convert model and ensure model is not None.
converter = lite.TFLiteConverter.from_session(sess, [in_tensor], converter = lite.TFLiteConverter.from_session(sess, [in_tensor],
@ -391,12 +400,12 @@ class FromSessionTest(TestModels, parameterized.TestCase):
self.assertTrue(([1, 16, 16, 3] == output_details[0]['shape']).all()) self.assertTrue(([1, 16, 16, 3] == output_details[0]['shape']).all())
self.assertEqual((0., 0.), output_details[0]['quantization']) self.assertEqual((0., 0.), output_details[0]['quantization'])
# TODO(nupurgarg): Verify value of contents in GraphViz.
def testGraphviz(self): def testGraphviz(self):
in_tensor = array_ops.placeholder( with ops.Graph().as_default():
shape=[1, 16, 16, 3], dtype=dtypes.float32) in_tensor = array_ops.placeholder(
out_tensor = in_tensor + in_tensor shape=[1, 16, 16, 3], dtype=dtypes.float32)
sess = session.Session() out_tensor = in_tensor + in_tensor
sess = session.Session()
# Convert model and ensure model is not None. # Convert model and ensure model is not None.
converter = lite.TFLiteConverter.from_session(sess, [in_tensor], converter = lite.TFLiteConverter.from_session(sess, [in_tensor],
@ -405,12 +414,12 @@ class FromSessionTest(TestModels, parameterized.TestCase):
graphviz_output = converter.convert() graphviz_output = converter.convert()
self.assertTrue(graphviz_output) self.assertTrue(graphviz_output)
# TODO(nupurgarg): Verify value of contents in GraphViz.
def testDumpGraphviz(self): def testDumpGraphviz(self):
in_tensor = array_ops.placeholder( with ops.Graph().as_default():
shape=[1, 16, 16, 3], dtype=dtypes.float32) in_tensor = array_ops.placeholder(
out_tensor = in_tensor + in_tensor shape=[1, 16, 16, 3], dtype=dtypes.float32)
sess = session.Session() out_tensor = in_tensor + in_tensor
sess = session.Session()
# Convert model and ensure model is not None. # Convert model and ensure model is not None.
converter = lite.TFLiteConverter.from_session(sess, [in_tensor], converter = lite.TFLiteConverter.from_session(sess, [in_tensor],
@ -441,10 +450,11 @@ class FromSessionTest(TestModels, parameterized.TestCase):
self.assertTrue(num_items_graphviz_video > num_items_graphviz) self.assertTrue(num_items_graphviz_video > num_items_graphviz)
def testInferenceInputType(self): def testInferenceInputType(self):
in_tensor = array_ops.placeholder( with ops.Graph().as_default():
shape=[1, 16, 16, 3], dtype=dtypes.float32) in_tensor = array_ops.placeholder(
out_tensor = in_tensor + in_tensor shape=[1, 16, 16, 3], dtype=dtypes.float32)
sess = session.Session() out_tensor = in_tensor + in_tensor
sess = session.Session()
# Convert model and ensure model is not None. # Convert model and ensure model is not None.
converter = lite.TFLiteConverter.from_session(sess, [in_tensor], converter = lite.TFLiteConverter.from_session(sess, [in_tensor],
@ -472,10 +482,11 @@ class FromSessionTest(TestModels, parameterized.TestCase):
self.assertTrue(([1, 16, 16, 3] == output_details[0]['shape']).all()) self.assertTrue(([1, 16, 16, 3] == output_details[0]['shape']).all())
def testDefaultRangesStats(self): def testDefaultRangesStats(self):
in_tensor = array_ops.placeholder( with ops.Graph().as_default():
shape=[1, 16, 16, 3], dtype=dtypes.float32) in_tensor = array_ops.placeholder(
out_tensor = in_tensor + in_tensor shape=[1, 16, 16, 3], dtype=dtypes.float32)
sess = session.Session() out_tensor = in_tensor + in_tensor
sess = session.Session()
# Convert model and ensure model is not None. # Convert model and ensure model is not None.
converter = lite.TFLiteConverter.from_session(sess, [in_tensor], converter = lite.TFLiteConverter.from_session(sess, [in_tensor],
@ -505,15 +516,16 @@ class FromSessionTest(TestModels, parameterized.TestCase):
self.assertTrue(output_details[0]['quantization'][0] > 0) # scale self.assertTrue(output_details[0]['quantization'][0] > 0) # scale
def testPostTrainingQuantizeDeprecatedAttribute(self): def testPostTrainingQuantizeDeprecatedAttribute(self):
in_tensor_1 = array_ops.placeholder( with ops.Graph().as_default():
shape=[33, 33], dtype=dtypes.float32, name='inputA') in_tensor_1 = array_ops.placeholder(
in_tensor_2 = constant_op.constant( shape=[33, 33], dtype=dtypes.float32, name='inputA')
np.random.uniform(low=-10., high=10., size=(33, 33)), in_tensor_2 = constant_op.constant(
shape=[33, 33], np.random.uniform(low=-10., high=10., size=(33, 33)),
dtype=dtypes.float32, shape=[33, 33],
name='inputB') dtype=dtypes.float32,
out_tensor = math_ops.matmul(in_tensor_1, in_tensor_2, name='output') name='inputB')
sess = session.Session() out_tensor = math_ops.matmul(in_tensor_1, in_tensor_2, name='output')
sess = session.Session()
quantized_converter = lite.TFLiteConverter.from_session( quantized_converter = lite.TFLiteConverter.from_session(
sess, [in_tensor_1], [out_tensor]) sess, [in_tensor_1], [out_tensor])
@ -528,17 +540,18 @@ class FromSessionTest(TestModels, parameterized.TestCase):
def testPostTrainingQuantize(self): def testPostTrainingQuantize(self):
np.random.seed(0) np.random.seed(0)
# We need the tensor to have more than 1024 elements for quantize_weights with ops.Graph().as_default():
# to kick in. Thus, the [33, 33] shape. # We need the tensor to have more than 1024 elements for quantize_weights
in_tensor_1 = array_ops.placeholder( # to kick in. Thus, the [33, 33] shape.
shape=[33, 33], dtype=dtypes.float32, name='inputA') in_tensor_1 = array_ops.placeholder(
in_tensor_2 = constant_op.constant( shape=[33, 33], dtype=dtypes.float32, name='inputA')
np.random.uniform(low=-10., high=10., size=(33, 33)), in_tensor_2 = constant_op.constant(
shape=[33, 33], np.random.uniform(low=-10., high=10., size=(33, 33)),
dtype=dtypes.float32, shape=[33, 33],
name='inputB') dtype=dtypes.float32,
out_tensor = math_ops.matmul(in_tensor_1, in_tensor_2, name='output') name='inputB')
sess = session.Session() out_tensor = math_ops.matmul(in_tensor_1, in_tensor_2, name='output')
sess = session.Session()
# Convert float model. # Convert float model.
float_converter = lite.TFLiteConverter.from_session(sess, [in_tensor_1], float_converter = lite.TFLiteConverter.from_session(sess, [in_tensor_1],
@ -574,8 +587,9 @@ class FromSessionTest(TestModels, parameterized.TestCase):
return (inp, output, calibration_gen) return (inp, output, calibration_gen)
def testPostTrainingCalibrateAndQuantize(self): def testPostTrainingCalibrateAndQuantize(self):
inp, output, calibration_gen = self._getCalibrationQuantizeModel() with ops.Graph().as_default():
sess = session.Session() inp, output, calibration_gen = self._getCalibrationQuantizeModel()
sess = session.Session()
# Convert float model. # Convert float model.
float_converter = lite.TFLiteConverter.from_session(sess, [inp], [output]) float_converter = lite.TFLiteConverter.from_session(sess, [inp], [output])
@ -604,8 +618,9 @@ class FromSessionTest(TestModels, parameterized.TestCase):
self.assertLess(len(quantized_tflite), len(float_tflite)) self.assertLess(len(quantized_tflite), len(float_tflite))
def testCalibrateAndQuantizeBuiltinInt8(self): def testCalibrateAndQuantizeBuiltinInt8(self):
inp, output, calibration_gen = self._getCalibrationQuantizeModel() with ops.Graph().as_default():
sess = session.Session() inp, output, calibration_gen = self._getCalibrationQuantizeModel()
sess = session.Session()
# Convert float model. # Convert float model.
float_converter = lite.TFLiteConverter.from_session(sess, [inp], [output]) float_converter = lite.TFLiteConverter.from_session(sess, [inp], [output])
@ -648,8 +663,9 @@ class FromSessionTest(TestModels, parameterized.TestCase):
def testQuantizeFloat16(self, use_rep_data, include_int8, def testQuantizeFloat16(self, use_rep_data, include_int8,
is_float16_quantized, is_error, is_float16_quantized, is_error,
is_post_training_quantized): is_post_training_quantized):
inp, output, calibration_gen = self._getCalibrationQuantizeModel() with ops.Graph().as_default():
sess = session.Session() inp, output, calibration_gen = self._getCalibrationQuantizeModel()
sess = session.Session()
# Convert float model. # Convert float model.
float_converter = lite.TFLiteConverter.from_session(sess, [inp], [output]) float_converter = lite.TFLiteConverter.from_session(sess, [inp], [output])
@ -698,8 +714,9 @@ class FromSessionTest(TestModels, parameterized.TestCase):
raise ValueError('Invalid test options.') raise ValueError('Invalid test options.')
def testInvalidQuantizeFloat16(self): def testInvalidQuantizeFloat16(self):
inp, output, _ = self._getCalibrationQuantizeModel() with ops.Graph().as_default():
sess = session.Session() inp, output, _ = self._getCalibrationQuantizeModel()
sess = session.Session()
# Specify float16 quantization # Specify float16 quantization
quantized_converter = lite.TFLiteConverter.from_session( quantized_converter = lite.TFLiteConverter.from_session(
@ -718,17 +735,18 @@ class FromSessionTest(TestModels, parameterized.TestCase):
def testInvalidPostTrainingQuantize(self): def testInvalidPostTrainingQuantize(self):
np.random.seed(0) np.random.seed(0)
# We need the tensor to have more than 1024 elements for quantize_weights with ops.Graph().as_default():
# to kick in. Thus, the [33, 33] shape. # We need the tensor to have more than 1024 elements for quantize_weights
in_tensor_1 = array_ops.placeholder( # to kick in. Thus, the [33, 33] shape.
shape=[33, 33], dtype=dtypes.float32, name='inputA') in_tensor_1 = array_ops.placeholder(
in_tensor_2 = constant_op.constant( shape=[33, 33], dtype=dtypes.float32, name='inputA')
np.random.uniform(low=-10., high=10., size=(33, 33)), in_tensor_2 = constant_op.constant(
shape=[33, 33], np.random.uniform(low=-10., high=10., size=(33, 33)),
dtype=dtypes.float32, shape=[33, 33],
name='inputB') dtype=dtypes.float32,
out_tensor = math_ops.matmul(in_tensor_1, in_tensor_2, name='output') name='inputB')
sess = session.Session() out_tensor = math_ops.matmul(in_tensor_1, in_tensor_2, name='output')
sess = session.Session()
# Attempt to convert to quantized weights model. # Attempt to convert to quantized weights model.
quantized_converter = lite.TFLiteConverter.from_session( quantized_converter = lite.TFLiteConverter.from_session(
@ -744,8 +762,9 @@ class FromSessionTest(TestModels, parameterized.TestCase):
'TFLITE_BUILTINS_INT8 or INT8 supported types.', str(error.exception)) 'TFLITE_BUILTINS_INT8 or INT8 supported types.', str(error.exception))
def testPostTrainingCalibrateAndQuantizeFloatNotAllowed(self): def testPostTrainingCalibrateAndQuantizeFloatNotAllowed(self):
inp, output, calibration_gen = self._getCalibrationQuantizeModel() with ops.Graph().as_default():
sess = session.Session() inp, output, calibration_gen = self._getCalibrationQuantizeModel()
sess = session.Session()
# Convert float model. # Convert float model.
float_converter = lite.TFLiteConverter.from_session(sess, [inp], [output]) float_converter = lite.TFLiteConverter.from_session(sess, [inp], [output])
@ -768,8 +787,9 @@ class FromSessionTest(TestModels, parameterized.TestCase):
self.assertLess(len(quantized_tflite), len(float_tflite)) self.assertLess(len(quantized_tflite), len(float_tflite))
def testPostTrainingCalibrateAndQuantizeInt8Inputs(self): def testPostTrainingCalibrateAndQuantizeInt8Inputs(self):
inp, output, calibration_gen = self._getCalibrationQuantizeModel() with ops.Graph().as_default():
sess = session.Session() inp, output, calibration_gen = self._getCalibrationQuantizeModel()
sess = session.Session()
# Convert float model. # Convert float model.
float_converter = lite.TFLiteConverter.from_session(sess, [inp], [output]) float_converter = lite.TFLiteConverter.from_session(sess, [inp], [output])
@ -801,10 +821,11 @@ class FromSessionTest(TestModels, parameterized.TestCase):
def testFloatTocoConverter(self): def testFloatTocoConverter(self):
"""Tests deprecated test TocoConverter.""" """Tests deprecated test TocoConverter."""
in_tensor = array_ops.placeholder( with ops.Graph().as_default():
shape=[1, 16, 16, 3], dtype=dtypes.float32) in_tensor = array_ops.placeholder(
out_tensor = in_tensor + in_tensor shape=[1, 16, 16, 3], dtype=dtypes.float32)
sess = session.Session() out_tensor = in_tensor + in_tensor
sess = session.Session()
# Convert model and ensure model is not None. # Convert model and ensure model is not None.
converter = lite.TocoConverter.from_session(sess, [in_tensor], [out_tensor]) converter = lite.TocoConverter.from_session(sess, [in_tensor], [out_tensor])
@ -817,9 +838,11 @@ class FromSessionTest(TestModels, parameterized.TestCase):
def testMultipleOutputNodeNames(self): def testMultipleOutputNodeNames(self):
"""Tests converting a graph with an op that have multiple outputs.""" """Tests converting a graph with an op that have multiple outputs."""
input_tensor = array_ops.placeholder(shape=[4], dtype=dtypes.float32) with ops.Graph().as_default():
out0, out1, out2, out3 = array_ops.split(input_tensor, [1, 1, 1, 1], axis=0) input_tensor = array_ops.placeholder(shape=[4], dtype=dtypes.float32)
sess = session.Session() out0, out1, out2, out3 = array_ops.split(
input_tensor, [1, 1, 1, 1], axis=0)
sess = session.Session()
# Convert model and ensure model is not None. # Convert model and ensure model is not None.
converter = lite.TFLiteConverter.from_session(sess, [input_tensor], converter = lite.TFLiteConverter.from_session(sess, [input_tensor],
@ -888,10 +911,11 @@ class FromSessionTest(TestModels, parameterized.TestCase):
self.assertEqual((0., 0.), output_details[0]['quantization']) self.assertEqual((0., 0.), output_details[0]['quantization'])
def testInferenceInputOutputTypeFloatDefault(self): def testInferenceInputOutputTypeFloatDefault(self):
in_tensor = array_ops.placeholder( with ops.Graph().as_default():
shape=[1, 16, 16, 3], dtype=dtypes.float32) in_tensor = array_ops.placeholder(
out_tensor = in_tensor + in_tensor shape=[1, 16, 16, 3], dtype=dtypes.float32)
sess = session.Session() out_tensor = in_tensor + in_tensor
sess = session.Session()
# Convert model and ensure model is not None. # Convert model and ensure model is not None.
converter = lite.TFLiteConverter.from_session(sess, [in_tensor], converter = lite.TFLiteConverter.from_session(sess, [in_tensor],
@ -916,11 +940,12 @@ class FromSessionTest(TestModels, parameterized.TestCase):
self.assertTrue(([1, 16, 16, 3] == output_details[0]['shape']).all()) self.assertTrue(([1, 16, 16, 3] == output_details[0]['shape']).all())
def testInferenceInputOutputTypeQuantizedUint8Default(self): def testInferenceInputOutputTypeQuantizedUint8Default(self):
in_tensor = array_ops.placeholder( with ops.Graph().as_default():
shape=[1, 16, 16, 3], dtype=dtypes.float32) in_tensor = array_ops.placeholder(
out_tensor = array_ops.fake_quant_with_min_max_args( shape=[1, 16, 16, 3], dtype=dtypes.float32)
in_tensor + in_tensor, min=0., max=1., name='output') out_tensor = array_ops.fake_quant_with_min_max_args(
sess = session.Session() in_tensor + in_tensor, min=0., max=1., name='output')
sess = session.Session()
# Convert model and ensure model is not None. # Convert model and ensure model is not None.
converter = lite.TFLiteConverter.from_session(sess, [in_tensor], converter = lite.TFLiteConverter.from_session(sess, [in_tensor],
@ -947,11 +972,12 @@ class FromSessionTest(TestModels, parameterized.TestCase):
self.assertTrue(([1, 16, 16, 3] == output_details[0]['shape']).all()) self.assertTrue(([1, 16, 16, 3] == output_details[0]['shape']).all())
def testReusingConverterWithDifferentPostTrainingQuantization(self): def testReusingConverterWithDifferentPostTrainingQuantization(self):
in_tensor = array_ops.placeholder( with ops.Graph().as_default():
shape=[1, 16, 16, 3], dtype=dtypes.float32) in_tensor = array_ops.placeholder(
out_tensor = array_ops.fake_quant_with_min_max_args( shape=[1, 16, 16, 3], dtype=dtypes.float32)
in_tensor + in_tensor, min=0., max=1., name='output') out_tensor = array_ops.fake_quant_with_min_max_args(
sess = session.Session() in_tensor + in_tensor, min=0., max=1., name='output')
sess = session.Session()
# Convert model and ensure model is not None. # Convert model and ensure model is not None.
converter = lite.TFLiteConverter.from_session(sess, [in_tensor], converter = lite.TFLiteConverter.from_session(sess, [in_tensor],
@ -969,16 +995,18 @@ class FromSessionTest(TestModels, parameterized.TestCase):
# This is a regression test for the case where shape of dynamic output # This is a regression test for the case where shape of dynamic output
# tensors changes between invocations. # tensors changes between invocations.
# See also https://github.com/tensorflow/tensorflow/issues/26549 # See also https://github.com/tensorflow/tensorflow/issues/26549
input_tensor = array_ops.placeholder(shape=[1, 1], dtype=dtypes.float32) with ops.Graph().as_default():
input2_tensor = array_ops.placeholder(shape=[1], dtype=dtypes.float32) input_tensor = array_ops.placeholder(shape=[1, 1], dtype=dtypes.float32)
input2_tensor = array_ops.placeholder(shape=[1], dtype=dtypes.float32)
# The bug is triggered only when dynamic tensor is intermediate. Putting # The bug is triggered only when dynamic tensor is intermediate. Putting
# some other ops around it. # some other ops around it.
neg = math_ops.negative(input2_tensor) neg = math_ops.negative(input2_tensor)
padding = array_ops.placeholder(shape=[2, 2], dtype=dtypes.int32) padding = array_ops.placeholder(shape=[2, 2], dtype=dtypes.int32)
output_tensor = array_ops.pad(input_tensor, padding) + neg output_tensor = array_ops.pad(input_tensor, padding) + neg
sess = session.Session()
sess = session.Session()
converter = lite.TFLiteConverter.from_session( converter = lite.TFLiteConverter.from_session(
sess, [input_tensor, padding, input2_tensor], [output_tensor]) sess, [input_tensor, padding, input2_tensor], [output_tensor])
tflite_model = converter.convert() tflite_model = converter.convert()
@ -1025,14 +1053,14 @@ class FromSessionTest(TestModels, parameterized.TestCase):
self.assertIn((func + 'add'), converter._debug_info.traces) self.assertIn((func + 'add'), converter._debug_info.traces)
@test_util.run_v1_only('Incompatible with 2.0.')
class FromFrozenGraphFile(test_util.TensorFlowTestCase): class FromFrozenGraphFile(test_util.TensorFlowTestCase):
def testFloat(self): def testFloat(self):
in_tensor = array_ops.placeholder( with ops.Graph().as_default():
shape=[1, 16, 16, 3], dtype=dtypes.float32) in_tensor = array_ops.placeholder(
_ = in_tensor + in_tensor shape=[1, 16, 16, 3], dtype=dtypes.float32)
sess = session.Session() _ = in_tensor + in_tensor
sess = session.Session()
# Write graph to file. # Write graph to file.
graph_def_file = os.path.join(self.get_temp_dir(), 'model.pb') graph_def_file = os.path.join(self.get_temp_dir(), 'model.pb')
@ -1064,10 +1092,11 @@ class FromFrozenGraphFile(test_util.TensorFlowTestCase):
self.assertEqual((0., 0.), output_details[0]['quantization']) self.assertEqual((0., 0.), output_details[0]['quantization'])
def testFloatWithShapesArray(self): def testFloatWithShapesArray(self):
in_tensor = array_ops.placeholder( with ops.Graph().as_default():
shape=[1, 16, 16, 3], dtype=dtypes.float32) in_tensor = array_ops.placeholder(
_ = in_tensor + in_tensor shape=[1, 16, 16, 3], dtype=dtypes.float32)
sess = session.Session() _ = in_tensor + in_tensor
sess = session.Session()
# Write graph to file. # Write graph to file.
graph_def_file = os.path.join(self.get_temp_dir(), 'model.pb') graph_def_file = os.path.join(self.get_temp_dir(), 'model.pb')
@ -1090,12 +1119,13 @@ class FromFrozenGraphFile(test_util.TensorFlowTestCase):
self.assertTrue(([1, 16, 16, 3] == input_details[0]['shape']).all()) self.assertTrue(([1, 16, 16, 3] == input_details[0]['shape']).all())
def testFreezeGraph(self): def testFreezeGraph(self):
in_tensor = array_ops.placeholder( with ops.Graph().as_default():
shape=[1, 16, 16, 3], dtype=dtypes.float32) in_tensor = array_ops.placeholder(
var = variable_scope.get_variable( shape=[1, 16, 16, 3], dtype=dtypes.float32)
'weights', shape=[1, 16, 16, 3], dtype=dtypes.float32) var = variable_scope.get_variable(
_ = in_tensor + var 'weights', shape=[1, 16, 16, 3], dtype=dtypes.float32)
sess = session.Session() _ = in_tensor + var
sess = session.Session()
# Write graph to file. # Write graph to file.
graph_def_file = os.path.join(self.get_temp_dir(), 'model.pb') graph_def_file = os.path.join(self.get_temp_dir(), 'model.pb')
@ -1110,10 +1140,11 @@ class FromFrozenGraphFile(test_util.TensorFlowTestCase):
str(error.exception)) str(error.exception))
def testPbtxt(self): def testPbtxt(self):
in_tensor = array_ops.placeholder( with ops.Graph().as_default():
shape=[1, 16, 16, 3], dtype=dtypes.float32) in_tensor = array_ops.placeholder(
_ = in_tensor + in_tensor shape=[1, 16, 16, 3], dtype=dtypes.float32)
sess = session.Session() _ = in_tensor + in_tensor
sess = session.Session()
# Write graph to file. # Write graph to file.
graph_def_file = os.path.join(self.get_temp_dir(), 'model.pbtxt') graph_def_file = os.path.join(self.get_temp_dir(), 'model.pbtxt')
@ -1166,10 +1197,11 @@ class FromFrozenGraphFile(test_util.TensorFlowTestCase):
str(error.exception)) str(error.exception))
def testFloatTocoConverter(self): def testFloatTocoConverter(self):
in_tensor = array_ops.placeholder( with ops.Graph().as_default():
shape=[1, 16, 16, 3], dtype=dtypes.float32) in_tensor = array_ops.placeholder(
_ = in_tensor + in_tensor shape=[1, 16, 16, 3], dtype=dtypes.float32)
sess = session.Session() _ = in_tensor + in_tensor
sess = session.Session()
# Write graph to file. # Write graph to file.
graph_def_file = os.path.join(self.get_temp_dir(), 'model.pb') graph_def_file = os.path.join(self.get_temp_dir(), 'model.pb')
@ -1188,10 +1220,11 @@ class FromFrozenGraphFile(test_util.TensorFlowTestCase):
def testGraphDebugInfo(self): def testGraphDebugInfo(self):
"""Test a frozen graph doesn't have debug info captured.""" """Test a frozen graph doesn't have debug info captured."""
in_tensor = array_ops.placeholder( with ops.Graph().as_default():
shape=[1, 16, 16, 3], dtype=dtypes.float32) in_tensor = array_ops.placeholder(
_ = in_tensor + in_tensor shape=[1, 16, 16, 3], dtype=dtypes.float32)
sess = session.Session() _ = in_tensor + in_tensor
sess = session.Session()
# Write graph to file. # Write graph to file.
graph_def_file = os.path.join(self.get_temp_dir(), 'model.pb') graph_def_file = os.path.join(self.get_temp_dir(), 'model.pb')
@ -1296,21 +1329,21 @@ class FromFrozenGraphObjectDetection(test_util.TensorFlowTestCase):
str(error.exception)) str(error.exception))
@test_util.run_v1_only('Incompatible with 2.0.')
class FromSavedModelTest(TestModels): class FromSavedModelTest(TestModels):
def _createSavedModel(self, shape): def _createSavedModel(self, shape):
"""Create a simple SavedModel.""" """Create a simple SavedModel."""
saved_model_dir = os.path.join(self.get_temp_dir(), 'simple_savedmodel') saved_model_dir = os.path.join(self.get_temp_dir(), 'simple_savedmodel')
with session.Session() as sess: with ops.Graph().as_default():
in_tensor_1 = array_ops.placeholder( with session.Session() as sess:
shape=shape, dtype=dtypes.float32, name='inputB') in_tensor_1 = array_ops.placeholder(
in_tensor_2 = array_ops.placeholder( shape=shape, dtype=dtypes.float32, name='inputB')
shape=shape, dtype=dtypes.float32, name='inputA') in_tensor_2 = array_ops.placeholder(
out_tensor = in_tensor_1 + in_tensor_2 shape=shape, dtype=dtypes.float32, name='inputA')
inputs = {'x': in_tensor_1, 'y': in_tensor_2} out_tensor = in_tensor_1 + in_tensor_2
outputs = {'z': out_tensor} inputs = {'x': in_tensor_1, 'y': in_tensor_2}
saved_model.simple_save(sess, saved_model_dir, inputs, outputs) outputs = {'z': out_tensor}
saved_model.simple_save(sess, saved_model_dir, inputs, outputs)
return saved_model_dir return saved_model_dir
def testSimpleModel(self): def testSimpleModel(self):
@ -1465,7 +1498,6 @@ class MyAddLayer(keras.layers.Layer):
return config return config
@test_util.run_v1_only('Incompatible with 2.0.')
class FromKerasFile(TestModels, parameterized.TestCase): class FromKerasFile(TestModels, parameterized.TestCase):
def setUp(self): def setUp(self):
@ -1578,6 +1610,7 @@ class FromKerasFile(TestModels, parameterized.TestCase):
def testSequentialModelInputArray(self): def testSequentialModelInputArray(self):
"""Test a Sequential tf.keras model testing input arrays argument.""" """Test a Sequential tf.keras model testing input arrays argument."""
ops.disable_eager_execution()
self._getSequentialModel() self._getSequentialModel()
# Invalid input array raises error. # Invalid input array raises error.
@ -1622,6 +1655,7 @@ class FromKerasFile(TestModels, parameterized.TestCase):
def testSequentialModelOutputArray(self): def testSequentialModelOutputArray(self):
"""Test a Sequential tf.keras model testing output arrays argument.""" """Test a Sequential tf.keras model testing output arrays argument."""
ops.disable_eager_execution()
self._getSequentialModel() self._getSequentialModel()
# Invalid output array raises error. # Invalid output array raises error.
@ -1747,12 +1781,10 @@ class FromKerasFile(TestModels, parameterized.TestCase):
output_details = interpreter.get_output_details() output_details = interpreter.get_output_details()
self.assertLen(output_details, 2) self.assertLen(output_details, 2)
self.assertEqual('dense_1/BiasAdd', output_details[0]['name'])
self.assertEqual(np.float32, output_details[0]['dtype']) self.assertEqual(np.float32, output_details[0]['dtype'])
self.assertTrue(([1, 4] == output_details[0]['shape']).all()) self.assertTrue(([1, 4] == output_details[0]['shape']).all())
self.assertEqual((0., 0.), output_details[0]['quantization']) self.assertEqual((0., 0.), output_details[0]['quantization'])
self.assertEqual('dropout/Identity', output_details[1]['name'])
self.assertEqual(np.float32, output_details[1]['dtype']) self.assertEqual(np.float32, output_details[1]['dtype'])
self.assertTrue(([1, 4] == output_details[1]['shape']).all()) self.assertTrue(([1, 4] == output_details[1]['shape']).all())
self.assertEqual((0., 0.), output_details[1]['quantization']) self.assertEqual((0., 0.), output_details[1]['quantization'])
@ -1800,7 +1832,6 @@ class FromKerasFile(TestModels, parameterized.TestCase):
output_details = interpreter.get_output_details() output_details = interpreter.get_output_details()
self.assertLen(output_details, 1) self.assertLen(output_details, 1)
self.assertEqual('time_distributed/Reshape_1', output_details[0]['name'])
self.assertEqual(np.float32, output_details[0]['dtype']) self.assertEqual(np.float32, output_details[0]['dtype'])
self.assertTrue(([1, 3, 3] == output_details[0]['shape']).all()) self.assertTrue(([1, 3, 3] == output_details[0]['shape']).all())
self.assertEqual((0., 0.), output_details[0]['quantization']) self.assertEqual((0., 0.), output_details[0]['quantization'])
@ -1839,17 +1870,18 @@ class FromKerasFile(TestModels, parameterized.TestCase):
self.assertValidDebugInfo(converter._debug_info) self.assertValidDebugInfo(converter._debug_info)
@test_util.run_v1_only('Incompatible with 2.0.')
class GrapplerTest(TestModels): class GrapplerTest(TestModels):
def testConstantFolding(self): def testConstantFolding(self):
ops.disable_eager_execution()
# Constant folding handles the tf.broadcast_to operation which was not # Constant folding handles the tf.broadcast_to operation which was not
# supported by the TFLite at the time this test was added. # supported by the TFLite at the time this test was added.
in_tensor = array_ops.placeholder(shape=[3, 3], dtype=dtypes.float32) with ops.Graph().as_default():
y_const = constant_op.constant([1., 2., 3.]) in_tensor = array_ops.placeholder(shape=[3, 3], dtype=dtypes.float32)
y_broadcast = gen_array_ops.broadcast_to(y_const, [3, 3]) y_const = constant_op.constant([1., 2., 3.])
out_tensor = math_ops.matmul(in_tensor, y_broadcast, name='output') y_broadcast = gen_array_ops.broadcast_to(y_const, [3, 3])
sess = session.Session() out_tensor = math_ops.matmul(in_tensor, y_broadcast, name='output')
sess = session.Session()
# Convert model. # Convert model.
converter = lite.TFLiteConverter.from_session(sess, [in_tensor], converter = lite.TFLiteConverter.from_session(sess, [in_tensor],