如何将图像文件加载到Tensorflow服务客户端,以便input_tensor'DecodeJpeg / contents:0'得到标量而不是形状[1]

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

我已成功训练,将图像分类模型导出到'retrained_graph.pb'。我的导出功能如下所示:

def export_model(sess, keys, architecture, saved_model_dir):
  if architecture == 'inception_v3':
    input_tensor = 'DecodeJpeg/contents:0'
  elif architecture.startswith('mobilenet_'):
    input_tensor = 'input:0'
  else:
    raise ValueError('Unkonwn architecture', architecture)
  in_image = sess.graph.get_tensor_by_name(input_tensor)
  inputs = {'image': tf.saved_model.utils.build_tensor_info(in_image)}

  out_classes = sess.graph.get_tensor_by_name('final_result:0')
  outputs = {'prediction': tf.saved_model.utils.build_tensor_info(out_classes),
             'classes': tf.saved_model.utils.build_tensor_info(tf.convert_to_tensor(list(keys))),}

  signature = tf.saved_model.signature_def_utils.build_signature_def(
    inputs=inputs,
    outputs=outputs,
    method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME
  )

  legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')

  builder = tf.saved_model.builder.SavedModelBuilder(saved_model_dir)
  builder.add_meta_graph_and_variables(
    sess, [tf.saved_model.tag_constants.SERVING],
    signature_def_map={
      tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature
    },
    legacy_init_op=legacy_init_op)
  builder.save()

我试图在我的笔记本电脑上运行Tensorflow服务中运行该模型,我在Docker中开始这样:

docker run -p 8500:8500 -t $USER/inception_serving

我的图像分类客户端(inception_client.py)如下所示:

import grpc
import tensorflow as tf

from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2_grpc


tf.app.flags.DEFINE_string('server', 'localhost:9000',
                           'PredictionService host:port')
tf.app.flags.DEFINE_string('image', '', 'path to image in JPEG format')
FLAGS = tf.app.flags.FLAGS


def main(_):
  channel = grpc.insecure_channel(FLAGS.server)
  stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
  # Send request
  with open(FLAGS.image, 'rb') as f:
    # See prediction_service.proto for gRPC request/response details.
    image_data = f.read()
    request = predict_pb2.PredictRequest()
    request.model_spec.name = 'model'
    request.model_spec.signature_name = tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY
    request.inputs['image'].CopyFrom(
        tf.contrib.util.make_tensor_proto(image_data, shape=[1]))
    result = stub.Predict(request, 10.0)  # 10 secs timeout
    print(result)

现在,当我尝试使用此命令对图像进行分类时:

tools/bazel_in_docker.sh bazel-bin/tensorflow_serving/example/inception_client   --server=127.0.0.1:8500 --image=./photo.jpg

我收到此错误:

debug_error_string = "{"created":"@1536674119.695152100","description":"Error received from 
peer","file":"src/core/lib/surface/call.cc","file_line":1095,"grpc_message":"contents must be 
scalar, got shape [1]\n\t [[Node: DecodeJpeg = DecodeJpeg[_output_shapes=[[?,?,3]], 
acceptable_fraction=1, channels=3, dct_method="", fancy_upscaling=true, ratio=1, 
try_recover_truncated=false, _device="/job:localhost/replica:0/task:0/
device:CPU:0"](_arg_DecodeJpeg/contents_0_0)]]","grpc_status":3}"

所以,我的问题是,如何将我的图像输入到request.inputs ['image']中,所以它的形状是正确的(标量值张量)?

tensorflow-serving
1个回答
0
投票

您需要加载图像 - 将jpeg解码为numpy数组格式。请参阅OpenCV imread开始。这是最快的

image = cv2.imread(img_path,1)
image = image.astype('f')

您需要做的预处理取决于您的模型,或者可能没有。

使用纯Tensorflow也可以,使用tf.read_file和tf.decode_jpeg

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