forked from Rayicer/TransFuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_isic.py
88 lines (67 loc) · 2.65 KB
/
test_isic.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
81
82
83
84
85
86
87
88
import torch
import torch.nn.functional as F
import numpy as np
import os, argparse
from lib.TransFuse import TransFuse_S
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
def mean_dice_np(y_true, y_pred, **kwargs):
"""
compute mean dice for binary segmentation map via numpy
"""
axes = (0, 1) # W,H axes of each image
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)
smooth = .001
dice = 2*(intersection + smooth)/(mask_sum + smooth)
return dice
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--ckpt_path', type=str, default='snapshots/TransFuse-19_best.pth')
parser.add_argument('--test_path', type=str,
default='data/', help='path to test dataset')
parser.add_argument('--save_path', type=str, default=None, help='path to save inference segmentation')
opt = parser.parse_args()
model = TransFuse_S().cuda()
model.load_state_dict(torch.load(opt.ckpt_path))
model.cuda()
model.eval()
if opt.save_path is not None:
os.makedirs(opt.save_path, exist_ok=True)
print('evaluating model: ', opt.ckpt_path)
image_root = '{}/data_test.npy'.format(opt.test_path)
gt_root = '{}/mask_test.npy'.format(opt.test_path)
test_loader = test_dataset(image_root, gt_root)
dice_bank = []
iou_bank = []
acc_bank = []
for i in range(test_loader.size):
image, gt = test_loader.load_data()
gt = 1*(gt>0.5)
image = image.cuda()
with torch.no_grad():
_, _, res = model(image)
res = res.sigmoid().data.cpu().numpy().squeeze()
res = 1*(res > 0.5)
if opt.save_path is not None:
imageio.imwrite(opt.save_path+'/'+str(i)+'_pred.jpg', res)
imageio.imwrite(opt.save_path+'/'+str(i)+'_gt.jpg', gt)
dice = mean_dice_np(gt, res)
iou = mean_iou_np(gt, res)
acc = np.sum(res == gt) / (res.shape[0]*res.shape[1])
acc_bank.append(acc)
dice_bank.append(dice)
iou_bank.append(iou)
print('Dice: {:.4f}, IoU: {:.4f}, Acc: {:.4f}'.
format(np.mean(dice_bank), np.mean(iou_bank), np.mean(acc_bank)))