【发布时间】: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