名称错误:尝试创建模型时,“scipy”未定义。

3

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

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? - Dr. Snoopy
4个回答

4

尝试运行 pip install scipypip3 install scipy 可以解决问题。


2
我安装了scipy包,但仍然出现相同的错误。 - Atakan Sever
弃用警告:使用distutils配置文件配置安装方案已被弃用,并且在不久的将来将不再起作用。如果您正在使用Homebrew或Linuxbrew Python,请参见https://github.com/Homebrew/homebrew-core/issues/76621上的讨论。 要求已满足:scipy在/opt/homebrew/lib/python3.9/site-packages(1.8.1)中。 要求已满足:numpy<1.25.0,>=1.17.3在/opt/homebrew/lib/python3.9/site-packages中(从scipy)。 - Atakan Sever

1
你需要做的是:
  1. 安装scipy pip install scipy
  2. 重新启动VS Code或重启Python内核并重新运行代码。

0

我点击了错误信息,它会将您重定向到源代码。 注释掉这两行并保存Python脚本。

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

代码截图
然后它就会完美地工作。


0

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

pip install scipy

然后在你的导入中添加scipy:

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

我已经安装了这个软件包,但在导入部分我无法导入scipy软件包。 - Atakan Sever
@AtakanSever 请展示新的代码和错误。您的计算机上只安装了一个Python版本吗?如果您有多个版本,事情可能会有点复杂,因为您必须为正确的Python版本和环境安装scipy。 - Malo
我正在使用Python 3.9.7(pyenv)。我可以在此版本中使用我的Keras和TensorFlow库。 - Atakan Sever
错误代码 import scipy ModuleNotFoundError: 找不到名为'scipy'的模块 - Atakan Sever
atakansever@atakan-Air CNNN % pip install scipy 警告:使用distutils配置文件配置安装方案已被弃用,并且在不久的将来将不再起作用。如果您正在使用Homebrew或Linuxbrew Python,请参见https://github.com/Homebrew/homebrew-core/issues/76621上的讨论。 要求已经满足:scipy在/opt/homebrew/lib/python3.9/site-packages(1.8.1)中。 要求已经满足:numpy<1.25.0,>=1.17.3在/opt/homebrew/lib/python3.9/site-packages(从scipy)中。 - Atakan Sever
错误信息表明您尚未激活安装库的目标环境。 - tripleee

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接