From 219e8d8d6fcfcddc302cbdc3809b300179fd747e Mon Sep 17 00:00:00 2001 From: Nupur Garg Date: Thu, 1 Aug 2019 10:39:04 -0700 Subject: [PATCH] Remove run_v1_only from lite_test Python tests. PiperOrigin-RevId: 261150274 --- tensorflow/lite/python/lite_test.py | 402 +++++++++++++++------------- 1 file changed, 217 insertions(+), 185 deletions(-) diff --git a/tensorflow/lite/python/lite_test.py b/tensorflow/lite/python/lite_test.py index ae68022751c..d206083d7db 100644 --- a/tensorflow/lite/python/lite_test.py +++ b/tensorflow/lite/python/lite_test.py @@ -101,14 +101,14 @@ class FromConstructor(TestModels): self.assertTrue(converter._has_valid_tensors()) -@test_util.run_v1_only('Incompatible with 2.0.') class FromSessionTest(TestModels, parameterized.TestCase): def testFloat(self): - in_tensor = array_ops.placeholder( - shape=[1, 16, 16, 3], dtype=dtypes.float32) - out_tensor = in_tensor + in_tensor - sess = session.Session() + with ops.Graph().as_default(): + in_tensor = array_ops.placeholder( + shape=[1, 16, 16, 3], dtype=dtypes.float32) + out_tensor = in_tensor + in_tensor + sess = session.Session() # Convert model and ensure model is not None. 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']) def testString(self): - in_tensor = array_ops.placeholder(shape=[4], dtype=dtypes.string) - out_tensor = array_ops.reshape(in_tensor, shape=[2, 2]) - sess = session.Session() + with ops.Graph().as_default(): + in_tensor = array_ops.placeholder(shape=[4], dtype=dtypes.string) + out_tensor = array_ops.reshape(in_tensor, shape=[2, 2]) + sess = session.Session() # Convert model and ensure model is not None. converter = lite.TFLiteConverter.from_session(sess, [in_tensor], @@ -164,13 +165,14 @@ class FromSessionTest(TestModels, parameterized.TestCase): # interpreter API after support has been added. def testQuantization(self): - in_tensor_1 = array_ops.placeholder( - shape=[1, 16, 16, 3], dtype=dtypes.float32, name='inputA') - in_tensor_2 = array_ops.placeholder( - shape=[1, 16, 16, 3], dtype=dtypes.float32, name='inputB') - out_tensor = array_ops.fake_quant_with_min_max_args( - in_tensor_1 + in_tensor_2, min=0., max=1., name='output') - sess = session.Session() + with ops.Graph().as_default(): + in_tensor_1 = array_ops.placeholder( + shape=[1, 16, 16, 3], dtype=dtypes.float32, name='inputA') + in_tensor_2 = array_ops.placeholder( + shape=[1, 16, 16, 3], dtype=dtypes.float32, name='inputB') + out_tensor = array_ops.fake_quant_with_min_max_args( + in_tensor_1 + in_tensor_2, min=0., max=1., name='output') + sess = session.Session() # Convert model and ensure model is not None. converter = lite.TFLiteConverter.from_session(sess, @@ -210,13 +212,14 @@ class FromSessionTest(TestModels, parameterized.TestCase): self.assertTrue(output_details[0]['quantization'][0] > 0) # scale def testQuantizationInvalid(self): - in_tensor_1 = array_ops.placeholder( - shape=[1, 16, 16, 3], dtype=dtypes.float32, name='inputA') - in_tensor_2 = array_ops.placeholder( - shape=[1, 16, 16, 3], dtype=dtypes.float32, name='inputB') - out_tensor = array_ops.fake_quant_with_min_max_args( - in_tensor_1 + in_tensor_2, min=0., max=1., name='output') - sess = session.Session() + with ops.Graph().as_default(): + in_tensor_1 = array_ops.placeholder( + shape=[1, 16, 16, 3], dtype=dtypes.float32, name='inputA') + in_tensor_2 = array_ops.placeholder( + shape=[1, 16, 16, 3], dtype=dtypes.float32, name='inputB') + out_tensor = array_ops.fake_quant_with_min_max_args( + in_tensor_1 + in_tensor_2, min=0., max=1., name='output') + sess = session.Session() # Convert model and ensure model is not None. converter = lite.TFLiteConverter.from_session(sess, @@ -232,11 +235,12 @@ class FromSessionTest(TestModels, parameterized.TestCase): def testIntermediateInputArray(self): """Convert a model from an intermediate input array.""" - in_tensor_init = array_ops.placeholder( - shape=[1, 16, 16, 3], dtype=dtypes.float32) - in_tensor_final = in_tensor_init + in_tensor_init - out_tensor = in_tensor_final + in_tensor_final - sess = session.Session() + with ops.Graph().as_default(): + in_tensor_init = array_ops.placeholder( + shape=[1, 16, 16, 3], dtype=dtypes.float32) + in_tensor_final = in_tensor_init + in_tensor_init + out_tensor = in_tensor_final + in_tensor_final + sess = session.Session() # Convert model and ensure model is not None. 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']) def testSizeNoneInvalid(self): - in_tensor = array_ops.placeholder(dtype=dtypes.float32) - out_tensor = in_tensor + in_tensor - sess = session.Session() + with ops.Graph().as_default(): + in_tensor = array_ops.placeholder(dtype=dtypes.float32) + out_tensor = in_tensor + in_tensor + sess = session.Session() # Test None as shape. converter = lite.TFLiteConverter.from_session(sess, [in_tensor], @@ -277,9 +282,10 @@ class FromSessionTest(TestModels, parameterized.TestCase): def testScalarValid(self): # Construct a graph using a scalar (empty shape) input. - in_tensor = array_ops.placeholder(dtype=dtypes.float32, shape=[]) - out_tensor = in_tensor + in_tensor - sess = session.Session() + with ops.Graph().as_default(): + in_tensor = array_ops.placeholder(dtype=dtypes.float32, shape=[]) + out_tensor = in_tensor + in_tensor + sess = session.Session() # Test conversion with the scalar input shape. converter = lite.TFLiteConverter.from_session(sess, [in_tensor], @@ -313,10 +319,11 @@ class FromSessionTest(TestModels, parameterized.TestCase): self.assertTrue((expected_output == output_data).all()) def testSizeInvalid(self): - in_tensor = array_ops.placeholder( - shape=[1, None, 16, 3], dtype=dtypes.float32) - out_tensor = in_tensor + in_tensor - sess = session.Session() + with ops.Graph().as_default(): + in_tensor = array_ops.placeholder( + shape=[1, None, 16, 3], dtype=dtypes.float32) + out_tensor = in_tensor + in_tensor + sess = session.Session() # Test invalid shape. None after 1st dimension. converter = lite.TFLiteConverter.from_session(sess, [in_tensor], @@ -329,10 +336,11 @@ class FromSessionTest(TestModels, parameterized.TestCase): str(error.exception)) def testBatchSizeValid(self): - in_tensor = array_ops.placeholder( - shape=[None, 16, 16, 3], dtype=dtypes.float32) - out_tensor = in_tensor + in_tensor - sess = session.Session() + with ops.Graph().as_default(): + in_tensor = array_ops.placeholder( + shape=[None, 16, 16, 3], dtype=dtypes.float32) + out_tensor = in_tensor + in_tensor + sess = session.Session() # Convert model and ensure model is not None. 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']) def testFreezeGraph(self): - in_tensor = array_ops.placeholder( - shape=[1, 16, 16, 3], dtype=dtypes.float32) - var = variable_scope.get_variable( - 'weights', shape=[1, 16, 16, 3], dtype=dtypes.float32) - out_tensor = in_tensor + var - sess = session.Session() - sess.run(_global_variables_initializer()) + with ops.Graph().as_default(): + in_tensor = array_ops.placeholder( + shape=[1, 16, 16, 3], dtype=dtypes.float32) + var = variable_scope.get_variable( + 'weights', shape=[1, 16, 16, 3], dtype=dtypes.float32) + out_tensor = in_tensor + var + sess = session.Session() + sess.run(_global_variables_initializer()) # Convert model and ensure model is not None. 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.assertEqual((0., 0.), output_details[0]['quantization']) - # TODO(nupurgarg): Verify value of contents in GraphViz. def testGraphviz(self): - in_tensor = array_ops.placeholder( - shape=[1, 16, 16, 3], dtype=dtypes.float32) - out_tensor = in_tensor + in_tensor - sess = session.Session() + with ops.Graph().as_default(): + in_tensor = array_ops.placeholder( + shape=[1, 16, 16, 3], dtype=dtypes.float32) + out_tensor = in_tensor + in_tensor + sess = session.Session() # Convert model and ensure model is not None. converter = lite.TFLiteConverter.from_session(sess, [in_tensor], @@ -405,12 +414,12 @@ class FromSessionTest(TestModels, parameterized.TestCase): graphviz_output = converter.convert() self.assertTrue(graphviz_output) - # TODO(nupurgarg): Verify value of contents in GraphViz. def testDumpGraphviz(self): - in_tensor = array_ops.placeholder( - shape=[1, 16, 16, 3], dtype=dtypes.float32) - out_tensor = in_tensor + in_tensor - sess = session.Session() + with ops.Graph().as_default(): + in_tensor = array_ops.placeholder( + shape=[1, 16, 16, 3], dtype=dtypes.float32) + out_tensor = in_tensor + in_tensor + sess = session.Session() # Convert model and ensure model is not None. 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) def testInferenceInputType(self): - in_tensor = array_ops.placeholder( - shape=[1, 16, 16, 3], dtype=dtypes.float32) - out_tensor = in_tensor + in_tensor - sess = session.Session() + with ops.Graph().as_default(): + in_tensor = array_ops.placeholder( + shape=[1, 16, 16, 3], dtype=dtypes.float32) + out_tensor = in_tensor + in_tensor + sess = session.Session() # Convert model and ensure model is not None. 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()) def testDefaultRangesStats(self): - in_tensor = array_ops.placeholder( - shape=[1, 16, 16, 3], dtype=dtypes.float32) - out_tensor = in_tensor + in_tensor - sess = session.Session() + with ops.Graph().as_default(): + in_tensor = array_ops.placeholder( + shape=[1, 16, 16, 3], dtype=dtypes.float32) + out_tensor = in_tensor + in_tensor + sess = session.Session() # Convert model and ensure model is not None. 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 def testPostTrainingQuantizeDeprecatedAttribute(self): - in_tensor_1 = array_ops.placeholder( - shape=[33, 33], dtype=dtypes.float32, name='inputA') - in_tensor_2 = constant_op.constant( - np.random.uniform(low=-10., high=10., size=(33, 33)), - shape=[33, 33], - dtype=dtypes.float32, - name='inputB') - out_tensor = math_ops.matmul(in_tensor_1, in_tensor_2, name='output') - sess = session.Session() + with ops.Graph().as_default(): + in_tensor_1 = array_ops.placeholder( + shape=[33, 33], dtype=dtypes.float32, name='inputA') + in_tensor_2 = constant_op.constant( + np.random.uniform(low=-10., high=10., size=(33, 33)), + shape=[33, 33], + dtype=dtypes.float32, + name='inputB') + out_tensor = math_ops.matmul(in_tensor_1, in_tensor_2, name='output') + sess = session.Session() quantized_converter = lite.TFLiteConverter.from_session( sess, [in_tensor_1], [out_tensor]) @@ -528,17 +540,18 @@ class FromSessionTest(TestModels, parameterized.TestCase): def testPostTrainingQuantize(self): np.random.seed(0) - # We need the tensor to have more than 1024 elements for quantize_weights - # to kick in. Thus, the [33, 33] shape. - in_tensor_1 = array_ops.placeholder( - shape=[33, 33], dtype=dtypes.float32, name='inputA') - in_tensor_2 = constant_op.constant( - np.random.uniform(low=-10., high=10., size=(33, 33)), - shape=[33, 33], - dtype=dtypes.float32, - name='inputB') - out_tensor = math_ops.matmul(in_tensor_1, in_tensor_2, name='output') - sess = session.Session() + with ops.Graph().as_default(): + # We need the tensor to have more than 1024 elements for quantize_weights + # to kick in. Thus, the [33, 33] shape. + in_tensor_1 = array_ops.placeholder( + shape=[33, 33], dtype=dtypes.float32, name='inputA') + in_tensor_2 = constant_op.constant( + np.random.uniform(low=-10., high=10., size=(33, 33)), + shape=[33, 33], + dtype=dtypes.float32, + name='inputB') + out_tensor = math_ops.matmul(in_tensor_1, in_tensor_2, name='output') + sess = session.Session() # Convert float model. float_converter = lite.TFLiteConverter.from_session(sess, [in_tensor_1], @@ -574,8 +587,9 @@ class FromSessionTest(TestModels, parameterized.TestCase): return (inp, output, calibration_gen) def testPostTrainingCalibrateAndQuantize(self): - inp, output, calibration_gen = self._getCalibrationQuantizeModel() - sess = session.Session() + with ops.Graph().as_default(): + inp, output, calibration_gen = self._getCalibrationQuantizeModel() + sess = session.Session() # Convert float model. 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)) def testCalibrateAndQuantizeBuiltinInt8(self): - inp, output, calibration_gen = self._getCalibrationQuantizeModel() - sess = session.Session() + with ops.Graph().as_default(): + inp, output, calibration_gen = self._getCalibrationQuantizeModel() + sess = session.Session() # Convert float model. 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, is_float16_quantized, is_error, is_post_training_quantized): - inp, output, calibration_gen = self._getCalibrationQuantizeModel() - sess = session.Session() + with ops.Graph().as_default(): + inp, output, calibration_gen = self._getCalibrationQuantizeModel() + sess = session.Session() # Convert float model. float_converter = lite.TFLiteConverter.from_session(sess, [inp], [output]) @@ -698,8 +714,9 @@ class FromSessionTest(TestModels, parameterized.TestCase): raise ValueError('Invalid test options.') def testInvalidQuantizeFloat16(self): - inp, output, _ = self._getCalibrationQuantizeModel() - sess = session.Session() + with ops.Graph().as_default(): + inp, output, _ = self._getCalibrationQuantizeModel() + sess = session.Session() # Specify float16 quantization quantized_converter = lite.TFLiteConverter.from_session( @@ -718,17 +735,18 @@ class FromSessionTest(TestModels, parameterized.TestCase): def testInvalidPostTrainingQuantize(self): np.random.seed(0) - # We need the tensor to have more than 1024 elements for quantize_weights - # to kick in. Thus, the [33, 33] shape. - in_tensor_1 = array_ops.placeholder( - shape=[33, 33], dtype=dtypes.float32, name='inputA') - in_tensor_2 = constant_op.constant( - np.random.uniform(low=-10., high=10., size=(33, 33)), - shape=[33, 33], - dtype=dtypes.float32, - name='inputB') - out_tensor = math_ops.matmul(in_tensor_1, in_tensor_2, name='output') - sess = session.Session() + with ops.Graph().as_default(): + # We need the tensor to have more than 1024 elements for quantize_weights + # to kick in. Thus, the [33, 33] shape. + in_tensor_1 = array_ops.placeholder( + shape=[33, 33], dtype=dtypes.float32, name='inputA') + in_tensor_2 = constant_op.constant( + np.random.uniform(low=-10., high=10., size=(33, 33)), + shape=[33, 33], + dtype=dtypes.float32, + name='inputB') + out_tensor = math_ops.matmul(in_tensor_1, in_tensor_2, name='output') + sess = session.Session() # Attempt to convert to quantized weights model. 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)) def testPostTrainingCalibrateAndQuantizeFloatNotAllowed(self): - inp, output, calibration_gen = self._getCalibrationQuantizeModel() - sess = session.Session() + with ops.Graph().as_default(): + inp, output, calibration_gen = self._getCalibrationQuantizeModel() + sess = session.Session() # Convert float model. 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)) def testPostTrainingCalibrateAndQuantizeInt8Inputs(self): - inp, output, calibration_gen = self._getCalibrationQuantizeModel() - sess = session.Session() + with ops.Graph().as_default(): + inp, output, calibration_gen = self._getCalibrationQuantizeModel() + sess = session.Session() # Convert float model. float_converter = lite.TFLiteConverter.from_session(sess, [inp], [output]) @@ -801,10 +821,11 @@ class FromSessionTest(TestModels, parameterized.TestCase): def testFloatTocoConverter(self): """Tests deprecated test TocoConverter.""" - in_tensor = array_ops.placeholder( - shape=[1, 16, 16, 3], dtype=dtypes.float32) - out_tensor = in_tensor + in_tensor - sess = session.Session() + with ops.Graph().as_default(): + in_tensor = array_ops.placeholder( + shape=[1, 16, 16, 3], dtype=dtypes.float32) + out_tensor = in_tensor + in_tensor + sess = session.Session() # Convert model and ensure model is not None. converter = lite.TocoConverter.from_session(sess, [in_tensor], [out_tensor]) @@ -817,9 +838,11 @@ class FromSessionTest(TestModels, parameterized.TestCase): def testMultipleOutputNodeNames(self): """Tests converting a graph with an op that have multiple outputs.""" - input_tensor = array_ops.placeholder(shape=[4], dtype=dtypes.float32) - out0, out1, out2, out3 = array_ops.split(input_tensor, [1, 1, 1, 1], axis=0) - sess = session.Session() + with ops.Graph().as_default(): + input_tensor = array_ops.placeholder(shape=[4], dtype=dtypes.float32) + 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. 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']) def testInferenceInputOutputTypeFloatDefault(self): - in_tensor = array_ops.placeholder( - shape=[1, 16, 16, 3], dtype=dtypes.float32) - out_tensor = in_tensor + in_tensor - sess = session.Session() + with ops.Graph().as_default(): + in_tensor = array_ops.placeholder( + shape=[1, 16, 16, 3], dtype=dtypes.float32) + out_tensor = in_tensor + in_tensor + sess = session.Session() # Convert model and ensure model is not None. 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()) def testInferenceInputOutputTypeQuantizedUint8Default(self): - in_tensor = array_ops.placeholder( - shape=[1, 16, 16, 3], dtype=dtypes.float32) - out_tensor = array_ops.fake_quant_with_min_max_args( - in_tensor + in_tensor, min=0., max=1., name='output') - sess = session.Session() + with ops.Graph().as_default(): + in_tensor = array_ops.placeholder( + shape=[1, 16, 16, 3], dtype=dtypes.float32) + out_tensor = array_ops.fake_quant_with_min_max_args( + in_tensor + in_tensor, min=0., max=1., name='output') + sess = session.Session() # Convert model and ensure model is not None. 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()) def testReusingConverterWithDifferentPostTrainingQuantization(self): - in_tensor = array_ops.placeholder( - shape=[1, 16, 16, 3], dtype=dtypes.float32) - out_tensor = array_ops.fake_quant_with_min_max_args( - in_tensor + in_tensor, min=0., max=1., name='output') - sess = session.Session() + with ops.Graph().as_default(): + in_tensor = array_ops.placeholder( + shape=[1, 16, 16, 3], dtype=dtypes.float32) + out_tensor = array_ops.fake_quant_with_min_max_args( + in_tensor + in_tensor, min=0., max=1., name='output') + sess = session.Session() # Convert model and ensure model is not None. 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 # tensors changes between invocations. # See also https://github.com/tensorflow/tensorflow/issues/26549 - input_tensor = array_ops.placeholder(shape=[1, 1], dtype=dtypes.float32) - input2_tensor = array_ops.placeholder(shape=[1], dtype=dtypes.float32) + with ops.Graph().as_default(): + 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 - # some other ops around it. - neg = math_ops.negative(input2_tensor) - padding = array_ops.placeholder(shape=[2, 2], dtype=dtypes.int32) - output_tensor = array_ops.pad(input_tensor, padding) + neg + # The bug is triggered only when dynamic tensor is intermediate. Putting + # some other ops around it. + neg = math_ops.negative(input2_tensor) + padding = array_ops.placeholder(shape=[2, 2], dtype=dtypes.int32) + output_tensor = array_ops.pad(input_tensor, padding) + neg + + sess = session.Session() - sess = session.Session() converter = lite.TFLiteConverter.from_session( sess, [input_tensor, padding, input2_tensor], [output_tensor]) tflite_model = converter.convert() @@ -1025,14 +1053,14 @@ class FromSessionTest(TestModels, parameterized.TestCase): self.assertIn((func + 'add'), converter._debug_info.traces) -@test_util.run_v1_only('Incompatible with 2.0.') class FromFrozenGraphFile(test_util.TensorFlowTestCase): def testFloat(self): - in_tensor = array_ops.placeholder( - shape=[1, 16, 16, 3], dtype=dtypes.float32) - _ = in_tensor + in_tensor - sess = session.Session() + with ops.Graph().as_default(): + in_tensor = array_ops.placeholder( + shape=[1, 16, 16, 3], dtype=dtypes.float32) + _ = in_tensor + in_tensor + sess = session.Session() # Write graph to file. 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']) def testFloatWithShapesArray(self): - in_tensor = array_ops.placeholder( - shape=[1, 16, 16, 3], dtype=dtypes.float32) - _ = in_tensor + in_tensor - sess = session.Session() + with ops.Graph().as_default(): + in_tensor = array_ops.placeholder( + shape=[1, 16, 16, 3], dtype=dtypes.float32) + _ = in_tensor + in_tensor + sess = session.Session() # Write graph to file. 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()) def testFreezeGraph(self): - in_tensor = array_ops.placeholder( - shape=[1, 16, 16, 3], dtype=dtypes.float32) - var = variable_scope.get_variable( - 'weights', shape=[1, 16, 16, 3], dtype=dtypes.float32) - _ = in_tensor + var - sess = session.Session() + with ops.Graph().as_default(): + in_tensor = array_ops.placeholder( + shape=[1, 16, 16, 3], dtype=dtypes.float32) + var = variable_scope.get_variable( + 'weights', shape=[1, 16, 16, 3], dtype=dtypes.float32) + _ = in_tensor + var + sess = session.Session() # Write graph to file. graph_def_file = os.path.join(self.get_temp_dir(), 'model.pb') @@ -1110,10 +1140,11 @@ class FromFrozenGraphFile(test_util.TensorFlowTestCase): str(error.exception)) def testPbtxt(self): - in_tensor = array_ops.placeholder( - shape=[1, 16, 16, 3], dtype=dtypes.float32) - _ = in_tensor + in_tensor - sess = session.Session() + with ops.Graph().as_default(): + in_tensor = array_ops.placeholder( + shape=[1, 16, 16, 3], dtype=dtypes.float32) + _ = in_tensor + in_tensor + sess = session.Session() # Write graph to file. graph_def_file = os.path.join(self.get_temp_dir(), 'model.pbtxt') @@ -1166,10 +1197,11 @@ class FromFrozenGraphFile(test_util.TensorFlowTestCase): str(error.exception)) def testFloatTocoConverter(self): - in_tensor = array_ops.placeholder( - shape=[1, 16, 16, 3], dtype=dtypes.float32) - _ = in_tensor + in_tensor - sess = session.Session() + with ops.Graph().as_default(): + in_tensor = array_ops.placeholder( + shape=[1, 16, 16, 3], dtype=dtypes.float32) + _ = in_tensor + in_tensor + sess = session.Session() # Write graph to file. graph_def_file = os.path.join(self.get_temp_dir(), 'model.pb') @@ -1188,10 +1220,11 @@ class FromFrozenGraphFile(test_util.TensorFlowTestCase): def testGraphDebugInfo(self): """Test a frozen graph doesn't have debug info captured.""" - in_tensor = array_ops.placeholder( - shape=[1, 16, 16, 3], dtype=dtypes.float32) - _ = in_tensor + in_tensor - sess = session.Session() + with ops.Graph().as_default(): + in_tensor = array_ops.placeholder( + shape=[1, 16, 16, 3], dtype=dtypes.float32) + _ = in_tensor + in_tensor + sess = session.Session() # Write graph to file. graph_def_file = os.path.join(self.get_temp_dir(), 'model.pb') @@ -1296,21 +1329,21 @@ class FromFrozenGraphObjectDetection(test_util.TensorFlowTestCase): str(error.exception)) -@test_util.run_v1_only('Incompatible with 2.0.') class FromSavedModelTest(TestModels): def _createSavedModel(self, shape): """Create a simple SavedModel.""" saved_model_dir = os.path.join(self.get_temp_dir(), 'simple_savedmodel') - with session.Session() as sess: - in_tensor_1 = array_ops.placeholder( - shape=shape, dtype=dtypes.float32, name='inputB') - in_tensor_2 = array_ops.placeholder( - shape=shape, dtype=dtypes.float32, name='inputA') - out_tensor = in_tensor_1 + in_tensor_2 - inputs = {'x': in_tensor_1, 'y': in_tensor_2} - outputs = {'z': out_tensor} - saved_model.simple_save(sess, saved_model_dir, inputs, outputs) + with ops.Graph().as_default(): + with session.Session() as sess: + in_tensor_1 = array_ops.placeholder( + shape=shape, dtype=dtypes.float32, name='inputB') + in_tensor_2 = array_ops.placeholder( + shape=shape, dtype=dtypes.float32, name='inputA') + out_tensor = in_tensor_1 + in_tensor_2 + inputs = {'x': in_tensor_1, 'y': in_tensor_2} + outputs = {'z': out_tensor} + saved_model.simple_save(sess, saved_model_dir, inputs, outputs) return saved_model_dir def testSimpleModel(self): @@ -1465,7 +1498,6 @@ class MyAddLayer(keras.layers.Layer): return config -@test_util.run_v1_only('Incompatible with 2.0.') class FromKerasFile(TestModels, parameterized.TestCase): def setUp(self): @@ -1578,6 +1610,7 @@ class FromKerasFile(TestModels, parameterized.TestCase): def testSequentialModelInputArray(self): """Test a Sequential tf.keras model testing input arrays argument.""" + ops.disable_eager_execution() self._getSequentialModel() # Invalid input array raises error. @@ -1622,6 +1655,7 @@ class FromKerasFile(TestModels, parameterized.TestCase): def testSequentialModelOutputArray(self): """Test a Sequential tf.keras model testing output arrays argument.""" + ops.disable_eager_execution() self._getSequentialModel() # Invalid output array raises error. @@ -1747,12 +1781,10 @@ class FromKerasFile(TestModels, parameterized.TestCase): output_details = interpreter.get_output_details() self.assertLen(output_details, 2) - self.assertEqual('dense_1/BiasAdd', output_details[0]['name']) self.assertEqual(np.float32, output_details[0]['dtype']) self.assertTrue(([1, 4] == output_details[0]['shape']).all()) 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.assertTrue(([1, 4] == output_details[1]['shape']).all()) self.assertEqual((0., 0.), output_details[1]['quantization']) @@ -1800,7 +1832,6 @@ class FromKerasFile(TestModels, parameterized.TestCase): output_details = interpreter.get_output_details() self.assertLen(output_details, 1) - self.assertEqual('time_distributed/Reshape_1', output_details[0]['name']) self.assertEqual(np.float32, output_details[0]['dtype']) self.assertTrue(([1, 3, 3] == output_details[0]['shape']).all()) self.assertEqual((0., 0.), output_details[0]['quantization']) @@ -1839,17 +1870,18 @@ class FromKerasFile(TestModels, parameterized.TestCase): self.assertValidDebugInfo(converter._debug_info) -@test_util.run_v1_only('Incompatible with 2.0.') class GrapplerTest(TestModels): def testConstantFolding(self): + ops.disable_eager_execution() # Constant folding handles the tf.broadcast_to operation which was not # supported by the TFLite at the time this test was added. - in_tensor = array_ops.placeholder(shape=[3, 3], dtype=dtypes.float32) - y_const = constant_op.constant([1., 2., 3.]) - y_broadcast = gen_array_ops.broadcast_to(y_const, [3, 3]) - out_tensor = math_ops.matmul(in_tensor, y_broadcast, name='output') - sess = session.Session() + with ops.Graph().as_default(): + in_tensor = array_ops.placeholder(shape=[3, 3], dtype=dtypes.float32) + y_const = constant_op.constant([1., 2., 3.]) + y_broadcast = gen_array_ops.broadcast_to(y_const, [3, 3]) + out_tensor = math_ops.matmul(in_tensor, y_broadcast, name='output') + sess = session.Session() # Convert model. converter = lite.TFLiteConverter.from_session(sess, [in_tensor],