无法导入 keras 应用程序

问题描述 投票:0回答:2

我正在尝试在 pycharm 中使用

keras
应用程序。我从以下导入开始我的脚本:

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

运行这段代码后,我收到此错误:

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
```

我已尝试按照建议导入适当的 keras 库,但问题仍然存在。我还尝试检查 json 文件以查看它是否包含正确的后端(确实如此)。

如何解决这个问题?

“为了清晰起见进行编辑”

我的完整导入如下:

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 (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
python tensorflow keras facial-identification
2个回答
2
投票

您是否打算使用 Tensorflow 框架来执行模型?如果是张量流那么我建议使用

import tensorflow as tf \ from tensorflow.keras.applications.vgg16 import VGG
。 Keras 内置于最新的 TF 框架中,因此我们不必进行显式导入

即使不然,如果你想直接使用 Keras,我相信代码应该是:

import keras \ from keras.applications.vgg16 import VGG16 \ vggmodel = VGG16(weights='imagenet', include_top=True)


0
投票

使用此 from tf_keras.applications.vgg16 import VGG16

© www.soinside.com 2019 - 2024. All rights reserved.