我想知道如何编辑/部署我的Keras模型(hdf5)文件,以便它们尽可能快地加载和运行(使用TensorFlow后端) .

目前,在训练之后,我唯一要做的就是将每个网络层设置为不可训练,这有点帮助 . 这是我在训练后如何保存模型的代码:

model = keras.models.load_model('some_path')
for x in model.layers:
    x.trainable = False

model.save('some_path')

但是在 生产环境 环境中还有更复杂的事情要做(可能将模型转换为某种二进制protobuf格式,使用其他一些库进行推理等等)

所以,我真的只想知道: when you have your model trained, what are the things to do to it to allow the fastest (a) load time and (b) inference speed, in both cpu and gpu mode

Motivation:

这个问题的动机来自一个项目,我在Web服务器中运行我的keras模型 . 我只在服务器启动时加载模型一次,然后我按照https://github.com/sugyan/tensorflow-mnist1中的示例,通过使用Flask和加载预训练模式显示了一个简单的restAPI示例 . 但是,如果您正在调试,则tensorflow在调试模式下的烧瓶项目中运行时出现问题,因此每次重新加载服务器时,我都必须重新加载模型,每个模型可能需要10-20秒(采用hdf5格式) .