【问题标题】:Error with keras model data augmented dimensions in First epoch在第一个时代,keras 模型数据增加维度的错误
【发布时间】:2021-11-08 10:46:09
【问题描述】:

所以我在 keras 模型中进行了数据增强。我正在使用 Fashin_Mnist 数据集。一切正常,但是当它完成第一个 epoch 时会引发错误。

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

我的数据:

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)

我的模特:

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')

   ])

数据增强:

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)

我见过很多类似的答案,但似乎没有一个适合我。非常感谢您的帮助。

【问题讨论】:

    标签: python tensorflow keras deep-learning data-augmentation


    【解决方案1】:

    我认为你应该改变这一行:

    validation_data = (x_train, y_val_binary)
    

    到这里:

    validation_data = (x_val, y_val_binary)
    

    那么,你的模型应该可以正常运行了。

    【讨论】:

      猜你喜欢
      • 2018-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-05
      • 1970-01-01
      • 2018-01-04
      • 2022-01-20
      相关资源
      最近更新 更多