我一直试图在MNIST上使用keras.preprocessing.image.ImageDataGenerator()
作为额外的例子。
我在Keras的Keras上使用了一个完全连接的网络。我首先在构建和编译模型之前将3D MNIST重塑为4D张量,然后使用数据增强来帮助拟合模型。
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)
model = Sequential(name="mlp")
model.add(ll.InputLayer([28, 28]))
model.add(ll.Flatten())
model.add(ll.Dense(512, kernel_regularizer=regularizers.l2(0.04)))
model.add(ll.Activation('relu'))
model.add(ll.Dense(512, kernel_regularizer=regularizers.l2(0.04)))
model.add(ll.Activation('relu'))
model.add(ll.Dense(256, kernel_regularizer=regularizers.l2(0.04)))
model.add(ll.Activation('relu'))
model.add(ll.Dense(128, kernel_regularizer=regularizers.l2(0.04)))
model.add(ll.Activation('relu'))
model.add(ll.Dense(32, kernel_regularizer=regularizers.l2(0.04)))
model.add(ll.Activation('relu'))
model.add(ll.Dense(10, activation='softmax'))
model.compile("adam", "categorical_crossentropy", metrics=["accuracy"])
gen = ImageDataGenerator(rotation_range=8, width_shift_range=0.08, shear_range=0.3,
height_shift_range=0.08, zoom_range=0.08)
test_gen = ImageDataGenerator()
train_generator = gen.flow(X_train, y_train, batch_size=64)
test_generator = test_gen.flow(X_test, y_test, batch_size=64)
...
model.fit_generator(train_generator, steps_per_epoch=60000//64, epochs=5,
validation_data=test_generator, validation_steps=10000//64)
我收到此错误:
5 model.fit_generator(train_generator, steps_per_epoch=60000//64, epochs=5,
----> 6 validation_data=test_generator, validation_steps=10000//64)
ValueError: Error when checking input: expected input_6 to have 3 dimensions, but got array with shape (256, 28, 28, 1)
但是当我省略它转换为4d张量时,会发生这种情况:
52 test_gen = ImageDataGenerator()
---> 53 train_generator = gen.flow(X_train, y_train, batch_size=64)
54 test_generator = test_gen.flow(X_test, y_test, batch_size=64)
ValueError: ('Input data in `NumpyArrayIterator` should have rank 4. You passed an array with shape', (50000, 28, 28))
似乎列车和测试生成器对象生成4D张量,但模型本身需要3D张量。
答案 0 :(得分:1)
当您的模型(输入图层)期待(batch, 28, 28, 1)
时,您的数据为(batch, 28, 28)
。
解决这个问题就像更改模型的输入形状一样简单:
#you don't need to add an input layer, actually, just pass the shape to the first layer:
model = Sequential(name="mlp")
model.add(ll.Flatten(input_shape=(28,28,1)))