cmsml.tensorflow

Classes, functions and tools for efficiently working with TensorFlow.

Functions:

import_tf([log_level, autograph_verbosity])

Imports TensorFlow and returns a 3-tuple containing the module itself, the v1 compatibility API (i.e.

save_graph(path, obj[, …])

Extracts a TensorFlow graph from an object obj and saves it at path.

load_graph(path[, create_session, …])

Reads a saved TensorFlow graph from path and returns it.

write_graph_summary(graph, summary_dir, **kwargs)

Writes the summary of a graph to a directory summary_dir using a tf.summary.FileWriter (v1) or tf.summary.create_file_writer (v2).

import_tf(log_level='WARNING', autograph_verbosity=3)[source]

Imports TensorFlow and returns a 3-tuple containing the module itself, the v1 compatibility API (i.e. the TensorFlow module itself if v1 is the primarily installed version), and the package version as a 3-tuple containing strings. Example:

tf, tf1, tf_version = import_tf()

At some point in the future, when v1 support might get fully removed from TensorFlow 2 or higher, the second tuple element might be None.

The verbosity of logs printed by TensorFlow and AutoGraph can be controlled through log_level and autograph_verbosity.

save_graph(path, obj, variables_to_constants=False, output_names=None, *args, **kwargs)[source]

Extracts a TensorFlow graph from an object obj and saves it at path. The graph is optionally transformed into a simpler representation with all its variables converted to constants when variables_to_constants is True. The saved file contains the graph as a protobuf. The accepted types of obj greatly depend on the available API versions.

When the v1 API is found (which is also the case when tf.compat.v1 is available in v2), Graph, GraphDef and Session objects are accepted. However, when variables_to_constants is True, obj must be a session and output_names should refer to names of operations whose subgraphs are extracted (usually just one).

For TensorFlow v2, obj can also be a compiled keras model, or either a polymorphic or concrete function as returned by tf.function. Polymorphic functions either must have a defined input signature (tf.function(input_signature=(...,))) or they must accept no arguments in the first place. See the TensorFlow documentation on concrete functions for more info.

args and kwargs are forwarded to tf.train.write_graph (v1) or tf.io.write_graph (v2).

load_graph(path, create_session=None, session_kwargs=None, as_text=None)[source]

Reads a saved TensorFlow graph from path and returns it. When create_session is True, a session object (compatible with the v1 API) is created and returned as the second value of a 2-tuple. The default value of create_session is True when TensorFlow v1 is detected, and False otherwise. In case a session is created, session_kwargs are forwarded to the session constructor as keyword arguments when set. When as_text is either True or None, and the file extension is ".pbtxt" or ".pb.txt", the content of the file at path is expected to be a human-readable text file. Otherwise, it is read as a binary protobuf file. Example:

graph = load_graph("path/to/model.pb", create_session=False)

graph, session = load_graph("path/to/model.pb", create_session=True)
write_graph_summary(graph, summary_dir, **kwargs)[source]

Writes the summary of a graph to a directory summary_dir using a tf.summary.FileWriter (v1) or tf.summary.create_file_writer (v2). This summary can be used later on to visualize the graph via tensorboard. graph can be either a graph object or a path to a protobuf file. In the latter case, load_graph() is used and all kwargs are forwarded.

Note

When used with TensorFlow v1, eager mode must be disabled.