I have 2 ProtoBuf Files, I currently load and forward pass each of them separately, by calling-
out1=session.run(graph1out, feed_dict={graph1inp:inp1})
followed by
final=session.run(graph2out, feed_dict={graph2inp:out1})
where graph1inp and graph1out are input node and output node of graph 1 and similar terminology for graph 2
Now, I want to connect graph1out with graph2inp such that I only have to run graph2out while feeding graph1inp with inp1. In other words connecting the input and output tensors of the 2 involved graphs in such a way that one run is sufficient to run inference on both trained ProtoBuf files.
Accepted answer does connect two graphs, however it does not restore the collections, global and trainable variables. After an exhaustive search I came to a better solution:
We use
tf.train.export_meta_graph
that exports also CollectionDef andmeta_graph.import_scoped_meta_graph
to import it. Here is where the connection happens, specifically ininput_map
parameter.Now graph is connected as well as global variables are being re-mapped.
You can also import meta graphs directly from a file.
Assuming that your Protobuf files contain serialized
tf.GraphDef
protos, you can use theinput_map
argument oftf.import_graph_def()
to connect the two graphs: