我一直在尝试用大约2000张图像的数据集在keras中训练VGG16网络。当在每个时期之后我始终达到约2%的极低精度时,就会出现问题,并且这个问题会一直保持并在该数字附近波动。但是,当我使用简单的leNet进行训练时,它可以达到约99%的高精度。我不知道问题是什么。我的代码中是否存在固有缺陷?深度有时是否会对您的准确性造成负面影响?这两段代码都附在下面。抱歉,我没有培训过程的屏幕截图。
VGG16代码:
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.models import Sequential
classifier = Sequential()
classifier.add(Conv2D(64, 3, 3, input_shape = (224,224, 3), activation = 'relu'))
classifier.add(Conv2D(64, 3, 3, activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2,2)))
classifier.add(Conv2D(128, 3, 3, activation = 'relu'))
classifier.add(Conv2D(128, 3, 3, activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2,2)))
classifier.add(Conv2D(256, 3, 3, activation = 'relu'))
classifier.add(Conv2D(256, 3, 3, activation = 'relu'))
classifier.add(Conv2D(256, 3, 3, activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2,2)))
classifier.add(Conv2D(512, 3, 3, activation = 'relu'))
classifier.add(Conv2D(512, 3, 3, activation = 'relu'))
classifier.add(Conv2D(512, 3, 3, activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2,2)))
classifier.add(Conv2D(512, 3, 3, activation = 'relu'))
classifier.add(Conv2D(512, 3, 3, activation = 'relu'))
classifier.add(Conv2D(512, 3, 3, activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2,2)))
classifier.add(Flatten())
classifier.add(Dense(4096, activation = 'relu'))
classifier.add(Dense(4096, activation = 'relu'))
classifier.add(Dense(1000, activation = 'relu'))
classifier.add(Dense(output_dim = 30, activation = 'softmax'))
classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
validation_split=0.25)
train_generator = train_datagen.flow_from_directory(
'/home/aslt/signData/top30Classes',
target_size=(224, 224),
batch_size= 15,
class_mode='categorical',
subset='training')
validation_generator = train_datagen.flow_from_directory(
'/home/aslt/signData/top30Classes',
target_size=(224, 224),
batch_size= 15,
class_mode='categorical',
subset='validation')
filePath="/home/aslt/modelFiles/callbacksVGGNetWeights.hdf5"
checkpoint = ModelCheckpoint(filePath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
classifier.fit_generator(
train_generator,
steps_per_epoch = train_generator.samples,
validation_data = validation_generator,
validation_steps = validation_generator.samples,
epochs = 10,
callbacks = [checkpoint])
classifier.save('aslModelVGG.h5')
LeNet代码:
import keras
from keras.callbacks import ModelCheckpoint
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.models import Sequential
classifier = Sequential()
classifier.add(Conv2D(32, 3, 3, input_shape = (64,64, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2,2)))
classifier.add(Flatten())
classifier.add(Dense(1000, activation = 'relu'))
classifier.add(Dense(output_dim = 30, activation = 'softmax'))
classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
validation_split=0.25)
train_generator = train_datagen.flow_from_directory(
'/home/aslt/signData/top30Classes',
target_size=(64, 64),
batch_size= 15,
class_mode='categorical',
subset='training')
validation_generator = train_datagen.flow_from_directory(
'/home/aslt/signData/top30Classes',
target_size=(64, 64),
batch_size= 15,
class_mode='categorical',
subset='validation')
filepath="/home/aslt/modelFiles"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
classifier.fit_generator(
train_generator,
steps_per_epoch = train_generator.samples,
validation_data = validation_generator,
validation_steps = validation_generator.samples,
epochs = 10,
callbacks = [checkpoint])
classifier.save('aslModelLeNet.h5')
另外,要清楚一点,我使用的是语音词典中的数据集。我将视频分割成图像。目的是能够通过图像分类识别美国手语单词。