-
Notifications
You must be signed in to change notification settings - Fork 348
/
Metrics.py
50 lines (35 loc) · 1.31 KB
/
Metrics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import numpy as np
from scipy import spatial
def dice_coeff(im1, im2, empty_score=1.0):
"""Calculates the dice coefficient for the images"""
im1 = np.asarray(im1).astype(np.bool)
im2 = np.asarray(im2).astype(np.bool)
if im1.shape != im2.shape:
raise ValueError("Shape mismatch: im1 and im2 must have the same shape.")
im1 = im1 > 0.5
im2 = im2 > 0.5
im_sum = im1.sum() + im2.sum()
if im_sum == 0:
return empty_score
# Compute Dice coefficient
intersection = np.logical_and(im1, im2)
#print(im_sum)
return 2. * intersection.sum() / im_sum
def numeric_score(prediction, groundtruth):
"""Computes scores:
FP = False Positives
FN = False Negatives
TP = True Positives
TN = True Negatives
return: FP, FN, TP, TN"""
FP = np.float(np.sum((prediction == 1) & (groundtruth == 0)))
FN = np.float(np.sum((prediction == 0) & (groundtruth == 1)))
TP = np.float(np.sum((prediction == 1) & (groundtruth == 1)))
TN = np.float(np.sum((prediction == 0) & (groundtruth == 0)))
return FP, FN, TP, TN
def accuracy_score(prediction, groundtruth):
"""Getting the accuracy of the model"""
FP, FN, TP, TN = numeric_score(prediction, groundtruth)
N = FP + FN + TP + TN
accuracy = np.divide(TP + TN, N)
return accuracy * 100.0