本文整理汇总了Python中tensorflow.python.saved_model.signature_def_utils.build_signature_def方法的典型用法代码示例。如果您正苦于以下问题:Python signature_def_utils.build_signature_def方法的具体用法?Python signature_def_utils.build_signature_def怎么用?Python signature_def_utils.build_signature_def使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.python.saved_model.signature_def_utils
的用法示例。
在下文中一共展示了signature_def_utils.build_signature_def方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build_signature
# 需要导入模块: from tensorflow.python.saved_model import signature_def_utils [as 别名]
# 或者: from tensorflow.python.saved_model.signature_def_utils import build_signature_def [as 别名]
def build_signature(inputs, outputs):
"""Build the signature.
Not using predic_signature_def in saved_model because it is replacing the
tensor name, b/35900497.
Args:
inputs: a dictionary of tensor name to tensor
outputs: a dictionary of tensor name to tensor
Returns:
The signature, a SignatureDef proto.
"""
signature_inputs = {key: saved_model_utils.build_tensor_info(tensor)
for key, tensor in inputs.items()}
signature_outputs = {key: saved_model_utils.build_tensor_info(tensor)
for key, tensor in outputs.items()}
signature_def = signature_def_utils.build_signature_def(
signature_inputs, signature_outputs,
signature_constants.PREDICT_METHOD_NAME)
return signature_def
示例2: export_model
# 需要导入模块: from tensorflow.python.saved_model import signature_def_utils [as 别名]
# 或者: from tensorflow.python.saved_model.signature_def_utils import build_signature_def [as 别名]
def export_model(self, model_dir, global_step_val, last_checkpoint):
"""Exports the model so that it can used for batch predictions."""
with self.graph.as_default():
with tf.Session() as session:
session.run(tf.global_variables_initializer())
self.saver.restore(session, last_checkpoint)
signature = signature_def_utils.build_signature_def(
inputs=self.inputs,
outputs=self.outputs,
method_name=signature_constants.PREDICT_METHOD_NAME)
signature_map = {signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
signature}
model_builder = saved_model_builder.SavedModelBuilder(model_dir)
model_builder.add_meta_graph_and_variables(session,
tags=[tag_constants.SERVING],
signature_def_map=signature_map,
clear_devices=True)
model_builder.save()
示例3: export_model
# 需要导入模块: from tensorflow.python.saved_model import signature_def_utils [as 别名]
# 或者: from tensorflow.python.saved_model.signature_def_utils import build_signature_def [as 别名]
def export_model(self, model_dir, global_step_val, last_checkpoint):
"""Exports the model so that it can used for batch predictions."""
with self.graph.as_default():
with tf.Session() as session:
session.run(tf.global_variables_initializer())
self.saver.restore(session, last_checkpoint)
signature = signature_def_utils.build_signature_def(
inputs=self.inputs,
outputs=self.outputs,
method_name=signature_constants.PREDICT_METHOD_NAME)
signature_map = {
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature
}
model_builder = saved_model_builder.SavedModelBuilder(model_dir)
model_builder.add_meta_graph_and_variables(
session,
tags=[tag_constants.SERVING],
signature_def_map=signature_map,
clear_devices=True)
model_builder.save()
示例4: export_model
# 需要导入模块: from tensorflow.python.saved_model import signature_def_utils [as 别名]
# 或者: from tensorflow.python.saved_model.signature_def_utils import build_signature_def [as 别名]
def export_model(self, model_dir, global_step_val, last_checkpoint):
"""Exports the model so that it can used for batch predictions."""
with self.graph.as_default():
with tf.Session() as session:
session.run(tf.global_variables_initializer())
self.saver.restore(session, last_checkpoint)
signature = signature_def_utils.build_signature_def(
inputs=self.inputs,
outputs=self.outputs,
method_name=signature_constants.PREDICT_METHOD_NAME)
signature_map = {signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
signature}
model_builder = saved_model_builder.SavedModelBuilder(model_dir)
model_builder.add_meta_graph_and_variables(session,
tags=[tag_constants.SERVING],
signature_def_map=signature_map,
clear_devices=True)
model_builder.save()
示例5: save_signature
# 需要导入模块: from tensorflow.python.saved_model import signature_def_utils [as 别名]
# 或者: from tensorflow.python.saved_model.signature_def_utils import build_signature_def [as 别名]
def save_signature(self, directory):
signature = signature_def_utils.build_signature_def(
inputs={
'input':
saved_model_utils.build_tensor_info(self.input),
'dropout_rate':
saved_model_utils.build_tensor_info(self.dropout_rate)
},
outputs={
'output': saved_model_utils.build_tensor_info(self.output)
},
method_name=signature_constants.PREDICT_METHOD_NAME)
signature_map = {
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature
}
model_builder = saved_model_builder.SavedModelBuilder(directory)
model_builder.add_meta_graph_and_variables(
self.sess,
tags=[tag_constants.SERVING],
signature_def_map=signature_map,
clear_devices=True)
model_builder.save(as_text=False)
示例6: export_model
# 需要导入模块: from tensorflow.python.saved_model import signature_def_utils [as 别名]
# 或者: from tensorflow.python.saved_model.signature_def_utils import build_signature_def [as 别名]
def export_model(self, model_dir, global_step_val, last_checkpoint):
"""Exports the model so that it can used for batch predictions."""
with self.graph.as_default():
with tf.Session() as session:
session.run(tf.global_variables_initializer())
self.saver.restore(session, last_checkpoint)
signature = signature_def_utils.build_signature_def(
inputs=self.inputs,
outputs=self.outputs,
method_name=signature_constants.PREDICT_METHOD_NAME)
signature_map = {signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
signature}
model_builder = saved_model_builder.SavedModelBuilder(model_dir)
model_builder.add_meta_graph_and_variables(session,
tags=[tag_constants.SERVING],
signature_def_map=signature_map,
clear_devices=True)
model_builder.save()
示例7: export_model
# 需要导入模块: from tensorflow.python.saved_model import signature_def_utils [as 别名]
# 或者: from tensorflow.python.saved_model.signature_def_utils import build_signature_def [as 别名]
def export_model(self, model_dir, global_step_val, last_checkpoint):
"""Exports the model so that it can used for batch predictions."""
with self.graph.as_default():
with tf.Session(config=self.config) as session:
session.run(tf.global_variables_initializer())
self.saver.restore(session, last_checkpoint)
signature = signature_def_utils.build_signature_def(
inputs=self.inputs,
outputs=self.outputs,
method_name=signature_constants.PREDICT_METHOD_NAME)
signature_map = {signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
signature}
model_builder = saved_model_builder.SavedModelBuilder(model_dir)
model_builder.add_meta_graph_and_variables(session,
tags=[tag_constants.SERVING],
signature_def_map=signature_map,
clear_devices=True)
model_builder.save()
示例8: export
# 需要导入模块: from tensorflow.python.saved_model import signature_def_utils [as 别名]
# 或者: from tensorflow.python.saved_model.signature_def_utils import build_signature_def [as 别名]
def export(self, last_checkpoint, output_dir):
"""Builds a prediction graph and xports the model.
Args:
last_checkpoint: The latest checkpoint from training.
output_dir: Path to the folder to be used to output the model.
"""
logging.info('Exporting prediction graph to %s', output_dir)
with tf.Session(graph=tf.Graph()) as sess:
# Build and save prediction meta graph and trained variable values.
input_signatures, output_signatures = self.build_prediction_graph()
# Remove this if once Tensorflow 0.12 is standard.
try:
init_op = tf.global_variables_initializer()
except AttributeError:
init_op = tf.initialize_all_variables()
sess.run(init_op)
trained_saver = tf.train.Saver()
trained_saver.restore(sess, last_checkpoint)
predict_signature_def = signature_def_utils.build_signature_def(
input_signatures, output_signatures,
signature_constants.PREDICT_METHOD_NAME)
# Create a saver for writing SavedModel training checkpoints.
build = builder.SavedModelBuilder(
os.path.join(output_dir, 'saved_model'))
build.add_meta_graph_and_variables(
sess, [tag_constants.SERVING],
signature_def_map={
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
predict_signature_def
},
assets_collection=tf.get_collection(tf.GraphKeys.ASSET_FILEPATHS))
build.save()
示例9: testBuildSignatureDef
# 需要导入模块: from tensorflow.python.saved_model import signature_def_utils [as 别名]
# 或者: from tensorflow.python.saved_model.signature_def_utils import build_signature_def [as 别名]
def testBuildSignatureDef(self):
x = array_ops.placeholder(dtypes.float32, 1, name="x")
x_tensor_info = utils.build_tensor_info(x)
inputs = dict()
inputs["foo-input"] = x_tensor_info
y = array_ops.placeholder(dtypes.float32, name="y")
y_tensor_info = utils.build_tensor_info(y)
outputs = dict()
outputs["foo-output"] = y_tensor_info
signature_def = signature_def_utils.build_signature_def(inputs, outputs,
"foo-method-name")
self.assertEqual("foo-method-name", signature_def.method_name)
# Check inputs in signature def.
self.assertEqual(1, len(signature_def.inputs))
x_tensor_info_actual = signature_def.inputs["foo-input"]
self.assertEqual("x:0", x_tensor_info_actual.name)
self.assertEqual(types_pb2.DT_FLOAT, x_tensor_info_actual.dtype)
self.assertEqual(1, len(x_tensor_info_actual.tensor_shape.dim))
self.assertEqual(1, x_tensor_info_actual.tensor_shape.dim[0].size)
# Check outputs in signature def.
self.assertEqual(1, len(signature_def.outputs))
y_tensor_info_actual = signature_def.outputs["foo-output"]
self.assertEqual("y:0", y_tensor_info_actual.name)
self.assertEqual(types_pb2.DT_FLOAT, y_tensor_info_actual.dtype)
self.assertEqual(0, len(y_tensor_info_actual.tensor_shape.dim))
示例10: _validate_inputs_tensor_info
# 需要导入模块: from tensorflow.python.saved_model import signature_def_utils [as 别名]
# 或者: from tensorflow.python.saved_model.signature_def_utils import build_signature_def [as 别名]
def _validate_inputs_tensor_info(self, builder, tensor_info):
with self.test_session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
foo_signature = signature_def_utils.build_signature_def({
"foo_inputs": tensor_info
}, dict(), "foo")
self.assertRaises(
AssertionError,
builder.add_meta_graph_and_variables,
sess, ["foo"],
signature_def_map={"foo_key": foo_signature})
示例11: _validate_outputs_tensor_info
# 需要导入模块: from tensorflow.python.saved_model import signature_def_utils [as 别名]
# 或者: from tensorflow.python.saved_model.signature_def_utils import build_signature_def [as 别名]
def _validate_outputs_tensor_info(self, builder, tensor_info):
with self.test_session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
foo_signature = signature_def_utils.build_signature_def(
dict(), {"foo_outputs": tensor_info}, "foo")
self.assertRaises(
AssertionError,
builder.add_meta_graph_and_variables,
sess, ["foo"],
signature_def_map={"foo_key": foo_signature})
示例12: export
# 需要导入模块: from tensorflow.python.saved_model import signature_def_utils [as 别名]
# 或者: from tensorflow.python.saved_model.signature_def_utils import build_signature_def [as 别名]
def export(model_version, model_dir, sess, inputs, y_op):
"""导出tensorflow_serving可用的模型(Saved Model方式)(推荐)
prediction_signature必备的三个参数分别是输入inputs、输出outputs和方法名method_name,如果缺失方法名将会报错:“grpc.framework.interfaces.face.face.AbortionError: AbortionError(code=StatusCode.INTERNAL, details="Expected prediction signature method_name to be one of {tensorflow/serving/predict, tensorflow/serving/classify, tensorflow/serving/regress}. Was: ")”。每一个SavedModel关联着一个独立的checkpoint。每一个图元都绑定一个或多个标签,这些标签用来明确图元被加载的方式。标签只接受两种类型:serve或者train,保存时可以同时包含两个标签。其中tag_constants.SERVING = "serve",tag_constants.TRAINING = "train"。模型用于TensorFlow Serving时,标签必须包含serve类型。如果标签只包含train类型,TensorFlow Serving加载模型时会报错:“E tensorflow_serving/core/aspired_versions_manager.cc:351] Servable {name: default version: 2} cannot be loaded: Not found: Could not find meta graph def matching supplied tags.”。定义signature_def_map时注意定义默认服务签名键,如果缺少则会报错:“grpc.framework.interfaces.face.face.AbortionError: AbortionError(code=StatusCode.FAILED_PRECONDITION, details="Default serving signature key not found.")”。
"""
if model_version <= 0:
print('Please specify a positive value for version number.')
sys.exit()
path = os.path.dirname(os.path.abspath(model_dir))
if os.path.isdir(path) == False:
logging.warning('Path (%s) not exists, making directories...', path)
os.makedirs(path)
export_path = os.path.join(
compat.as_bytes(model_dir),
compat.as_bytes(str(model_version)))
if os.path.isdir(export_path) == True:
logging.warning('Path (%s) exists, removing directories...', export_path)
shutil.rmtree(export_path)
builder = saved_model_builder.SavedModelBuilder(export_path)
tensor_info_x = utils.build_tensor_info(inputs)
tensor_info_y = utils.build_tensor_info(y_op)
prediction_signature = signature_def_utils.build_signature_def(
inputs={'x': tensor_info_x},
outputs={'y': tensor_info_y},
method_name=signature_constants.PREDICT_METHOD_NAME)
builder.add_meta_graph_and_variables(
sess,
[tag_constants.SERVING],
signature_def_map={
'predict_text': prediction_signature,
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: prediction_signature
})
builder.save()
示例13: export_saved_model
# 需要导入模块: from tensorflow.python.saved_model import signature_def_utils [as 别名]
# 或者: from tensorflow.python.saved_model.signature_def_utils import build_signature_def [as 别名]
def export_saved_model(sess, export_path, input_tensor, output_tensor):
from tensorflow.python.saved_model import builder as saved_model_builder
from tensorflow.python.saved_model import signature_constants
from tensorflow.python.saved_model import signature_def_utils
from tensorflow.python.saved_model import tag_constants
from tensorflow.python.saved_model import utils
builder = saved_model_builder.SavedModelBuilder(export_path)
prediction_signature = signature_def_utils.build_signature_def(
inputs={'images': utils.build_tensor_info(input_tensor)},
outputs={
'scores': utils.build_tensor_info(output_tensor)
},
method_name=signature_constants.PREDICT_METHOD_NAME)
legacy_init_op = tf.group(
tf.tables_initializer(), name='legacy_init_op')
builder.add_meta_graph_and_variables(
sess, [tag_constants.SERVING],
signature_def_map={
'predict_images':
prediction_signature,
},
legacy_init_op=legacy_init_op)
builder.save()
示例14: export_model
# 需要导入模块: from tensorflow.python.saved_model import signature_def_utils [as 别名]
# 或者: from tensorflow.python.saved_model.signature_def_utils import build_signature_def [as 别名]
def export_model():
"""Exports the model"""
trained_checkpoint_prefix = 'linear_regression'
loaded_graph = tf.Graph()
with tf.Session(graph=loaded_graph) as sess:
sess.run(tf.global_variables_initializer())
# Restore from checkpoint:
loader = tf.train.import_meta_graph(trained_checkpoint_prefix + '.meta')
loader.restore(sess, trained_checkpoint_prefix)
# Add signature:
graph = tf.get_default_graph()
inputs = tf.saved_model.utils.build_tensor_info(graph.get_tensor_by_name('X:0'))
outputs = tf.saved_model.utils.build_tensor_info(graph.get_tensor_by_name('y_model:0'))
signature = signature_def_utils.build_signature_def(inputs={'X': inputs},
outputs={'y_model': outputs},
method_name=signature_constants.PREDICT_METHOD_NAME)
signature_map = {signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature}
# Export model:
builder = tf.saved_model.builder.SavedModelBuilder('./my_model')
builder.add_meta_graph_and_variables(sess, signature_def_map=signature_map,
tags=[tf.saved_model.tag_constants.SERVING])
builder.save()
# Export the model:
开发者ID:PacktPublishing,项目名称:Mastering-OpenCV-4-with-Python,代码行数:34,代码来源:tensorflow_save_and_load_using_model_builder.py
示例15: export_model
# 需要导入模块: from tensorflow.python.saved_model import signature_def_utils [as 别名]
# 或者: from tensorflow.python.saved_model.signature_def_utils import build_signature_def [as 别名]
def export_model():
"""Exports the model"""
trained_checkpoint_prefix = 'softmax_regression_model_mnist'
loaded_graph = tf.Graph()
with tf.Session(graph=loaded_graph) as sess:
sess.run(tf.global_variables_initializer())
# Restore from checkpoint
loader = tf.train.import_meta_graph(trained_checkpoint_prefix + '.meta')
loader.restore(sess, trained_checkpoint_prefix)
# Add signature:
graph = tf.get_default_graph()
inputs = tf.saved_model.utils.build_tensor_info(graph.get_tensor_by_name('myInput:0'))
outputs = tf.saved_model.utils.build_tensor_info(graph.get_tensor_by_name('myOutput:0'))
signature = signature_def_utils.build_signature_def(inputs={'myInput': inputs},
outputs={'myOutput': outputs},
method_name=signature_constants.PREDICT_METHOD_NAME)
signature_map = {signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature}
# Export model:
builder = tf.saved_model.builder.SavedModelBuilder('./my_model')
builder.add_meta_graph_and_variables(sess, signature_def_map=signature_map,
tags=[tf.saved_model.tag_constants.SERVING])
builder.save()
# Export the model:
开发者ID:PacktPublishing,项目名称:Mastering-OpenCV-4-with-Python,代码行数:34,代码来源:mnist_tensorflow_save_and_load_model_builder.py