如何在keras中进行多类图像分类? [英] How to do multi-class image classification in keras?

查看:535
本文介绍了如何在keras中进行多类图像分类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我所做的.我得到了用于狗/猫图像分类的代码,并且我编译运行后获得了80%的准确性.我在火车和验证文件夹中又添加了一个类(飞机)文件夹.在以下代码中进行了更改

Here is what I did. I got the code for dog/cat image classification and I compiled and ran and got 80% accuracy. I added one more class (aeroplane) folder to the train and validation folder. Made changes in the following codes

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

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='categorical')

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='categorical')

binary class_mode更改为categorical,也丢失了categorical_crossentropy.还将输出布局sigmoid更改为softmax. 收到以下错误.

changed binary class_mode to categorical and also loss to categorical_crossentropy. Also changed output layout sigmoid to softmax. Receives the following error.

ValueError: Error when checking target: expected activation_10 to have shape (None, 1) but got array with shape (16, 3)

我是否需要像下面提到的那样将训练标签明确地更改为分类? (我从网站使用多标签分类中阅读了此内容keras )

Do I need to explicity change the training labels to categorical like mentioned below? (I read this from the site multilabel classification using keras)

train_labels = to_categorical(train_labels, num_classes=num_classes) 

我不确定这里会发生什么.请帮忙.我对深度学习还比较陌生.

I am not sure what happens here. Please help. I am relatively new to deep learning.

模型

model = Sequential()

model.add(Conv2D(32, (3, 3), input_shape=input_shape))
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(Flatten())
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='rmsprop',
              metrics=['accuracy'])

# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)
# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='categorical')


validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='categorical')
model.fit_generator(
    train_generator,
    steps_per_epoch=nb_train_samples // batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=nb_validation_samples // batch_size)

推荐答案

对于多类分类,最后一个密集层的节点数必须等于类数,然后激活softmax,即最后一个模型的两层应该是:

For multi-class classification, the last dense layer must have a number of nodes equal to the number of classes, followed by softmax activation, i.e. the last two layers of your model should be:

model.add(Dense(num_classes))
model.add(Activation('softmax'))

此外,您的标签(训练和测试)都必须是一键编码的;因此,假设您最初的猫和狗被标记为整数(0/1),而您的新类别(飞机)最初也被类似地标记为"2",则应按以下方式进行转换:

Additionally, your labels (both train and test) must be one-hot encoded; so, assuming that your initial cats and dogs were labeled as integers (0/1), and your new category (airplane) is initially similarly labeled as '2', you should convert them as follows:

train_labels = keras.utils.to_categorical(train_labels, num_classes)
test_labels = keras.utils.to_categorical(test_labels, num_classes)

最后,在术语层面上,您正在执行的是多类,而不是多标签分类(我已经编辑了帖子的标题)-最后一个术语用于解决问题样本可能属于多个类别.

Finally, on a terminology level, what you are doing is multi-class, and not multi-label classification (I have edited the title of your post) - the last term is used for problems where the samples might belong to more than one categories.

这篇关于如何在keras中进行多类图像分类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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