无法从 'tensorflow.python.eager.context' 导入名称 'get_config'?

cannot import name 'get_config' from 'tensorflow.python.eager.context'?

我正尝试在 colabhttps://github.com/divamgupta/image-segmentation-keras

上学习这个 repo 的教程

但我一次又一次地收到此错误

cannot import name 'get_config' from 'tensorflow.python.eager.context' (/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/context.py)

我在谷歌上搜索了很多,所以我找到了一些为他们解决同样问题的解决方案,比如升级 tensorflow 版本或使用 from tensorflow import keras 而不是 import keras

或添加此代码段

!pip install --upgrade tensorflow
!pip install --upgrade tensorflow-gpu

但是其中 none 成功了!

完整错误代码如下

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-10-5c4963dc4fff> in <module>()
      1 from tensorflow import keras
      2 from tensorflow.keras.preprocessing import image
----> 3 from keras_segmentation.models.unet import vgg_unet
      4 
      5 model = vgg_unet(n_classes=50 ,  input_height=320, input_width=640  )

3 frames
/usr/local/lib/python3.7/dist-packages/keras_segmentation/models/unet.py in <module>()
----> 1 from keras.models import *
      2 from keras.layers import *
      3 
      4 from .config import IMAGE_ORDERING
      5 from .model_utils import get_segmentation_model

/usr/local/lib/python3.7/dist-packages/keras/__init__.py in <module>()
     23 
     24 # See b/110718070#comment18 for more details about this import.
---> 25 from keras import models
     26 
     27 from keras.engine.input_layer import Input

/usr/local/lib/python3.7/dist-packages/keras/models.py in <module>()
     17 
     18 import tensorflow.compat.v2 as tf
---> 19 from keras import backend
     20 from keras import metrics as metrics_module
     21 from keras import optimizer_v1

/usr/local/lib/python3.7/dist-packages/keras/backend.py in <module>()
     35 from tensorflow.python.distribute import distribute_coordinator as dc
     36 from tensorflow.python.distribute import distribute_coordinator_context as dc_context
---> 37 from tensorflow.python.eager.context import get_config
     38 from tensorflow.python.framework import config
     39 from keras import backend_config

ImportError: cannot import name 'get_config' from 'tensorflow.python.eager.context' (/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/context.py)

---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------

完整代码如下

!pip install keras-segmentation
!pip install keras==2.4.3
#!pip install tensorflow==2.4.1
!pip install tensorflow==2.5.0
!apt-get install -y libsm6 libxext6 libxrender-dev
!pip install opencv-python

!pip install --upgrade tensorflow
!pip install --upgrade tensorflow-gpu

! wget https://github.com/divamgupta/datasets/releases/download/seg/dataset1.zip && unzip dataset1.zip

from tensorflow import keras
from tensorflow.keras.preprocessing import image
from keras_segmentation.models.unet import vgg_unet

model = vgg_unet(n_classes=50 ,  input_height=320, input_width=640  )

model.train(
    train_images =  "dataset1/images_prepped_train/",
    train_annotations = "dataset1/annotations_prepped_train/",
    checkpoints_path = "/tmp/vgg_unet_1" , epochs=5  
)

out = model.predict_segmentation(
    inp="dataset1/images_prepped_test/0016E5_07965.png",
    out_fname="/tmp/out.png"
)

%matplotlib inline

import matplotlib
import matplotlib.pyplot as plt

plt.imshow(out)

from IPython.display import Image
Image('/tmp/out.png')

o = model.predict_segmentation(
    inp="dataset1/images_prepped_test/0016E5_07965.png",
    out_fname="/tmp/out.png" , overlay_img=True, show_legends=True,
    class_names = [ "Sky",    "Building", "Pole","Road","Pavement","Tree","SignSymbol", "Fence", "Car","Pedestrian", "Bicyclist"]

)

from IPython.display import Image
Image('/tmp/out.png')

如果您能提供帮助,我将不胜感激 提前致谢

来自评论

It was just a matter of version with tensorflow and keras. I looked into traceback tensorflow error messages and opened it and changed import keras to from tensorflow import keras issue was resolved (Paraphrased from z2ouu).