增加图像分类器中 类 的数量

Increase the number of classes in image classifier

我在 keras 中使用 CNN 完成了一个关于两个对象即狗和猫的图像分类的程序。现在如何增加 类,即狗、猫和青蛙的数量?

代码如下:

from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.callbacks import ModelCheckpoint

classifier = Sequential()

classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))

classifier.add(Conv2D(32, (3, 3),  activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))

classifier.add(Flatten())

classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

filepath="weights-improvment-{epoch:02d}-{val_acc:.2f}.hdf5"
checpoint=ModelCheckpoint(filepath,monitor='val_acc',verbose=1,save_best_only=True,mode='max')
callback_list=[checpoint]

from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('training_set',
target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')
test_set = test_datagen.flow_from_directory('test_set',
target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')
classifier.fit_generator(training_set, 
steps_per_epoch = 8000,
epochs = 10,
validation_data = test_set,
validation_steps = 2000)

classifier.save('model_after_trained.h5')

为了class化两个以上的classes,最后一层的神经元(单元)数必须改为classes的数量预测。

假设你要预测3个物体,最后一层必须改成: classifier.add(Dense(units = 3, activation = 'sigmoid'))

请找到下面的link,这将帮助您使用 CNN 进行多class class化:https://www.codesofinterest.com/2017/08/bottleneck-features-multi-class-classification-keras.html

希望对您有所帮助!!!