如何修复“运行时错误:使用 TensorFlow 2.0 时‘get_session’不可用." [英] How to fix 'RuntimeError: `get_session` is not available when using TensorFlow 2.0.'
问题描述
我是 Python 新手,我一直在尝试运行此代码.但我不断收到此错误.
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:
使用 TensorFlow 后端.回溯(最近一次通话):文件detector.py",第 6 行,在检测器 = ObjectDetection() 文件C:\Python36\lib\site-packages\imageai\Detection__init__.py",行88、在初始化self.sess = K.get_session() 文件 "C:\Python36\lib\site-packages\keras\backend\tensorflow_backend.py",第 379 行,在 get_session 中get_session
不可用"运行时错误:get_session
在使用 TensorFlow 2.0 时不可用.
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.
推荐答案
答案:在 TF 2.0 中,您应该使用 tf.compat.v1.Session()
而不是 tf.Session()
使用以下代码去除Tensorflow2.0
中的错误:
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()
即在上面的代码中,将代码的这一行 self.sess = tf.compat.v1.keras.backend.get_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()
参考:
这篇关于如何修复“运行时错误:使用 TensorFlow 2.0 时‘get_session’不可用."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!