TensorFlow Serving: Pass image to classifier

左心房为你撑大大i 提交于 2019-12-08 03:02:10

问题


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)

which, further down the line, becomes:

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)

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' 

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'

Neither does:

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

which gives the error:

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?


回答1:


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)


来源:https://stackoverflow.com/questions/51608408/tensorflow-serving-pass-image-to-classifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!