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)