MCPcopy Create free account
hub / github.com/buaacxf/VIPTR / encode

Method encode

utils.py:19–38  ·  view source on GitHub ↗

convert text-label into text-index. input: text: text labels of each image. [batch_size] batch_max_length: max length of text label in the batch. 25 by default output: text: text index for CTCLoss. [batch_size, batch_max_length] length

(self, text, batch_max_length=25)

Source from the content-addressed store, hash-verified

17 self.character = ['[CTCblank]'] + dict_character # dummy '[CTCblank]' token for CTCLoss (index 0)
18
19 def encode(self, text, batch_max_length=25):
20 """convert text-label into text-index.
21 input:
22 text: text labels of each image. [batch_size]
23 batch_max_length: max length of text label in the batch. 25 by default
24
25 output:
26 text: text index for CTCLoss. [batch_size, batch_max_length]
27 length: length of each text. [batch_size]
28 """
29 length = [len(s) for s in text]
30
31 # The index used for padding (=0) would not affect the CTC loss calculation.
32 batch_text = torch.LongTensor(len(text), batch_max_length).fill_(0)
33 for i, t in enumerate(text):
34 text = list(t)
35 text = [self.dict[char] for char in text]
36 # text = [self.dict[char] if char in self.dict.keys() else 0 for char in text]
37 batch_text[i][:len(text)] = torch.LongTensor(text)
38 return (batch_text.to(device), torch.IntTensor(length).to(device))
39
40 def decode(self, text_index, length):
41 """ convert text-index into text-label. """

Callers 6

validationFunction · 0.45
__init__Method · 0.45
__getitem__Method · 0.45
__init__Method · 0.45
__getitem__Method · 0.45
__call__Method · 0.45

Calls

no outgoing calls

Tested by 1

validationFunction · 0.36