gpt4 book ai didi

python - 名称错误 : name 'scipy' is not defined when trying to create a model

转载 作者:行者123 更新时间:2023-12-05 04:26:50 45 4
gpt4 key购买 nike

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

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

最佳答案

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

关于python - 名称错误 : name 'scipy' is not defined when trying to create a model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72927229/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com