现在,我们可以成功地使用 Tensorflow Serving 来提供模型服务。我们使用以下方法导出模型并使用 Tensorflow Serving 托管它。
------------
For exporting
------------------
from tensorflow.contrib.session_bundle import exporter
K.set_learning_phase(0)
export_path = ... # where to save the exported graph
export_version = ... # version number (integer)
saver = tf.train.Saver(sharded=True)
model_exporter = exporter.Exporter(saver)
signature = exporter.classification_signature(input_tensor=model.input,
scores_tensor=model.output)
model_exporter.init(sess.graph.as_graph_def(),
default_graph_signature=signature)
model_exporter.export(export_path, tf.constant(export_version), sess)
--------------------------------------
For hosting
-----------------------------------------------
bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server --port=9000 --model_name=default --model_base_path=/serving/models
但是我们的问题是 - 我们希望 keras 与 Tensorflow 服务集成。我们希望使用 Keras 通过 Tensorflow 服务来提供模型。 我们希望这样做的原因是 - 在我们的架构中,我们遵循几种不同的方法来训练我们的模型,例如 deeplearning4j + Keras , Tensorflow + Keras,但对于服务,我们只想使用一种可服务引擎,即 Tensorflow Serving。我们没有看到任何直接的方法来实现这一目标。有意见吗?
谢谢。
最佳答案
最近 TensorFlow 改变了导出模型的方式,因此网络上提供的大多数教程都已过时。老实说,我不知道 deeplearning4j 是如何工作的,但我经常使用 Keras。我设法创建了一个简单的示例,我已经在此 issue 上发布了该示例。在 TensorFlow Serving Github 中。
我不确定这是否对您有帮助,但我想分享一下我的做法,也许会给您一些见解。在创建自定义模型之前,我的第一次尝试是使用 Keras 上可用的经过训练的模型,例如 VGG19。我按如下方式执行此操作。
模型创建
import keras.backend as K
from keras.applications import VGG19
from keras.models import Model
# very important to do this as a first thing
K.set_learning_phase(0)
model = VGG19(include_top=True, weights='imagenet')
# The creation of a new model might be optional depending on the goal
config = model.get_config()
weights = model.get_weights()
new_model = Model.from_config(config)
new_model.set_weights(weights)
导出模型
from tensorflow.python.saved_model import builder as saved_model_builder
from tensorflow.python.saved_model import utils
from tensorflow.python.saved_model import tag_constants, signature_constants
from tensorflow.python.saved_model.signature_def_utils_impl import build_signature_def, predict_signature_def
from tensorflow.contrib.session_bundle import exporter
export_path = 'folder_to_export'
builder = saved_model_builder.SavedModelBuilder(export_path)
signature = predict_signature_def(inputs={'images': new_model.input},
outputs={'scores': new_model.output})
with K.get_session() as sess:
builder.add_meta_graph_and_variables(sess=sess,
tags=[tag_constants.SERVING],
signature_def_map={'predict': signature})
builder.save()
一些旁注
- 它可能会因 Keras、TensorFlow 和 TensorFlow Serving 的不同而有所不同 版本。我用的是最新的。
- 请注意签名的名称,因为它们也应该在客户端中使用。
- 创建客户端时,需要执行的所有预处理步骤 必须执行模型(例如 preprocess_input())。我没有尝试 在图中添加这样的步骤作为 Inception 客户端示例。
关于在同一服务器内提供不同的模型,我认为类似于创建 model_config_file 的东西可能会对您有所帮助。为此,您可以创建一个与此类似的配置文件:
model_config_list: {
config: {
name: "my_model_1",
base_path: "/tmp/model_1",
model_platform: "tensorflow"
},
config: {
name: "my_model_2",
base_path: "/tmp/model_2",
model_platform: "tensorflow"
}
}
最后,您可以像这样运行客户端:
bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server --port=9000 --config_file=model_config.conf
关于tensorflow - 使用 Tensorflow Serving 为 Keras 模型提供服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43649591/