-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMyTest.py
80 lines (67 loc) · 2.73 KB
/
MyTest.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
72
73
74
75
76
77
78
79
80
import torch
import torch.nn.functional as F
import numpy as np
import os, argparse
from scipy import misc
from lib.Fuzzy_Res2Net import FuzzyNet
from utils.dataloader import test_dataset
import imageio
def mean_iou_np(y_true, y_pred, **kwargs):
"""
compute mean iou for binary segmentation map via numpy
"""
axes = (0, 1)
intersection = np.sum(np.abs(y_pred * y_true), axis=axes)
mask_sum = np.sum(np.abs(y_true), axis=axes) + np.sum(np.abs(y_pred), axis=axes)
union = mask_sum - intersection
smooth = .001
iou = (intersection + smooth) / (union + smooth)
return iou
parser = argparse.ArgumentParser()
parser.add_argument('--testsize', type=int, default=352, help='testing size')
parser.add_argument('--pth_path', type=str, default='./snapshots/PraNet_Res2Net/PraNet-39.pth')
for _data_name in ['CVC-300', 'CVC-ClinicDB', 'Kvasir', 'CVC-ColonDB', 'ETIS-LaribPolypDB']:
data_path = '/home/kpatel/projects/def-wangcs/kpatel/cross_transformer/data_poly/TestDataset/{}/'.format(_data_name)
save_path = './results/PraNet/{}/'.format(_data_name)
opt = parser.parse_args()
model = FuzzyNet()
model.load_state_dict(torch.load(opt.pth_path))
model.cuda()
model.eval()
os.makedirs(save_path, exist_ok=True)
image_root = '{}/images/'.format(data_path)
gt_root = '{}/masks/'.format(data_path)
test_loader = test_dataset(image_root, gt_root, opt.testsize)
DSC = 0
IOU = 0
for i in range(test_loader.size):
image, gt, name = test_loader.load_data()
gt = np.asarray(gt, np.float32)
gt /= (gt.max() + 1e-8)
image = image.cuda()
res5, res4, res3, res2 = model(image)
res = res2
res = F.upsample(res, size=gt.shape, mode='bilinear', align_corners=False)
res = res.sigmoid().data.cpu().numpy().squeeze()
res = (res - res.min()) / (res.max() - res.min() + 1e-8)
input = res
target = np.array(gt)
N = gt.shape
smooth = 1
input_flat = np.reshape(input, (-1))
target_flat = np.reshape(target, (-1))
intersection = (input_flat * target_flat)
#union = np.logical_or(input_flat, target_flat)
dice = (2 * intersection.sum() + smooth) / (input.sum() + target.sum() + smooth)
dice = '{:.4f}'.format(dice)
dice = float(dice)
DSC = DSC + dice
iou = (intersection.sum() + smooth) / (input.sum() + target.sum() + smooth - intersection.sum())
IOU = IOU + float(iou)
#mean_loss.append(np.mean(loss_bank))
#misc.imsave(save_path+name, res)
#imageio.imwrite(save_path+name, res)
print(_data_name)
print(DSC / test_loader.size)
print(IOU / test_loader.size)
#print(np.mean(iou_bank))