尽管我使用的是StratifiedKFold,但准确性始终为0.5

时间:2019-05-07 14:35:53

标签: deep-learning classification cross-validation resnet transfer-learning

我正在使用经过预先训练的ResNet50模型对malaria dataset进行分类。我在其后分别添加了两个密集层(分别具有1024个,2048个单元)和一个使用softmax函数的分类层(对于S型,结果更糟)。我使用StratifiedKFold验证了此模型,但第一次折叠后精度始终为0.5。

第一次折叠后,所有纪元都是这样的:

22047/22047  [==============================] - 37s 3ms/step - loss: 8.0596 - acc: 0.5000

这是我的模特

height = 100 #dimensions of image
width = 100
channel = 3 #RGB
classes = 2

batch_size = 64 #vary depending on the GPU
epochs = 10
folds = 5
optimizer = "Adam"
metrics = ["accuracy"]
loss = 'categorical_crossentropy'

random_state = 1377
chanDim = -1

model = ResNet50(include_top=False, weights="imagenet", input_shape=(height, width, channel))

# Get the ResNet50 layers up to res5c_branch2c
model = Model(input=model.input, output=model.get_layer('res5c_branch2c').output)

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

Flatten1 = Flatten()(model.output)

F1 = Dense(1024, activation='relu')(Flatten1)
D1 = Dropout(0.5)(F1)

F2 = Dense(2048, activation='relu')(D1)
D2 = Dropout(0.2)(F2)

F3 = Dense(classes, activation='softmax')(D2)

model = Model(inputs = model.input, outputs = F3)

# Compile the model
model.compile(loss = loss, optimizer = optimizer, metrics = metrics)

这是验证部分:

# Create a model compatible with sklearn
model = KerasClassifier(build_fn=customResnetBuild, epochs=epochs, batch_size=batch_size)
kfold = StratifiedKFold(n_splits=folds, shuffle=False, random_state=random_state)

# Make a custom score for classification report method to get results for mean of the all folds
def classification_report_with_accuracy_score(y_true, y_pred):
    originalclass.extend(y_true)
    predictedclass.extend(y_pred)
    return accuracy_score(y_true, y_pred) # return accuracy score

scores = cross_val_score(model, data, labels, cv=kfold, error_score="raise", scoring=make_scorer(classification_report_with_accuracy_score) )
print(classification_report(originalclass, predictedclass)) 

结果

Mean of results:  0.6404469896025613
          precision    recall  f1-score   support

       0       0.86      0.34      0.48     13781
       1       0.59      0.94      0.72     13779

   micro avg       0.64      0.64      0.64     27560
   macro avg       0.72      0.64      0.60     27560
weighted avg       0.72      0.64      0.60     27560

1 个答案:

答案 0 :(得分:0)

This是答案。概括地说,问题是#parameters比#dataset多,而usage of trainable=false是错误的。