-
Notifications
You must be signed in to change notification settings - Fork 5
/
evaluate.py
157 lines (107 loc) · 4.68 KB
/
evaluate.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import argparse
from omegaconf import OmegaConf
import sys
import os
import tqdm
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from utils.htr_dataset import HTRDataset
from models import HTRNet
from utils.metrics import CER, WER
class HTREval(nn.Module):
def __init__(self, config):
super(HTREval, self).__init__()
self.config = config
self.prepare_dataloaders()
self.prepare_net()
def prepare_dataloaders(self):
config = self.config
# prepare datset loader
dataset_folder = config.data.path
fixed_size = (config.preproc.image_height, config.preproc.image_width)
val_set = HTRDataset(dataset_folder, 'val', fixed_size=fixed_size, transforms=None)
print('# validation lines ' + str(val_set.__len__()))
test_set = HTRDataset(dataset_folder, 'test', fixed_size=fixed_size, transforms=None)
print('# testing lines ' + str(test_set.__len__()))
# load classes from the training set saved in the data folder
classes = np.load(os.path.join(dataset_folder, 'classes.npy'))
val_loader = DataLoader(val_set, batch_size=config.eval.batch_size,
shuffle=False, num_workers=config.eval.num_workers)
test_loader = DataLoader(test_set, batch_size=config.eval.batch_size,
shuffle=False, num_workers=config.eval.num_workers)
self.loaders = {'val': val_loader, 'test': test_loader}
# create dictionaries for character to index and index to character
# 0 index is reserved for CTC blank
cdict = {c:(i+1) for i,c in enumerate(classes)}
icdict = {(i+1):c for i,c in enumerate(classes)}
self.classes = {
'classes': classes,
'c2i': cdict,
'i2c': icdict
}
def prepare_net(self):
config = self.config
device = config.device
print('Preparing Net - Architectural elements:')
print(config.arch)
classes = self.classes['classes']
net = HTRNet(config.arch, len(classes) + 1)
if config.resume is not None:
print('resuming from checkpoint: {}'.format(config.resume))
load_dict = torch.load(config.resume)
load_status = net.load_state_dict(load_dict, strict=True)
print(load_status)
net.to(device)
# print number of parameters
n_params = sum(p.numel() for p in net.parameters() if p.requires_grad)
print('Number of parameters: {}'.format(n_params))
self.net = net
def decode(self, tdec, tdict, blank_id=0):
tt = [v for j, v in enumerate(tdec) if j == 0 or v != tdec[j - 1]]
dec_transcr = ''.join([tdict[t] for t in tt if t != blank_id])
return dec_transcr
def test(self, epoch, tset='test'):
config = self.config
device = config.device
self.net.eval()
if tset=='test':
loader = self.loaders['test']
elif tset=='val':
loader = self.loaders['val']
else:
print("not recognized set in test function")
print('####################### Evaluating {} set at epoch {} #######################'.format(tset, epoch))
cer, wer = CER(), WER(mode=config.eval.wer_mode)
for (imgs, transcrs) in tqdm.tqdm(loader):
imgs = imgs.to(device)
with torch.no_grad():
o = self.net(imgs)
# if o tuple keep only the first element
if config.arch.head_type == 'both':
o = o[0]
tdecs = o.argmax(2).permute(1, 0).cpu().numpy().squeeze()
for tdec, transcr in zip(tdecs, transcrs):
transcr = transcr.strip()
dec_transcr = self.decode(tdec, self.classes['i2c']).strip()
cer.update(dec_transcr, transcr)
wer.update(dec_transcr, transcr)
cer_score = cer.score()
wer_score = wer.score()
print('CER at epoch {}: {:.3f}'.format(epoch, cer_score))
print('WER at epoch {}: {:.3f}'.format(epoch, wer_score))
self.net.train()
def parse_args():
conf = OmegaConf.load(sys.argv[1])
OmegaConf.set_struct(conf, True)
sys.argv = [sys.argv[0]] + sys.argv[2:] # Remove the configuration file name from sys.argv
conf.merge_with_cli()
return conf
if __name__ == '__main__':
# ----------------------- initialize configuration ----------------------- #
config = parse_args()
max_epochs = config.train.num_epochs
htr_eval = HTREval(config)
htr_eval.test(0, 'val')
htr_eval.test(0, 'test')