question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ImportError: cannot import name 'tf' from 'keras.backend'

See original GitHub issue

I am having problems to execute the pre-trained models:

import keras_segmentation

model = keras_segmentation.pretrained.pspnet_50_ADE_20K() 
out = model.predict_segmentation(
    inp="input_image.jpg",
    out_fname="out.png"
)
ImportError                               Traceback (most recent call last)
<ipython-input-22-1a69a6bbd448> in <module>
      1 print(keras.__version__)
----> 2 model = keras_segmentation.pretrained.pspnet_50_ADE_20K()

~/anaconda3/lib/python3.7/site-packages/keras_segmentation/pretrained.py in pspnet_50_ADE_20K()
     44     latest_weights =  keras.utils.get_file( "pspnet50_ade20k.h5" , model_url  )
     45 
---> 46     return model_from_checkpoint_path( model_config , latest_weights  )
     47 
     48 

~/anaconda3/lib/python3.7/site-packages/keras_segmentation/pretrained.py in model_from_checkpoint_path(model_config, latest_weights)
      7 def model_from_checkpoint_path( model_config , latest_weights  ):
      8 
----> 9         model = model_from_name[ model_config['model_class']  ]( model_config['n_classes'] , input_height=model_config['input_height'] , input_width=model_config['input_width'] )
     10         model.load_weights(latest_weights)
     11         return model

~/anaconda3/lib/python3.7/site-packages/keras_segmentation/models/pspnet.py in pspnet_50(n_classes, input_height, input_width)
    103 
    104 def pspnet_50( n_classes ,  input_height=473, input_width=473 ):
--> 105     from ._pspnet_2 import _build_pspnet
    106 
    107     nb_classes = n_classes

~/anaconda3/lib/python3.7/site-packages/keras_segmentation/models/_pspnet_2.py in <module>
     10 from keras.optimizers import SGD
     11 
---> 12 from keras.backend import tf as ktf
     13 import tensorflow as tf
     14 

ImportError: cannot import name 'tf' from 'keras.backend' (/home/alex/anaconda3/lib/python3.7/site-packages/keras/backend/__init__.py)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
sainatarajancommented, Oct 10, 2019

If anyone else is having this issue, just install via pip using this command: pip install git+https://github.com/divamgupta/image-segmentation-keras.git

0reactions
davidADSPcommented, Mar 1, 2020

Works with

tensorflow==2.1.0
keras==2.3.1
keras-segmentation==0.3.0

pip install keras-segmentation gives version 0.2.0 which is showing errors - use pip install git+https://github.com/divamgupta/image-segmentation-keras.git instead

Read more comments on GitHub >

github_iconTop Results From Across the Web

ImportError: cannot import name 'backend' - Stack Overflow
I encounter an ImportError when importing backend from keras from keras import backend. The output is. Using TensorFlow backend.
Read more >
How to avoid KERAS import errors in your notebooks - Kaggle
Recently I had an issue while importing KERAS libraries with issues such as. ImportError: cannot import name 'is_tf_type' from ...
Read more >
cannot import name get_config from tensorflow.python.eager ...
importerror: cannot import name get_config from tensorflow.python.eager.context error occurs because of incompatibility of keras module.
Read more >
Error using tensorflowjs_converter (ImportError: cannot import ...
Error using tensorflowjs_converter (ImportError: cannot import name model_fn) ... in <module> from tensorflow.contrib.tpu.python.tpu.keras_support import ...
Read more >
cannot import name 'export_saved_model - 腾讯云开发者社区
Import Error : cannot import name 'export_saved_model ... install\lib\site-packages\keras\backend\__init__.py", line 1, in <module> from ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found