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)
| 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. """ |
no outgoing calls