How to run multiple graphs in a Session - Tensorflow API

saikishor picture saikishor · Oct 7, 2017 · Viewed 12.6k times · Source

Tensorflow API has provided few pre-trained models and allowed us to trained them with any dataset.

I would like to know how to initialize and use multiple graphs in one tensorflow session. I want to import two trained models in two graphs and utilize them for object detection, but I am lost in trying to run multiple graphs in one session.

Is there any particular method to work with multiple graphs in one session?.

Another issue is, even if I create two different sessions for 2 different graphs and try to work with them, I end up getting similar result in the second one as of first instantiated session .

Answer

golmschenk picture golmschenk · Oct 7, 2017

Each Session can only have a single Graph. That being said, depending on what you're specifically trying to do, you have a couple options.

The first option is to create two separate sessions and load one graph into each session, as explained in the documentation here. You mentioned you were getting unexpectedly similar results from each session with that approach, but without more details it's hard to figure out what the problem is in your case specifically. I would suspect either the same graph was loaded to each session or when you try to run the each session separately the same session is being run twice, but without more details it's hard to tell.

The second option is to load both graphs as subgraphs of the main session graph. You can create two scopes within the graph, and build the graph for each of the graphs you want to load within that scope. Then you can just treat them as independent graphs since there are no connections between them. When running normally graph global functions, you'll need to specify which scope those functions are applying to. For example, when preforming an update on one of the subgraphs with its optimizer, you'll need to get only the trainable variables for that subgraph's scope using something like what is shown in this answer.

Unless you explicitly need the two graphs to be able to interact in someway within the TensorFlow graph, I would recommend the first approach so that you don't need to jump through the extra hoops having the subgraphs will require (such as needing to filter which scope your working with at any given moment, and the possibility of graph global things being shared between the two).