-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_iou1.py
57 lines (42 loc) · 1.43 KB
/
test_iou1.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
import argparse
import os
import numpy as np
from PIL import Image
def iou(mask_path,gt_path):
files=os.listdir(gt_path)
num = 0
GT = np.array([0])
MASK = np.array([0])
for file in files:
mask1=mask_path+'/'+file.split('.')[0]+'.jpg'
gt1=gt_path+'/'+file
mask=np.array(Image.open(mask1).convert('L'))/255.0
gt=np.array(Image.open(gt1).convert('L'))/255.0
# mask = 1-mask
if gt.shape == mask.shape:
num+=1
if num%10 == 0:
print(num)
gt[gt>0.5] = 1.0
gt[gt<=0.5] = 0.0
mask[mask>0.5] = 1.0
mask[mask<=0.5] = 0.0
gt = 1-gt
mask = 1-mask
GT = np.concatenate((GT,gt.reshape(-1)),0)
MASK = np.concatenate((MASK,mask.reshape(-1)),0)
pred = MASK[1:]
target = GT[1:]
pred_inds = pred == 1
target_inds = target == 1
intersection = (pred_inds[target_inds]).sum() # Cast to long to prevent overflows
union = pred_inds.sum() + target_inds.sum() - intersection
ious = float(intersection) / float(union)
print(ious)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='PyTorch Template')
parser.add_argument('--mask_path', type=str)
parser.add_argument('--gt_path', type=str)
args=parser.parse_args()
iou(args.mask_path,args.gt_path)
print(args.mask_path.split('/')[-1])