-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.py
51 lines (39 loc) · 1.73 KB
/
evaluate.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
51
import os
from sklearn.metrics import jaccard_score, f1_score, precision_recall_fscore_support
import cv2
import matplotlib.pyplot as plt
import argparse
def calculate_metrics(pred_path, gt_path):
preds = sorted(os.listdir(pred_path))
gts = sorted(os.listdir(gt_path))
iou_total, dice_total = 0, 0 # J & F
for gt, pred in zip(gts, preds):
gt_image = cv2.imread(os.path.join(gt_path, gt), cv2.IMREAD_GRAYSCALE)
pred_image = cv2.imread(os.path.join(pred_path, pred), cv2.IMREAD_GRAYSCALE)
gt_image[gt_image != 0] = 255
pred_image[pred_image > 0] = 255
gt_image = gt_image / 255
pred_image = pred_image / 255
iou = jaccard_score(gt_image.flatten(), pred_image.flatten()) # Jaccard
dice = f1_score(gt_image.flatten(), pred_image.flatten()) # F1 score
iou_total += iou
dice_total += dice
# p,r,_,_ = precision_recall_fscore_support(gt_image.flatten(), pred_image.flatten())
# f1_ = 2*p*r/(p+r)
# f_total += f1_
avrg_iou = iou_total / len(gts)
avrg_dice = dice_total / len(gts)
return avrg_iou, avrg_dice
if __name__ == '__main__':
print('start evaluation...')
parser = argparse.ArgumentParser()
parser.add_argument('--pred_path', type=str, default='./results/predicted_mask', help='path to predicted masks')
parser.add_argument('--gt_path', type=str, help='path to ground truth masks')
args = parser.parse_args()
pred_path = args.pred_path
gt_path = args.gt_path
# pred_path = '/data/SurgicalToolTracking/results/predicted_mask'
# gt_path = '/data/CholecSeg8k/val/video1/masks'
avrg_iou, avrg_dice = calculate_metrics(pred_path, gt_path)
print(avrg_iou)
print(avrg_dice)