使用 flow_from_directory 进行多类和可变大小的图像分类 [英] Multi-class and variable size image classification with flow_from_directory

查看:85
本文介绍了使用 flow_from_directory 进行多类和可变大小的图像分类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大小可变的 7 类图像.

I have a 7-classes images with variable sizes.

已通过 flow_from_directory 完成调整大小,但此处弹出错误信息 Error when checks target: expected activation_21 to have shape (1,) but got array with shape (7,).

The resize has been done through flow_from_directory, but here pops the error saying Error when checking target: expected activation_21 to have shape (1,) but got array with shape (7,).

文件夹:

data/
    train/
        dogs/
            dog001.jpg
            dog002.jpg
            ...
        cats/
            cat001.jpg
            cat002.jpg
            ...
        sheep/
            sheep001.jpg
            sheep002.jpg
            ...
    validation/
        dogs/
            dog001.jpg
            dog002.jpg
            ...
        cats/
            cat001.jpg
            cat002.jpg
            ...
        sheep/
            sheep001.jpg
            sheep002.jpg
            ...

我的模型是一个简单的 CNN:

My model is a simple CNN:

train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
        training_path,  # this is the target directory
        target_size=(200, 350),  # all images will be resized to 200x350
        batch_size=batch_size, class_mode='categorical'
        )  

validation_generator = test_datagen.flow_from_directory(
        validation_path,
        target_size=(200, 350),
        batch_size=batch_size,class_mode='categorical'
        )

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(200, 350, 3),data_format='channels_last'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

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

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

model.add(GlobalMaxPooling2D())  # 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(7))
model.add(Activation('softmax'))

model.compile(loss='sparse_categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])


model.fit_generator(
        train_generator,
        steps_per_epoch=500 // batch_size,
        epochs=10,
        validation_data=validation_generator,
        validation_steps=500 // batch_size)

模型摘要是:

________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_13 (Conv2D)           (None, 198, 348, 32)      896       
_________________________________________________________________
activation_17 (Activation)   (None, 198, 348, 32)      0         
_________________________________________________________________
max_pooling2d_13 (MaxPooling (None, 99, 174, 32)       0         
_________________________________________________________________
conv2d_14 (Conv2D)           (None, 97, 172, 32)       9248      
_________________________________________________________________
activation_18 (Activation)   (None, 97, 172, 32)       0         
_________________________________________________________________
max_pooling2d_14 (MaxPooling (None, 48, 86, 32)        0         
_________________________________________________________________
conv2d_15 (Conv2D)           (None, 46, 84, 64)        18496     
_________________________________________________________________
activation_19 (Activation)   (None, 46, 84, 64)        0         
_________________________________________________________________
max_pooling2d_15 (MaxPooling (None, 23, 42, 64)        0         
_________________________________________________________________
global_max_pooling2d_3 (Glob (None, 64)                0         
_________________________________________________________________
dense_5 (Dense)              (None, 64)                4160      
_________________________________________________________________
activation_20 (Activation)   (None, 64)                0         
_________________________________________________________________
dropout_3 (Dropout)          (None, 64)                0         
_________________________________________________________________
dense_6 (Dense)              (None, 7)                 455       
_________________________________________________________________
activation_21 (Activation)   (None, 7)                 0         
=================================================================
Total params: 33,255
Trainable params: 33,255
Non-trainable params: 0
_________________________________________________________________

我也尝试生成单独的 x_input 和 y_input np.arrays,但我不知道如何调整图像输入的大小,因为它们的大小不同.因此我无法获得 4 维输入向量,这种方法给了我这样的错误:

I have also tried generating seperate x_input and y_input np.arrays, but I don`t know how to resize the input of images since they have different size. Thus I cannot obtain a 4-dimensional input vector, and this approach gives me error like this:

Error when checking input: expected conv2d_16_input to have 4 dimensions, but got array with shape (5721, 1)

推荐答案

你的代码需要保持一致,在你的 flow_from_generator 调用中你将 class mode 设置为 categorical,其中生成 one-hot 编码的类标签,但您使用 sparse_categorical_crossentropy 损失,它需要整数标签(不是 one-hot 编码的标签).

Your code needs to be consistent, in your flow_from_generator calls you set class mode to categorical, which produces one-hot encoded class labels, but you use the sparse_categorical_crossentropy loss, which expects integer labels (not one-hot encoded ones).

您可以将类模式设置为 sparse 以获得正确的标签,或者将损失更改为 categorical_crossentropy.

You could set the class mode to sparse in order to get the right labels, or change the loss to categorical_crossentropy.

这篇关于使用 flow_from_directory 进行多类和可变大小的图像分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆