通过 Google Cloud ML 部署 Keras 模型 [英] Deploying Keras Models via Google Cloud ML

查看:28
本文介绍了通过 Google Cloud ML 部署 Keras 模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用 Google Cloud ML 来托管我的 Keras 模型,以便我可以调用 API 并进行一些预测.我在 Keras 方面遇到了一些问题.

I am looking to use Google Cloud ML to host my Keras models so that I can call the API and make some predictions. I am running into some issues from the Keras side of things.

到目前为止,我已经能够使用 TensorFlow 构建模型并将其部署在 CloudML 上.为了让它起作用,我必须对我的基本 TF 代码进行一些更改.更改记录在此处:https://cloud.google.com/ml/docs/how-tos/preparing-models#code_changes

So far I have been able to build a model using TensorFlow and deploy it on CloudML. In order for this to work I had to make some changes to my basic TF code. The changes are documented here: https://cloud.google.com/ml/docs/how-tos/preparing-models#code_changes

我还能够使用 Keras 训练类似的模型.我什至可以使用与 TF 相同的导出和 export.meta 格式保存模型.

I have also been able to train a similar model using Keras. I can even save the model in the same export and export.meta format as I would get with TF.

from keras import backend as K

saver = tf.train.Saver()
session = K.get_session()
saver.save(session, 'export')

我缺少的部分是如何将输入和输出的占位符添加到我在 Keras 上构建的图表中?

The part I am missing is how do I add the placeholders for input and output into the graph I build on Keras?

推荐答案

在 Google Cloud ML Engine 上训练您的模型后(查看 这个很棒的教程 ),我用

After training your model on Google Cloud ML Engine (check out this awesome tutorial ), I named the input and output of my graph with

signature = predict_signature_def(inputs={'NAME_YOUR_INPUT': new_Model.input},
                                  outputs={'NAME_YOUR_OUTPUT': new_Model.output})

您可以在下方查看已训练的 keras 模型model.h5"的完整导出示例.

You can see the full exporting example for an already trained keras model 'model.h5' below.

import keras.backend as K
import tensorflow as tf
from keras.models import load_model, Sequential
from tensorflow.python.saved_model import builder as saved_model_builder
from tensorflow.python.saved_model import tag_constants, signature_constants
from tensorflow.python.saved_model.signature_def_utils_impl import predict_signature_def

# reset session
K.clear_session()
sess = tf.Session()
K.set_session(sess)

# disable loading of learning nodes
K.set_learning_phase(0)

# load model
model = load_model('model.h5')
config = model.get_config()
weights = model.get_weights()
new_Model = Sequential.from_config(config)
new_Model.set_weights(weights)

# export saved model
export_path = 'YOUR_EXPORT_PATH' + '/export'
builder = saved_model_builder.SavedModelBuilder(export_path)

signature = predict_signature_def(inputs={'NAME_YOUR_INPUT': new_Model.input},
                                  outputs={'NAME_YOUR_OUTPUT': new_Model.output})

with K.get_session() as sess:
    builder.add_meta_graph_and_variables(sess=sess,
                                         tags=[tag_constants.SERVING],
                                         signature_def_map={
                                             signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature})
    builder.save()

您还可以查看我的完整实施.

如果我的回答解决了您的问题,请在此处给我一个加号:)

edit: And if my answer solved your problem, just leave me an uptick here :)

这篇关于通过 Google Cloud ML 部署 Keras 模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆