【问题标题】:Cannot create keras model while using flow_from_directory使用 flow_from_directory 时无法创建 keras 模型
【发布时间】:2017-05-06 03:58:26
【问题描述】:

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

【问题讨论】:

    标签: machine-learning computer-vision keras


    【解决方案1】:

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

    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 架构。

    【讨论】:

      猜你喜欢
      • 2020-01-01
      • 1970-01-01
      • 2017-09-05
      • 2018-06-30
      • 2019-08-25
      • 2021-06-24
      • 2020-12-11
      • 2020-05-31
      • 1970-01-01
      相关资源
      最近更新 更多