image-segmentation-keras icon indicating copy to clipboard operation
image-segmentation-keras copied to clipboard

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

Open dennex opened this issue 4 years ago • 4 comments
trafficstars

Hello,

I tried running the colab example after changing the lines !pip install git+https://github.com/divamgupta/image-segmentation-keras to

!pip uninstall tensorflow tensorboard tensorboard-data-server tensorflow-datasets tensorflow-estimator tensorflow-gcs-config tensorflow-hub tensorflow-metadata tensorflow-probability keras keras-nightly Keras-Preprocessing keras-vis -y
!pip install tensorflow==2.4
!pip install git+https://github.com/divamgupta/image-segmentation-keras

as suggested in another post because colab updated their tensorflow to 2.5. The problem I'm getting is this error in the backend for vgg_unet call.

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

dennex avatar Aug 12 '21 23:08 dennex

Hi @dennex, I recently had the same issue after making these changes. I wanted to train a mobilenet_unet on my dataset. This suggested either

(i) upgrading the tensorflow version or (ii) modifying import statements.

Since the purpose of the suggested changes was to "downgrade" the tensorflow version intentionally, the first solution (upgrading version) would not be much of use for this case. Also, simply adding from tensorflow import keras to the colab example notebook did not work for me. I had to make this change in the code files and then it worked. To do so, I tried the following steps and it worked for me:

(1) cloned the repo on my local machine

(2) made the following changes to the relevant code files (this step should be made in those model files you want to work with):

from keras.models.import * -> from tensorflow.keras.models import *
import keras.backend as K -> import tensorflow.keras.backend as K
keras.utils.get_file -> tensorflow.keras.utils.get_file    

(and so on )

(3) uploaded the code folder along with the dataset in the suggested structure onto my google drive

(4) mounted the drive on a fresh colab notebook

(5) %cd path to image-segmentation-keras

(6) added the rest of the code provided in the example notebook to my notebook

from keras_segmentation.models.unet import mobilenet_unet
model = mobilenet_unet(n_classes ,  input_height, input_width)
model.train(train_images, train_annotations, checkpoints_path, epochs)

I was able to train the model and run inference after these changes. Hope this helps!

mihirm05 avatar Aug 13 '21 07:08 mihirm05

I had the same issue. I used the exact same tensorflow and keras version suggested in homepage of this library. Keras ( recommended version : 2.4.3 ) and Tensorflow ( recommended version : 2.4.1 ) solved the issue.

dippatel1994 avatar Aug 16 '21 18:08 dippatel1994

Thanks, I just uninstalled/reinstalled Keras 2.4.3 and tensorflow 2.4.1 and it works! Great library!

dennex avatar Aug 17 '21 20:08 dennex

I do not have a CUDA 11 compatible GPU card. Thus, I have to use TF<2.4.3. (I prefer to use the GPU) I'm using model = pspnet_50_ADE_20K() By @mihirm05 suggestion, I tried changing keras. to tensorflow.keras everywhere, but in keras_segmentation/models/_pspnet_2.py in line 9 --> from tensorflow.keras.layers.merge import Concatenate, Add gives a ModuleNotFoundError: No module named 'tensorflow.keras.layers.merge', and removing tensorflow gives initial error (which we were getting without putting tensorflow.). ~~So, any solution around this?~~

Solution : Change from tensorflow.keras.layers.merge import Concatenate, Add to from tensorflow.keras.layers import Concatenate, Add.

varungupta31 avatar Apr 12 '22 07:04 varungupta31