训练期间的准确度为10%,但是在火车数据上使用相同模型进行的预测仅提供3.5%的准确度

时间:2020-08-24 09:46:07

标签: python-3.x tensorflow machine-learning keras computer-vision

我正在用超过100次迭代/纪元以下的代码来训练VGG16 block5,在block5的输出之后添加了两个密集且丢失的层。

seq = Sequential()
vgg_base = VGG16(weights='imagenet', input_shape=(224, 224, 3),include_top=False, pooling='avg')
seq.add(vgg_base)


for layer in vgg_base.layers:
    layer.trainable = False
for layer in vgg_base.layers[:14]:
    layer.trainable = False
for layer in vgg_base.layers[15:]:
    layer.trainable = True
    
regularizer = tf.keras.regularizers.l2(1e-2)

for layer in vgg_base.layers[15:]:
    for attr in ['kernel_regularizer']:
        if hasattr(layer, attr):
          setattr(layer, attr, regularizer)

vgg_base.summary()
#seq.add(Flatten())

input_a = Input(shape=(224, 224, 3))

out_a = seq(input_a)

hidden1 = Dense(128, activation='relu', name='dense_1', kernel_regularizer=tf.keras.regularizers.l2(0.001),
                              activity_regularizer=tf.keras.regularizers.l2(0.001))(out_a)
hidden_drp1 = Dropout(0.5)(hidden1)
hidden2 = Dense(32, activation='relu', name='dense_2',kernel_regularizer=tf.keras.regularizers.l2(0.001),
                              activity_regularizer=tf.keras.regularizers.l2(0.001))(hidden_drp1)
hidden_drp2 = Dropout(0.2)(hidden2)
out = Dense(1, activation='sigmoid', name='dense_3')(hidden_drp2)

model = Model(input_a, out)

训练设置:

if os.path.exists(filepath):
     print('weights found... loading...')
     model.load_weights(filepath)

train_datagen = ImageDataGenerator(rescale=1.0/255)
                                   #,validation_split=0.4)
#        validation_split=0.3) # set validation split

train_path = 'C:/fromtrainv4/'

train_set = train_datagen.flow_from_directory(
    train_path,
    target_size=(224,224),
    shuffle=True,
    color_mode='rgb',
    batch_size=100,
    class_mode='binary')
optim = Adam(lr=0.001)
loss = 'binary_crossentropy'
metrics = ['binary_accuracy', 'acc']

model.compile(loss=loss,
              optimizer=optim,
              metrics=metrics)

checkpoint = ModelCheckpoint(filepath,
                             monitor='loss',
                             verbose=1,
                             save_weights_only=False,
                             save_best_only=True,
                             mode='min')

my_callback = [tf.keras.callbacks.EarlyStopping(monitor='loss', patience=10),
               checkpoint,tf.keras.callbacks.TensorBoard(log_dir='C:\\traning_logs\\')]
step_size_train = train_set.n//train_set.batch_size
history = model.fit_generator(train_set,
                             steps_per_epoch=step_size_train,
                             shuffle=False,
                             epochs=100,
                             callbacks=my_callback,
                             class_weight='balanced')

我确保数据的缩放比例与image_generator和其他所有缩放比例相同。 我使用以下代码加载模型:

from tensorflow.keras.models import load_model
model = load_model('C:/models/model.h5')

系统规格:

Processor: Intell Xeon
Ram: 16GB
GPU: NVIDIA RTX 2080 Super
Memory: M2. 256GB

Interpreter: Spyder Python 3.7.1
tensorflow version: 1.14.0
keras version: 2.3.1

有人可以指导我我可能做错了什么。如果需要更多有关此信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

我想您的模型正在执行所谓的“过度拟合”。

简而言之,通过对同一数据集进行100次训练迭代,它会牢记您的训练数据集,因此在对其进行测试时会获得很好的结果。但是,对于使用不同输入的新数据集进行测试会导致结果降低的结果,目前尚无法找到解决问题的一般方法。