且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在keras中进行多类图像分类?

更新时间:2022-06-22 22:37:14

对于多类分类,最后一个密集层的节点数必须等于类数,然后激活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.