你好,我按照这里的指南制作了一个图像分类器 https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html,它只能分类成两个类别。这些代码可以给我F1分数和混淆矩阵。我的数据集目前是不同种类的蘑菇,我想知道是否有办法使用这些代码制作一个多类别的图像分类器?
import numpyfrom keras.preprocessing.image import ImageDataGeneratorfrom keras.models import Sequentialfrom keras.layers import Conv2D, MaxPooling2Dfrom keras.layers import Activation, Dropout, Flatten, Densefrom keras import backend as Kimport matplotlib.pyplot as plt# dimensions of our images.img_width, img_height = 150, 150train_data_dir = r'C:\Users\Acer\imagerec\Mushrooms\TRAIN'validation_data_dir = r'C:\Users\Acer\imagerec\Mushrooms\VAL'nb_train_samples = 7025nb_validation_samples = 6262epochs = 50batch_size = 16if K.image_data_format() == 'channels_first': input_shape = (3, img_width, img_height)else: input_shape = (img_width, img_height, 3)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('sigmoid'))model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])# this is the augmentation configuration we will use for trainingtrain_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 rescalingtest_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='binary')validation_generator = test_datagen.flow_from_directory( validation_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='binary')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)model.save_weights('first_try.h5')from sklearn.metrics import classification_reportfrom sklearn.metrics import confusion_matriximport seaborn as snstest_steps_per_epoch = numpy.math.ceil(validation_generator.samples / validation_generator.batch_size)predictions = model.predict_generator(validation_generator, steps=test_steps_per_epoch)# Get most likely classpredicted_classes = numpy.argmax(predictions, axis=1)true_classes = validation_generator.classesclass_labels = list(validation_generator.class_indices.keys())report = classification_report(true_classes, predicted_classes, target_names=class_labels)print(report)cm=confusion_matrix(true_classes,predicted_classes)sns.heatmap(cm, annot=True)print(cm)plt.show()
回答:
你的代码中明确定义了二元分类。要将其转换为多类别任务,比如说N类,你需要将最后一层的1 Dense
更改为N Dense
,并将激活函数从sigmoid
更改为softmax
。最后但同样重要的是,如果你的类别已经是独热编码,你应该将损失函数从binary_crossentropy更改为categorical_crossentropy。否则,你可能需要使用sparse_categorical_crossentropy。
应用这些更改后,你的代码那部分应该看起来像这样:
model.add(Dense(N))model.add(Activation('softmax'))model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
其中N是你拥有的不同类别的数量。
编辑:你还需要将生成器中的class_mode从”binary”更改为”categorical”。你还应该检查如何生成标签(独热编码)。