0

I followed this link How to calculate F1 Macro in Keras? to calculate F1 score of my binary data.

And I also wrote my own code to calculate F1 score after the evaluation and prediction is completed. My own code is:

model = VGG.build(80, 80, 6)
model.load_weights('/home/matlabclient01/Documents/Warfana/Final/models_6/m13.h5')
model.compile(loss="binary_crossentropy", optimizer=opt, metrics=[f1])

predY = model.predict(data, batch_size=1000, verbose=1)
y_classes = predY.argmax(axis=-1)    #y_classes contain predictions

#data1 contain actual labels
temp = data1
temp = np.array(temp)
results = y_classes == temp

TP = TN = FP = FN = 0

if len(y_classes) == len(temp):
    for i in range(len(y_classes)):
        if y_classes[i] == temp[i] and temp[i] == 1:
            TP=TP+1
        elif y_classes[i] == temp[i] and temp[i] == 0:
            TN=TN+1
        elif y_classes[i] != temp[i] and temp[i] == 1:
            FN=FN+1
        elif y_classes[i] != temp[i] and temp[i] == 0:
            FP=FP+1

print("TP = ",TP)
print("FP = ",FP)
print("TN = ",TN)
print("FN = ",FN)

precision = TP/(len(y_classes[y_classes==1]))
recall = TP/(len(temp[temp==1]))
fmeasure = (2*precision*recall)/(precision+recall)

print("FMeasure = ",fmeasure)

These are the different results. Keras Custom Formula and my own code's Results

Results:

1st F-measure = 0.822 (Results from Keras Custom Formula)

Last Fmeasure = 0.31 (Results from my own code)

  • Have you tried substituting the divisor in your `precision` with `(TP+FP)`, and the divisor in `recall` with `(TP+FN)`? Just to make sure your `len()` calls do return something similar. – dennlinger Sep 17 '18 at 06:03
  • Yeah I have done that, it gives same result. – Warfana Ali Sep 17 '18 at 06:09
  • Well `len(temp[temp=1]) != (TP+FN)` – Raunaq Jain Sep 17 '18 at 06:11
  • Aside from that, the post you quoted already gives an answer to the question: It is a known bug in Keras (see [here](https://github.com/keras-team/keras/issues/5400)). Why the custom kernel does not work is weird, though. – dennlinger Sep 17 '18 at 06:12

0 Answers0