【问题标题】:How to create a serving input function to provide a prediction for images on google cloud platform?如何创建服务输入函数来为谷歌云平台上的图像提供预测?
【发布时间】:2018-07-25 03:57:01
【问题描述】:

来自this谷歌云文档和this oneand the stackoverflow answer 在这篇文章中 rhaertel80,我认为将图像发送到模型以在谷歌云上进行预测的 json 请求的推荐格式是:

{'instances': {'image_bytes': {'b64': base64.b64encode(jpeg_data)}}, {'image_bytes':...}}

下一步是创建serving_input_fn()(described in the google cloud docsthis GCP tutorial),它可以处理请求将发送的嵌套字典。

为此,我需要创建 'features' 和 'receiver_tensor' 以传递给 serving_input_fn() 需要返回的the ServingInputReciever function

但是,我看不出receiver_tensor 是一个以键和张量为值的字典的要求如何适合json 请求的嵌套格式。 (据我了解,receiver_tensors 是请求的占位符)。

如果请求不包含嵌套字典,则方法似乎相当简单,如教程和 this answer 中所示。

问题

那么,如何格式化 serving_input_fn() 以接收所描述形式的图像请求并创建满足 ServingInputReceiver 函数要求的特征和接收器张量?

部分困难可能是我不明白 serving_input_fn() 需要处理什么。它会一口气完成整个请求吗?还是每个实例一次传递一个?或者有没有其他方法可以理解函数将处理什么

更多详情

有关更多上下文,我正在使用 tf.Estimator 和 train_and_evaluate function 来训练模型并将其部署到谷歌云。模型的输入是一个 tf.dataset,其中包含 ({'spectrogram': image}, label) 的元组,其中 image 是一个张量。

我创建 input_fn 的尝试假设实例列表的一个元素一次传递:

def serving_input_fn():
    feature_placeholders = {'image_bytes': {'b64': tf.placeholder(dtype=tf.string,
                                                                  shape=[None],
                                                                  name='source')}}
    input_images = convert_bytestrings_to_images(feature_placeholders['image_bytes']['b64'])
    features = {'spectrogram': image for image in input_images}
    return tf.estimator.export.ServingInputReceiver(features, feature_placeholders)

导致错误的原因

ValueError: receiver_tensor image_bytes must be a Tensor.

而且我也不确定功能的形式是否正确。

【问题讨论】:

  • 我问了一个类似的question here,得到的答案可能对人们有所帮助

标签: python tensorflow machine-learning google-cloud-platform google-cloud-ml


【解决方案1】:

b64 由 ML Engine 透明处理。因此,虽然输入 JSON 需要具有 b64,但您的服务输入函数将只接收原始字节。所以你的服务输入 fn 需要是:

def serving_input_fn():
    feature_placeholders = {'image_bytes': 
                            tf.placeholder(tf.string, shape=())}
    input_images = convert_bytestrings_to_images(feature_placeholders['image_bytes']))
    features = {'spectrogram': image for image in input_images}
    return tf.estimator.export.ServingInputReceiver(features, feature_placeholders)

【讨论】:

    猜你喜欢
    • 2018-11-03
    • 2021-04-07
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 2017-09-27
    • 2020-12-13
    相关资源
    最近更新 更多