diff options
Diffstat (limited to 'text_recognizer/data')
-rw-r--r-- | text_recognizer/data/base_data_module.py | 32 | ||||
-rw-r--r-- | text_recognizer/data/base_dataset.py | 12 | ||||
-rw-r--r-- | text_recognizer/data/emnist.py | 4 | ||||
-rw-r--r-- | text_recognizer/data/emnist_lines.py | 18 | ||||
-rw-r--r-- | text_recognizer/data/iam.py | 6 | ||||
-rw-r--r-- | text_recognizer/data/iam_extended_paragraphs.py | 4 | ||||
-rw-r--r-- | text_recognizer/data/iam_lines.py | 8 | ||||
-rw-r--r-- | text_recognizer/data/iam_paragraphs.py | 13 | ||||
-rw-r--r-- | text_recognizer/data/iam_synthetic_paragraphs.py | 4 |
9 files changed, 52 insertions, 49 deletions
diff --git a/text_recognizer/data/base_data_module.py b/text_recognizer/data/base_data_module.py index 15c286a..a0c8416 100644 --- a/text_recognizer/data/base_data_module.py +++ b/text_recognizer/data/base_data_module.py @@ -2,7 +2,7 @@ from pathlib import Path from typing import Callable, Dict, Optional, Tuple, Type, TypeVar -import attr +from attrs import define, field from pytorch_lightning import LightningDataModule from torch.utils.data import DataLoader @@ -20,29 +20,29 @@ def load_and_print_info(data_module_class: type) -> None: print(dataset) -@attr.s(repr=False) +@define(repr=False) class BaseDataModule(LightningDataModule): """Base PyTorch Lightning DataModule.""" - def __attrs_pre_init__(self) -> None: + def __attrs_post_init__(self) -> None: """Pre init constructor.""" super().__init__() - mapping: Type[AbstractMapping] = attr.ib() - transform: Optional[Callable] = attr.ib(default=None) - test_transform: Optional[Callable] = attr.ib(default=None) - target_transform: Optional[Callable] = attr.ib(default=None) - train_fraction: float = attr.ib(default=0.8) - batch_size: int = attr.ib(default=16) - num_workers: int = attr.ib(default=0) - pin_memory: bool = attr.ib(default=True) + mapping: Type[AbstractMapping] = field() + transform: Optional[Callable] = field(default=None) + test_transform: Optional[Callable] = field(default=None) + target_transform: Optional[Callable] = field(default=None) + train_fraction: float = field(default=0.8) + batch_size: int = field(default=16) + num_workers: int = field(default=0) + pin_memory: bool = field(default=True) # Placeholders - data_train: BaseDataset = attr.ib(init=False, default=None) - data_val: BaseDataset = attr.ib(init=False, default=None) - data_test: BaseDataset = attr.ib(init=False, default=None) - dims: Tuple[int, ...] = attr.ib(init=False, default=None) - output_dims: Tuple[int, ...] = attr.ib(init=False, default=None) + data_train: BaseDataset = field(init=False, default=None) + data_val: BaseDataset = field(init=False, default=None) + data_test: BaseDataset = field(init=False, default=None) + dims: Tuple[int, ...] = field(init=False, default=None) + output_dims: Tuple[int, ...] = field(init=False, default=None) @classmethod def data_dirname(cls: T) -> Path: diff --git a/text_recognizer/data/base_dataset.py b/text_recognizer/data/base_dataset.py index b9567c7..c57cbcc 100644 --- a/text_recognizer/data/base_dataset.py +++ b/text_recognizer/data/base_dataset.py @@ -1,7 +1,7 @@ """Base PyTorch Dataset class.""" from typing import Callable, Dict, Optional, Sequence, Tuple, Union -import attr +from attrs import define, field import torch from torch import Tensor from torch.utils.data import Dataset @@ -9,7 +9,7 @@ from torch.utils.data import Dataset from text_recognizer.data.transforms.load_transform import load_transform_from_file -@attr.s +@define class BaseDataset(Dataset): r"""Base Dataset class that processes data and targets through optional transfroms. @@ -21,10 +21,10 @@ class BaseDataset(Dataset): target transforms. """ - data: Union[Sequence, Tensor] = attr.ib() - targets: Union[Sequence, Tensor] = attr.ib() - transform: Union[Optional[Callable], str] = attr.ib(default=None) - target_transform: Union[Optional[Callable], str] = attr.ib(default=None) + data: Union[Sequence, Tensor] = field() + targets: Union[Sequence, Tensor] = field() + transform: Union[Optional[Callable], str] = field(default=None) + target_transform: Union[Optional[Callable], str] = field(default=None) def __attrs_pre_init__(self) -> None: """Pre init constructor.""" diff --git a/text_recognizer/data/emnist.py b/text_recognizer/data/emnist.py index dc8d31a..94882bf 100644 --- a/text_recognizer/data/emnist.py +++ b/text_recognizer/data/emnist.py @@ -6,7 +6,7 @@ import shutil from typing import Dict, List, Optional, Sequence, Set, Tuple import zipfile -import attr +from attrs import define import h5py from loguru import logger as log import numpy as np @@ -35,7 +35,7 @@ ESSENTIALS_FILENAME = ( ) -@attr.s(auto_attribs=True) +@define(auto_attribs=True) class EMNIST(BaseDataModule): """Lightning DataModule class for loading EMNIST dataset. diff --git a/text_recognizer/data/emnist_lines.py b/text_recognizer/data/emnist_lines.py index c267286..43d55b9 100644 --- a/text_recognizer/data/emnist_lines.py +++ b/text_recognizer/data/emnist_lines.py @@ -3,7 +3,7 @@ from collections import defaultdict from pathlib import Path from typing import DefaultDict, List, Tuple -import attr +from attrs import define, field import h5py from loguru import logger as log import numpy as np @@ -33,17 +33,17 @@ IMAGE_X_PADDING = 28 MAX_OUTPUT_LENGTH = 89 # Same as IAMLines -@attr.s(auto_attribs=True, repr=False) +@define(auto_attribs=True, repr=False) class EMNISTLines(BaseDataModule): """EMNIST Lines dataset: synthetic handwritten lines dataset made from EMNIST.""" - max_length: int = attr.ib(default=128) - min_overlap: float = attr.ib(default=0.0) - max_overlap: float = attr.ib(default=0.33) - num_train: int = attr.ib(default=10_000) - num_val: int = attr.ib(default=2_000) - num_test: int = attr.ib(default=2_000) - emnist: EMNIST = attr.ib(init=False, default=None) + max_length: int = field(default=128) + min_overlap: float = field(default=0.0) + max_overlap: float = field(default=0.33) + num_train: int = field(default=10_000) + num_val: int = field(default=2_000) + num_test: int = field(default=2_000) + emnist: EMNIST = field(init=False, default=None) def __attrs_post_init__(self) -> None: """Post init constructor.""" diff --git a/text_recognizer/data/iam.py b/text_recognizer/data/iam.py index 766f3e0..8166863 100644 --- a/text_recognizer/data/iam.py +++ b/text_recognizer/data/iam.py @@ -9,7 +9,7 @@ from typing import Any, Dict, List import xml.etree.ElementTree as ElementTree import zipfile -import attr +from attrs import define, field from boltons.cacheutils import cachedproperty from loguru import logger as log import toml @@ -27,7 +27,7 @@ DOWNSAMPLE_FACTOR = 2 # If images were downsampled, the regions must also be. LINE_REGION_PADDING = 16 # Add this many pixels around the exact coordinates. -@attr.s(auto_attribs=True) +@define(auto_attribs=True) class IAM(BaseDataModule): r"""The IAM Lines dataset. @@ -44,7 +44,7 @@ class IAM(BaseDataModule): contributed to one set only. """ - metadata: Dict = attr.ib(init=False, default=toml.load(METADATA_FILENAME)) + metadata: Dict = field(init=False, default=toml.load(METADATA_FILENAME)) def prepare_data(self) -> None: """Prepares the IAM dataset.""" diff --git a/text_recognizer/data/iam_extended_paragraphs.py b/text_recognizer/data/iam_extended_paragraphs.py index 22d00f1..52c10c3 100644 --- a/text_recognizer/data/iam_extended_paragraphs.py +++ b/text_recognizer/data/iam_extended_paragraphs.py @@ -1,5 +1,5 @@ """IAM original and sythetic dataset class.""" -import attr +from attrs import define, field from torch.utils.data import ConcatDataset from text_recognizer.data.base_data_module import BaseDataModule, load_and_print_info @@ -8,7 +8,7 @@ from text_recognizer.data.iam_synthetic_paragraphs import IAMSyntheticParagraphs from text_recognizer.data.transforms.load_transform import load_transform_from_file -@attr.s(auto_attribs=True, repr=False) +@define(auto_attribs=True, repr=False) class IAMExtendedParagraphs(BaseDataModule): """A dataset with synthetic and real handwritten paragraph.""" diff --git a/text_recognizer/data/iam_lines.py b/text_recognizer/data/iam_lines.py index a79c202..34cf605 100644 --- a/text_recognizer/data/iam_lines.py +++ b/text_recognizer/data/iam_lines.py @@ -7,7 +7,7 @@ import json from pathlib import Path from typing import List, Sequence, Tuple -import attr +from attrs import define, field from loguru import logger as log import numpy as np from PIL import Image, ImageFile, ImageOps @@ -35,14 +35,14 @@ MAX_LABEL_LENGTH = 89 MAX_WORD_PIECE_LENGTH = 72 -@attr.s(auto_attribs=True, repr=False) +@define(auto_attribs=True, repr=False) class IAMLines(BaseDataModule): """IAM handwritten lines dataset.""" - dims: Tuple[int, int, int] = attr.ib( + dims: Tuple[int, int, int] = field( init=False, default=(1, IMAGE_HEIGHT, IMAGE_WIDTH) ) - output_dims: Tuple[int, int] = attr.ib(init=False, default=(MAX_LABEL_LENGTH, 1)) + output_dims: Tuple[int, int] = field(init=False, default=(MAX_LABEL_LENGTH, 1)) def prepare_data(self) -> None: """Creates the IAM lines dataset if not existing.""" diff --git a/text_recognizer/data/iam_paragraphs.py b/text_recognizer/data/iam_paragraphs.py index 033b93e..b605bbc 100644 --- a/text_recognizer/data/iam_paragraphs.py +++ b/text_recognizer/data/iam_paragraphs.py @@ -3,7 +3,7 @@ import json from pathlib import Path from typing import Dict, List, Optional, Sequence, Tuple -import attr +from attrs import define, field from loguru import logger as log import numpy as np from PIL import Image, ImageOps @@ -33,15 +33,15 @@ MAX_LABEL_LENGTH = 682 MAX_WORD_PIECE_LENGTH = 451 -@attr.s(auto_attribs=True, repr=False) +@define(auto_attribs=True, repr=False) class IAMParagraphs(BaseDataModule): """IAM handwriting database paragraphs.""" # Placeholders - dims: Tuple[int, int, int] = attr.ib( + dims: Tuple[int, int, int] = field( init=False, default=(1, IMAGE_HEIGHT, IMAGE_WIDTH) ) - output_dims: Tuple[int, int] = attr.ib(init=False, default=(MAX_LABEL_LENGTH, 1)) + output_dims: Tuple[int, int] = field(init=False, default=(MAX_LABEL_LENGTH, 1)) def prepare_data(self) -> None: """Create data for training/testing.""" @@ -86,7 +86,10 @@ class IAMParagraphs(BaseDataModule): length=self.output_dims[0], ) return BaseDataset( - data, targets, transform=transform, target_transform=target_transform, + data, + targets, + transform=transform, + target_transform=target_transform, ) log.info(f"Loading IAM paragraph regions and lines for {stage}...") diff --git a/text_recognizer/data/iam_synthetic_paragraphs.py b/text_recognizer/data/iam_synthetic_paragraphs.py index ea59098..7143951 100644 --- a/text_recognizer/data/iam_synthetic_paragraphs.py +++ b/text_recognizer/data/iam_synthetic_paragraphs.py @@ -2,7 +2,7 @@ import random from typing import Any, List, Sequence, Tuple -import attr +from attrs import define from loguru import logger as log import numpy as np from PIL import Image @@ -34,7 +34,7 @@ PROCESSED_DATA_DIRNAME = ( ) -@attr.s(auto_attribs=True, repr=False) +@define(auto_attribs=True, repr=False) class IAMSyntheticParagraphs(IAMParagraphs): """IAM Handwriting database of synthetic paragraphs.""" |