TensorFlow Loads Two Graphs

Load Two Graphs

We can only have one default graph in one TensorFlow session. To open and execute two graphs in one script, we should load two graphs and then create sessions based on these graphs separately.

Firstly, create two graphs:

with tf.Graph().as_default() as wav2letter_graph:
    tf.import_graph_def(load_graph(wav2letter_graph_file), name="")

with tf.Graph().as_default() as deep_speech_graph:
    tf.import_graph_def(load_graph(deep_speech_graph_file), name="")

Then create two sessions and inference in these sessions respectively:

with tf.Session(graph=wav2letter_graph) as sess:
    # Do something
with tf.Session(graph=deep_speech_graph) as sess:
    # Do something

Difference Between Graph and GraphDef

# Read GraphDef from protobuf file
with tf.gfile.GFile(graph_def_pb_file, "rb") as f:
    graph_def = tf.GraphDef()

# Import GraphDef as the default graph
with tf.Graph().as_default() as graph:
    tf.import_graph_def(graph_def, name="")