Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/trackable/asset.py: 58%
38 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
1# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Asset-type Trackable object."""
16import os
18from tensorflow.python.eager import context
19from tensorflow.python.framework import dtypes
20from tensorflow.python.framework import ops
21from tensorflow.python.framework import tensor_conversion_registry
22from tensorflow.python.lib.io import file_io
23from tensorflow.python.ops import array_ops
24from tensorflow.python.ops import resource_variable_ops
25from tensorflow.python.saved_model import path_helpers
26from tensorflow.python.trackable import base
27from tensorflow.python.util.tf_export import tf_export
30@tf_export("saved_model.Asset")
31class Asset(base.Trackable):
32 """Represents a file asset to hermetically include in a SavedModel.
34 A SavedModel can include arbitrary files, called assets, that are needed
35 for its use. For example a vocabulary file used initialize a lookup table.
37 When a trackable object is exported via `tf.saved_model.save()`, all the
38 `Asset`s reachable from it are copied into the SavedModel assets directory.
39 Upon loading, the assets and the serialized functions that depend on them
40 will refer to the correct filepaths inside the SavedModel directory.
42 Example:
44 ```
45 filename = tf.saved_model.Asset("file.txt")
47 @tf.function(input_signature=[])
48 def func():
49 return tf.io.read_file(filename)
51 trackable_obj = tf.train.Checkpoint()
52 trackable_obj.func = func
53 trackable_obj.filename = filename
54 tf.saved_model.save(trackable_obj, "/tmp/saved_model")
56 # The created SavedModel is hermetic, it does not depend on
57 # the original file and can be moved to another path.
58 tf.io.gfile.remove("file.txt")
59 tf.io.gfile.rename("/tmp/saved_model", "/tmp/new_location")
61 reloaded_obj = tf.saved_model.load("/tmp/new_location")
62 print(reloaded_obj.func())
63 ```
65 Attributes:
66 asset_path: A path, or a 0-D `tf.string` tensor with path to the asset.
67 """
69 def __init__(self, path):
70 """Record the full path to the asset."""
71 if isinstance(path, os.PathLike):
72 path = os.fspath(path)
73 # The init_scope prevents functions from capturing `path` in an
74 # initialization graph, since it is transient and should not end up in a
75 # serialized function body.
76 with ops.init_scope(), ops.device("CPU"):
77 self._path = ops.convert_to_tensor(
78 path, dtype=dtypes.string, name="asset_path")
80 @property
81 def asset_path(self):
82 """Fetch the current asset path."""
83 return self._path
85 @classmethod
86 def _deserialize_from_proto(cls, object_proto, export_dir, asset_file_def,
87 **unused_kwargs):
88 proto = object_proto.asset
89 filename = file_io.join(
90 path_helpers.get_assets_dir(export_dir),
91 asset_file_def[proto.asset_file_def_index].filename)
92 asset = cls(filename)
93 if not context.executing_eagerly():
94 ops.add_to_collection(ops.GraphKeys.ASSET_FILEPATHS, asset.asset_path)
95 return asset
97 def _add_trackable_child(self, name, value):
98 setattr(self, name, value)
100 def _export_to_saved_model_graph(self, tensor_map, **unused_kwargs):
101 # TODO(b/205008097): Instead of mapping 1-1 between trackable asset
102 # and asset in the graph def consider deduping the assets that
103 # point to the same file.
104 asset_path_initializer = array_ops.placeholder(
105 shape=self.asset_path.shape,
106 dtype=dtypes.string,
107 name="asset_path_initializer")
108 asset_variable = resource_variable_ops.ResourceVariable(
109 asset_path_initializer)
111 tensor_map[self.asset_path] = asset_variable
112 return [self.asset_path]
115tensor_conversion_registry.register_tensor_conversion_function(
116 Asset, lambda asset, **kw: ops.convert_to_tensor(asset.asset_path, **kw))