0

I'm using pre-trained ResNet50 model to classify malaria dataset. I added two dense layer after it with 1024, 2048 units respectively and one classification layer using softmax function (results are worse with sigmoid). I used StratifiedKFold to validate this model but accuracy is always 0.5 after first fold.

After first fold all the epochs are the same like this:

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

This is my model:

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)

This is validation part:

# 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)) 

Result

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
Yunus YILDIRIM
  • 91
  • 1
  • 13

1 Answers1

0

This is the answer. To recap the problem is #parameters more than #dataset and usage of trainable=false is wrong.

Yunus YILDIRIM
  • 91
  • 1
  • 13