ImportError: cannot import name '_obtain_input_shape' from keras - keras

In Keras,
I'm trying to import _obtain_input_shape as follows:
from keras.applications.imagenet_utils import _obtain_input_shape
However, I get the following error:
ImportError: cannot import name '_obtain_input_shape'
The reason I'm trying to import _obtain_input_shape is so that I can determine the input shape(so as to load VGG-Face as follows :
I'm using it to determine the correct input shape of the input tensor as follow:
input_shape = _obtain_input_shape(input_shape,
default_size=224,
min_size=48,
data_format=K.image_data_format(),
require_flatten=include_top)`
Please assist?
Thanks in advance.

You don't have to downgrade Keras 2.2.2.
In Keras 2.2.2 there is no _obtain_input_shape method in the keras.applications.imagenet_utils module. You can find it under keras-applications with the modul name keras_applications (underscore).
So you don't have to downgrade your Keras to 2.2.0 just change:
from keras.applications.imagenet_utils import _obtain_input_shape
to
from keras_applications.imagenet_utils import _obtain_input_shape

I have found a method that works well. You just use
from keras_applications.imagenet_utils import _obtain_input_shape
Notice: It is keras_applications instead of keras.application.

This issue occured because of the version of keras.
In my case, I was downgrade keras 2.2.2 to 2.2.0, and the problem was solved.

In Colab I solved it by importing Keras and installing :
import keras
!pip install keras_applications
from keras_applications.imagenet_utils import _obtain_input_shape

keras_applications.imagenet_utils is deprecated
Traceback (most recent call last):
File "inception_v3.py", line 36, in
from keras_applications.imagenet_utils import _obtain_input_shape
ModuleNotFoundError: No module named 'keras_application

from keras.applications.imagenet_utils import obtain_input_shape
Not _obtain_input_shape. This works fine with keras==2.5.0rc0 (pip install keras==2.5.0rc0)

for keras 2.2.4:
Change the line like below to make it work.
from keras_applications.imagenet_utils import _obtain_input_shape
Note: It is importing from keras_applications and does not from keras.applications as before.

Related

module 'tensorflow_core.compat.v2' has no attribute '__internal__'

I am using python 3.6.9 , keras 2.3.1, and (tf 2.0.0 or tf 2.1.0).
Got the compatibility versions from this table.
When I import keras I get this error.
Error:
AttributeError: module 'tensorflow_core.compat.v2' has no attribute '__internal__'
Solution:
Install Libraries
!pip install tensorflow==2.1
!pip install keras==2.3.1
Import
from tensorflow import keras
I was able to replicate your issue with the below combination as shown below
!pip install tensorflow==2.1.0
!pip install keras==2.3.1
import keras
Output:
Using TensorFlow backend.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-3-88d96843a926> in <module>()
----> 1 import keras
8 frames
/usr/local/lib/python3.7/dist-packages/keras/initializers/__init__.py in populate_deserializable_objects()
47
48 LOCAL.ALL_OBJECTS = {}
---> 49 LOCAL.GENERATED_WITH_V2 = tf.__internal__.tf2.enabled()
50
51 # Compatibility aliases (need to exist in both V1 and V2).
AttributeError: module 'tensorflow_core.compat.v2' has no attribute '__internal__'
Fixed code:
Here issue is due to incompatibility between TF2.1 and Keras 2.3.1. Your issue can be resolved in two ways
Upgrade to Keras2.5.0rc0 and import keras
import keras from tensorflow as from tensorflow import keras
I faced a similar error while trying to import Tokenizer from keras.preprocessing
This caused error -
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
This fixed it-
from tensorflow.keras import preprocessing
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences

ImportError: cannot import name 'deprecated' when import gensim

everyone!
I'm trying to import gensim on jupyter but I got the following error:
ImportError: cannot import name 'deprecated'
What can I do?
PS: I tried in gensim version 3.8.1 and 3.8.3

How to import WordEmbeddingSimilarityIndex function from gensim module?

When i try to import WordEmbeddingSimilarityIndex, it's giving me the following error:
>> from gensim.models import WordEmbeddingSimilarityIndex
ImportError: cannot import name 'WordEmbeddingSimilarityIndex
The same issue occurs for SparseTermSimilarityMatrix function:
>> from gensim.similarities import SparseTermSimilarityMatrix
ImportError: cannot import name 'SparseTermSimilarityMatrix
Note: I have installed and imported gensim, gensim.models and gensim.similarities. But still it's giving me the ImportError while importing the above mentioned functions.
Can you tell me what I am doing wrong, please?
Fix is change "models" to "similarities"
from gensim.similarities import WordEmbeddingSimilarityIndex
it works in gensim 4.0.1
Try to check the version of gensim that you are using. Usually, the older versions of gensim cause this issue.
from gensim.models import WordEmbeddingSimilarityIndex
print(gensim.__version__)
if the gensim version is 3.6.x or older update it to 3.7.x or latest version by running the below command. Once you update gensim version should get rid of this issue.
pip install --upgrade gensim

Importing conv_block from resnet50

I cant seem to import identity_block and conv_block from the resnet architecture.
https://github.com/keras-team/keras-applications/blob/master/keras_applications/resnet50.py
from keras.applications.resnet50 import ResNet50,decode_predictions,identity_block, conv_block
It"s resulting in an import error.
from keras.applications.resnet50 import ResNet50,decode_predictions,identity_block, conv_block
ImportError: cannot import name 'identity_block'
You can use the following code, it is inside resnet50.
from keras.applications.resnet50 import ResNet50,decode_predictions,resnet50
identity_block, conv_block = resnet50.identity_block, resnet50.conv_block
I got the following error
ImportError: cannot import name 'resnet50' from 'keras.applications.resnet50' (/home/mike/miniconda3/lib/python3.7/site-packages/keras/applications/resnet50.py)
The official Keras library has a big change and can not be called directly by the client script in the case you mentioned. I have met the same issue and solved it as follows.
1. Download keras.applications and put keras_applications into the current directory
It is called as a library by the client script.
2. Make an independent script of resnet50_custom.py
change the original import statment
from keras.applications.imagenet_utils import _obtain_input_shape
to the new statements as follows
from keras_applications.imagenet_utils import _obtain_input_shape
3. Add the following import statement into the client script.
from resnet50_custom import identity_block, conv_block
4. Change "include_top=include_top" to "require_flatten=include_top"
input_shape = _obtain_input_shape(input_shape,default_size=224, \
min_size=197, data_format=None, require_flatten=include_top)
Cheers.

ImportError: No module named keras.preprocessing

Following the tutorial:
http://www.pyimagesearch.com/2016/08/10/imagenet-classification-with-python-and-keras/#comment-419896
Using these files:
https://github.com/fchollet/deep-learning-models
I get 2 separate errors depending on how I execute:
Running in PyCharm:
Using TensorFlow backend.
usage: test_imagenet.py [-h] -i IMAGE
test_imagenet.py: error: the following arguments are required: -i/--image
Running in cmd line:
C:\Users\AppData\Local\Programs\Python\Python35\Scripts>python deep-learning-models/test_imagenet.py --image deep-learning-models/images/dog.jpg
Traceback (most recent call last):
File "deep-learning-models/test_imagenet.py", line 2, in <module>
from keras.preprocessing import image as image_utils
ImportError: No module named keras.preprocessing
How do I resolve?
Its best if you solve this problem outside running the above script... Here is what you can try in your command line environment to make sure it works outside your script:
>>> import keras
Using TensorFlow backend.
>>> keras.__version__
'1.2.1'
>>> keras.preprocessing
<module 'keras.preprocessing' from '/usr/local/lib/python2.7/dist-packages/keras/preprocessing/__init__.pyc'>
>>> from keras.preprocessing import image as image_utils
>>>
Make sure you have latest version of keras installed. If you get above working then it could be the environment issue where above script is not able to find the keras package. However if above does not work or work partially you would need to install keras again by removing it first..
$ pip install keras --user
Every dependency in a python project need to be installed using pip or easy_install or from the source code. You will have to install the keras module as mentioned here.
This happened to me. It turned out I was working in a pyvenv which wasn't activated. Just run source bin/activate on Linux/Mac or Scripts\activate.bat on Windows
from keras.models import Sequential
from keras import legacy_tf_layer
from keras.preprocessing import image as image_utils
from keras.preprcessing.text import Toknizer
import pandas as pd
from sklearn.model_selection import train_test_spli

Resources