forked from YuhuiMa/DFN-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluation.py
More file actions
58 lines (41 loc) · 1.56 KB
/
evaluation.py
File metadata and controls
58 lines (41 loc) · 1.56 KB
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
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import argparse
import numpy as np
from PIL import Image
parser = argparse.ArgumentParser()
parser.add_argument("--gt_dir", help="path to folder containing ground truth")
parser.add_argument("--pred_dir", help="path to folder containing output images of the model")
parser.add_argument("--result_txt", help="path to txt file saving results")
a = parser.parse_args()
filenames = os.listdir(a.gt_dir)
mean_iou = 0
count = 0
if os.path.exists(a.result_txt):
os.remove(a.result_txt)
fd = open(a.result_txt, 'a')
for filename in filenames:
if os.path.exists(a.pred_dir + "/" + filename):
gt = Image.open(a.gt_dir + "/" + filename).convert("L")
gt_map = np.array(gt, np.bool).astype(np.float32)
gt_count = np.sum(gt_map)
pred = Image.open(a.pred_dir + "/" + filename).convert("L")
pred_map = np.array(pred, np.bool).astype(np.float32)
pred_count = np.sum(pred_map)
overlap_map = output_map * gt_map
overlap_count = np.sum(overlap_map)
iou = overlap_count / (gt_count + output_count - overlap_count)
mean_iou += iou
count += 1
print("IOU of " + filename + " is: " + str(iou))
fd.write("IOU of " + filename + " is: " + str(iou) + "\n")
if count == 0:
print("No images exist in both " + a.gt_dir + " and " + a.pred_dir)
fd.write("No images exist in both " + a.gt_dir + " and " + a.pred_dir)
else:
mean_iou /= count
print("mean iou: {}".format(mean_iou))
fd.write("mean iou: {}".format(mean_iou))