diff options
Diffstat (limited to 'text_recognizer/data/mappings')
-rw-r--r-- | text_recognizer/data/mappings/__init__.py | 1 | ||||
-rw-r--r-- | text_recognizer/data/mappings/base.py | 37 | ||||
-rw-r--r-- | text_recognizer/data/mappings/emnist.py | 15 |
3 files changed, 10 insertions, 43 deletions
diff --git a/text_recognizer/data/mappings/__init__.py b/text_recognizer/data/mappings/__init__.py index 84177cb..635f506 100644 --- a/text_recognizer/data/mappings/__init__.py +++ b/text_recognizer/data/mappings/__init__.py @@ -1,3 +1,2 @@ """Mapping modules.""" -from text_recognizer.data.mappings.base import AbstractMapping from text_recognizer.data.mappings.emnist import EmnistMapping diff --git a/text_recognizer/data/mappings/base.py b/text_recognizer/data/mappings/base.py deleted file mode 100644 index 572ac95..0000000 --- a/text_recognizer/data/mappings/base.py +++ /dev/null @@ -1,37 +0,0 @@ -"""Mapping to and from word pieces.""" -from abc import ABC, abstractmethod -from typing import Dict, List - -from torch import Tensor - - -class AbstractMapping(ABC): - def __init__( - self, input_size: List[int], mapping: List[str], inverse_mapping: Dict[str, int] - ) -> None: - self.input_size = input_size - self.mapping = mapping - self.inverse_mapping = inverse_mapping - - def __len__(self) -> int: - return len(self.mapping) - - @property - def num_classes(self) -> int: - return self.__len__() - - @abstractmethod - def get_token(self, *args, **kwargs) -> str: - ... - - @abstractmethod - def get_index(self, *args, **kwargs) -> Tensor: - ... - - @abstractmethod - def get_text(self, *args, **kwargs) -> str: - ... - - @abstractmethod - def get_indices(self, *args, **kwargs) -> Tensor: - ... diff --git a/text_recognizer/data/mappings/emnist.py b/text_recognizer/data/mappings/emnist.py index ecd862e..606d200 100644 --- a/text_recognizer/data/mappings/emnist.py +++ b/text_recognizer/data/mappings/emnist.py @@ -6,22 +6,27 @@ from typing import Dict, List, Optional, Sequence, Union, Tuple import torch from torch import Tensor -from text_recognizer.data.mappings.base import AbstractMapping - ESSENTIALS_FILENAME = Path(__file__).parents[0].resolve() / "emnist_essentials.json" -class EmnistMapping(AbstractMapping): +class EmnistMapping: """Mapping for EMNIST labels.""" def __init__( - self, extra_symbols: Optional[Sequence[str]] = None, lower: bool = True + self, + input_size: List[int], + mapping: List[str], + inverse_mapping: Dict[str, int], + extra_symbols: Optional[Sequence[str]] = None, + lower: bool = True, ) -> None: + self.input_size = input_size + self.mapping = mapping + self.inverse_mapping = inverse_mapping self.extra_symbols = set(extra_symbols) if extra_symbols is not None else None self.mapping, self.inverse_mapping, self.input_size = self._load_mapping() if lower: self._to_lower() - super().__init__(self.input_size, self.mapping, self.inverse_mapping) def _load_mapping(self) -> Tuple[List, Dict[str, int], List[int]]: """Return the EMNIST mapping.""" |