# See the License for the specific language governing permissions and # limitations under the License. """Contains TensorFlow or Google-specific utilities for Gin configuration.""" import os from gin import config import tensorflow as tf # pylint: disable=g-direct-tensorflow-import from tensorflow.core.framework import summary_pb2 # pylint: enable=g-direct-tensorflow-import # Register TF file reader for Gin's parse_config_file. config.register_file_reader(tf.io.gfile.GFile, tf.io.gfile.exists) @config.configurable def singleton_per_graph(constructor): key = (config.current_scope_str(), tf.compat.v1.get_default_graph()) return config.singleton_value(key, constructor) class GinConfigSaverHook(tf.estimator.SessionRunHook): """A SessionRunHook that saves and summarizes the operative config. This hook will save Gin's operative configuration to a specified directory, as well as optionally summarizing it so that it will appear in TensorBoard's "Text" tab.
treated as a Python package and the filename as a gin file. Path to where the package is located on the fileystem is determined and then joined with the filename. `importlib.resources` was not used because it is new in python 3.7. Args: config_path: String path to the gin file, e.g. /foo/bar/config/walker2d.gin. Returns: Absolute path to gin config in python system path. Raises: ValueError: if the package cannot be loaded. """ head, filename = os.path.split(config_path) pkg = head.replace('/', '.') # importlib.util.find_spec is valid for python >= 3.4. spec = importlib.util.find_spec(pkg) # type: ignore if spec is None: raise ValueError('Package not found', pkg) file_sys_path = spec.origin # file_sys_path often ends with __init__.py. path = os.path.join(os.path.dirname(file_sys_path), filename) return path # Register TF file reader for Gin's parse_config_file. config.register_file_reader(system_path_reader, system_path_file_exists)
from __future__ import absolute_import from __future__ import division from __future__ import print_function import os from gin import config import tensorflow as tf # pylint: disable=g-direct-tensorflow-import from tensorflow.core.framework import summary_pb2 # pylint: enable=g-direct-tensorflow-import # Register TF file reader for Gin's parse_config_file. config.register_file_reader(tf.gfile.Open, tf.gfile.Exists) @config.configurable def singleton_per_graph(constructor): key = (config.current_scope_str(), tf.get_default_graph()) return config.singleton_value(key, constructor) class GinConfigSaverHook(tf.train.SessionRunHook): """A SessionRunHook that saves and summarizes the operative config. This hook will save Gin's operative configuration to a specified directory, as well as optionally summarizing it so that it will appear in TensorBoard's "Text" tab.
def __init__(self, *args, **kwargs): super(ResourceReaderTest, self).__init__(*args, **kwargs) config.register_file_reader(resource_reader.system_path_reader, resource_reader.system_path_file_exists)