使用flow_from_directory时无法创建keras模型

时间:2017-05-06 03:58:26

标签: machine-learning computer-vision keras

我正在尝试创建一个模型来拟合来自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%。我觉得很差。

1 个答案:

答案 0 :(得分:1)

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

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

dropout_1(Dropout)(无,32,32,512)0

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

dropout_2(Dropout)(无,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(Dropout)(无,32,32,512)0

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

dropout_2(Dropout)(无,32,32,512)0

flatten_1(Flatten)(无,524288)0

dense_3(密集)(无,10)5242890

总参数:5,507,594

可训练的参数:5,507,594

不可训练的参数:0

此外,您刚刚使用了模型中的密集和丢失图层。为了获得更好的准确性,您应该谷歌各种CNN架构,其中包括密集和最大化层。