-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
73 lines (56 loc) · 1.89 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
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '1'
import os.path as op
import numpy as np
from tqdm import tqdm
import torch
from torchvision import transforms as T
from torchvision.transforms import InterpolationMode
from dec import CLIP
from ds import get_dl
import cfg
tensor2numpy = lambda x: x.detach().cpu().numpy()
def load_dec():
dec = CLIP(cfg)
dec = dec.to(cfg.device)
dec_ckpt = torch.load(cfg.dec_ckpt_path)
dec.load_state_dict(dec_ckpt)
dec.eval()
return dec
def load_gt(gt_path):
with open(gt_path, 'r') as f:
gts = f.read()
gts = [int(gt) for gt in gts]
gts = np.array(gts)
return gts
def test(dl, dec, gts):
preds = []
with torch.no_grad():
for font_img, path in tqdm(dl):
font_img = font_img.to(cfg.device)
logit = dec(font_img)
pred = torch.argmax(logit, dim=1)
preds.append(tensor2numpy(pred))
preds = np.concatenate(preds, axis=0)
acc = (preds == gts).sum().item() / len(gts)
print(f'Accuracy: {acc:.4f}')
return preds, acc
def main(test_dir):
cfg.device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
cfg.data_dir = test_dir
dl = get_dl(cfg, transform=T.Compose([
T.Resize((cfg.font_img_size, cfg.font_img_size), interpolation=InterpolationMode.BILINEAR),
T.Resize((cfg.clip_img_size, cfg.clip_img_size), interpolation=InterpolationMode.BILINEAR),
T.ToTensor(),
T.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
]))
dec = load_dec()
gts = load_gt(cfg.gt_path)
pred, acc = test(dl, dec, gts)
return pred, acc
if __name__ == '__main__':
for pt in cfg.pt_list:
print('pt: {}'.format(pt))
for scenario in cfg.scenario_list:
print('scenario: {}'.format(scenario))
main(test_dir=op.join(cfg.root, f'{scenario}/FontGuard_{cfg.font_name}_{pt}'))