对于每个样本,我都有一个2D数组,它不是我想通过张量流服务进行推断的图像。过去,由于使用以下serving_input_receiver_fn
的{{3}}的答案,我已经能够成功部署tensorflow服务:
HEIGHT = 199
WIDTH = 199
CHANNELS = 1
def serving_input_receiver_fn():
def decode_and_resize(image_str_tensor):
"""Decodes jpeg string, resizes it and returns a uint8 tensor."""
image = tf.image.decode_jpeg(image_str_tensor, channels=CHANNELS)
image = tf.expand_dims(image, 0)
image = tf.image.resize_bilinear(
image, [HEIGHT, WIDTH], align_corners=False)
image = tf.squeeze(image, squeeze_dims=[0])
image = tf.cast(image, dtype=tf.uint8)
return image
# Optional; currently necessary for batch prediction.
key_input = tf.placeholder(tf.string, shape=[None])
key_output = tf.identity(key_input)
input_ph = tf.placeholder(tf.string, shape=[None], name='image_binary')
images_tensor = tf.map_fn(
decode_and_resize, input_ph, back_prop=False, dtype=tf.uint8)
images_tensor = tf.image.convert_image_dtype(images_tensor, dtype=tf.float32)
return tf.estimator.export.ServingInputReceiver(
{'images': images_tensor},
{'bytes': input_ph})
但是,对于非图像数组,以下内容变得不清楚:
tf.io.decode_image
,但似乎并没有保留2D数组的维数。base64.b64encode(img_data)
对图像数据本身进行了编码。对于一般的2D阵列,我应该如何编码?简而言之,如何将链接的帖子的答案推广到非图像阵列的情况?