我可以获得有关 tensorflow lite 模型的指标吗?

Can I get metrics on a tensorflow lite model?

我一直在研究具有自定义指标的复杂 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()

模型应该是不同的,因为转换器进行图形转换(例如融合激活和折叠批量规范)并且生成的图形仅用于推理场景。

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

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

然后将您的指标应用于输出。