我正在使用Azure ML Workbench
来执行二元分类。到目前为止,一切正常,我具有良好的准确性,我想将模型部署为 Web 服务以进行推理。
我真的不知道从哪里开始:azure 提供了这个文档,但该示例使用 sklearn
和 pickle
,而不是tensorflow
。
什至不确定我是否应该使用 tf.train.Saver()
或 tf.saved_model_builder()
保存和恢复模型.
如果有人有一个在 azure ml 工作台中使用香草张量流的好例子,那就太好了。
好的,所以对于任何想知道同样的事情的人,我找到了答案。我没有使用pickle
模型,而是按照这个将我的模型保存为protobuf
。然后,我编写 init((、run(( 和 load_graph(( 方法,如下所示:
def init():
global persistent_session, model, x, y, keep_prob, inputs_dc, prediction_dc
#load the model and connect the inputs / outputs
model = load_graph(os.path.join(os.environ['AZUREML_NATIVE_SHARE_DIRECTORY'], 'frozen_model.pb'))
x = model.get_tensor_by_name('prefix/Placeholder:0')
y = model.get_tensor_by_name('prefix/convNet/sample_prediction:0')
keep_prob = model.get_tensor_by_name('prefix/Placeholder_3:0')
persistent_session = tf.Session(graph=model)
# load the graph from protobuf file
def load_graph(frozen_graph_filename):
with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
with tf.Graph().as_default() as graph:
tf.import_graph_def(graph_def, name="prefix")
return graph
# run the inference
def run(input_array):
import json
global clcf2, inputs_dc, prediction_dc
try:
prediction = persistent_session.run(y, feed_dict={ x: input_array, keep_prob:1.0})
print("prediction : ", prediction)
inputs_dc.collect(input_array)
prediction_dc.collect(prediction.tolist())
return prediction
except Exception as e:
return (str(e))
return json.dumps(str(prediction.tolist()))
可能需要一些清洁,但它有效!