我使用Keras CNN模型遇到二进制图像分类问题(总共约700张图像),现在其训练准确度约为95%,测试准确度约为85%,如何提高测试准确度?
我已经尝试过Hyperas超参数优化,并使用OpenCV生成经过预处理的阈值图像,使其最多可容纳约4.5K数据,以及Keras API中的数据增强,但这是我能得到的最好的结果。
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'))
optimizer = keras.optimizers.RMSprop(lr = 0.0002,
rho = 0.95,
epsilon = 1e-07,
decay = 0.00001
)
model.compile(loss = 'binary_crossentropy',
optimizer = optimizer,
#optimizer = 'rmsprop',
metrics = ['accuracy'])
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_datagen = ImageDataGenerator(
### Newly added 05.14.
featurewise_center = True,
samplewise_center = True,
featurewise_std_normalization = True,
samplewise_std_normalization = True,
zca_whitening = True,
zca_epsilon = 1e-06,
channel_shift_range = 0.1,
###
rotation_range = 360, ### Used to be 180
width_shift_range = 0.2,
height_shift_range = 0.2,
brightness_range = (0.8, 1.2),
rescale = 1. / 255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True,
vertical_flip = True)
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')
history = 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
#callbacks = callbacks
)
基本上,我希望测试数据的准确性达到90%以上,并保持现在的训练准确性。