File size: 15,296 Bytes
964201e
 
 
 
 
 
 
a976004
964201e
 
86f4fb9
a976004
964201e
bc0f7fc
964201e
 
 
 
a976004
b06784f
 
 
a976004
 
 
 
b06784f
a976004
 
b06784f
 
 
a976004
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b06784f
a976004
b06784f
a976004
 
b06784f
 
 
a976004
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8526729
a976004
 
 
01d9a57
b06784f
01d9a57
b06784f
01d9a57
 
b06784f
 
 
01d9a57
 
 
 
 
 
 
 
86f4fb9
 
 
 
 
 
 
 
e7affb8
 
 
86f4fb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8063aac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
964201e
a976004
 
 
 
 
01d9a57
855e240
86f4fb9
a976004
01d9a57
 
 
 
 
 
86f4fb9
 
 
01d9a57
964201e
 
68dd12a
a976004
855e240
964201e
 
 
 
 
 
86f4fb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8063aac
86f4fb9
 
 
 
 
3a87836
86f4fb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01d9a57
964201e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b25c65
964201e
 
 
 
 
 
 
 
 
 
a976004
 
 
 
 
86f4fb9
01d9a57
 
 
 
 
 
86f4fb9
01d9a57
855e240
01d9a57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
855e240
 
 
 
86f4fb9
 
964201e
86f4fb9
 
 
 
 
01d9a57
a976004
 
 
964201e
 
 
 
 
 
 
 
bc0f7fc
 
 
964201e
 
 
b06784f
 
 
855e240
86f4fb9
68dd12a
964201e
 
 
 
 
 
 
bc0f7fc
 
 
 
 
 
 
 
86f4fb9
bc0f7fc
 
 
a976004
bc0f7fc
 
 
 
 
 
 
 
86f4fb9
bc0f7fc
 
 
a976004
bc0f7fc
 
 
 
 
 
 
 
86f4fb9
bc0f7fc
 
 
a976004
964201e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
from font_dataset.fontlabel import FontLabel
from font_dataset.font import DSFont, load_font_with_exclusion
from . import config


import math
import os
import random
import pickle
import torch
import torchvision
import torchvision.transforms.functional as TF
from typing import List, Dict, Tuple
from torch.utils.data import Dataset, DataLoader, ConcatDataset
from pytorch_lightning import LightningDataModule
from PIL import Image


class RandomColorJitter(object):
    def __init__(
        self, brightness=0.5, contrast=0.5, saturation=0.5, hue=0.05, preserve=0.2
    ):
        self.brightness = brightness
        self.contrast = contrast
        self.saturation = saturation
        self.hue = hue
        self.preserve = preserve

    def __call__(self, batch):
        if random.random() < self.preserve:
            return batch

        image, label = batch
        text_color = label[2:5].clone().view(3, 1, 1)
        stroke_color = label[7:10].clone().view(3, 1, 1)

        brightness = random.uniform(1 - self.brightness, 1 + self.brightness)
        image = TF.adjust_brightness(image, brightness)
        text_color = TF.adjust_brightness(text_color, brightness)
        stroke_color = TF.adjust_brightness(stroke_color, brightness)

        contrast = random.uniform(1 - self.contrast, 1 + self.contrast)
        image = TF.adjust_contrast(image, contrast)
        text_color = TF.adjust_contrast(text_color, contrast)
        stroke_color = TF.adjust_contrast(stroke_color, contrast)

        saturation = random.uniform(1 - self.saturation, 1 + self.saturation)
        image = TF.adjust_saturation(image, saturation)
        text_color = TF.adjust_saturation(text_color, saturation)
        stroke_color = TF.adjust_saturation(stroke_color, saturation)

        hue = random.uniform(-self.hue, self.hue)
        image = TF.adjust_hue(image, hue)
        text_color = TF.adjust_hue(text_color, hue)
        stroke_color = TF.adjust_hue(stroke_color, hue)

        label[2:5] = text_color.view(3)
        label[7:10] = stroke_color.view(3)
        return image, label


class RandomCrop(object):
    def __init__(self, crop_factor: float = 0.1, preserve: float = 0.2):
        self.crop_factor = crop_factor
        self.preserve = preserve

    def __call__(self, batch):
        if random.random() < self.preserve:
            return batch

        image, label = batch
        width, height = image.size

        # use random value to decide scaling factor on x and y axis
        random_height = random.random() * self.crop_factor
        random_width = random.random() * self.crop_factor
        # use random value again to decide scaling factor for 4 borders
        random_top = random.random() * random_height
        random_left = random.random() * random_width
        # calculate new width and height and position
        top = int(random_top * height)
        left = int(random_left * width)
        height = int(height - random_height * height)
        width = int(width - random_width * width)
        # crop image
        image = TF.crop(image, top, left, height, width)

        label[[5, 6, 10]] = label[[5, 6, 10]] / (1 - random_width)
        return image, label


class RandomRotate(object):
    def __init__(self, max_angle: int = 15, preserve: float = 0.2):
        self.max_angle = max_angle
        self.preserve = preserve

    def __call__(self, batch):
        if random.random() < self.preserve:
            return batch

        image, label = batch

        angle = random.uniform(-self.max_angle, self.max_angle)
        image = TF.rotate(image, angle)
        label[11] = label[11] + angle / 180
        return image, label


class RandomNoise(object):
    def __init__(self, max_noise: float = 0.05, preserve: float = 0.1):
        self.max_noise = max_noise
        self.preserve = preserve

    def __call__(self, image):
        if random.random() < self.preserve:
            return image
        return torch.clamp(
            image + torch.randn_like(image) * random.random() * self.max_noise, 0, 1
        )


class RandomDownSample(object):
    def __init__(self, max_ratio: float = 2, preserve: float = 0.5):
        self.max_ratio = max_ratio
        self.preserve = preserve

    def __call__(self, image):
        if random.random() < self.preserve:
            return image
        ratio = random.uniform(1, self.max_ratio)
        return TF.resize(
            image, (int(image.size[1] / ratio), int(image.size[0] / ratio))
        )


class RandomCropPreserveAspectRatio(object):
    def __call__(self, batch):
        image, label = batch
        width, height = image.size

        if width == height:
            return batch

        if width > height:
            x = random.randint(0, width - height)
            image = TF.crop(image, 0, x, height, height)
            label[[5, 6, 10]] = label[[5, 6, 10]] / height * width
        else:
            y = random.randint(0, height - width)
            image = TF.crop(image, y, 0, width, width)
        return image, label


class RandomHorizontalFlip(object):
    def __init__(self, preserve: float = 0.5):
        self.preserve = preserve

    def __call__(self, batch):
        if random.random() < self.preserve:
            return batch

        image, label = batch
        image = TF.hflip(image)
        label[11] = 1 - label[11]

        return image, label


class FontDataset(Dataset):
    def __init__(
        self,
        path: str,
        config_path: str = "configs/font.yml",
        regression_use_tanh: bool = False,
        transforms: str = None,
        crop_roi_bbox: bool = False,
        preserve_aspect_ratio_by_random_crop: bool = False,
    ):
        """Font dataset

        Args:
            path (str): path to the dataset
            config_path (str, optional): path to font config file. Defaults to "configs/font.yml".
            regression_use_tanh (bool, optional): whether use tanh as regression normalization. Defaults to False.
            transforms (str, optional): choose from None, 'v1', 'v2', 'v3'. Defaults to None.
            crop_roi_bbox (bool, optional): whether to crop text roi bbox, must be true when transform='v2' or 'v3'. Defaults to False.
            preserve_aspect_ratio_by_random_crop (bool, optional): whether to preserve aspect ratio by random cropping maximum squares. Defaults to False.
        """
        self.path = path
        self.fonts = load_font_with_exclusion(config_path)
        self.regression_use_tanh = regression_use_tanh
        self.transforms = transforms
        self.crop_roi_bbox = crop_roi_bbox

        self.images = [
            os.path.join(path, f) for f in os.listdir(path) if f.endswith(".jpg")
        ]
        self.images.sort()

        if transforms == "v2" or transforms == "v3":
            assert (
                crop_roi_bbox
            ), "crop_roi_bbox must be true when transform='v2' or 'v3'"

        if transforms is None:
            label_image_transforms = []
            image_transforms = [
                torchvision.transforms.Resize((config.INPUT_SIZE, config.INPUT_SIZE)),
                torchvision.transforms.ToTensor(),
            ]
        elif transforms == "v1":
            label_image_transforms = [
                RandomColorJitter(preserve=0.2),
                RandomCrop(preserve=0.2),
            ]
            image_transforms = [
                torchvision.transforms.Resize((config.INPUT_SIZE, config.INPUT_SIZE)),
                torchvision.transforms.ToTensor(),
            ]
        elif transforms == "v2":
            label_image_transforms = [
                RandomColorJitter(preserve=0.2),
                RandomCrop(crop_factor=0.54, preserve=0),
                RandomRotate(preserve=0.2),
            ]
            image_transforms = [
                torchvision.transforms.GaussianBlur(
                    random.randint(1, 3) * 2 - 1, sigma=(0.1, 5.0)
                ),
                torchvision.transforms.Resize((config.INPUT_SIZE, config.INPUT_SIZE)),
                torchvision.transforms.ToTensor(),
                RandomNoise(max_noise=0.05, preserve=0.1),
            ]
        elif transforms == "v3":
            label_image_transforms = [
                RandomColorJitter(preserve=0.2),
                RandomCrop(crop_factor=0.54, preserve=0),
                RandomRotate(preserve=0.2),
                RandomHorizontalFlip(preserve=0.5),
            ]
            image_transforms = [
                torchvision.transforms.GaussianBlur(
                    random.randint(1, 3) * 2 - 1, sigma=(0.1, 5.0)
                ),
                RandomDownSample(max_ratio=2, preserve=0.5),
                torchvision.transforms.Resize((config.INPUT_SIZE, config.INPUT_SIZE)),
                torchvision.transforms.ToTensor(),
                RandomNoise(max_noise=0.05, preserve=0.1),
            ]
        else:
            raise ValueError(f"Unknown transform: {transforms}")

        if preserve_aspect_ratio_by_random_crop:
            label_image_transforms.append(RandomCropPreserveAspectRatio())

        if len(label_image_transforms) == 0:
            self.transform_label_image = None
        else:
            self.transform_label_image = torchvision.transforms.Compose(
                label_image_transforms
            )
        if len(image_transforms) == 0:
            self.transform_image = None
        else:
            self.transform_image = torchvision.transforms.Compose(image_transforms)

    def __len__(self):
        return len(self.images)

    def fontlabel2tensor(self, label: FontLabel, label_path) -> torch.Tensor:
        out = torch.zeros(12, dtype=torch.float)
        try:
            out[0] = self.fonts[label.font.path]
        except KeyError:
            print(f"Unqualified font: {label.font.path}")
            print(f"Label path: {label_path}")
            raise KeyError
        out[1] = 0 if label.text_direction == "ltr" else 1
        # [0, 1]
        out[2] = label.text_color[0] / 255.0
        out[3] = label.text_color[1] / 255.0
        out[4] = label.text_color[2] / 255.0
        out[5] = label.text_size / label.image_width
        out[6] = label.stroke_width / label.image_width
        if label.stroke_color:
            out[7] = label.stroke_color[0] / 255.0
            out[8] = label.stroke_color[1] / 255.0
            out[9] = label.stroke_color[2] / 255.0
        else:
            out[7:10] = out[2:5]
        out[10] = label.line_spacing / label.image_width
        out[11] = label.angle / 180.0 + 0.5

        return out

    def __getitem__(self, index: int) -> Tuple[torch.Tensor, torch.Tensor]:
        # Load image
        image_path = self.images[index]
        image = Image.open(image_path).convert("RGB")

        # Load label
        label_path = image_path.replace(".jpg", ".bin")
        with open(label_path, "rb") as f:
            label: FontLabel = pickle.load(f)

        # preparation
        if (self.transforms == "v1") or (self.transforms is None):
            if self.crop_roi_bbox:
                left, top, width, height = label.bbox
                image = TF.crop(image, top, left, height, width)
                label.image_width = width
                label.image_height = height
        elif self.transforms == "v2" or self.transforms == "v3":
            # crop from 30% to 130% of bbox
            left, top, width, height = label.bbox

            right = left + width
            bottom = top + height

            width_delta = width * 0.07
            height_delta = height * 0.07

            left = max(0, int(left - width_delta))
            top = max(0, int(top - height_delta))

            right = min(image.width, int(right + width_delta))
            bottom = min(image.height, int(bottom + height_delta))

            width = right - left
            height = bottom - top

            image = TF.crop(image, top, left, height, width)
            label.image_width = width
            label.image_height = height

        # encode label
        label = self.fontlabel2tensor(label, label_path)

        # transform
        if self.transform_label_image is not None:
            image, label = self.transform_label_image((image, label))
        if self.transform_image is not None:
            image = self.transform_image(image)

        # normalize label
        if self.regression_use_tanh:
            label[2:12] = label[2:12] * 2 - 1

        return image, label


class FontDataModule(LightningDataModule):
    def __init__(
        self,
        config_path: str = "configs/font.yml",
        train_paths: List[str] = ["./dataset/font_img/train"],
        val_paths: List[str] = ["./dataset/font_img/val"],
        test_paths: List[str] = ["./dataset/font_img/test"],
        train_shuffle: bool = True,
        val_shuffle: bool = False,
        test_shuffle: bool = False,
        train_transforms: bool = None,
        val_transforms: bool = None,
        test_transforms: bool = None,
        crop_roi_bbox: bool = False,
        preserve_aspect_ratio_by_random_crop: bool = False,
        regression_use_tanh: bool = False,
        **kwargs,
    ):
        super().__init__()
        self.dataloader_args = kwargs
        self.train_shuffle = train_shuffle
        self.val_shuffle = val_shuffle
        self.test_shuffle = test_shuffle
        self.train_dataset = ConcatDataset(
            [
                FontDataset(
                    train_path,
                    config_path,
                    regression_use_tanh,
                    train_transforms,
                    crop_roi_bbox,
                    preserve_aspect_ratio_by_random_crop,
                )
                for train_path in train_paths
            ]
        )
        self.val_dataset = ConcatDataset(
            [
                FontDataset(
                    val_path,
                    config_path,
                    regression_use_tanh,
                    val_transforms,
                    crop_roi_bbox,
                    preserve_aspect_ratio_by_random_crop,
                )
                for val_path in val_paths
            ]
        )
        self.test_dataset = ConcatDataset(
            [
                FontDataset(
                    test_path,
                    config_path,
                    regression_use_tanh,
                    test_transforms,
                    crop_roi_bbox,
                    preserve_aspect_ratio_by_random_crop,
                )
                for test_path in test_paths
            ]
        )

    def get_train_num_iter(self, num_device: int) -> int:
        return math.ceil(
            len(self.train_dataset) / (self.dataloader_args["batch_size"] * num_device)
        )

    def train_dataloader(self):
        return DataLoader(
            self.train_dataset,
            shuffle=self.train_shuffle,
            **self.dataloader_args,
        )

    def val_dataloader(self):
        return DataLoader(
            self.val_dataset,
            shuffle=self.val_shuffle,
            **self.dataloader_args,
        )

    def test_dataloader(self):
        return DataLoader(
            self.test_dataset,
            shuffle=self.test_shuffle,
            **self.dataloader_args,
        )