11

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.

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

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')

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

4

4 回答 4

14

在 Google Cloud ML Engine 上训练您的模型后(查看这个很棒的教程 ),我将图表的输入和输出命名为

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

您可以在下面看到已训练的 keras 模型“model.h5”的完整导出示例。

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()

你也可以看到我的完整实现

编辑:如果我的回答解决了您的问题,请在此处给我留言 :)

于 2017-05-28T22:01:32.150 回答
3

我发现为了在谷歌云上使用 keras,必须使用 setup.py 脚本安装它,并将其放在运行 gcloud 命令的同一位置文件夹中:

├── setup.py
└── trainer
    ├── __init__.py
    ├── cloudml-gpu.yaml
    ├── example5-keras.py

在 setup.py 中放置如下内容:

from setuptools import setup, find_packages

setup(name='example5',
  version='0.1',
  packages=find_packages(),
  description='example to run keras on gcloud ml-engine',
  author='Fuyang Liu',
  author_email='fuyang.liu@example.com',
  license='MIT',
  install_requires=[
      'keras',
      'h5py'
  ],
  zip_safe=False)

然后您可以开始在 gcloud 上运行您的作业,例如:

export BUCKET_NAME=tf-learn-simple-sentiment
export JOB_NAME="example_5_train_$(date +%Y%m%d_%H%M%S)"
export JOB_DIR=gs://$BUCKET_NAME/$JOB_NAME
export REGION=europe-west1

gcloud ml-engine jobs submit training $JOB_NAME \
  --job-dir gs://$BUCKET_NAME/$JOB_NAME \
  --runtime-version 1.0 \
  --module-name trainer.example5-keras \
  --package-path ./trainer \
  --region $REGION \
  --config=trainer/cloudml-gpu.yaml \
  -- \
  --train-file gs://tf-learn-simple-sentiment/sentiment_set.pickle

要使用 GPU,然后cloudml-gpu.yaml在您的模块中添加一个文件,其中包含以下内容:

trainingInput:
  scaleTier: CUSTOM
  # standard_gpu provides 1 GPU. Change to complex_model_m_gpu for 4 
GPUs
  masterType: standard_gpu
  runtimeVersion: "1.0"
于 2017-04-05T23:13:17.337 回答
1

我对 Keras 了解不多。我咨询了一些专家,以下应该可行:

from keras import backend as k

# Build the model first
model = ...    

# Declare the inputs and outputs for CloudML
inputs = dict(zip((layer.name for layer in model.input_layers),
                  (t.name for t in model.inputs)))
tf.add_to_collection('inputs', json.dumps(inputs))

outputs = dict(zip((layer.name for layer in model.output_layers),
                   (t.name for t in model.outputs)))
tf.add_to_collection('outputs', json.dumps(outputs))

# Fit/train the model
model.fit(...)

# Export the model
saver = tf.train.Saver()
session = K.get_session()
saver.save(session, 'export')

一些重要的点:

  • 您必须在创建模型之后但在调用 K.get_session()、fit 等之前调用 tf.add_to_collection,
  • 当您将输入和输出层添加到图表时,您应该确保设置它们的名称,因为您在发送预测请求时需要参考它们。
于 2017-02-03T22:34:39.603 回答
0

这是另一个可能有帮助的答案。假设您已经有一个 keras 模型,您应该能够将其附加到脚本的末尾并获得模型的 ML Engine 兼容版本(协议缓冲区)。请注意,您需要将saved_model.pb文件和带有变量的同级目录上传到 ML Engine 才能正常工作。另请注意,.pb 文件必须命名为saved_model.pbsaved_model.pbtxt.

假设您的模型是名称model

from tensorflow import saved_model

model_builder = saved_model.builder.SavedModelBuilder("exported_model")                                                     
inputs = {                                                                          
    'input': saved_model.utils.build_tensor_info(model.input)                    
}                                                                                   
outputs = {                                                                         
    'earnings': saved_model.utils.build_tensor_info(model.output)                
}                                                                                                                                                
signature_def = saved_model.signature_def_utils.build_signature_def(             
    inputs=inputs,                                                                  
    outputs=outputs,                                                                
    method_name=saved_model.signature_constants.PREDICT_METHOD_NAME              
)                                                                            
model_builder.add_meta_graph_and_variables(                                         
    K.get_session(),                                                                
    tags=[saved_model.tag_constants.SERVING],                                    
    signature_def_map={saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature_def
    })                                                                                                                                                                       
model_builder.save()   

将模型导出到目录/exported_model

于 2018-08-06T04:09:35.267 回答