首页 > 解决方案 > 我可以获取 tensorflow lite 模型的指标吗?

问题描述

我一直在研究一个带有自定义指标的复杂 Keras 模型,最近我将它转换为 tensorflow lite。模型不完全相同,输出也不同,但是很难评估,因为输出是大小为 128 的张量。有什么方法可以在这个模型上运行我的自定义指标?我一直在使用 Tf 1.14。下面是一些相关的代码。

# compiler and train the model
model.save('model.h5')

# save the model in TFLite
converter = tf.lite.TFLiteConverter.from_keras_model_file('model.h5', custom_objects={'custom_metric': custom_metric})
tflite_model = converter.convert()
open('model.tflite', 'wb').write(tflite_model)

# run the model
interpreter = tf.lite.Interpreter(model_path='model.tflite')
interpreter.allocate_tensors()
input_dets = interpreter.get_input_details()
output_dets = interpreter.get_output_details()
input_shape = input_dets[0]['shape']
input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32)
interpreter.set_tensor(input_dets[0]['index'], input_data)
interpreter.invoke()

标签: pythontensorflowkerastensorflow-lite

解决方案


这些模型应该是不同的,因为转换器会进行图形转换(例如熔断器激活和折叠批量规范),并且生成的图形仅针对推理场景。

运行指标:解释器提供了一个 API 来获取输出值(作为一个数组):

output = interpreter.tensor(interpreter.get_output_details()[0]["index"])

然后将指标应用于输出。


推荐阅读