palavi bhole
palavi bhole

Reputation: 111

How to fix 'RuntimeError: `get_session` is not available when using TensorFlow 2.0.'

I am new to python and I have be trying to run this code. But I keep getting this error.

from imageai.Detection import ObjectDetection
import os

execution_path = os.getcwd()

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()
self.sess = tf.compat.v1.keras.backend.get_session()
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "image.jpg"), output_image_path=os.path.join(execution_path , "imagenew.jpg"))

for eachObject in detections:
    print(eachObject["name"] , " : " , eachObject["percentage_probability"] )

I am supposed to get the percentage for the objects in the image but instead I am getting this:

Using TensorFlow backend. Traceback (most recent call last): File "detector.py", line 6, in detector = ObjectDetection() File "C:\Python36\lib\site-packages\imageai\Detection__init__.py", line 88, in init self.sess = K.get_session() File "C:\Python36\lib\site-packages\keras\backend\tensorflow_backend.py", line 379, in get_session 'get_session is not available ' RuntimeError: get_session is not available when using TensorFlow 2.0.

Upvotes: 11

Views: 21305

Answers (5)

Grace Ataguba
Grace Ataguba

Reputation: 1

Any one with this get_session. This worked for me and I would like to share. Replace the line self.sess = K.get_session() with self.sess = tf.compat.v1.InteractiveSession()

Upvotes: 0

Jason
Jason

Reputation: 3535

As Tensorflow website (https://www.tensorflow.org/guide/migrate) suggest about migrate, you could import tensorflow as following to keep compatibility when you have tensorflow 2.0 installed: import tensorflow.compat.v1 as tf

Upvotes: 0

Akib_Zahid
Akib_Zahid

Reputation: 154

'get_session' is not available when using tensorflow 2.0. It is available in tensorflow 1.14.0

Just write in the command line

pip install tensorflow==1.14.0

Upvotes: 1

DataFramed
DataFramed

Reputation: 1631

Answer: In TF 2.0 you should use tf.compat.v1.Session() instead of tf.Session() Use the following code to get rid of the error in Tensorflow2.0:

import tensorflow as tf

tf.compat.v1.Session()

i.e. in your code above, replace this line self.sess = tf.compat.v1.keras.backend.get_session()of code with

self.sess = tf.compat.v1.Session()

Reference:

  1. https://github.com/tensorflow/tensorflow/issues/26844#issuecomment-474038678

Upvotes: 10

Pomadomaphin
Pomadomaphin

Reputation: 116

Looks like someone else had the same issue. they didn't mark an answer though.

How to fix module 'tensorflow' has no attribute 'get_default_session'

The code there worked for me after downloading the model and a test image.

you might need the cudnn library https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html

The result should look like this

running resnet50 cocos model

Upvotes: 0

Related Questions