本文整理汇总了Python中tensorflow.python.saved_model.signature_def_utils.build_signature_def函数的典型用法代码示例。如果您正苦于以下问题:Python build_signature_def函数的具体用法?Python build_signature_def怎么用?Python build_signature_def使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了build_signature_def函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
def setUp(self):
"""Write test SavedModels to a temp directory."""
with session.Session(graph=ops.Graph()) as sess:
x = variables.VariableV1(5, name="x")
y = variables.VariableV1(11, name="y")
z = x + y
self.evaluate(variables.global_variables_initializer())
foo_sig_def = signature_def_utils.build_signature_def(
{"foo_input": utils.build_tensor_info(x)},
{"foo_output": utils.build_tensor_info(z)})
bar_sig_def = signature_def_utils.build_signature_def(
{"bar_x": utils.build_tensor_info(x),
"bar_y": utils.build_tensor_info(y)},
{"bar_z": utils.build_tensor_info(z)})
builder = saved_model_builder.SavedModelBuilder(SIMPLE_ADD_SAVED_MODEL)
builder.add_meta_graph_and_variables(
sess, ["foo_graph"], {"foo": foo_sig_def, "bar": bar_sig_def})
builder.save()
# Write SavedModel with a main_op
assign_op = control_flow_ops.group(state_ops.assign(y, 7))
builder = saved_model_builder.SavedModelBuilder(SAVED_MODEL_WITH_MAIN_OP)
builder.add_meta_graph_and_variables(
sess, ["foo_graph"], {"foo": foo_sig_def, "bar": bar_sig_def},
main_op=assign_op)
builder.save()
示例2: _v1_multi_metagraph_saved_model
def _v1_multi_metagraph_saved_model(self):
export_graph = ops.Graph()
with export_graph.as_default():
start = array_ops.placeholder(
shape=[None], dtype=dtypes.float32, name="start")
v = resource_variable_ops.ResourceVariable(21.)
first_output = array_ops.identity(start * v, name="first_output")
second_output = array_ops.identity(v, name="second_output")
with session_lib.Session() as session:
session.run(v.initializer)
path = os.path.join(self.get_temp_dir(), "saved_model", str(ops.uid()))
builder = builder_impl.SavedModelBuilder(path)
builder.add_meta_graph_and_variables(
session, tags=["first"],
signature_def_map={
"first_key": signature_def_utils.build_signature_def(
{"first_start": utils_impl.build_tensor_info(start)},
{"first_output": utils_impl.build_tensor_info(
first_output)})})
builder.add_meta_graph(
tags=["second"],
signature_def_map={
"second_key": signature_def_utils.build_signature_def(
{"second_start": utils_impl.build_tensor_info(start)},
{"second_output": utils_impl.build_tensor_info(
second_output)})})
builder.save()
return path
示例3: testSignatureDefs
def testSignatureDefs(self):
export_dir = self._get_export_dir("test_signature_defs")
builder = saved_model_builder.SavedModelBuilder(export_dir)
# Graph with a single variable and a single entry in the signature def map.
# SavedModel is invoked to add with weights.
with self.test_session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
# Build and populate an empty SignatureDef for testing.
foo_signature = signature_def_utils.build_signature_def(dict(),
dict(), "foo")
builder.add_meta_graph_and_variables(
sess, ["foo"], signature_def_map={"foo_key": foo_signature})
# Graph with the same single variable and multiple entries in the signature
# def map. No weights are saved by SavedModel.
with self.test_session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 43)
# Build and populate a different SignatureDef for testing.
bar_signature = signature_def_utils.build_signature_def(dict(),
dict(), "bar")
# Also, build a different SignatureDef corresponding to "foo_key" defined
# in the previous graph.
foo_new_signature = signature_def_utils.build_signature_def(dict(),
dict(),
"foo_new")
builder.add_meta_graph(
["bar"],
signature_def_map={
"bar_key": bar_signature,
"foo_key": foo_new_signature
})
# Save the SavedModel to disk.
builder.save()
# Restore the graph with tag "foo". The single entry in the SignatureDef map
# corresponding to "foo_key" should exist.
with self.test_session(graph=ops.Graph()) as sess:
foo_graph = loader.load(sess, ["foo"], export_dir)
self.assertEqual(
42, ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0].eval())
foo_signature = foo_graph.signature_def
self.assertEqual(len(foo_signature), 1)
self.assertEqual("foo", foo_signature["foo_key"].method_name)
# Restore the graph with tag "bar". The SignatureDef map should have two
# entries. One corresponding to "bar_key" and another corresponding to the
# new value of "foo_key".
with self.test_session(graph=ops.Graph()) as sess:
bar_graph = loader.load(sess, ["bar"], export_dir)
self.assertEqual(
42, ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0].eval())
bar_signature = bar_graph.signature_def
self.assertEqual(len(bar_signature), 2)
self.assertEqual("bar", bar_signature["bar_key"].method_name)
self.assertEqual("foo_new", bar_signature["foo_key"].method_name)
示例4: testGetSignatureDefByKey
def testGetSignatureDefByKey(self):
x = array_ops.placeholder(dtypes.float32, 1, name="x")
x_tensor_info = utils.build_tensor_info(x)
y = array_ops.placeholder(dtypes.float32, name="y")
y_tensor_info = utils.build_tensor_info(y)
foo_signature_def = signature_def_utils.build_signature_def({
"foo-input": x_tensor_info
}, {"foo-output": y_tensor_info}, "foo-method-name")
bar_signature_def = signature_def_utils.build_signature_def({
"bar-input": x_tensor_info
}, {"bar-output": y_tensor_info}, "bar-method-name")
meta_graph_def = meta_graph_pb2.MetaGraphDef()
self._add_to_signature_def_map(
meta_graph_def, {"foo": foo_signature_def,
"bar": bar_signature_def})
# Look up a key that does not exist in the SignatureDefMap.
missing_key = "missing-key"
with self.assertRaisesRegexp(
ValueError,
"No SignatureDef with key '%s' found in MetaGraphDef" % missing_key):
signature_def_contrib_utils.get_signature_def_by_key(
meta_graph_def, missing_key)
# Look up the key, `foo` which exists in the SignatureDefMap.
foo_signature_def = signature_def_contrib_utils.get_signature_def_by_key(
meta_graph_def, "foo")
self.assertTrue("foo-method-name", foo_signature_def.method_name)
# Check inputs in signature def.
self.assertEqual(1, len(foo_signature_def.inputs))
self._check_tensor_info(foo_signature_def.inputs, "foo-input", "x:0")
# Check outputs in signature def.
self.assertEqual(1, len(foo_signature_def.outputs))
self._check_tensor_info(foo_signature_def.outputs, "foo-output", "y:0")
# Look up the key, `bar` which exists in the SignatureDefMap.
bar_signature_def = signature_def_contrib_utils.get_signature_def_by_key(
meta_graph_def, "bar")
self.assertTrue("bar-method-name", bar_signature_def.method_name)
# Check inputs in signature def.
self.assertEqual(1, len(bar_signature_def.inputs))
self._check_tensor_info(bar_signature_def.inputs, "bar-input", "x:0")
# Check outputs in signature def.
self.assertEqual(1, len(bar_signature_def.outputs))
self._check_tensor_info(bar_signature_def.outputs, "bar-output", "y:0")
示例5: build_graph_helper
def build_graph_helper():
g = ops.Graph()
with g.as_default():
x = variables.VariableV1(5, name="x")
y = variables.VariableV1(11, name="y")
z = x + y
foo_sig_def = signature_def_utils.build_signature_def({
"foo_input": utils.build_tensor_info(x)
}, {"foo_output": utils.build_tensor_info(z)})
bar_sig_def = signature_def_utils.build_signature_def({
"bar_x": utils.build_tensor_info(x),
"bar_y": utils.build_tensor_info(y)
}, {"bar_z": utils.build_tensor_info(z)})
return g, {"foo": foo_sig_def, "bar": bar_sig_def}, y
示例6: export
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()
示例7: testBuildSignatureDef
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))
示例8: _validate_outputs_tensor_info_accept
def _validate_outputs_tensor_info_accept(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")
builder.add_meta_graph_and_variables(
sess, ["foo"],
signature_def_map={"foo_key": foo_signature})
示例9: export
def export(model_version, model_dir, sess, x, y_op):
"""导出tensorflow_serving可用的模型
SavedModel(tensorflow.python.saved_model)提供了一种跨语言格式来保存和恢复训练后的TensorFlow模型。它使用方法签名来定义Graph的输入和输出,使上层系统能够更方便地生成、调用或转换TensorFlow模型。
SavedModelBuilder类提供保存Graphs、Variables及Assets的方法。所保存的Graphs必须标注用途标签。在这个实例中我们打算将模型用于服务而非训练,因此我们用SavedModel预定义好的tag_constant.Serving标签。
为了方便地构建签名,SavedModel提供了signature_def_utils API。我们通过signature_def_utils.build_signature_def()来构建predict_signature。一个predict_signature至少包含以下参数:
* inputs = {'x': tensor_info_x} 指定输入的tensor信息
* outputs = {'y': tensor_info_y} 指定输出的tensor信息
* method_name = signature_constants.PREDICT_METHOD_NAME
method_name定义方法名,它的值应该是tensorflow/serving/predict、tensorflow/serving/classify和tensorflow/serving/regress三者之一。Builder标签用来明确Meta Graph被加载的方式,只接受serve和train两种类型。
"""
if model_version <= 0:
logging.warning('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(x)
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},
# signature_constants.CLASSIFY_METHOD_NAME = "tensorflow/serving/classify"
# signature_constants.PREDICT_METHOD_NAME = "tensorflow/serving/predict"
# signature_constants.REGRESS_METHOD_NAME = "tensorflow/serving/regress"
# 如果缺失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: ")
method_name=signature_constants.PREDICT_METHOD_NAME)
builder.add_meta_graph_and_variables(
sess,
# tag_constants.SERVING = "serve"
# tag_constants.TRAINING = "train"
# 如果只有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.
[tag_constants.SERVING],
signature_def_map={
'predict_text': prediction_signature,
# 如果缺失会报错:
# grpc.framework.interfaces.face.face.AbortionError: AbortionError(code=StatusCode.FAILED_PRECONDITION, details="Default serving signature key not found.")
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: prediction_signature
})
builder.save()
示例10: _generate_signatures
def _generate_signatures(signature_functions, resource_map):
"""Validates and calls `signature_functions` in the default graph.
Args:
signature_functions: A dictionary mapping string keys to concrete TensorFlow
functions (e.g. from `_canonicalize_signatures`) which will be used to
generate SignatureDefs.
resource_map: A dictionary mapping from resource tensors in the eager
context to resource tensors in the Graph being exported. This dictionary
is used to re-bind resources captured by functions to tensors which will
exist in the SavedModel.
Returns:
Each function in the `signature_functions` dictionary is called with
placeholder Tensors, generating a function call operation and output
Tensors. The placeholder Tensors, the function call operation, and the
output Tensors from the function call are part of the default Graph.
This function then returns a dictionary with the same structure as
`signature_functions`, with the concrete functions replaced by SignatureDefs
implicitly containing information about how to call each function from a
TensorFlow 1.x Session / the C++ Loader API. These SignatureDefs reference
the generated placeholders and Tensor outputs by name.
The caller is expected to include the default Graph set while calling this
function as a MetaGraph in a SavedModel, including the returned
SignatureDefs as part of that MetaGraph.
"""
signatures = {}
for signature_key, func in sorted(signature_functions.items()):
# Register the inference function for this signature in the exported
# graph. There is no direct use for the gradient of this function, so we
# don't generate/register a gradient function here (but may end up with one
# if another function relies on it). Users can still take symbolic gradients
# of the function on import, the gradient just won't be in the saved
# graph. When exporting a signature which already computes gradients, this
# stops us from taking needless second-order gradients.
func.add_to_graph(register_gradient_functions=False)
export_captures = _map_captured_resources_to_created_resources(
func.graph.captures, resource_map)
mapped_inputs, exterior_argument_placeholders = (
_map_function_inputs_to_created_inputs(
func.inputs, export_captures, signature_key, func.name))
# Calls the function quite directly, since we have new captured resource
# tensors we need to feed in which weren't part of the original function
# definition.
# pylint: disable=protected-access
outputs = _normalize_outputs(
func._build_call_outputs(
func._inference_function.call(context.context(), mapped_inputs)),
func.name, signature_key)
# pylint: enable=protected-access
signatures[signature_key] = signature_def_utils.build_signature_def(
_tensor_dict_to_tensorinfo(exterior_argument_placeholders),
_tensor_dict_to_tensorinfo(outputs))
return signatures
示例11: _validate_inputs_tensor_info_fail
def _validate_inputs_tensor_info_fail(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})
示例12: _WriteInputSavedModel
def _WriteInputSavedModel(self, input_saved_model_dir):
"""Write the saved model as an input for testing."""
g, var, inp, out = self._GetGraph()
signature_def = signature_def_utils.build_signature_def(
inputs={"myinput": utils.build_tensor_info(inp)},
outputs={"myoutput": utils.build_tensor_info(out)},
method_name=signature_constants.PREDICT_METHOD_NAME)
saved_model_builder = builder.SavedModelBuilder(input_saved_model_dir)
with self.session(graph=g, config=self._GetConfigProto()) as sess:
sess.run(var.initializer)
saved_model_builder.add_meta_graph_and_variables(
sess, [tag_constants.SERVING],
signature_def_map={"mypredict": signature_def})
saved_model_builder.save()
示例13: _signature_with_no_inputs
def _signature_with_no_inputs(self):
export_graph = ops.Graph()
with export_graph.as_default():
array_ops.placeholder(name="x", shape=[], dtype=dtypes.float32)
output = random_ops.random_normal([2])
with session_lib.Session() as session:
path = os.path.join(self.get_temp_dir(), "saved_model", str(ops.uid()))
b = builder_impl.SavedModelBuilder(path)
b.add_meta_graph_and_variables(
session,
tags=[tag_constants.SERVING],
signature_def_map={
"key": signature_def_utils.build_signature_def(
{}, dict(value=utils_impl.build_tensor_info(output)))})
b.save()
return path
示例14: _generate_signatures
def _generate_signatures(signature_functions, resource_map):
"""Validates and calls `signature_functions` in the default graph.
Args:
signature_functions: A dictionary mapping string keys to concrete TensorFlow
functions (e.g. from `_canonicalize_signatures`) which will be used to
generate SignatureDefs.
resource_map: A dictionary mapping from resource tensors in the eager
context to resource tensors in the Graph being exported. This dictionary
is used to re-bind resources captured by functions to tensors which will
exist in the SavedModel.
Returns:
Each function in the `signature_functions` dictionary is called with
placeholder Tensors, generating a function call operation and output
Tensors. The placeholder Tensors, the function call operation, and the
output Tensors from the function call are part of the default Graph.
This function then returns a dictionary with the same structure as
`signature_functions`, with the concrete functions replaced by SignatureDefs
implicitly containing information about how to call each function from a
TensorFlow 1.x Session / the C++ Loader API. These SignatureDefs reference
the generated placeholders and Tensor outputs by name.
The caller is expected to include the default Graph set while calling this
function as a MetaGraph in a SavedModel, including the returned
SignatureDefs as part of that MetaGraph.
"""
signatures = {}
for signature_key, function in sorted(signature_functions.items()):
if function.graph.captures:
argument_inputs = function.graph.inputs[:-len(function.graph.captures)]
else:
argument_inputs = function.graph.inputs
mapped_inputs, exterior_argument_placeholders = (
_map_function_arguments_to_created_inputs(
argument_inputs, signature_key, function.name))
outputs = _normalize_outputs(
_call_function_with_mapped_captures(
function, mapped_inputs, resource_map),
function.name, signature_key)
signatures[signature_key] = signature_def_utils.build_signature_def(
_tensor_dict_to_tensorinfo(exterior_argument_placeholders),
_tensor_dict_to_tensorinfo(outputs),
method_name=signature_constants.PREDICT_METHOD_NAME)
return signatures
示例15: test_load_saved_model_with_no_variables
def test_load_saved_model_with_no_variables(self, builder_cls):
"""Test that SavedModel runs saver when there appear to be no variables.
When no variables are detected, this may mean that the variables were saved
to different collections, or the collections weren't saved to the
SavedModel. If the SavedModel MetaGraphDef contains a saver, it should still
run in either of these cases.
Args:
builder_cls: SavedModelBuilder or _SavedModelBuilder class
"""
path = _get_export_dir("no_variable_saved_model")
with session.Session(graph=ops.Graph()) as sess:
x = variables.VariableV1(
5, name="x", collections=["not_global_variable"])
y = variables.VariableV1(
11, name="y", collections=["not_global_variable"])
self.assertFalse(variables._all_saveable_objects())
z = x + y
self.evaluate(variables.variables_initializer([x, y]))
foo_sig_def = signature_def_utils.build_signature_def(
{"foo_input": utils.build_tensor_info(x)},
{"foo_output": utils.build_tensor_info(z)})
builder = saved_model_builder.SavedModelBuilder(path)
builder.add_meta_graph_and_variables(
sess, ["foo_graph"], {"foo": foo_sig_def},
saver=tf_saver.Saver([x, y]))
builder.save()
loader = loader_impl.SavedModelLoader(path)
with self.session(graph=ops.Graph()) as sess:
saver, _ = loader.load_graph(sess.graph, ["foo_graph"])
self.assertFalse(variables._all_saveable_objects())
self.assertIsNotNone(saver)
with self.session(graph=ops.Graph()) as sess:
loader.load(sess, ["foo_graph"])
self.assertEqual(5, sess.graph.get_tensor_by_name("x:0").eval())
self.assertEqual(11, sess.graph.get_tensor_by_name("y:0").eval())