How to use K.get_session in Tensorflow 2.0 or how to migrate it?

bylukas picture bylukas · Oct 6, 2019 · Viewed 20.1k times · Source
def __init__(self, **kwargs):
    self.__dict__.update(self._defaults) # set up default values
    self.__dict__.update(kwargs) # and update with user overrides
    self.class_names = self._get_class()
    self.anchors = self._get_anchors()
    self.sess = K.get_session()

RuntimeError: get_session is not available when using TensorFlow 2.0.

Answer

donglinjy picture donglinjy · Oct 18, 2019

Tensorflow 2.0 does not expose the backend.get_session directly any more but the code still there and expose for tf1.

https://github.com/tensorflow/tensorflow/blob/r2.0/tensorflow/python/keras/backend.py#L465

You can use it with tf1 compatible interface:

sess = tf.compat.v1.keras.backend.get_session()

Or import tenforflow backend with internal path:

import tensorflow.python.keras.backend as K
sess = K.get_session()