最近,TensorFlow改变了导出模型的方式,因此Web上提供的大多数教程都已过时。老实说,我不知道deeplearning4j是如何运作的,但我经常使用Keras。我设法创建了一个简单的例子,我已经在TensorFlow服务Github的issue上发布了这个例子。
我不确定这是否会对你有所帮助,但我想分享一下我的做法,也许它会给你一些见解。我在创建自定义模型之前的第一个试验是使用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的不同而不同
版。我使用了最新的。
请注意签名的名称,因为它们也应该在客户端中使用。
创建客户端时,需要执行所有预处理步骤
必须执行model(例如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