python - 无法导入 keras 应用程序

标签 python tensorflow keras facial-identification

我正在尝试在 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

最佳答案

您是否计划使用 Tensorflow 框架来执行模型?如果是 tensorflow 那么我建议使用 将tensorflow导入为tf\from tensorflow.keras.applications.vgg16 import VGG。 Keras 内置于最新的 TF 框架中,因此我们不必显式导入

即使不然,如果你想直接使用 Keras,我相信代码应该是: 导入keras\from keras.applications.vgg16导入VGG16\vggmodel = VGG16(weights='imagenet', include_top=True)

关于python - 无法导入 keras 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63200541/

相关文章:

python - 带有数据生成器的 keras VAE

python - 在 Python 中堆叠多维数组的数组

python - """TypeError: only length-1 arrays can be converted to Python scalars"""尝试在具有 7 段显示的 Pi3 上运行计数器时

python - Pyinstaller 图像不加载

python - 如何使用 BeautifulSoup 将 UTF-8 编码的 HTML 正确解析为 Unicode 字符串?

python - 未实现错误: Layers with arguments in `__init__` must override `get_config`

python - 保存使用 keras 训练的 TF 模型,然后在 Go 中进行评估

python - assertEqual - 两个相同的列表,为什么我得到奇怪的结果?

python - 加载keras权重直到特定层

python - Tensorflow 中哪些层受 dropout 层的影响?