3

我有一个 Keras 图,其形状为 (?, 224, 224, 3) 的 float32 张量,我想将其导出到 Tensorflow Serving,以便使用 RESTful 进行预测。问题是我不能输入张量,而是编码 b64 字符串,因为这是 REST API 的限制。这意味着在导出图形时,输入需要是需要解码的字符串。

如何在不重新训练图形本身的情况下“注入”要转换为旧张量的新输入?我已经尝试了几个例子[1] [2]

我目前有以下用于导出的代码:

image = tf.placeholder(dtype=tf.string, shape=[None], name='source')


signature = predict_signature_def(inputs={'image_bytes': image},
                                 outputs={'output': model.output})

我不知何故需要找到一种将图像转换为model.input的方法,或者一种让模型输出连接到图像的方法。

任何帮助将不胜感激!

4

2 回答 2

2

您可以使用tf.decode_base64

image = tf.placeholder(dtype=tf.string, shape=[None], name='source')
image_b64decoded = tf.decode_base64(image)
signature = predict_signature_def(inputs={'image_bytes': image_b64decoded},
                                 outputs={'output': model.output})

编辑:

如果您需要使用tf.image.decode_image,您可以使用以下方法使其与多个输入一起工作tf.map_fn

image = tf.placeholder(dtype=tf.string, shape=[None], name='source')
image_b64decoded = tf.decode_base64(image)
image_decoded = tf.map_fn(tf.image.decode_image, image_b64decoded, dtype=tf.uint8)

当然,只要图像具有所有相同的尺寸,这将起作用。然而,结果是一个形状完全未知的张量,因为tf.image.decode_image可以根据图像的类型输出不同数量的维度。您可以重塑它或使用另一个tf.image.decode_*调用,因此至少您在张量中有已知数量的维度。

于 2018-08-07T14:01:09.853 回答
-1

创建 export_model 可能是一种更简单的方法。 tensorflow.org 中的一个示例

  1. 带有 float32、形状 (?, 224, 224, 3) 张量的 Keras 图

model = ...

  1. 定义一个函数来预处理 b64 图像
def preprocess_input(base64_input_bytes):
    def decode_bytes(img_bytes):
        img = tf.image.decode_jpeg(img_bytes, channels=3)
        img = tf.image.resize(img, (224, 224))
        img = tf.image.convert_image_dtype(img, tf.float32)
        return img

    base64_input_bytes = tf.reshape(base64_input_bytes, (-1,))
    return tf.map_fn(lambda img_bytes:
                     decode_bytes(img_bytes),
                     elems=base64_input_bytes,                     
                     fn_output_signature=tf.float32)
  1. 导出服务模型
serving_inputs = tf.keras.layers.Input(shape=(), dtype=tf.string, name='b64_input_bytes')
serving_x = tf.keras.layers.Lambda(preprocess_input, name='decode_image_bytes')(serving_inputs)
serving_x = model(serving_x)
serving_model = tf.keras.Model(serving_inputs, serving_x)
tf.saved_model.save(serving_model, serving_model_path)
  1. 服务
import requests
data = json.dumps({"signature_name": "serving_default", "instances": [{"b64_input_bytes": {"b64": b64str_1}}, {"b64_input_bytes": {"b64": b64str_2}}]})
headers = {"content-type": "application/json"}
json_response = requests.post('http://localhost:8501/v1/models/{model_name}:predict', data=data, headers=headers)
predictions = json.loads(json_response.text)['predictions']
于 2021-10-14T03:18:58.480 回答