-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
151 lines (127 loc) · 4.9 KB
/
utils.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
#!/usr/bin/python
# encoding: utf-8
import chainer
from chainer import Variable
import collections
import numpy as np
from dataset import resizeNormalize
class strLabelConverter(object):
"""Convert between str and label.
NOTE:
Insert `blank` to the alphabet for CTC.
Args:
alphabet (str): set of the possible characters.
ignore_case (bool, default=True): whether or not to ignore all of the case.
"""
def __init__(self, alphabet, ignore_case=True):
self._ignore_case = ignore_case
if self._ignore_case:
alphabet = alphabet.lower()
self.alphabet = alphabet + '-' # for `-1` index
self.dict = {}
for i, char in enumerate(alphabet):
# NOTE: 0 is reserved for 'blank'
self.dict[char] = i + 1
def encode(self, text):
"""Support batch or single str.
Args:
text (str or list of str): texts to convert.
Returns:
numpy array [length_0 + length_1 + ... length_{n - 1}]: encoded texts.
numpy array [n]: length of each text.
"""
if isinstance(text, str):
text = [
self.dict[char.lower() if self._ignore_case else char]
for char in text
]
length = [len(text)]
elif isinstance(text, collections.Iterable):
length = [len(s) for s in text]
text = ''.join(text)
text, _ = self.encode(text)
return (np.array(text).astype(np.int8), np.array(length).astype(np.int8))
def decode(self, t, length, raw=False):
"""Decode encoded texts back into strs.
Args:
numpy array [length_0 + length_1 + ... length_{n - 1}]: encoded texts.
numpy array [n]: length of each text.
Raises:
AssertionError: when the texts and its length does not match.
Returns:
text (str or list of str): texts to convert.
"""
if length.size == 1:
length = length[0]
assert t.size == length, "text with length: {} does not match declared length: {}".format(t.numel(), length)
if raw:
return ''.join([self.alphabet[i - 1] for i in t])
else:
char_list = []
for i in range(length):
if t[i] != 0 and (not (i > 0 and t[i - 1] == t[i])):
char_list.append(self.alphabet[t[i] - 1])
return ''.join(char_list)
else:
# batch mode
assert t.size == length.sum(), "texts with length: {} does not match declared length: {}".format(t.numel(), length.sum())
texts = []
index = 0
for i in range(length.size):
l = length[i]
texts.append(
self.decode(
t[index:index + l], Variable(np.array([l])).data, raw=raw))
index += l
return texts
class AlignConverter(object):
def __init__(self, alphabet, imgH=32, imgW=100, keep_ratio=False, min_ratio=1):
self.imgH = imgH
self.imgW = imgW
self.keep_ratio = keep_ratio
self.min_ratio = min_ratio
self.textconverter = strLabelConverter(alphabet)
def __call__(self, batch, device=None):
if len(batch) == 0:
raise ValueError('batch is empty')
batchlist = batch
imgH = self.imgH
imgW = self.imgW
if self.keep_ratio:
ratios = []
for item in batchlist:
w, h = item[0].shape
ratios.append(w / float(h))
ratios.sort()
max_ratio = ratios[-1]
imgW = int(np.float(max_ratio * imgH))
imgW = max(imgH * self.min_ratio, imgW)
transform = resizeNormalize((imgH, imgW))
items = []
for item in batchlist:
img = transform(item[0])
t, l = self.textconverter.encode(item[1])
items.append((img, t))
return variable_sequence_convert(items, device)
def variable_sequence_convert(batch, device):
if device is None:
def to_device(x):
return x
elif device < 0:
to_device = chainer.cuda.to_cpu
else:
def to_device(x):
return chainer.cuda.to_gpu(x, device, chainer.cuda.Stream.null)
def to_device_batch(batch):
if device is None:
return batch
elif device < 0:
return [to_device(x) for x in batch]
else:
xp = chainer.cuda.cupy.get_array_module(*batch)
concat = xp.concatenate(batch, axis=0)
sections = np.cumsum([len(x) for x in batch[:-1]], dtype='i')
concat_dev = to_device(concat)
batch_dev = chainer.cuda.cupy.split(concat_dev, sections)
return batch_dev
return tuple([to_device_batch([x for x, _ in batch]), to_device_batch([y for _, y in batch])])