-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
143 lines (116 loc) · 5.06 KB
/
test.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import argparse
import os
import yaml
import torch
from torch.utils.data import DataLoader
from tqdm import tqdm
import datasets
import models
import utils
from torchvision import transforms
from torchvision import utils as vutils
def batched_predict(model, inp, coord, bsize):
with torch.no_grad():
model.gen_feat(inp)
n = coord.shape[1]
ql = 0
preds = []
while ql < n:
qr = min(ql + bsize, n)
pred = model.query_rgb(coord[:, ql: qr, :])
preds.append(pred)
ql = qr
pred = torch.cat(preds, dim=1)
return pred, preds
def tensor2PIL(tensor):
toPIL = transforms.ToPILImage()
return toPIL(tensor)
def img_reverse(tensor):
reverse = transforms.Compose([
transforms.Normalize(mean=[-0.485 / 0.229, -0.456 / 0.224, -0.406 / 0.225],
std=[1. / 0.229, 1. / 0.224, 1. / 0.225]),
transforms.Resize((480, 480)),
])
return reverse(tensor)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def eval_psnr(loader, model, data_norm=None, eval_type=None, eval_bsize=None,
verbose=False, save=False):
model.eval()
if data_norm is None:
data_norm = {
'inp': {'sub': [0], 'div': [1]},
'gt': {'sub': [0], 'div': [1]}
}
if eval_type == 'f1':
metric_fn = utils.calc_f1
metric1, metric2, metric3, metric4 = 'f1', 'auc', 'none', 'none'
elif eval_type == 'fmeasure':
metric_fn = utils.calc_fmeasure
metric1, metric2, metric3, metric4 = 'f_mea', 'mae', 'none', 'none'
elif eval_type == 'ber':
metric_fn = utils.calc_ber
metric1, metric2, metric3, metric4 = 'shadow', 'non_shadow', 'ber', 'none'
elif eval_type == 'cod':
metric_fn = utils.calc_cod
metric1, metric2, metric3, metric4 = 'sm', 'em', 'wfm', 'mae'
val_metric1 = utils.Averager()
val_metric2 = utils.Averager()
val_metric3 = utils.Averager()
val_metric4 = utils.Averager()
pbar = tqdm(loader, leave=False, desc='val')
cnt = 0
if not os.path.exists('./tmp'):
os.makedirs('./tmp')
for batch in pbar:
for k, v in batch.items():
batch[k] = v.cuda()
inp = batch['inp']
with torch.no_grad():
pred = torch.sigmoid(model.infer(inp))
result1, result2, result3, result4 = metric_fn(pred, batch['gt'])
val_metric1.add(result1.item(), inp.shape[0])
val_metric2.add(result2.item(), inp.shape[0])
val_metric3.add(result3.item(), inp.shape[0])
val_metric4.add(result4.item(), inp.shape[0])
if verbose:
pbar.set_description('val {} {:.4f}'.format(metric1, val_metric1.item()))
pbar.set_description('val {} {:.4f}'.format(metric2, val_metric2.item()))
pbar.set_description('val {} {:.4f}'.format(metric3, val_metric3.item()))
pbar.set_description('val {} {:.4f}'.format(metric4, val_metric4.item()))
if save:
for p in range(pred.shape[0]):
pil = tensor2PIL(torch.tensor(pred[p]*255, dtype=torch.uint8))
pil.save(f'tmp/{cnt}-{p}-pred.png')
pil = tensor2PIL(torch.tensor(batch['gt'][p]*255, dtype=torch.uint8))
pil.save(f'tmp/{cnt}-{p}-gt.png')
pil = img_reverse(torch.tensor(batch['inp'][p], dtype=torch.float32))
vutils.save_image(pil, f'tmp/{cnt}-{p}-fig.jpg')
cnt += 1
return val_metric1.item(), val_metric2.item(), val_metric3.item(), val_metric4.item()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--config', default='none')
parser.add_argument('--model', default='./configs/cod-tssam-vit-h.yaml')
parser.add_argument('--prompt', default='none')
parser.add_argument("--local_rank", type=int, default=-1, help="")
parser.add_argument('--save', default='False')
args = parser.parse_args()
with open(args.config, 'r') as f:
config = yaml.load(f, Loader=yaml.FullLoader)
spec = config['test_dataset']
dataset = datasets.make(spec['dataset'])
dataset = datasets.make(spec['wrapper'], args={'dataset': dataset})
loader = DataLoader(dataset, batch_size=spec['batch_size'],
num_workers=8, shuffle=False)
model = models.make(config['model']).cuda()
sam_checkpoint = torch.load(args.model, map_location='cuda:0')
model.load_state_dict(sam_checkpoint, strict=True)
metric1, metric2, metric3, metric4 = eval_psnr(loader, model,
data_norm=config.get('data_norm'),
eval_type=config.get('eval_type'),
eval_bsize=config.get('eval_bsize'),
verbose=True, save=args.save)
print('metric1: {:.4f}'.format(metric1))
print('metric2: {:.4f}'.format(metric2))
print('metric3: {:.4f}'.format(metric3))
print('metric4: {:.4f}'.format(metric4))