如何将图像文件加载到Tensorflow Serve客户端input_tensor以便"DecodeJpeg/content: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 Serve 中运行该模型,我在 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]nt [[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']中,使其形状正确(标量值张量(?

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

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

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

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

最新更新