我是 python 新手,我一直在尝试运行这段代码。但我不断收到此错误。
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"] )
我应该获取图像中对象的百分比,但我得到的是:
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
中的错误:
import tensorflow as tf
tf.compat.v1.Session()
即在上面的代码中,将此代码行 self.sess = tf.compat.v1.keras.backend.get_session()
替换为
self.sess = tf.compat.v1.Session()
引用:
关于python - 如何修复使用 TensorFlow 2.0 时“'RuntimeError: ` get_session”不可用的问题。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58556906/