简体   繁体   English

第一个时代中 keras 模型数据增强维度的错误

[英]Error with keras model data augmented dimensions in First epoch

So I have done data augmentation in a keras model.所以我在 keras 模型中做了数据增强。 I am using Fashin_Mnist dataset.我正在使用 Fashin_Mnist 数据集。 Everything goes okay but when it finished the first epoch it throws an error.一切正常,但是当它完成第一个 epoch 时,它会抛出一个错误。

The error: ValueError: Shapes (32, 1) and (32, 10) are incompatible

My data:我的数据:

img_rows = 28
img_cols = 28
batch_size = 512
img_shape = (img_rows, img_cols, 1)

x_train = x_train.reshape(x_train.shape[0], *img_shape)
x_test = x_test.reshape(x_test.shape[0], *img_shape)
x_val = x_val.reshape(x_val.shape[0], *img_shape)

label_as_binary = LabelBinarizer()
y_train_binary = label_as_binary.fit_transform(y_train)
y_test_binary = label_as_binary.fit_transform(y_test)
y_val_binary = label_as_binary.fit_transform(y_val)

My model:我的型号:

model2 = Sequential([
   Conv2D(filters=32, kernel_size=3, activation='relu', 
   input_shape=img_shape, padding="same"),
   MaxPooling2D(pool_size=2),
   Conv2D(filters=32, kernel_size=3, activation='relu', 
   padding="same"),
   MaxPooling2D(pool_size=2),
   Dropout(0.25),
   Conv2D(filters=64, kernel_size=3, activation='relu', 
   padding="same"),
   MaxPooling2D(pool_size=2),
   Conv2D(filters=64, kernel_size=3, activation='relu', 
   padding="same"),
   MaxPooling2D(pool_size=2),
   Dropout(0.25),

   Flatten(),
   Dense(512, activation='relu'),
   Dense(10, activation='softmax')

   ])

The data augmentation:数据增强:

datagen = ImageDataGenerator(horizontal_flip=True, rotation_range=45, 
 width_shift_range=0.2, height_shift_range=0.2, zoom_range=0.1)
datagen.fit(x_train)
for x_batch, y_batch in datagen.flow(x_train, y_train, batch_size=9):
    for i in range(0, 9):
        pyplot.subplot(330 + 1 + i)
        pyplot.imshow(x_batch[i].reshape(28, 28), 
 cmap=pyplot.get_cmap('gray'))
   pyplot.show()
 break

model2.compile(loss='categorical_crossentropy', 
optimizer=Adadelta(learning_rate=0.01), metrics=['accuracy'])
history = model2.fit_generator(datagen.flow(x_train,y_train_binary, 
  batch_size=batch_size),
epochs = 10, validation_data = (x_train, y_val_binary), verbose = 1)

I have seen many similar answers but none of them seem to fit mine.我看过很多类似的答案,但似乎没有一个适合我。 Help is much appreciated.非常感谢帮助。

I think you should change this line:我认为你应该改变这一行:

validation_data = (x_train, y_val_binary)

to this:对此:

validation_data = (x_val, y_val_binary)

Then, your model should run properly.然后,您的模型应该可以正常运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM