-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e468b22
commit 16a5b0f
Showing
3 changed files
with
94 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Copyright (C) 2022, Mindee. | ||
|
||
# This program is licensed under the Apache License 2.0. | ||
# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details. | ||
|
||
from typing import List, Tuple | ||
|
||
import numpy as np | ||
|
||
from ....datasets import encode_sequences | ||
from ..core import RecognitionPostProcessor | ||
|
||
|
||
class _ViTSTR: | ||
|
||
vocab: str | ||
max_length: int | ||
|
||
def build_target( | ||
self, | ||
gts: List[str], | ||
) -> Tuple[np.ndarray, List[int]]: | ||
"""Encode a list of gts sequences into a np array and gives the corresponding* | ||
sequence lengths. | ||
Args: | ||
gts: list of ground-truth labels | ||
Returns: | ||
A tuple of 2 tensors: Encoded labels and sequence lengths (for each entry of the batch) | ||
""" | ||
encoded = encode_sequences( | ||
sequences=gts, | ||
vocab=self.vocab, | ||
target_size=self.max_length, | ||
eos=len(self.vocab), | ||
sos=len(self.vocab) + 1, | ||
pad=len(self.vocab) + 2, | ||
) | ||
seq_len = [len(word) for word in gts] | ||
return encoded, seq_len | ||
|
||
|
||
class _ViTSTRPostProcessor(RecognitionPostProcessor): | ||
"""Abstract class to postprocess the raw output of the model | ||
Args: | ||
vocab: string containing the ordered sequence of supported characters | ||
""" | ||
|
||
def __init__( | ||
self, | ||
vocab: str, | ||
) -> None: | ||
|
||
super().__init__(vocab) | ||
self._embedding = list(vocab) + ["<eos>"] + ["<sos>"] + ["<pad>"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters