2

I'm trying to follow this repo's tutorial on colabhttps://github.com/divamgupta/image-segmentation-keras

but I'm getting this error again and again

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

I googled it a lot so I found some solutions that solved same problems for them, like upgrade tensorflow version or using from tensorflow import keras instead of import keras

or add this code snippet

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

But none of them worked!

Full error codes are as follow

---------------------------------------------------------------------------
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.
---------------------------------------------------------------------------

Full codes are as follow

!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')

I would appreciate any of your help thanks in advance

z2ouu
  • 41
  • 1
  • 10
  • I use mac and run your code and don't get errors. – I'mahdi Aug 08 '21 at 08:40
  • I ran my code on google colab, does it affects in anyway? I tried to run it on local, but I got another installing error on tensorflow. And I'm using m1 macbook pro – z2ouu Aug 08 '21 at 08:44
  • I run on Jupyter Notebook – I'mahdi Aug 08 '21 at 08:46
  • I'm so sorry to bother you, but what is your tensorflow and keras version? I've been trying this till now on my local but I failed :( – z2ouu Aug 08 '21 at 15:12
  • I haven't TensorFlow and Keras before, I install them with your code – I'mahdi Aug 08 '21 at 15:53
  • 1
    Have you tried all suggestions from: https://stackoverflow.com/questions/66964492/importerror-cannot-import-name-get-config-from-tensorflow-python-eager-conte ? – draganstankovic Aug 10 '21 at 06:11
  • yeah, as you can see in my full code, I tried some of the suggestions that match with my problem.. But it didn't helped – z2ouu Aug 11 '21 at 15:16
  • Actually I solved it by myself yesterday, and it was just a matter of version with tensorflow and keras. I looked into traceback tensorflow error messages and opend it and changed 'import keras' to 'from tensorflow import keras' – z2ouu Aug 11 '21 at 15:18

1 Answers1

1

From comments

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).