【发布时间】:2019-01-09 09:01:09
【问题描述】:
我已经按照本教程构建了一个通过 tensorflow 服务进行图像分类的服务器/客户端演示 https://github.com/tmlabonte/tendies/blob/master/minimum_working_example/tendies-basic-tutorial.ipynb
客户
它接受图像作为输入,将其转换为 Base64,使用 JSON 将其传递给服务器
input_image = open(image, "rb").read()
print("Raw bitstring: " + str(input_image[:10]) + " ... " + str(input_image[-10:]))
# Encode image in b64
encoded_input_string = base64.b64encode(input_image)
input_string = encoded_input_string.decode("utf-8")
print("Base64 encoded string: " + input_string[:10] + " ... " + input_string[-10:])
# Wrap bitstring in JSON
instance = [{"images": input_string}]
data = json.dumps({"instances": instance})
print(data[:30] + " ... " + data[-10:])
r = requests.post('http://localhost:9000/v1/models/cnn:predict', data=data)
#json.loads(r.content)
print(r.text)
服务器
将模型加载为 .h5 后,服务器必须保存为 SavedModel。 图像必须作为 Base64 编码字符串从客户端传递到服务器。
model=tf.keras.models.load_model('./model.h5')
input_bytes = tf.placeholder(tf.string, shape=[], name="input_bytes")
# input_bytes = tf.reshape(input_bytes, [])
# Transform bitstring to uint8 tensor
input_tensor = tf.image.decode_jpeg(input_bytes, channels=3)
# Convert to float32 tensor
input_tensor = tf.image.convert_image_dtype(input_tensor, dtype=tf.float32)
input_tensor = input_tensor / 127.5 - 1.0
# Ensure tensor has correct shape
input_tensor = tf.reshape(input_tensor, [64, 64, 3])
# CycleGAN's inference function accepts a batch of images
# So expand the single tensor into a batch of 1
input_tensor = tf.expand_dims(input_tensor, 0)
# x = model.input
y = model(input_tensor)
然后 input_bytes 成为 SavedModel 中 predition_signature 的输入
tensor_info_x = tf.saved_model.utils.build_tensor_info(input_bytes)
最后服务器结果如下:
§ saved_model_cli show --dir ./ --all
signature_def['predict']:
The given SavedModel SignatureDef contains the following input(s):
inputs['images'] tensor_info:
dtype: DT_STRING
shape: ()
name: input_bytes:0
The given SavedModel SignatureDef contains the following output(s):
outputs['scores'] tensor_info:
dtype: DT_FLOAT
shape: (1, 4)
name: sequential_1/dense_2/Softmax:0
Method name is: tensorflow/serving/predict
发送图片
当我发送 base64 图像时,我从服务器收到一个关于输入形状的运行时错误,似乎不是标量:
Using TensorFlow backend.
Raw bitstring: b'\xff\xd8\xff\xe0\x00\x10JFIF' ... b'0;s\xcfJ(\xa0h\xff\xd9'
Base64 encoded string: /9j/4AAQSk ... 9KKKBo/9k=
{"instances": [{"images": "/9j ... Bo/9k="}]}
{ "error": "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_input_bytes_0_0)]]" }
正如您从服务器中看到的,input_bytes 与shape=[] 一样是标量,我也尝试使用tf.reshape(input_bytes, []) 对其进行重塑,但没办法,我总是遇到同样的错误。
我在 Internet 和 Stackoverflow 中没有找到关于此错误的任何解决方案。你能建议如何解决它吗?
谢谢!
【问题讨论】:
标签: python tensorflow tensorflow-serving