TensorFlow Serving:将图像传递给分类器 [英] TensorFlow Serving: Pass image to classifier

查看:51
本文介绍了TensorFlow Serving:将图像传递给分类器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have built a simple classifier in Tensorflow (Python, tensorflow 1.9.0 and tensorflow-serving 1.9.0) which classifies objects into one of 5 classes. Now, I would like to serve that model. I have exported it and given it a classification signature (and only a classification signature):

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 服务器时,我可以看到正在提供模型.我的问题是如何将图像传递给来自客户端.代码如下:

And when I start the TF server I can see that the model is being served. My problem is how to pass an image to is from the client. The code is as follows:

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

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

And this is where I am sort of lost and somewhat confused. For a PredictionRequest the code is:

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

但这不适用于分类请求.错误是:

but that does not work for a ClassificationRequest. The error is:

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.

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

My question, therefore, is: What do I need to do pass an image to the classifier using a ClassificationRequest?

推荐答案

我不确定这是否符合最佳实践,但这似乎有效.作为一个纯 python 用户,我不得不说这感觉就像 hocus pocus.我花了一段时间,但我通过查看 protobuf 文件的定义和这个文档.

I'm not sure if this is according to best practices but this seems to work. As a pure python user, I have to say this feels like hocus pocus. I took me a while but I figured it out by looking at the definition of the protobuf files and this 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)

这篇关于TensorFlow Serving:将图像传递给分类器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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