作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 keras 学习数据增强。链接:https://keras.io/preprocessing/image/该链接使用 CNN,但是当我尝试使用下面给出的密集层时,出现错误。错误在 datagen.fit()。描述说输入应该是等级 4。如何解决?
#import dataset
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
#change shape from image to vector
X_train = X_train.reshape(50000, 32 * 32 * 3)
X_test = X_test.reshape(10000, 32 * 32 * 3)
#preprocess
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255.0
X_test /= 255.0
#change labels from numeric to one hot encoded
Y_train = to_categorical(y_train, 10)
Y_test = to_categorical(y_test, 10)
model = Sequential()
model.add(Dense(1024, input_shape=(3072, )))
model.add(Activation('relu'))
model.add(Dense(10))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
datagen = ImageDataGenerator(
featurewise_center=True,
featurewise_std_normalization=True,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True)
datagen.fit(X_train)
# fits the model on batches with real-time data augmentation:
model.fit_generator(datagen.flow(X_train, Y_train, batch_size=32),
steps_per_epoch=len(X_train) / 32, epochs=epochs,verbose=1,
validation_data=datagen.flow(X_test, Y_test, batch_size=32))
错误
---------------------------------------------------------------------------
ValueError
---> 57 datagen.fit(X_train)
ValueError: Input to `.fit()` should have rank 4. Got array with shape: (50000, 3072)
最佳答案
你 reshape 了你的数组。 ImageDataGenerator 需要 4 阶输入矩阵(图像索引、高度、宽度、深度)。您的 Reshaping 给出了 2 阶输入矩阵。因此错误。解决方法是删除 reshape ,然后在第一个密集层上方添加一个 CNN 层(只是建议)。
关于python - `.fit()` 的输入应具有等级 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59745740/
我是一名优秀的程序员,十分优秀!