拟合模型时ImageDataGenerator的形状问题

问题描述

我想请教您如何解决这个问题。 我有两个图片文件夹,一个作为训练集,另一个作为验证。 我所做的是使用 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,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,2)))
model.add(Dropout(0.2))

model.add(Conv2D(64,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

感谢您的时间

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)