我正在嘗試對在 PASCAL VOC 2012 數據集的 Imagenet 權重上預訓練的 ResNet50 模型執行遷移學習。由于它是一個多標簽數據集,我sigmoid在最后一層使用激活函數和binary_crossentropy損失。指標是precision,recall and accuracy。下面是我用來為 20 個類構建模型的代碼(PASCAL VOC 有 20 個類)。img_height,img_width = 128,128num_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.outputx = 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=Truefor layer in model.layers[:-3]: layer.trainable=Falseadam = 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 張圖像。
ResNet50 模型在 keras 中沒有通過遷移學習進行學習
慕的地6264312
2022-06-22 20:42:23