-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
123 lines (98 loc) · 4.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
import argparse
import os
import yaml
import torch
from torch.utils.data import DataLoader
from tqdm import tqdm
import datasets
import modelSAM
import utils
from torchvision import transforms
from mmcv.runner import load_checkpoint
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)
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):
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')
for batch in pbar:
for k, v in batch.items():
batch[k] = v.cuda()
inp = batch['inp']
#print((batch['gt']).shape)
#print(inp.shape)
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()))
return val_metric1.item(), val_metric2.item(), val_metric3.item(), val_metric4.item()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--config')
parser.add_argument('--model')
parser.add_argument('--prompt', default='none')
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'])
print(type(dataset))
dataset = datasets.make(spec['wrapper'], args={'dataset': dataset})
print(type(dataset))
loader = DataLoader(dataset, batch_size=spec['batch_size'],
num_workers=8)
model = modelSAM.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)
print('metric1: {:.4f}'.format(metric1))
print('metric2: {:.4f}'.format(metric2))
print('metric3: {:.4f}'.format(metric3))
print('metric4: {:.4f}'.format(metric4))