我的CNN的验证准确性从未超过75%

时间:2018-07-24 15:47:29

标签: python keras

我是CNN的新手,我跟随Building powerful image classification models using very little data并从头开始构建自己的模型。

运行模型时,验证精度会波动,但不会增加。我有10节课。我尝试提高学习率,但没有太大的不同。似乎模型总是过拟合。我的验证准确性通常会在0.70到0.73之间波动,而训练会不断增加。

对于我的模型,我尝试遵循NiN模型,但是由于没有太多参考可供我参考,因此我不确定这是否是正确的代码。

我正在使用此数据生成器,无法进行k折交叉验证。有什么建议可以用来提高我的验证和测试准确性吗?

非常感谢您。

nb_train_samples = 8804
nb_validation_samples = 2512
nb_test_samples = 1263

train_data_dir = 'C:/Users/Z/Documents/Python Scripts/10 species dataset TVT/Training Dataset'
validation_data_dir = 'C:/Users/Z/Documents/Python Scripts/10 species dataset TVT/Validation Dataset'
test_data_dir = 'C:/Users/Z/Documents/Python Scripts/10 species dataset TVT/Testing Dataset'

if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)

if 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(192, (5, 5), input_shape=input_shape,strides=4))
model.add(Activation('relu'))

model.add(Conv2D(160, (1, 1)))
model.add(Activation('relu'))

model.add(Conv2D(96, (1, 1)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))

model.add(Conv2D(192, (5, 5)))
model.add(Activation('relu'))

model.add(Conv2D(192, (1, 1)))
model.add(Activation('relu'))

model.add(Conv2D(192, (1, 1)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))

model.add(Conv2D(192, (3, 3)))
model.add(Activation('relu'))

model.add(Conv2D(192, (1, 1)))
model.add(Activation('relu'))

model.add(Conv2D(10, (1, 1)))
model.add(Activation('relu'))
model.add(GlobalAveragePooling2D(data_format=None))

model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(10))
model.add(Activation('softmax'))

learning_rate = 0.00001
decay_rate = learning_rate / epochs
adam = Adam(lr=learning_rate, decay=decay_rate)
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])

train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
validation_datagen = ImageDataGenerator(rescale=1. / 255)
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 = validation_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical', shuffle=False)

test_generator = test_datagen.flow_from_directory(
test_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical', shuffle=False)

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)

test_steps_per_epoch = np.math.ceil(test_generator.samples / test_generator.batch_size)
predictions = model.predict_generator(test_generator, steps=test_steps_per_epoch)
predicted_classes = np.argmax(predictions, axis=1)
true_classes=test_generator.classes
class_labels = list(test_generator.class_indices.keys())
report = metrics.classification_report(true_classes, predicted_classes, target_names=class_labels)

0 个答案:

没有答案