summaryrefslogtreecommitdiff
path: root/text_recognizer/data/iam_synthetic_paragraphs.py
blob: 45d7904b11ba5a7913ac05337811abdbd5aeaeab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
"""IAM Synthetic Paragraphs Dataset class."""
import random
from typing import Any, Callable, List, Optional, Sequence, Tuple

import numpy as np
from loguru import logger as log
from PIL import Image

import text_recognizer.metadata.iam_synthetic_paragraphs as metadata
from text_recognizer.data.base_data_module import load_and_print_info
from text_recognizer.data.base_dataset import BaseDataset, convert_strings_to_labels
from text_recognizer.data.iam import IAM
from text_recognizer.data.iam_lines import (
    line_crops_and_labels,
    load_line_crops_and_labels,
    save_images_and_labels,
)
from text_recognizer.data.iam_paragraphs import (
    IAMParagraphs,
    get_dataset_properties,
    resize_image,
)
from text_recognizer.data.tokenizer import Tokenizer
from text_recognizer.data.transforms.pad import Pad
from text_recognizer.data.transforms.paragraph import ParagraphStem


class IAMSyntheticParagraphs(IAMParagraphs):
    """IAM Handwriting database of synthetic paragraphs."""

    def __init__(
        self,
        tokenizer: Tokenizer,
        transform: Optional[Callable] = None,
        test_transform: Optional[Callable] = None,
        target_transform: Optional[Callable] = None,
        train_fraction: float = 0.8,
        batch_size: int = 16,
        num_workers: int = 0,
        pin_memory: bool = True,
    ) -> None:
        super().__init__(
            tokenizer,
            transform,
            test_transform,
            target_transform,
            train_fraction,
            batch_size,
            num_workers,
            pin_memory,
        )

    def prepare_data(self) -> None:
        """Prepare IAM lines to be used to generate paragraphs."""
        if metadata.PROCESSED_DATA_DIRNAME.exists():
            return

        log.info("Preparing IAM lines for synthetic paragraphs dataset.")
        log.info("Cropping IAM line regions and loading labels.")

        iam = IAM(tokenizer=self.tokenizer)
        iam.prepare_data()

        crops_train, labels_train = line_crops_and_labels(iam, "train")
        crops_test, labels_test = line_crops_and_labels(iam, "test")

        crops_train = [
            resize_image(crop, metadata.IMAGE_SCALE_FACTOR) for crop in crops_train
        ]
        crops_test = [
            resize_image(crop, metadata.IMAGE_SCALE_FACTOR) for crop in crops_test
        ]

        log.info(f"Saving images and labels at {metadata.PROCESSED_DATA_DIRNAME}")
        save_images_and_labels(
            crops_train, labels_train, "train", metadata.PROCESSED_DATA_DIRNAME
        )
        save_images_and_labels(
            crops_test, labels_test, "test", metadata.PROCESSED_DATA_DIRNAME
        )

    def setup(self, stage: str = None) -> None:
        """Loading synthetic dataset."""

        log.info(f"IAM Synthetic dataset steup for stage {stage}...")

        if stage == "fit" or stage is None:
            line_crops, line_labels = load_line_crops_and_labels(
                "train", metadata.PROCESSED_DATA_DIRNAME
            )
            data, paragraphs_labels = generate_synthetic_paragraphs(
                line_crops=line_crops, line_labels=line_labels
            )

            targets = convert_strings_to_labels(
                strings=paragraphs_labels,
                mapping=self.tokenizer.inverse_mapping,
                length=self.output_dims[0],
            )
            self.data_train = BaseDataset(
                data,
                targets,
                transform=self.transform,
                target_transform=self.target_transform,
            )

    def __repr__(self) -> str:
        """Return information about the dataset."""
        basic = (
            "IAM Synthetic Paragraphs Dataset\n"  # pylint: disable=no-member
            f"Num classes: {len(self.tokenizer)}\n"
            f"Input dims : {self.dims}\n"
            f"Output dims: {self.output_dims}\n"
        )
        if self.data_train is None:
            return basic

        x, y = next(iter(self.train_dataloader()))
        x = x[0] if isinstance(x, list) else x
        data = (
            f"Train/val/test sizes: {len(self.data_train)}, 0, 0\n"
            "Train Batch x stats: "
            f"{(x.shape, x.dtype, x.min(), x.mean(), x.std(), x.max())}\n"
            f"Train Batch y stats: {(y.shape, y.dtype, y.min(), y.max())}\n"
        )
        return basic + data


def generate_synthetic_paragraphs(
    line_crops: List[Image.Image], line_labels: List[str], max_batch_size: int = 9
) -> Tuple[List[Image.Image], List[str]]:
    """Generate synthetic paragraphs from randomly joining different subsets."""
    paragraphs_properties = get_dataset_properties()

    indices = list(range(len(line_labels)))

    if max_batch_size >= paragraphs_properties["num_lines"]["max"]:
        raise ValueError("max_batch_size greater or equalt to max num lines.")

    batched_indices_list = [[index] for index in indices]
    batched_indices_list.extend(
        generate_random_batches(
            values=indices, min_batch_size=2, max_batch_size=max_batch_size // 2
        )
    )
    batched_indices_list.extend(
        generate_random_batches(
            values=indices, min_batch_size=2, max_batch_size=max_batch_size
        )
    )
    batched_indices_list.extend(
        generate_random_batches(
            values=indices,
            min_batch_size=max_batch_size // 2 + 1,
            max_batch_size=max_batch_size,
        )
    )

    paragraphs_crops, paragraphs_labels = [], []
    for paragraph_indices in batched_indices_list:
        paragraph_label = metadata.NEW_LINE_TOKEN.join(
            [line_labels[i] for i in paragraph_indices]
        )
        if len(paragraph_label) > paragraphs_properties["label_length"]["max"]:
            log.info(
                (
                    "Label longer than longest label in original IAM paragraph dataset"
                    " - hence dropping."
                )
            )
            continue

        paragraph_crop = join_line_crops_to_form_paragraph(
            [line_crops[i] for i in paragraph_indices]
        )
        max_paragraph_shape = paragraphs_properties["crop_shape"]["max"]

        if (
            paragraph_crop.height > max_paragraph_shape[0]
            or paragraph_crop.width > max_paragraph_shape[1]
        ):
            log.info(
                (
                    "Crop larger than largest crop in original IAM paragraphs dataset"
                    " - hence dropping"
                )
            )
            continue

        paragraphs_crops.append(paragraph_crop)
        paragraphs_labels.append(paragraph_label)

    if len(paragraphs_crops) != len(paragraphs_labels):
        raise ValueError("Number of crops does not match number of labels.")

    return paragraphs_crops, paragraphs_labels


def join_line_crops_to_form_paragraph(line_crops: Sequence[Image.Image]) -> Image.Image:
    """Horizontally stack line crops and return a single image forming a paragraph."""
    crop_shapes = np.array([line.size[::-1] for line in line_crops])
    paragraph_height = crop_shapes[:, 0].sum()
    paragraph_width = crop_shapes[:, 1].max()

    paragraph_image = Image.new(
        mode="L", size=(paragraph_width, paragraph_height), color=0
    )
    current_height = 0
    for line_crop in line_crops:
        paragraph_image.paste(line_crop, box=(0, current_height))
        current_height += line_crop.height

    return paragraph_image


def generate_random_batches(
    values: List[Any], min_batch_size: int, max_batch_size: int
) -> List[List[Any]]:
    """Generate random batches of elements in values without replacement."""
    shuffled_values = values.copy()
    random.shuffle(shuffled_values)

    start_index = 0
    grouped_values_list = []
    while start_index < len(shuffled_values):
        num_values = random.randint(min_batch_size, max_batch_size)
        grouped_values_list.append(
            shuffled_values[start_index : start_index + num_values]
        )
        start_index += num_values

    if sum([len(grp) for grp in grouped_values_list]) != len(values):
        raise ValueError("Length of groups does not match length of values.")

    return grouped_values_list


def create_synthetic_iam_paragraphs() -> None:
    """Creates and prints IAM Synthetic Paragraphs dataset."""
    transform = ParagraphStem()
    test_transform = ParagraphStem()
    target_transform = Pad(metadata.MAX_LABEL_LENGTH, 3)
    load_and_print_info(
        IAMSyntheticParagraphs(
            transform=transform,
            test_transform=test_transform,
            target_transform=target_transform,
        )
    )