【问题标题】:NameError: name 'scipy' is not defined when trying to create a modelNameError:尝试创建模型时未定义名称 \'scipy\'
【发布时间】:2023-01-23 10:47:40
【问题描述】:

我目前正在尝试使用迁移学习创建模型,但出现错误

NameError: name 'scipy' is not defined

我要从视频中学习。我们已经将一些数据集加载到计算机上,我正在尝试将这些数据集转换为“.json”和“.h5”文件。我必须运行您在第一部分中看到的代码并创建模型。本来应该有像视频里那样的下载,结果却报错,无法解决。

这是我的代码:

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense
from keras.applications.vgg16 import VGG16
import matplotlib.pyplot as plt
from glob import glob
from keras.utils import img_to_array
from keras.utils import load_img

train_path = "/Users/atakansever/Desktop/CNNN/fruits-360_dataset/fruits-360/Training/"
test_path = "/Users/atakansever/Desktop/CNNN/fruits-360_dataset/fruits-360/Test/"

# img = load_img(train_path + "Tangelo/0_100.jpg")
# plt.imshow(img)
# plt.axes("off")
# plt.show()

numberOfClass = len(glob(train_path + "/*"))
# print(numberOfClass)

vgg = VGG16()
# print(vgg.summary())

vgg_layer_list = vgg.layers
# print(vgg_layer_list)

model = Sequential()
for i in range(len(vgg_layer_list)-1):
    model.add(vgg_layer_list[i])

# print(model.summary())

for layers in model.layers:
    layers.trainable = False

model.add(Dense(numberOfClass, activation="softmax"))
# print(model.summary())

model.compile(loss = "categorical_crossentropy",optimizer = "rmsprop",metrics = ["accuracy"])

#train
train_data = ImageDataGenerator().flow_from_directory(train_path, target_size=(224,224))
test_data = ImageDataGenerator().flow_from_directory(test_path, target_size=(224,224))

batch_size = 32

hist = model.fit_generator(train_data,
steps_per_epoch=1600//batch_size,
epochs=25,
validation_data= test_data,
validation_steps=800//batch_size)

这是错误

pyenv shell 3.9.7
atakansever@atakan-Air CNNN % pyenv shell 3.9.7
pyenv: shell integration not enabled. Run `pyenv init' for instructions.
atakansever@atakan-Air CNNN % /Users/atakansever/.pyenv/versions/3.9.7/bin/python /Users/atakansever/Desktop/CNNN/fruits.py
Metal device set to: Apple M1

systemMemory: 8.00 GB
maxCacheSize: 2.67 GB

2022-07-10 11:17:50.428036: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.
2022-07-10 11:17:50.428259: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)
Found 67692 images belonging to 131 classes.
Found 22688 images belonging to 131 classes.
/Users/atakansever/Desktop/CNNN/fruits.py:53: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.
  hist = model.fit_generator(train_data, steps_per_epoch=1600//batch_size,epochs=25,validation_data= test_data,validation_steps=800//batch_size)
Traceback (most recent call last):
  File "/Users/atakansever/Desktop/CNNN/fruits.py", line 53, in <module>
    hist = model.fit_generator(train_data, steps_per_epoch=1600//batch_size,epochs=25,validation_data= test_data,validation_steps=800//batch_size)
  File "/Users/atakansever/.pyenv/versions/3.9.7/lib/python3.9/site-packages/keras/engine/training.py", line 2260, in fit_generator
    return self.fit(
  File "/Users/atakansever/.pyenv/versions/3.9.7/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/Users/atakansever/.pyenv/versions/3.9.7/lib/python3.9/site-packages/keras/preprocessing/image.py", line 2244, in apply_affine_transform
    if scipy is None:
NameError: name 'scipy' is not defined

【问题讨论】:

  • 您有哪些版本的 Keras 和 keras_preprocessing?\

标签: python tensorflow keras


【解决方案1】:

尝试 pip install scipypip3 install scipy 会解决问题

【讨论】:

  • 我安装了 scipy 包,但我仍然得到同样的错误
  • 弃用:使用 distutils 配置文件配置安装方案已弃用,在不久的将来将不再有效。如果您使用的是 Homebrew 或 Linuxbrew Python,请参阅 github.com/Homebrew/homebrew-core/issues/76621 的讨论 已满足要求:/opt/homebrew/lib/python3.9/site-packages (1.8.1) 中的 scipy 已满足要求:numpy<1.25。 0,>=1.17.3 在 /opt/homebrew/lib/python3.9/site-packages (来自 scipy) (1.22.4)
【解决方案2】:

首先,如果尚未安装 scipy 包,请安装它:

pip install scipy

然后将 scipy 添加到您的导入中:

import scipy # This is new!
from keras.preprocessing.image import ImageDataGenerator
# ... all your imports

【讨论】:

  • 我安装了包,但无法在导入部分导入 scipy 包
  • @AtakanSever 请显示新代码和错误。您的计算机上安装了 olny 1 Python 版本吗?如果你有多个版本,事情可能会有点复杂,因为你必须为正确的 python 版本和环境安装 scipy
  • 我正在使用 python 3.9.7(pyenv)。我可以在这个版本中使用我的 keras 和 tensorflow 库。
  • hata kodu `import scipy ModuleNotFoundError: No module named 'scipy'`
  • atakansever@atakan-Air CNNN % pip install scipy 弃用:使用 distutils 配置文件配置安装方案已弃用,在不久的将来将不再有效。如果您使用的是 Homebrew 或 Linuxbrew Python,请参阅 github.com/Homebrew/homebrew-core/issues/76621 的讨论 已满足要求:/opt/homebrew/lib/python3.9/site-packages (1.8.1) 中的 scipy 已满足要求:numpy<1.25。 0,>=1.17.3 在 /opt/homebrew/lib/python3.9/site-packages (来自 scipy) (1.22.4)
【解决方案3】:

我单击了错误消息,它将您定向到源代码。 评论那两行并保存 python 脚本。

# if scipy is None:   
#   raise ImportError('Image transformations require SciPy. '   
#                     'Install SciPy.')

Commect code image
然后它会完美地工作。

【讨论】:

    【解决方案4】:

    你必须:

    1. 安装 scipy pip install scipy
    2. 将 VS 代码重新启动到您的 IDE,或者可能重新启动 Python 内核并重新运行代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-06
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多