python - TensorFlow 服务 : Pass image to classifier

标签 python tensorflow tensorflow-serving

我在 Tensorflow(Python、tensorflow 1.9.0 和 tensorflow-serving 1.9.0)中构建了一个简单的分类器,它将对象分类为 5 个类之一。现在,我想为该模型提供服务。我已经导出它并给它一个分类签名(并且只有一个分类签名):

classification_signature = tf.saved_model.signature_def_utils.build_signature_def(
    inputs={signature_constants.CLASSIFY_INPUTS: classification_inputs},
    outputs={
        signature_constants.CLASSIFY_OUTPUT_CLASSES:
            classification_outputs_classes
    },
    method_name=signature_constants.CLASSIFY_METHOD_NAME)

进一步,变成:

builder.add_meta_graph_and_variables(
            sess, [tag_constants.SERVING],
            signature_def_map={
                'classification_results':
                    classification_signature
            },
            clear_devices=True, legacy_init_op=legacy_init_op)

当我启动 TF 服务器时,我可以看到模型正在被服务。我的问题是如何从客户端传递图像。代码如下:

request = classification_pb2.ClassificationRequest()
request.model_spec.name = model
request.model_spec.signature_name = 'classification_results' 

这就是我有点迷失和有点困惑的地方。对于 PredictionRequest,代码是:

request.inputs['inputs'].CopyFrom(
    tf.contrib.util.make_tensor_proto(image.astype(dtype=np.uint8), shape=[1, height, width, 3]))

但这对于 ClassificationRequest 不起作用。错误是:

File "TestServices.py", line 99, in make_request
  request.inputs['inputs'].CopyFrom(
     AttributeError: 'ClassificationRequest' object has no attribute 'inputs'

也没有:

request.input.CopyFrom(input_pb2.Input(
    tf.contrib.util.make_tensor_proto(image.astype(dtype=np.uint8), shape=[1, height, width, 3])
    )
)

这给出了错误:

File "TestServices.py", line 102, in make_request
  tf.contrib.util.make_tensor_proto(image.astype(dtype=np.uint8), shape=[1,height, width, 3])
    TypeError: Parameter to CopyFrom() must be instance of same class: 
    expected tensorflow.serving.Input got tensorflow.TensorProto.

因此,我的问题是:我需要做什么才能使用 ClassificationRequest 将图像传递到分类器?

最佳答案

我不确定这是否符合最佳实践,但这似乎有效。作为一个纯粹的 python 用户,我不得不说这感觉就像是骗局。我花了一段时间,但我通过查看 definition 找到了答案protobuf 文件和这个 documentation .

import tensorflow as tf
import numpy as np
from tensorflow_serving.apis import classification_pb2, input_pb2
image = np.random.rand(1, 32,32,3)

def _bytes_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

request = classification_pb2.ClassificationRequest()
request.model_spec.name = 'model'
request.model_spec.signature_name = 'classification_results' 

# Wrap numpy image in an example protobuf
example = tf.train.Example(features=tf.train.Features(feature={'image': _bytes_feature(image.tostring())}))

inp = input_pb2.Input()
inp.example_list.examples.extend([example])

request.input.CopyFrom(inp)

关于python - TensorFlow 服务 : Pass image to classifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51608408/

相关文章:

python - 将信息从小部件模块传递到主窗口

python - 使用Python将图像转换为十六进制格式

python - 通过 tf.data.Dataset 将大型 numpy 数组输入 TensorFlow 估计器

machine-learning - 参数无效错误预期 begin[0] = 0

tensorflow - 在 keras.sequential 模型中使用 keras.layers.Add()

python - 如何从odoo中的二进制图像获取完整路径

javascript - TensorFlow.js 是否使用 GPU 进行计算?

python-2.7 - tensorflow 服务错误: Invalid argument: JSON object: does not have named input

csv - 如何处理 .csv 输入以在 Tensorflow Serving 批量转换中使用?

python - 用于单圈时间优化的 Gekko(python)