我正在尝试对在 PASCAL VOC 2012 数据集的 Imagenet 权重上预训练的 ResNet50 模型执行迁移学习。由于它是一个多标签数据集,我sigmoid在最后一层使用激活函数和binary_crossentropy损失。指标是precision,recall and accuracy。下面是我用来为 20 个类构建模型的代码(PASCAL VOC 有 20 个类)。
img_height,img_width = 128,128
num_classes = 20
#If imagenet weights are being loaded,
#input must have a static square shape (one of (128, 128), (160, 160), (192, 192), or (224, 224))
base_model = applications.resnet50.ResNet50(weights= 'imagenet', include_top=False, input_shape= (img_height,img_width,3))
x = base_model.output
x = GlobalAveragePooling2D()(x)
#x = Dropout(0.7)(x)
predictions = Dense(num_classes, activation= 'sigmoid')(x)
model = Model(inputs = base_model.input, outputs = predictions)
for layer in model.layers[-2:]:
layer.trainable=True
for layer in model.layers[:-3]:
layer.trainable=False
adam = Adam(lr=0.0001)
model.compile(optimizer= adam, loss='binary_crossentropy', metrics=['accuracy',precision_m,recall_m])
#print(model.summary())
X_train, X_test, Y_train, Y_test = train_test_split(x_train, y, random_state=42, test_size=0.2)
savingcheckpoint = ModelCheckpoint('ResnetTL.h5',monitor='val_loss',verbose=1,save_best_only=True,mode='min')
earlystopcheckpoint = EarlyStopping(monitor='val_loss',patience=10,verbose=1,mode='min',restore_best_weights=True)
model.fit(X_train, Y_train, epochs=epochs, validation_data=(X_test,Y_test), batch_size=batch_size,callbacks=[savingcheckpoint,earlystopcheckpoint],shuffle=True)
model.save_weights('ResnetTLweights.h5')
它运行了 35 个 epoch,直到 earlystopping,指标如下(没有 Dropout 层):
我发现验证集的准确率和召回率与带有和不带有 dropout 层的训练集相比非常低。我该如何解释这个?这是否意味着模型过度拟合。如果是这样,我该怎么办?截至目前,模型预测是相当随机的(完全不正确)。数据集大小为 11000 张图像。
暮色呼如
慕森王
相关分类