Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 805
palavi bhole Avatar asked Oct 25 '19 10:10

palavi bhole


2 Answers

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
like image 131
DataFramed Avatar answered Oct 19 '22 18:10

DataFramed


'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
like image 1
Akib_Zahid Avatar answered Oct 19 '22 17:10

Akib_Zahid