ML tips -Working with multiple models in Keras TensorFlow graphs

Privalov Vladimir
1 min readSep 13, 2019

--

Here is my first article in a serie of notes on machine Learning.

If you are not familiar how TensorFlow graphs and session work you can be confused how working with Keras models behaves sometimes. Working with multiple models can be such a case.

Briefly TensorFlow represents computations performed in TF models (NNs) as a graph of individual operations under the hood. Computations are executed on the graph when the graph executed in a session. You can read more on this topic here.

Let’s imagine we are working with two different classes. Both classes use Keras models: create CNN models, load weights etc. When loading weights for model in second class we can get such an error:

Error Tensor(“norm_layer/l2_normalize:0”, shape=(?, 128), dtype=float32) is not an element of this graph

The problem is Keras shares a global session between models if no default TF session provided.

Solution is simple:

self.graph = tf.Graph()with self.graph.as_default():
self.session = tf.Session(graph=self.graph)
with self.session.as_default():
self.model = WideResNet(face_size, depth=depth, k=width)()
model_dir = <model_path>
...
self.model.load_weights(fpath)

Here we create new TensorFlow Graph and Session and run loading model inside this new TF session.

When performing prediction use this code:

with self.graph.as_default():
with self.session.as_default():
result = self.model.predict(np.expand_dims(img, axis=0), batch_size=1)

Simply run prediction inside the TF Session initialized before. That’s it. Enjoy Machine Learning with TensorFlow and good luck.

--

--

Responses (2)