训练损失减少,验证损失一点也不减少,两者的准确性均较低

时间:2019-02-04 19:02:32

标签: python tensorflow keras conv-neural-network training-data

因此,我认识到这是一个常见问题,但我很好奇是否有人可以帮助发现我的代码或方法中的错误。我正在微调喀拉拉邦的DenseNet CNN(在ImageNet上进行了预训练),以对牡蛎和非牡蛎航空图像进行分类。您可以在此处看到我的图像的示例,其中[1,0]表示非牡蛎,[0,1]表示牡蛎。

我的模型能够根据训练损失对我的训练数据进行过拟合,但验证损失从未真正减少。话虽如此,当我实际评估训练和验证数据的准确性时,它们都徘徊在65%左右的准确性上-在二进制分类中非常糟糕。我的火车/ val分区是从相同的随机混编主数据集中创建的,每个牡蛎图像和非牡蛎图像大约有5000个。任何想法可能出什么问题吗?我认识到该图像与ImageNet完全不同,但我仍然认为它会提供一些改进。

enter image description here

我正在微调Keras中的内置DenseNet:

from keras.applications.densenet import DenseNet121
from keras.optimizers import SGD

base_model=DenseNet121(weights='imagenet',include_top=False) #imports the mobilenet model and discards the last 1000 neuron layer.

x=base_model.output
x=GlobalAveragePooling2D()(x)
x=Dense(512,activation='relu')(x) #we add dense layers so that the model can learn more complex functions and classify for better results.
#x=Dense(1024,activation='relu')(x) #dense layer 2
x=Dense(256,activation='relu')(x) #dense layer 3
predictions=Dense(2,activation='softmax')(x) #final layer with softmax activation

# this is the model we will train
model = Model(inputs=base_model.input, outputs=predictions)

首先冻结除了我的随机初始化的完全连接的新层之外的所有内容:

for layer in base_model.layers:
    layer.trainable=False

然后我编译并拟合:

model.compile(optimizer=SGD(lr=0.0001, momentum=0.9, decay=0.0, nesterov=False), 
              loss='categorical_crossentropy',metrics=['accuracy'])

batch_size = 20
INPUT_SHAPE = (224,224)
data_dir = '../data/patch/'

# set the model up to save the best weights based on val_loss
#filepath="../data/patch/logs/mobilenet--{epoch:02d}-{val_loss:.2f}.hdf5"
#checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')
#callbacks_list = [checkpoint]

model.fit_generator(
    generate_data(data_dir, batch_size, 'train', augment=False),
    len(os.listdir(data_dir + 'images/')) // batch_size,
    epochs=50,
    validation_data=generate_data(data_dir, batch_size, 'val'),
    validation_steps= 700 // batch_size,
)

接下来,我解冻到DenseNet的顶部3rd,重新编译,然后再次运行

for layer in model.layers[:368]:
    layer.trainable=False
for layer in model.layers[368:]:
    layer.trainable=True

model.compile(optimizer=SGD(lr=0.0001, momentum=0.9, decay=0.0, nesterov=False), 
              loss='categorical_crossentropy',metrics=['accuracy'])

model.fit_generator(
    generate_data(data_dir, batch_size, 'train', augment=False),
    len(os.listdir(data_dir + 'images/')) // batch_size,
    epochs=50,
    validation_data=generate_data(data_dir, batch_size, 'val'),
    validation_steps= 700 // batch_size,
)

0 个答案:

没有答案