拟合模型时ImageDataGenerator的形状问题

时间:2021-02-04 11:27:17

标签: python tensorflow keras deep-learning image-preprocessing

我想请教您如何解决这个问题。 我有两个图片文件夹,一个作为训练集,另一个作为验证。 我所做的是使用 ImageDataGenerator:

train_datagen = ImageDataGenerator(
        rescale=1./255,
        rotation_range=40, #integer degree range for random rotations
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)


# to have a better performance in accuracy measure I just rescale the test
test_datagen = ImageDataGenerator(rescale=1./255)

# apply ImageDataGenerator on requierd folde 
train_generator = train_datagen.flow_from_directory(
        '/content/drive/MyDrive/NN_HW2/training/training/',  # this is the target directory
        target_size=(150, 150),  # all images will be resized to 150x150
        batch_size=batch_size,
        class_mode='categorical')  # since I am in multicalss calssification 

# this is a similar generator, for validation data
validation_generator = test_datagen.flow_from_directory(
        '/content/drive/MyDrive/NN_HW2/validation/validation/',
        target_size=(150, 150),
        batch_size=batch_size,
        class_mode='categorical')

然后,我这样定义我的模型:

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(150, 150, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(Flatten())  # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('softmax'))


model.compile(loss='categorical_crossentropy',
              optimizer='Adam',
              metrics=['accuracy']
              )

因此,当我尝试拟合模型时,colab 会引发以下错误:

model.fit_generator(
        train_generator,
        steps_per_epoch=1000//batch_size,
        epochs=25,
        validation_data=validation_generator,
        steps_per_epoch=500//batch_size
        )
Matrix size-incompatible: In[0]: [16,10], In[1]: [64,1]
     [[node gradient_tape/sequential_3/dense_7/MatMul (defined at <ipython-input-22-f61b6c381681>:7) ]] [Op:__inference_train_function_3405]

Function call stack:
train_function

感谢您的时间

1 个答案:

答案 0 :(得分:0)

在下面的代码中

model.fit_generator(
        train_generator,
        steps_per_epoch=1000//batch_size,
        epochs=25,
        validation_data=validation_generator,
        steps_per_epoch=500//batch_size
        )

您定义了 steps_per_epoch 两次。你要做的是保留第一个,删除第二个并替换为

 validation_steps=500//batch_size 

或者,由于您使用的是生成器,您可以保留 steps_per_epoch=None 和 validation_steps=None。 model.fit_generator 正在贬值,所以只需使用 model.fit。