2

I am trying to use a keras application in pycharm. I start my script off with the following imports:

from keras_vggface.vggface import VGGFace
from keras_vggface.utils import preprocess_input
from keras_vggface.utils import decode_predictions

Upon running this block of code, I get this error:

ImportError: You need to first `import keras` in order to use `keras_applications`. For instance, you can do:

```
import keras
from keras_applications import vgg16
```

Or, preferably, this equivalent formulation:

```
from keras import applications
```

I have tried importing the appropriate keras libraries as suggested, but the problem persists. I have also tried checking the json file to see if it contains the correct backend(it does).

How can I resolve this issue?

"edit for clarity"

My full imports go as follows:

from PIL import Image # for extracting image
from numpy import asarray
from numpy import expand_dims

from matplotlib import pyplot
from mtcnn.mtcnn import MTCNN # because i am too lazy to make one myself
import keras
from keras_applications import vgg16

from keras_vggface.vggface import VGGFace
from keras_vggface.utils import preprocess_input
from keras_vggface.utils import decode_predictions

Traceback:

Traceback (most recent call last):
  File "C:/Users/###/PycharmProjects/##/#.py", line 17, in <module>
    from keras_applications import vgg16
  File "C:\Users\###\anaconda3\envs\tensor\lib\site-packages\keras_applications\vgg16.py", line 17, in <module>
    backend = get_keras_submodule('backend')
  File "C:\Users\###\anaconda3\envs\tensor\lib\site-packages\keras_applications\__init__.py", line 39, in get_keras_submodule
    raise ImportError('You need to first `import keras` '
ImportError: You need to first `import keras` in order to use `keras_applications`. For instance, you can do:

```
import keras
from keras_applications import vgg16
```

Or, preferably, this equivalent formulation:

```
from keras import applications
```


Process finished with exit code 1
Alex Jun
  • 29
  • 1
  • 1
  • 4

1 Answers1

2

Are you planning to use the Tensorflow framework for executing the model. If it is tensorflow then i suggest using import tensorflow as tf \ from tensorflow.keras.applications.vgg16 import VGG. Keras comes in-built in latest TF framework and hence we dont have to do an explicit import

even otherwise if you want to use Keras directly i believe the code should be : import keras \ from keras.applications.vgg16 import VGG16 \ vggmodel = VGG16(weights='imagenet', include_top=True)

Vivek
  • 161
  • 3