【问题标题】:No module named 'imagenet_utils' for Jupyter but exists on SpyderJupyter 没有名为“imagenet_utils”的模块,但在 Spyder 上存在
【发布时间】:2018-01-09 16:48:31
【问题描述】:
import numpy as np
import os
import time
from vgg16 import VGG16
from keras.preprocessing import image
from imagenet_utils import preprocess_input, decode_predictions
from keras.layers import Dense, Activation, Flatten
from keras.layers import merge, Input
from keras.models import Model
from keras.utils import np_utils
from sklearn.utils import shuffle
from sklearn.cross_validation import train_test_split
from keras.models import load_model

我在我的电脑上使用 Spyder 运行 vgg 模型,一切正常,但是当我尝试在 Jupyter 中运行代码以使用云 gpu 时,出现以下错误:

 >>>>>
ImportError Traceback (most recent call last)
<ipython-input-12-4398e37e021e> in <module>()
----> 1 from imagenet_utils import preprocess_input, decode_predictions

ImportError: No module named 'imagenet_utils'<<<

当它在另一个 IDE 中运行良好时,为什么我有这个?我正在使用 Jupyter 使用 Floyd Web 服务在云中训练模型。

【问题讨论】:

  • 您确定在同一个(虚拟)环境中运行 spyder 和 jupyter?
  • @desertnaut 是的,我打开 anaconda 选择正确的环境并运行它仍然有同样的问题

标签: python-3.x keras


【解决方案1】:

尝试像这样导入它:

from keras.applications.vgg16 import preprocess_input, decode_prediction

这是根据 Keras 应用程序doc page 的标准方式。有关 Keras 应用程序的更多参考资料也可以在此处找到。

注意:注意它是decode_prediction,而不是复数形式的decode_predictions

【讨论】:

  • ImportError Traceback (最近一次调用最后一次) in () ----> 1 from keras.applications.vgg16 import preprocess_input,decode_prediction ImportError: cannot导入名称'decode_prediction'
  • preprocessing_input 工作不 decode_prediction
  • @sachsom 哎呀,它确实有一个!这是预测(单数)而不是复数(predictionS):) 对不起,我的错。我确定它现在有效,更新了我的答案。
  • @sachsom 很高兴我能帮上忙 :) 有几件事可能会导致这种情况。我怀疑您的 imagenet_utils 库可能没有像您的 spyder 那样为您的 jupyter 安装。此外,一些 IDE(如 spyder)会自动导入并了解您默认使用的几个库。一个例子是,在 Spyder 中,您可以省略 import numpy 行,因为 spyder 默认导入它。在 IDE 之外的其他地方运行时,这可能会导致兼容性问题。这就是为什么我建议您始终在终端上尝试您的代码以确保不会发生此类事情。
  • @sachsom 同样,您对外部库的依赖越少越好。如果 Keras 已经有一个 decode_prediction 方法,最好选择它而不是任何其他外部选项。一些博客给出了使用 imagenet_utils 包的 Keras 示例,但这并不意味着它是唯一的方法。
【解决方案2】:

与旧版本的 keras(或 tensorflow)相反,您在导入 imagenet_utils 函数 preprocess_input 和 decode_predictions 时需要特定于模型。

例如,如果您正在使用 Tensorflow 2 并使用 ResNet50,则必须按照以下方式进行操作

from tensorflow.keras.applications.resnet50 import decode_predictions, preprocess_input

在这种情况下,它是 decode_predictions(复数)而不是 decode_prediction。 VGG16 和其他模型类似

【讨论】:

    【解决方案3】:

    尝试导入:

    from tensorflow.keras.applications.vgg16 import decode_predictions
    

    【讨论】:

      猜你喜欢
      • 2022-08-04
      • 2021-06-23
      • 2021-05-05
      • 2022-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-15
      • 2013-09-20
      相关资源
      最近更新 更多