我正在使用keras对VGG网络进行一些小实验。 我使用的数据集是花卉数据集,有5个类别,包括玫瑰,向日葵,蒲公英,郁金香和雏菊。
有些事情我无法弄清楚: 当我使用一个小的CNN网络(不是VGG,在下面的代码中)时,它快速收敛并且在仅仅大约8个时期之后达到了大约75%的验证准确度。
然后我切换到VGG网络(代码中注释掉的区域)。网络的损失和准确性根本没有改变,输出如下:
大纪元1/50 402/401 [==============================] - 199s 495ms /步 - 损失: 13.3214 - acc:0.1713 - val_loss:13.0144 - val_acc:0.1926
大纪元2/50 402/401 [==============================] - 190s 473ms /步 - 损失:13.3473 - acc:0.1719 - val_loss :13.0144 - val_acc:0.1926
大纪元3/50 402/401 [==============================] - 204s 508ms /步 - 损失:13.3423 - acc:0.1722 - val_loss :13.0144 - val_acc:0.1926
大纪元4/50 402/401 [==============================] - 190s 472ms /步 - 损失:13.3522 - acc:0.1716 - val_loss :13.0144 - val_acc:0.1926
大纪元5/50 402/401 [==============================] - 189s 471ms /步 - 损失:13.3364 - acc:0.1726 - val_loss :13.0144 - val_acc:0.1926
大纪元6/50 402/401 [==============================] - 189s 471ms /步 - 损失:13.3453 - acc:0.1720 - val_loss :13.0144 - val_acc:0.1926 大纪元7/50
Epoch 7/50 402/401 [==============================] - 189s 471ms /步 - 损失: 13.3503 - acc:0.1717 - val_loss:13.0144 - val_acc:0.1926
PS:我也用其他数据集和框架进行了这个实验(带有tensorflow和slim的place365数据集)。结果是一样的。我已经研究过VGG论文(Simonyan& Zisserman),它说有很多阶段需要训练像VGG这样的深层网络,比如从A阶段到E阶段,不同的网络结构。我不确定是否必须按照VGG文件中描述的方式训练我的VGG网络。其他在线课程也没有提到这个复杂的培训过程。 有人有什么想法吗?
我的代码:
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
# dimensions of our images.
img_width, img_height = 224, 224
train_data_dir = './data/train'
validation_data_dir = './data/val'
nb_train_samples = 3213
nb_validation_samples = 457
epochs = 50
batch_size = 8
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)
# random cnn model:
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(5))
model.add(Activation('softmax'))
# vgg model:
'''model = Sequential([
Conv2D(64, (3, 3), input_shape=input_shape, padding='same',
activation='relu'),
Conv2D(64, (3, 3), activation='relu', padding='same'),
MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
Conv2D(128, (3, 3), activation='relu', padding='same'),
Conv2D(128, (3, 3), activation='relu', padding='same',),
MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
Conv2D(256, (3, 3), activation='relu', padding='same',),
Conv2D(256, (3, 3), activation='relu', padding='same',),
Conv2D(256, (3, 3), activation='relu', padding='same',),
MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
Conv2D(512, (3, 3), activation='relu', padding='same',),
Conv2D(512, (3, 3), activation='relu', padding='same',),
Conv2D(512, (3, 3), activation='relu', padding='same',),
MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
Conv2D(512, (3, 3), activation='relu', padding='same',),
Conv2D(512, (3, 3), activation='relu', padding='same',),
Conv2D(512, (3, 3), activation='relu', padding='same',),
MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
Flatten(),
Dense(256, activation='relu'),
Dense(256, activation='relu'),
Dense(5, activation='softmax')
])'''
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
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 = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical')
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('flowers.h5')