最近老山在研究在modelarts上部署mask-rcnn,源代码提供的是keras模型。我们可以将keras转化成savedModel模型,在TensorFlow Serving上部署,可参考老山的上篇部署文章。至于输入和输出张量,到已经预先存在model.input和model.output中了。
不多说,直接上代码。
from keras import backend as K
import tensorflow as tf
# 在此之前,先加载keras模型
# 。。。
# 加载完成
with K.get_session() as sess:
export_path = './saved_model'
builder = tf.saved_model.builder.SavedModelBuilder(export_path)
signature_inputs = {
'input_image': tf.saved_model.utils.build_tensor_info(model.input[0]),
'input_image_meta': tf.saved_model.utils.build_tensor_info(model.input[1]),
'input_anchors': tf.saved_model.utils.build_tensor_info(model.input[2]),
}
signature_outputs = {
'mrcnn_detection':tf.saved_model.utils.build_tensor_info(model.output[0]),
'mrcnn_class':tf.saved_model.utils.build_tensor_info(model.output[1]),
'mrcnn_bbox':tf.saved_model.utils.build_tensor_info(model.output[2]),
'mrcnn_mask':tf.saved_model.utils.build_tensor_info(model.output[3]),
'ROI':tf.saved_model.utils.build_tensor_info(model.output[4]),
'rpn_class':tf.saved_model.utils.build_tensor_info(model.output[5]),
'rpn_bbox':tf.saved_model.utils.build_tensor_info(model.output[6]),
}
classification_signature_def = tf.saved_model.signature_def_utils.build_signature_def(
inputs=signature_inputs,
outputs=signature_outputs,
method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME)
builder.add_meta_graph_and_variables(
sess,
[tf.saved_model.tag_constants.SERVING],
signature_def_map={
'root': classification_signature_def
},
)
builder.save()
作者:山找海味
转载:https://blog.csdn.net/devcloud/article/details/101272608
查看评论