我是 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"] )
Run Code Online (Sandbox Code Playgroud)
我应该得到图像中对象的百分比,但我得到的是:
使用 TensorFlow 后端。回溯(最近一次调用):文件“detector.py”,第 6 行,在检测器 = ObjectDetection() 文件“C:\Python36\lib\site-packages\imageai\Detection__init__.py”,第 88 行,在init self .sess = K.get_session() File "C:\Python36\lib\site-packages\keras\backend\tensorflow_backend.py", line 379, in get_session '
get_sessionis not available ' RuntimeError:get_sessionis 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()
Run Code Online (Sandbox Code Playgroud)
即在上面的代码中,将这行self.sess = tf.compat.v1.keras.backend.get_session()代码替换为
self.sess = tf.compat.v1.Session()
Run Code Online (Sandbox Code Playgroud)
参考:
看起来其他人也有同样的问题。但他们没有标记答案。
如何修复模块“tensorflow”没有属性“get_default_session”
下载模型和测试图像后,那里的代码对我有用。
您可能需要 cudnn 库https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html
结果应该是这样的