gpt4 book ai didi

machine-learning - 使用 flow_from_directory 时无法创建 keras 模型

转载 作者:行者123 更新时间:2023-11-30 09:51:47 26 4
gpt4 key购买 nike

我正在尝试创建一个模型来适应 cifar-10 数据集中的数据。我有一个来自示例的工作卷积神经网络,但是当我尝试创建多层感知器时,我不断遇到形状不匹配问题。

#https://gist.github.com/fchollet/0830affa1f7f19fd47b06d4cf89ed44d
#https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html


from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
from keras.optimizers import RMSprop


# dimensions of our images.
img_width, img_height = 32, 32

train_data_dir = 'pro-data/train'
validation_data_dir = 'pro-data/test'
nb_train_samples = 2000
nb_validation_samples = 800
epochs = 50
batch_size = 16

if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)

model = Sequential()
model.add(Dense(512, activation='relu', input_shape=input_shape))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(10, activation='softmax'))

model.summary()

model.compile(loss='categorical_crossentropy',
optimizer=RMSprop(),
metrics=['accuracy'])

# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)

# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator()

train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')

model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=nb_validation_samples // batch_size)

score = model.evaluate_generator(validation_generator, 1000)
print("Accuracy = ", score[1])

我收到的错误是这样的:

ValueError: Error when checking target: expected dense_3 to have 4 dimensions, but got array with shape (16, 1)

但是如果将输入层的 input_shape 更改为不正确的值“(784,)”,我会收到此错误:

ValueError: Error when checking input: expected dense_1_input to have 2 dimensions, but got array with shape (16, 32, 32, 3)

这是我使用 flow_from_directory 获得工作 cnn 模型的地方: https://gist.github.com/fchollet/0830affa1f7f19fd47b06d4cf89ed44d

如果有人好奇的话,我使用卷积神经网络模型得到的 cifar10 的准确率仅为 10%。我觉得很穷。

最佳答案

根据您的模型,您的模型摘要为

dense_1(密集)(无、32、32、512)2048

<小时/>

dropout_1(辍学)(无、32、32、512)0

<小时/>

dense_2(密集)(无、32、32、512)262656

<小时/>

dropout_2(辍学)(无、32、32、512)0

<小时/>

dense_3(密集)(无、32、32、10)5130

总参数:269,834

可训练参数:269,834

不可训练的参数:0

您的输出格式是(32,32,10)

在 cifar-10 数据集中,您想要分类为 10 个标签

尝试添加

model.add(Flatten())

在最后一个致密层之前。

现在你的输出层是

<小时/>

层(类型)输出形状参数#

<小时/>

dense_1(密集)(无、32、32、512)2048

<小时/>

dropout_1(辍学)(无、32、32、512)0

<小时/>

dense_2(密集)(无、32、32、512)262656

<小时/>

dropout_2(辍学)(无、32、32、512)0

<小时/>

flatten_1(展平)(无,524288)0

<小时/>

dense_3(密集)(无,10)5242890

总参数:5,507,594

可训练参数:5,507,594

不可训练的参数:0

此外,您刚刚在模型中使用了密集层和丢失层。为了获得更好的准确性,您应该搜索由密集层和最大池层组成的各种 CNN 架构。

关于machine-learning - 使用 flow_from_directory 时无法创建 keras 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43816571/

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