32

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.

Tshilidzi Mudau
  • 7,373
  • 6
  • 36
  • 49
  • There is probably a better way to do what you're trying. What exactly is it you want to do with `_obtain_input_shape`? – Daniel Möller Mar 05 '18 at 14:54
  • Hi @DanielMöller, I have edited my question to include more information . – Tshilidzi Mudau Mar 05 '18 at 15:02
  • You know that keras doesn't have a VGG-Face, right? The input shape should be chosen by you when you create a model. If you want the default input shape, just pass `(224,224,3)`. – Daniel Möller Mar 05 '18 at 15:06
  • Thanks for the responses Daniel, yes I know. I want to load VGG-face pre-trained weights. I will try what you suggested and let you know . – Tshilidzi Mudau Mar 05 '18 at 15:13
  • Anybody know how to import this for tensorflow.keras? – Austin Feb 07 '19 at 21:12
  • @Austin You can always go to the original code definition and copy paste the needed code into an own library. Load that library instead. The _obtain_input_shape can be found here : https://github.com/keras-team/keras-applications/blob/master/keras_applications/imagenet_utils.py – Florida Man Sep 14 '20 at 10:57

7 Answers7

61

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
Tshilidzi Mudau
  • 7,373
  • 6
  • 36
  • 49
Geeocode
  • 5,705
  • 3
  • 20
  • 34
11

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.

petezurich
  • 9,280
  • 9
  • 43
  • 57
galoiszhang
  • 131
  • 1
  • 3
7

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.

user5803658
  • 71
  • 1
  • 4
2

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
Frightera
  • 4,773
  • 2
  • 13
  • 28
1

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

Mike Chen
  • 377
  • 3
  • 5
1
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)

0

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.

Jyotirmay
  • 1,533
  • 3
  • 21
  • 41