简体   繁体   English

拟合model时ImageDataGenerator的形状问题

[英]Shape problem of ImageDataGenerator when fitting the model

I would like to ask you how to solve this problem.我想问你如何解决这个问题。 I have two folders of pictures, one as train set and the other as validation.我有两个图片文件夹,一个作为火车组,另一个作为验证。 What i've done is to use 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, 150),
        batch_size=batch_size,
        class_mode='categorical')

Then, I defined my model like this:然后,我像这样定义了我的 model:

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

So, when I try to fit the model, colab raise me the following error:因此,当我尝试安装 model 时,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

Thank you for the time谢谢你的时间

in your code below在下面的代码中

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

You defined steps_per_epoch twice.您定义了 steps_per_epoch 两次。 What you want to do is leave the first one, delete the second one and replace it with您要做的是保留第一个,删除第二个并将其替换为

 validation_steps=500//batch_size 

Alternatively since you are using generators, you can leave steps_per_epoch=None and validation_steps=None.或者,由于您使用的是生成器,您可以保留 steps_per_epoch=None 和 validation_steps=None。 model.fit_generator is being depreciate so just use model.fit. model.fit_generator 正在贬值,所以只需使用 model.fit。

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

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