本文整理汇总了Python中tensorflow.python.saved_model.loader.load方法的典型用法代码示例。如果您正苦于以下问题:Python loader.load方法的具体用法?Python loader.load怎么用?Python loader.load使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.python.saved_model.loader
的用法示例。
在下文中一共展示了loader.load方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testNoOverwrite
# 需要导入模块: from tensorflow.python.saved_model import loader [as 别名]
# 或者: from tensorflow.python.saved_model.loader import load [as 别名]
def testNoOverwrite(self):
export_dir = os.path.join(test.get_temp_dir(), "test_no_overwrite")
builder = saved_model_builder.SavedModelBuilder(export_dir)
# Graph with a single variable. SavedModel invoked to:
# - add with weights.
with self.test_session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
builder.add_meta_graph_and_variables(sess, ["foo"])
# Save the SavedModel to disk in text format.
builder.save(as_text=True)
# Restore the graph with tag "foo", whose variables were saved.
with self.test_session(graph=ops.Graph()) as sess:
loader.load(sess, ["foo"], export_dir)
self.assertEqual(
42, ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0].eval())
# An attempt to create another builder with the same export directory should
# result in an assertion error.
self.assertRaises(AssertionError, saved_model_builder.SavedModelBuilder,
export_dir)
示例2: testClearDevices
# 需要导入模块: from tensorflow.python.saved_model import loader [as 别名]
# 或者: from tensorflow.python.saved_model.loader import load [as 别名]
def testClearDevices(self):
export_dir = os.path.join(test.get_temp_dir(), "test_clear_devices")
builder = saved_model_builder.SavedModelBuilder(export_dir)
# Specify a device and save a variable.
ops.reset_default_graph()
with session.Session(
target="",
config=config_pb2.ConfigProto(device_count={"CPU": 2})) as sess:
with sess.graph.device("/cpu:0"):
self._init_and_validate_variable(sess, "v", 42)
builder.add_meta_graph_and_variables(
sess, [tag_constants.TRAINING], clear_devices=True)
# Save the SavedModel to disk.
builder.save()
# Restore the graph with a single predefined tag whose variables were saved
# without any device information.
with self.test_session(graph=ops.Graph()) as sess:
loader.load(sess, [tag_constants.TRAINING], export_dir)
self.assertEqual(
42, ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0].eval())
示例3: testNoOverwrite
# 需要导入模块: from tensorflow.python.saved_model import loader [as 别名]
# 或者: from tensorflow.python.saved_model.loader import load [as 别名]
def testNoOverwrite(self):
export_dir = os.path.join(tf.test.get_temp_dir(), "test_no_overwrite")
builder = saved_model_builder.SavedModelBuilder(export_dir)
# Graph with a single variable. SavedModel invoked to:
# - add with weights.
with self.test_session(graph=tf.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
builder.add_meta_graph_and_variables(sess, ["foo"])
# Save the SavedModel to disk in text format.
builder.save(as_text=True)
# Restore the graph with tag "foo", whose variables were saved.
with self.test_session(graph=tf.Graph()) as sess:
loader.load(sess, ["foo"], export_dir)
self.assertEqual(
42, tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)[0].eval())
# An attempt to create another builder with the same export directory should
# result in an assertion error.
self.assertRaises(AssertionError, saved_model_builder.SavedModelBuilder,
export_dir)
示例4: get_meta_graph_def
# 需要导入模块: from tensorflow.python.saved_model import loader [as 别名]
# 或者: from tensorflow.python.saved_model.loader import load [as 别名]
def get_meta_graph_def(saved_model_dir, tag_set):
"""DEPRECATED: Use saved_model_utils.get_meta_graph_def instead.
Gets MetaGraphDef from SavedModel. Returns the MetaGraphDef for the given
tag-set and SavedModel directory.
Args:
saved_model_dir: Directory containing the SavedModel to inspect or execute.
tag_set: Group of tag(s) of the MetaGraphDef to load, in string format,
separated by ','. For tag-set contains multiple tags, all tags must be
passed in.
Raises:
RuntimeError: An error when the given tag-set does not exist in the
SavedModel.
Returns:
A MetaGraphDef corresponding to the tag-set.
"""
return saved_model_utils.get_meta_graph_def(saved_model_dir, tag_set)
开发者ID:PacktPublishing,项目名称:Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda,代码行数:22,代码来源:saved_model_cli.py
示例5: local_predict
# 需要导入模块: from tensorflow.python.saved_model import loader [as 别名]
# 或者: from tensorflow.python.saved_model.loader import load [as 别名]
def local_predict(args):
"""Runs prediction locally."""
sess = session.Session()
_ = loader.load(sess, [tag_constants.SERVING], args.model_dir)
# get the mappings between aliases and tensor names
# for both inputs and outputs
input_alias_map = json.loads(sess.graph.get_collection('inputs')[0])
output_alias_map = json.loads(sess.graph.get_collection('outputs')[0])
aliases, tensor_names = zip(*output_alias_map.items())
for input_file in args.input:
feed_dict = collections.defaultdict(list)
for line in tf_record.tf_record_iterator(input_file):
feed_dict[input_alias_map['examples_bytes']].append(line)
if args.dry_run:
print('Feed data dict %s to graph and fetch %s' % (
feed_dict, tensor_names))
else:
result = sess.run(fetches=tensor_names, feed_dict=feed_dict)
for row in zip(*result):
print(json.dumps(
{name: (value.tolist() if getattr(value, 'tolist', None) else value)
for name, value in zip(aliases, row)}))
示例6: get_meta_graph_def
# 需要导入模块: from tensorflow.python.saved_model import loader [as 别名]
# 或者: from tensorflow.python.saved_model.loader import load [as 别名]
def get_meta_graph_def(saved_model_dir, tag_set):
"""Gets MetaGraphDef from SavedModel.
Returns the MetaGraphDef for the given tag-set and SavedModel directory.
Args:
saved_model_dir: Directory containing the SavedModel to inspect or execute.
tag_set: Group of tag(s) of the MetaGraphDef to load, in string format,
separated by ','. For tag-set contains multiple tags, all tags must be
passed in.
Raises:
RuntimeError: An error when the given tag-set does not exist in the
SavedModel.
Returns:
A MetaGraphDef corresponding to the tag-set.
"""
saved_model = reader.read_saved_model(saved_model_dir)
set_of_tags = set(tag_set.split(','))
for meta_graph_def in saved_model.meta_graphs:
if set(meta_graph_def.meta_info_def.tags) == set_of_tags:
return meta_graph_def
raise RuntimeError('MetaGraphDef associated with tag-set ' + tag_set +
' could not be found in SavedModel')
示例7: load_session_bundle_or_saved_model_bundle_from_path
# 需要导入模块: from tensorflow.python.saved_model import loader [as 别名]
# 或者: from tensorflow.python.saved_model.loader import load [as 别名]
def load_session_bundle_or_saved_model_bundle_from_path(export_dir,
tags=None,
target="",
config=None):
"""Load session bundle from the given path.
The function reads input from the export_dir, constructs the graph data to the
default graph and restores the parameters for the session created.
Args:
export_dir: the directory that contains files exported by exporter.
tags: Set of string tags to identify the required MetaGraphDef when model is
saved as SavedModel. These should correspond to the tags used when
saving the variables using the SavedModel `save()` API.
target: The execution engine to connect to. See target in tf.Session()
config: A ConfigProto proto with configuration options. See config in
tf.Session()
Returns:
session: a tensorflow session created from the variable files.
meta_graph: a meta graph proto saved in the exporter directory.
Raises:
RuntimeError: if the required files are missing or contain unrecognizable
fields, i.e. the exported model is invalid.
"""
metagraph_def = None
sess = None
if loader.maybe_saved_model_directory(export_dir):
sess = session.Session(target, graph=None, config=config)
metagraph_def = loader.load(sess, tags, export_dir)
elif session_bundle.maybe_session_bundle_dir(export_dir):
sess, metagraph_def = _load_saved_model_from_session_bundle_path(export_dir,
target,
config)
else:
raise RuntimeError("SessionBundle or SavedModelBundle not found at "
"specified export location: %s" % export_dir)
return sess, metagraph_def
示例8: RunModel
# 需要导入模块: from tensorflow.python.saved_model import loader [as 别名]
# 或者: from tensorflow.python.saved_model.loader import load [as 别名]
def RunModel(saved_model_dir, signature_def_key, tag, text, ngrams_list=None):
saved_model = reader.read_saved_model(saved_model_dir)
meta_graph = None
for meta_graph_def in saved_model.meta_graphs:
if tag in meta_graph_def.meta_info_def.tags:
meta_graph = meta_graph_def
break
if meta_graph_def is None:
raise ValueError("Cannot find saved_model with tag" + tag)
signature_def = signature_def_utils.get_signature_def_by_key(
meta_graph, signature_def_key)
text = text_utils.TokenizeText(text)
ngrams = None
if ngrams_list is not None:
ngrams_list = text_utils.ParseNgramsOpts(ngrams_list)
ngrams = text_utils.GenerateNgrams(text, ngrams_list)
example = inputs.BuildTextExample(text, ngrams=ngrams)
example = example.SerializeToString()
inputs_feed_dict = {
signature_def.inputs["inputs"].name: [example],
}
if signature_def_key == "proba":
output_key = "scores"
elif signature_def_key == "embedding":
output_key = "outputs"
else:
raise ValueError("Unrecognised signature_def %s" % (signature_def_key))
output_tensor = signature_def.outputs[output_key].name
with tf.Session() as sess:
loader.load(sess, [tag], saved_model_dir)
outputs = sess.run(output_tensor,
feed_dict=inputs_feed_dict)
return outputs
示例9: testGraphWithoutVariables
# 需要导入模块: from tensorflow.python.saved_model import loader [as 别名]
# 或者: from tensorflow.python.saved_model.loader import load [as 别名]
def testGraphWithoutVariables(self):
export_dir = os.path.join(test.get_temp_dir(), "test_graph_has_variables")
builder = saved_model_builder.SavedModelBuilder(export_dir)
# Graph with no variables.
with self.test_session(graph=ops.Graph()) as sess:
constant_5_name = constant_op.constant(5.0).name
builder.add_meta_graph_and_variables(sess, ["foo"])
# Second graph with no variables
with self.test_session(graph=ops.Graph()) as sess:
constant_6_name = constant_op.constant(6.0).name
builder.add_meta_graph(["bar"])
# Save the SavedModel to disk.
builder.save()
# Restore the graph with tag "foo".
with self.test_session(graph=ops.Graph()) as sess:
loader.load(sess, ["foo"], export_dir)
# Read the constant a from the graph.
a = ops.get_default_graph().get_tensor_by_name(constant_5_name)
b = constant_op.constant(6.0)
c = a * b
self.assertEqual(30.0, sess.run(c))
# Restore the graph with tag "bar".
with self.test_session(graph=ops.Graph()) as sess:
loader.load(sess, ["bar"], export_dir)
# Read the constant a from the graph.
a = ops.get_default_graph().get_tensor_by_name(constant_6_name)
b = constant_op.constant(5.0)
c = a * b
self.assertEqual(30.0, sess.run(c))
示例10: testSaveAsText
# 需要导入模块: from tensorflow.python.saved_model import loader [as 别名]
# 或者: from tensorflow.python.saved_model.loader import load [as 别名]
def testSaveAsText(self):
export_dir = os.path.join(test.get_temp_dir(), "test_astext")
builder = saved_model_builder.SavedModelBuilder(export_dir)
# Graph with a single variable. SavedModel invoked to:
# - add with weights.
with self.test_session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
builder.add_meta_graph_and_variables(sess, ["foo"])
# Graph with the same single variable. SavedModel invoked to:
# - simply add the model (weights are not updated).
with self.test_session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 43)
builder.add_meta_graph(["bar"])
# Save the SavedModel to disk in text format.
builder.save(as_text=True)
# Restore the graph with tag "foo", whose variables were saved.
with self.test_session(graph=ops.Graph()) as sess:
loader.load(sess, ["foo"], export_dir)
self.assertEqual(
42, ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0].eval())
# Restore the graph with tag "bar", whose variables were not saved.
with self.test_session(graph=ops.Graph()) as sess:
loader.load(sess, ["bar"], export_dir)
self.assertEqual(
42, ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0].eval())
示例11: testAssets
# 需要导入模块: from tensorflow.python.saved_model import loader [as 别名]
# 或者: from tensorflow.python.saved_model.loader import load [as 别名]
def testAssets(self):
export_dir = os.path.join(test.get_temp_dir(), "test_assets")
builder = saved_model_builder.SavedModelBuilder(export_dir)
with self.test_session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
# Build an asset collection.
ignored_filepath = os.path.join(
compat.as_bytes(test.get_temp_dir()), compat.as_bytes("ignored.txt"))
file_io.write_string_to_file(ignored_filepath, "will be ignored")
asset_collection = self._build_asset_collection("hello42.txt",
"foo bar baz",
"asset_file_tensor")
builder.add_meta_graph_and_variables(
sess, ["foo"], assets_collection=asset_collection)
# Save the SavedModel to disk.
builder.save()
with self.test_session(graph=ops.Graph()) as sess:
foo_graph = loader.load(sess, ["foo"], export_dir)
self._validate_asset_collection(export_dir, foo_graph.collection_def,
"hello42.txt", "foo bar baz",
"asset_file_tensor:0")
ignored_asset_path = os.path.join(
compat.as_bytes(export_dir),
compat.as_bytes(constants.ASSETS_DIRECTORY),
compat.as_bytes("ignored.txt"))
self.assertFalse(file_io.file_exists(ignored_asset_path))
示例12: testCustomMainOp
# 需要导入模块: from tensorflow.python.saved_model import loader [as 别名]
# 或者: from tensorflow.python.saved_model.loader import load [as 别名]
def testCustomMainOp(self):
export_dir = os.path.join(test.get_temp_dir(), "test_main_op")
builder = saved_model_builder.SavedModelBuilder(export_dir)
with self.test_session(graph=ops.Graph()) as sess:
# Add `v1` and `v2` variables to the graph.
v1 = variables.Variable(1, name="v1")
ops.add_to_collection("v", v1)
v2 = variables.Variable(2, name="v2")
ops.add_to_collection("v", v2)
# Initialize another variable `v3` to 42.
v3 = variables.Variable(42, name="v3")
ops.add_to_collection("v", v3)
# Set up an assignment op to be run as part of the main_op.
with ops.control_dependencies([main_op.main_op()]):
add_v1_v2 = math_ops.add(v1._ref(), v2._ref())
custom_main_op = control_flow_ops.group(state_ops.assign(v3, add_v1_v2))
sess.run(custom_main_op)
builder.add_meta_graph_and_variables(
sess, ["foo"], main_op=custom_main_op)
# Save the SavedModel to disk.
builder.save()
with self.test_session(graph=ops.Graph()) as sess:
loader.load(sess, ["foo"], export_dir)
self.assertEqual(1, ops.get_collection("v")[0].eval())
self.assertEqual(2, ops.get_collection("v")[1].eval())
# Evaluates to the sum of the first two variables and assigned as part of
# the main_op, following a restore.
self.assertEqual(3, ops.get_collection("v")[2].eval())
示例13: testLegacyInitOp
# 需要导入模块: from tensorflow.python.saved_model import loader [as 别名]
# 或者: from tensorflow.python.saved_model.loader import load [as 别名]
def testLegacyInitOp(self):
export_dir = os.path.join(test.get_temp_dir(), "test_legacy_init_op")
builder = saved_model_builder.SavedModelBuilder(export_dir)
with self.test_session(graph=ops.Graph()) as sess:
# Add `v1` and `v2` variables to the graph.
v1 = variables.Variable(1, name="v1")
ops.add_to_collection("v", v1)
v2 = variables.Variable(2, name="v2")
ops.add_to_collection("v", v2)
# Initialize another variable `v3` to 42.
v3 = variables.Variable(42, name="v3", trainable=False, collections=[])
ops.add_to_collection("v", v3)
# Set up an assignment op to be run as part of the legacy_init_op.
assign_v3 = state_ops.assign(v3, math_ops.add(v1, v2))
legacy_init_op = control_flow_ops.group(assign_v3, name="legacy_init_op")
sess.run(variables.global_variables_initializer())
builder.add_meta_graph_and_variables(
sess, ["foo"], legacy_init_op=legacy_init_op)
# Save the SavedModel to disk.
builder.save()
with self.test_session(graph=ops.Graph()) as sess:
loader.load(sess, ["foo"], export_dir)
self.assertEqual(1, ops.get_collection("v")[0].eval())
self.assertEqual(2, ops.get_collection("v")[1].eval())
# Evaluates to the sum of the first two variables and assigned as part of
# the legacy_init_op, following a restore.
self.assertEqual(3, ops.get_collection("v")[2].eval())
示例14: testMultipleAssets
# 需要导入模块: from tensorflow.python.saved_model import loader [as 别名]
# 或者: from tensorflow.python.saved_model.loader import load [as 别名]
def testMultipleAssets(self):
export_dir = os.path.join(test.get_temp_dir(), "test_multiple_assets")
builder = saved_model_builder.SavedModelBuilder(export_dir)
with self.test_session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
# Build an asset collection specific to `foo` graph.
asset_collection = self._build_asset_collection("foo.txt", "content_foo",
"asset_file_tensor")
# Add the asset collection as part of the graph with tag "foo".
builder.add_meta_graph_and_variables(
sess, ["foo"], assets_collection=asset_collection)
with self.test_session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
# Build an asset collection specific to `bar` graph.
asset_collection = self._build_asset_collection("bar.txt", "content_bar",
"asset_file_tensor")
# Add the asset collection as part of the graph with tag "bar".
builder.add_meta_graph(["bar"], assets_collection=asset_collection)
# Save the SavedModel to disk.
builder.save()
# Check assets restored for graph with tag "foo".
with self.test_session(graph=ops.Graph()) as sess:
foo_graph = loader.load(sess, ["foo"], export_dir)
self._validate_asset_collection(export_dir, foo_graph.collection_def,
"foo.txt", "content_foo",
"asset_file_tensor:0")
# Check assets restored for graph with tag "bar".
with self.test_session(graph=ops.Graph()) as sess:
bar_graph = loader.load(sess, ["bar"], export_dir)
self._validate_asset_collection(export_dir, bar_graph.collection_def,
"bar.txt", "content_bar",
"asset_file_tensor:0")
示例15: testSaveAsText
# 需要导入模块: from tensorflow.python.saved_model import loader [as 别名]
# 或者: from tensorflow.python.saved_model.loader import load [as 别名]
def testSaveAsText(self):
export_dir = os.path.join(tf.test.get_temp_dir(), "test_astext")
builder = saved_model_builder.SavedModelBuilder(export_dir)
# Graph with a single variable. SavedModel invoked to:
# - add with weights.
with self.test_session(graph=tf.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
builder.add_meta_graph_and_variables(sess, ["foo"])
# Graph with the same single variable. SavedModel invoked to:
# - simply add the model (weights are not updated).
with self.test_session(graph=tf.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 43)
builder.add_meta_graph(["bar"])
# Save the SavedModel to disk in text format.
builder.save(as_text=True)
# Restore the graph with tag "foo", whose variables were saved.
with self.test_session(graph=tf.Graph()) as sess:
loader.load(sess, ["foo"], export_dir)
self.assertEqual(
42, tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)[0].eval())
# Restore the graph with tag "bar", whose variables were not saved.
with self.test_session(graph=tf.Graph()) as sess:
loader.load(sess, ["bar"], export_dir)
self.assertEqual(
42, tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)[0].eval())