【问题标题】:Can I get metrics on a tensorflow lite model?我可以获取 tensorflow lite 模型的指标吗?
【发布时间】:2019-11-19 21:18:55
【问题描述】:

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

【问题讨论】:

    标签: python tensorflow keras tensorflow-lite


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 这就是我最终不得不做的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多