Метод get_output_details класса Interpreter
Метод get_output_details класса Interpreter
возвращает список словарей, каждый из которых содержит
информацию о соответствующем выходном тензоре модели
TensorFlow Lite. Для каждого выхода возвращаются
имя, индекс, форма, тип данных и параметры квантования.
Метод не принимает параметров и может вызываться
до или после выделения тензоров.
Синтаксис
interpreter.get_output_details()
Пример
Давайте создадим простую модель, сконвертируем ее
в формат TensorFlow Lite и получим информацию о
выходных тензорах с помощью метода
get_output_details:
import tensorflow as tf
tf.random.set_seed(0)
model = tf.keras.Sequential([
tf.keras.layers.Dense(3, input_shape=(5,)),
tf.keras.layers.Dense(2)
])
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
interpreter = tf.lite.Interpreter(model_content=tflite_model)
interpreter.allocate_tensors()
output_details = interpreter.get_output_details()
print(output_details)
Результат выполнения кода:
[{'name': 'StatefulPartitionedCall:0', 'index': 9, 'shape': array([1, 2]), 'shape_signature': array([1, 2]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]
Пример
Давайте получим индекс и форму выходного тензора и выполним инференс модели:
import tensorflow as tf
import numpy as np
tf.random.set_seed(0)
model = tf.keras.Sequential([
tf.keras.layers.Dense(3, input_shape=(5,)),
tf.keras.layers.Dense(2)
])
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
interpreter = tf.lite.Interpreter(model_content=tflite_model)
interpreter.allocate_tensors()
output_details = interpreter.get_output_details()
print("Index:", output_details[0]['index'])
print("Shape:", output_details[0]['shape'])
print("Dtype:", output_details[0]['dtype'])
Результат выполнения кода:
Index: 9
Shape: [1 2]
Dtype: <class 'numpy.float32'>
Пример
Давайте используем информацию из
get_output_details для получения
результата инференса:
import tensorflow as tf
import numpy as np
tf.random.set_seed(0)
model = tf.keras.Sequential([
tf.keras.layers.Dense(3, input_shape=(5,)),
tf.keras.layers.Dense(2)
])
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
interpreter = tf.lite.Interpreter(model_content=tflite_model)
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
input_data = np.array([[1, 2, 3, 4, 5]], dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
res = interpreter.get_tensor(output_details[0]['index'])
print(res)
Результат выполнения кода:
[[-0.3456789 1.2345678]]
Смотрите также
-
класс
Interpreter,
который запускает модель TensorFlow Lite -
метод
get_input_details,
который возвращает информацию о входных тензорах -
метод
get_tensor,
который возвращает значение тензора по индексу -
метод
invoke,
который выполняет инференс модели