-
Notifications
You must be signed in to change notification settings - Fork 29
/
evaluate.py
71 lines (56 loc) · 2.13 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import numpy as np
import pandas as pd
import tifffile as tif
import argparse
import os
from collections import OrderedDict
from tqdm import tqdm
from train_tools.measures import evaluate_f1_score_cellseg
def main():
### Directory path arguments ###
parser = argparse.ArgumentParser("Compute F1 score for cell segmentation results")
parser.add_argument(
"--gt_path",
type=str,
help="path to ground truth; file names end with _label.tiff",
required=True,
)
parser.add_argument(
"--pred_path", type=str, help="path to segmentation results", required=True
)
parser.add_argument("--save_path", default=None, help="path where to save metrics")
args = parser.parse_args()
# Get files from the paths
gt_path, pred_path = args.gt_path, args.pred_path
names = sorted(os.listdir(pred_path))
names_total = []
precisions_total, recalls_total, f1_scores_total = [], [], []
for name in tqdm(names):
assert name.endswith(
"_label.tiff"
), "The suffix of label name should be _label.tiff"
# Load the images
gt = tif.imread(os.path.join(gt_path, name))
pred = tif.imread(os.path.join(pred_path, name))
# Evaluate metrics
precision, recall, f1_score = evaluate_f1_score_cellseg(gt, pred, threshold=0.5)
names_total.append(name)
precisions_total.append(np.round(precision, 4))
recalls_total.append(np.round(recall, 4))
f1_scores_total.append(np.round(f1_score, 4))
# Refine data as dataframe
cellseg_metric = OrderedDict()
cellseg_metric["Names"] = names_total
cellseg_metric["Precision"] = precisions_total
cellseg_metric["Recall"] = recalls_total
cellseg_metric["F1_Score"] = f1_scores_total
cellseg_metric = pd.DataFrame(cellseg_metric)
print("mean F1 Score:", np.mean(cellseg_metric["F1_Score"]))
# Save results
if args.save_path is not None:
os.makedirs(args.save_path, exist_ok=True)
cellseg_metric.to_csv(
os.path.join(args.save_path, "seg_metric.csv"), index=False
)
if __name__ == "__main__":
main()