MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / testFunctionalModel

Method testFunctionalModel

tensorflow/lite/python/lite_test.py:1703–1757  ·  view source on GitHub ↗

Test a Functional tf.keras model with default inputs.

(self, test_context)

Source from the content-addressed store, hash-verified

1701 @parameterized.named_parameters(('_graph', context.graph_mode),
1702 ('_eager', context.eager_mode))
1703 def testFunctionalModel(self, test_context):
1704 """Test a Functional tf.keras model with default inputs."""
1705 with test_context():
1706 inputs = keras.layers.Input(shape=(3,), name='input')
1707 x = keras.layers.Dense(2)(inputs)
1708 output = keras.layers.Dense(3)(x)
1709
1710 model = keras.models.Model(inputs, output)
1711 model.compile(
1712 loss=keras.losses.MSE,
1713 optimizer='sgd',
1714 metrics=[keras.metrics.categorical_accuracy])
1715 x = np.random.random((1, 3))
1716 y = np.random.random((1, 3))
1717 model.train_on_batch(x, y)
1718
1719 model.predict(x)
1720 fd, self._keras_file = tempfile.mkstemp('.h5')
1721 try:
1722 keras.models.save_model(model, self._keras_file)
1723 finally:
1724 os.close(fd)
1725
1726 # Convert to TFLite model.
1727 converter = lite.TFLiteConverter.from_keras_model_file(self._keras_file)
1728 tflite_model = converter.convert()
1729 self.assertTrue(tflite_model)
1730
1731 # Check tensor details of converted model.
1732 interpreter = Interpreter(model_content=tflite_model)
1733 interpreter.allocate_tensors()
1734
1735 input_details = interpreter.get_input_details()
1736 self.assertLen(input_details, 1)
1737 self.assertEqual('input', input_details[0]['name'])
1738 self.assertEqual(np.float32, input_details[0]['dtype'])
1739 self.assertTrue(([1, 3] == input_details[0]['shape']).all())
1740 self.assertEqual((0., 0.), input_details[0]['quantization'])
1741
1742 output_details = interpreter.get_output_details()
1743 self.assertLen(output_details, 1)
1744 self.assertEqual(np.float32, output_details[0]['dtype'])
1745 self.assertTrue(([1, 3] == output_details[0]['shape']).all())
1746 self.assertEqual((0., 0.), output_details[0]['quantization'])
1747
1748 # Check inference of converted model.
1749 input_data = np.array([[1, 2, 3]], dtype=np.float32)
1750 interpreter.set_tensor(input_details[0]['index'], input_data)
1751 interpreter.invoke()
1752 tflite_result = interpreter.get_tensor(output_details[0]['index'])
1753
1754 keras_model = keras.models.load_model(self._keras_file)
1755 keras_result = keras_model.predict(input_data)
1756
1757 np.testing.assert_almost_equal(tflite_result, keras_result, 5)
1758
1759 def testFunctionalModelMultipleInputs(self):
1760 """Test a Functional tf.keras model with multiple inputs and outputs."""

Callers

nothing calls this directly

Calls 15

compileMethod · 0.95
train_on_batchMethod · 0.95
predictMethod · 0.95
allocate_tensorsMethod · 0.95
get_input_detailsMethod · 0.95
get_output_detailsMethod · 0.95
set_tensorMethod · 0.95
invokeMethod · 0.95
get_tensorMethod · 0.95
InterpreterClass · 0.90
closeMethod · 0.65
InputMethod · 0.45

Tested by

no test coverage detected