1

请帮忙!在 GCP AI 平台中,我一直在为我的掩码 rcnn 对象检测模型获取任何类型的预测响应而陷入困境。到目前为止,我已经在大约 200 张图像上训练了一个简单的模型,该模型使用 matterport repo 以 h5 格式输出权重文件。在一个新的 python 脚本中,我像这样加载这些权重:

# LOAD MODEL
from config import mask_config
from model import MaskRCNN

config = get_config()

model = MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)
model.load_weights(H5_WEIGHT_PATH, by_name=True)

然后,我使用以下代码创建了一个冻结图 .pb 文件:

def freeze_model(model, name):
    frozen_graph = freeze_session(
        sess,
        output_names=[out.op.name for out in model.outputs][:4])
    directory = PATH_TO_SAVE_FROZEN_PB
#     directory = './'
    tf.train.write_graph(frozen_graph, directory, name , as_text=False)
    print("*"*80)
    print("Finish converting keras model to Frozen PB")
    print('PATH: ', PATH_TO_SAVE_FROZEN_PB)
#     print('PATH: ', './')
    print("*" * 80)

freeze_model(model.keras_model, FROZEN_NAME)

到目前为止,一切都很好!然后我继续使我的模型 tensorflow 服务准备就绪,如下所示:

def make_serving_ready(model_path, save_serve_path, version_number):
    import tensorflow as tf

    export_dir = os.path.join(save_serve_path, str(version_number))
    graph_pb = model_path

    builder = tf.saved_model.builder.SavedModelBuilder(export_dir)

    with tf.gfile.GFile(graph_pb, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())

    sigs = {}
    
    # tf.import_graph_def(graph_model_def, name='', input_map={"input_image": img_uint8})
    with tf.Session(graph=tf.Graph()) as sess:
        # name="" is important to ensure we don't get spurious prefixing
        tf.import_graph_def(graph_def, name="")
        g = tf.get_default_graph()
        input_image = g.get_tensor_by_name("input_image:0")
        input_image_meta = g.get_tensor_by_name("input_image_meta:0")
        input_anchors = g.get_tensor_by_name("input_anchors:0")

        output_detection = g.get_tensor_by_name("mrcnn_detection/Reshape_1:0")
        output_mask = g.get_tensor_by_name("mrcnn_mask/Reshape_1:0")

        sigs[signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY] = \
            tf.saved_model.signature_def_utils.predict_signature_def(
                {"input_image": input_image, 'input_image_meta': input_image_meta, 'input_anchors': input_anchors},
#                 {"image_bytes": img_uint8, 'input_image_meta': input_image_meta, 'input_anchors': input_anchors},
                {"mrcnn_detection/Reshape_1": output_detection, 'mrcnn_mask/Reshape_1': output_mask})

        builder.add_meta_graph_and_variables(sess,
                                             [tag_constants.SERVING],
                                             signature_def_map=sigs)

    builder.save()
    print("*" * 80)
    print("FINISH CONVERTING FROZEN PB TO SERVING READY")
    print("PATH:", PATH_TO_SAVE_TENSORFLOW_SERVING_MODEL)
    print("*" * 80)

# Now convert frozen graph to Tensorflow Serving Ready
make_serving_ready(os.path.join(PATH_TO_SAVE_FROZEN_PB, FROZEN_NAME),
                     PATH_TO_SAVE_TENSORFLOW_SERVING_MODEL,
                     VERSION_NUMBER)

print("COMPLETED")

然后我将上述代码的输出 (saved_model.pb) 部署到 ai 平台模型

我想弄清楚的是 - 我如何修改上面的代码以接受 base64 编码的图像?我已成功将模型部署到 GCP AI 平台上,但是当我进行示例输入以测试预测时,我需要使用 gcp 请求的格式:

{"instances":[
{"image_bytes":{"b64":abcdefg},{"key":"1"}
]}

因此,当我将图像转换为 base64 编码图像并输入上述格式时,出现此错误:

{
  "error": "{ \"error\": \"Failed to process element: 0 key: image_bytes of \\'instances\\' list. Error: Invalid argument: JSON object: does not have named input: image_bytes\" }"
}

然后我回到我的代码并尝试更改我的 input_image 变量以接受解码的图像格式:

                # concatenate decoder graph and original graph
                image = tf.map_fn(decode_and_resize, image_str_tensor, back_prop=False, dtype=tf.uint8)
                tf.import_graph_def(graph_def, name="", input_map={'input_image:0':image})

但后来我得到这个错误:

ValueError: Input 0 of node zero_padding2d_1/Pad_1 was passed uint8 from decoder/map/TensorArrayStack/TensorArrayGatherV3:0 incompatible with expected float.

所以我对如何让这件事运行起来完全一无所知。有没有人可以解决这个问题??!!!

4

1 回答 1

0

根据https://cloud.google.com/ai-platform/prediction/docs/online-predict#for---json-request

编码字符串必须格式化为一个 JSON 对象,带有一个名为 b64 的键。

{
  "instances": [
    {
      "image_bytes": {"b64": "ASa8asdf"}
    },
    {
      "image_bytes": {"b64": "JLK7ljk3"}
    }
  ]
}

在您的 TensorFlow 模型代码中,您必须为二进制输入和输出张量命名别名,以便它们以“_bytes”结尾。

于 2020-12-15T00:20:42.997 回答