为什么我的Keras CNN模型无法提高准确性?

时间:2019-08-16 14:33:14

标签: python tensorflow keras neural-network deep-learning

我正在尝试使用Keras训练基于VGG16的CNN模型,而我的准确度不能超过40%〜50%。我使用了辍学层,l2正则化,权重初始化程序和许多参数来增强数据,以找到最佳结果,但是什么也没发生。

我尝试在Google Colab上运行我的模型,它不是一个巨大的数据集,只是PASCAL VOC 2012挑战中的3个文件夹,您可以在上面看到我的代码:

#imports ...

#Path to train dir ...

#Model
init = keras.initializers.RandomUniform(minval=0, maxval=0.001)
input_shape = (224,224,3)
img_input = layers.Input(shape=input_shape)
x = layers.Conv2D(64, (3, 3),
                  activation='relu',
                  padding='valid',
                  kernel_initializer=init,
                  kernel_regularizer=regularizers.l2(0.00001),
                  name='block1_conv1')(img_input)
x = layers.Conv2D(64, (3, 3),
                  activation='relu',
                  padding='valid',
                  kernel_initializer=init,
                  kernel_regularizer=regularizers.l2(0.00001),
                  name='block1_conv2')(x)
x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)

# Block 2
x = layers.Conv2D(128, (3, 3),
                  activation='relu',
                  padding='valid',
                  kernel_initializer=init,
                  kernel_regularizer=regularizers.l2(0.00001),
                  name='block2_conv1')(x)
x = layers.Conv2D(128, (3, 3),
                  activation='relu',
                  padding='valid',
                  kernel_initializer=init,
                  kernel_regularizer=regularizers.l2(0.00001),
                  name='block2_conv2')(x)
x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)

# Block 3
x = layers.Conv2D(256, (3, 3),
                  activation='relu',
                  padding='valid',
                  kernel_initializer=init,
                  kernel_regularizer=regularizers.l2(0.00001),
                  name='block3_conv1')(x)
x = layers.Conv2D(256, (3, 3),
                  activation='relu',
                  padding='valid',
                  kernel_initializer=init,
                  kernel_regularizer=regularizers.l2(0.00001),
                  name='block3_conv2')(x)
x = layers.Conv2D(256, (3, 3),
                  activation='relu',
                  padding='valid',
                  kernel_initializer=init,
                  kernel_regularizer=regularizers.l2(0.00001),
                  name='block3_conv3')(x)
x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)

# Block 4
x = layers.Conv2D(512, (3, 3),
                  activation='relu',
                  padding='valid',
                  kernel_initializer=init,
                  kernel_regularizer=regularizers.l2(0.00001),
                  name='block4_conv1')(x)
x = layers.Conv2D(512, (3, 3),
                  activation='relu',
                  padding='valid',
                  kernel_initializer=init,
                  kernel_regularizer=regularizers.l2(0.00001),
                  name='block4_conv2')(x)
x = layers.Conv2D(512, (3, 3),
                  activation='relu',
                  padding='valid',
                  kernel_initializer=init,
                  kernel_regularizer=regularizers.l2(0.00001),
                  name='block4_conv3')(x)
x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)

# Block 5
x = layers.Conv2D(512, (3, 3),
                  activation='relu',
                  padding='valid',
                  kernel_initializer=init,
                  kernel_regularizer=regularizers.l2(0.00001),
                  name='block5_conv1')(x)
x = layers.Conv2D(512, (3, 3),
                  activation='relu',
                  padding='valid',
                  kernel_initializer=init,
                  kernel_regularizer=regularizers.l2(0.00001),
                  name='block5_conv2')(x)
x = layers.Conv2D(10, (3, 3),
                  activation='relu',
                  padding='valid',
                  kernel_initializer=init,
                  kernel_regularizer=regularizers.l2(0.00001),
                  name='block5_conv3')(x)
x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)

x = layers.Flatten(name='flatten')(x)
x = layers.Dense(4096, activation='relu', name='fc1', kernel_regularizer=regularizers.l2(0.00001), kernel_initializer=init)(x)
x = layers.Dropout(0.5)(x)
x = layers.Dense(4096, activation='relu', name='fc2', kernel_regularizer=regularizers.l2(0.00001), kernel_initializer=init)(x)
x = layers.Dropout(0.5)(x)
x = layers.Dense(3, activation='softmax', name='predictions')(x)

model = Model(img_input, x, name='overfeat_vgg16')
#model.summary()

#creating generators
train_datagen = ImageDataGenerator(rescale=1./255, zoom_range=0.4, horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(train_dir, classes=["bird","cat","dog"], shuffle=True, target_size=(224, 224), batch_size=64, class_mode='categorical')
validation_generator = test_datagen.flow_from_directory(train_dir, classes=["bird","cat","dog"], shuffle=True, target_size=(224, 224), batch_size=64, class_mode='categorical')

#compiling model
sgd = keras.optimizers.SGD(lr=0.01, momentum=0.9, decay=0.0005, nesterov=True)
#adam = keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])

#training
model.fit_generator(
        train_generator,
        steps_per_epoch=5850 // 64,
        epochs=100,
        shuffle = True,
        validation_data=validation_generator,
        validation_steps=5850 // 64)
model.save_weights('first_try.h5')

有当前结果:

史诗1/100 91/91 [=============================]-172秒2秒/步-损失:1.0449-acc:0.4301-val_loss :1.0432-val_acc:0.4239

史诗2/100 91/91 [==============================]-155秒2秒/步-损失:1.0421-acc:0.4188-val_loss :1.0411-val_acc:0.3984

史诗3/100 91/91 [==============================]-155秒2秒/步-损失:1.0388-acc:0.4263-val_loss :1.0382-val_acc:0.4250

...并且对于所有时期,结果保持不变。

我只是不知道该怎么做才能获得更好的结果。

0 个答案:

没有答案