从tensorflow培训谷歌CloudML部署单独创建一个服务图?

问题描述 投票:1回答:1

我试图部署tf.keras图像分类模型,以谷歌CloudML引擎。我一定要包括代码,以创建单独的训练服图形让它成为我的模型在一个web应用程序?我已经有我的SavedModel格式(saved_model.pb和可变文件)的模式,所以我不知道如果我需要做的这个额外的步骤,以得到它的工作。

例如这是直接从代码GCP Tensorflow部署模型documentation

def json_serving_input_fn():
  """Build the serving inputs."""
  inputs = {}
  for feat in INPUT_COLUMNS:
    inputs[feat.name] = tf.placeholder(shape=[None], dtype=feat.dtype)

  return tf.estimator.export.ServingInputReceiver(inputs, inputs)
json tensorflow tensorflow-serving google-cloud-ml
1个回答
0
投票

你可能会训练你的模型与实际的图像文件,而最好是发送图像作为编码字节串托管于CloudML的典范。因此,你需要导出模型时,你提到指定ServingInputReceiver功能。一些样板代码为Keras模型做到这一点:

# Convert keras model to TF estimator
tf_files_path = './tf'
estimator =\
    tf.keras.estimator.model_to_estimator(keras_model=model,
                                          model_dir=tf_files_path)

# Your serving input function will accept a string
# And decode it into an image
def serving_input_receiver_fn():
    def prepare_image(image_str_tensor):
        image = tf.image.decode_png(image_str_tensor,
                                    channels=3)
        return image  # apply additional processing if necessary

    # Ensure model is batchable
    # https://stackoverflow.com/questions/52303403/
    input_ph = tf.placeholder(tf.string, shape=[None])
    images_tensor = tf.map_fn(
        prepare_image, input_ph, back_prop=False, dtype=tf.float32)

    return tf.estimator.export.ServingInputReceiver(
        {model.input_names[0]: images_tensor},
        {'image_bytes': input_ph})

# Export the estimator - deploy it to CloudML afterwards
export_path = './export'
estimator.export_savedmodel(
    export_path,
    serving_input_receiver_fn=serving_input_receiver_fn)

你可以参考this very helpful answer一个更完整的参考和其他选项导出模型。

编辑:如果这种方法抛出一个ValueError: Couldn't find trained model at ./tf.错误,你可以尝试一下,我在this answer的变通方法解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.