python - 如何修复使用 TensorFlow 2.0 时“'RuntimeError: ` get_session”不可用的问题。

标签 python tensorflow

我是 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()

引用:

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

关于python - 如何修复使用 TensorFlow 2.0 时“'RuntimeError: ` get_session”不可用的问题。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58556906/

相关文章:

tensorflow - 没有简单的方法将 Tensorboard 输出添加到预定义的估计器函数 DnnClassifier?

tensorflow - 什么是 tensorflow.python.data.ops.dataset_ops.PrefetchDataset?

python - 如何在python中制作函数列表?

python - 将 Glue ETL 作业加载到雪花中时出错

python - Delphi 或 C 代码与 Python 脚本之间的桥梁

python - 如何将Dense层参数的数据类型设置为float16?

tensorflow - tensorflow 中 Masking() Keras 函数的等效性是什么?还有batch norm、conv、relu支持Masking吗?

python - numpy.linalg.norm 的作用是什么?

python - 当 Python 处于优化模式时,如何对某些行为进行单元测试?

python - 是否可以将 python 及其必要的库导出到与环境无关的文件中?