File size: 3,790 Bytes
33c93f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import io
import random

import torch
from PIL import Image, ImageEnhance, ImageFilter, ImageOps
from torchvision import transforms as T
from torchvision.transforms import functional as TF

MEAN = (0.485, 0.456, 0.406)
STD = (0.229, 0.224, 0.225)


def _tensor(image: Image.Image) -> torch.Tensor:
    return TF.normalize(TF.to_tensor(image.resize((64, 64), Image.Resampling.BICUBIC)), MEAN, STD)


class TrainTransforms:
    def __init__(self):
        self.clean_transform = T.Compose([T.Resize((64, 64)), T.ToTensor()])
        self.contrastive_transform = T.Compose([
            T.RandomResizedCrop(64, scale=(0.45, 1.0)),
            T.RandomHorizontalFlip(),
            T.RandomApply([T.ColorJitter(0.45, 0.45, 0.25, 0.05)], p=0.8),
            T.RandomGrayscale(p=0.1),
            T.RandomApply([T.GaussianBlur(5, sigma=(0.2, 2.0))], p=0.4),
            T.ToTensor(), T.Normalize(MEAN, STD),
        ])
        self.degraded_transform = T.Compose([
            T.RandomResizedCrop(64, scale=(0.45, 1.0)),
            T.RandomPerspective(0.15, p=0.4),
            T.ColorJitter(0.45, 0.45, 0.25, 0.05),
            T.RandomApply([T.GaussianBlur(7, sigma=(0.5, 2.5))], p=0.5),
            T.ToTensor(), T.Normalize(MEAN, STD),
        ])

    def clean(self, image):
        return self.clean_transform(image)

    def contrastive(self, image):
        return self.contrastive_transform(image)

    def degraded(self, image):
        return self.degraded_transform(image)

    def rotation(self, image):
        label = random.randrange(4)
        return _tensor(image.rotate(90 * label)), label


def inference_transform(image: Image.Image) -> torch.Tensor:
    return _tensor(image)


def deterministic_degrade(image: Image.Image, seed: int) -> Image.Image:
    rng = random.Random(seed)
    image = image.convert("RGB")
    width, height = image.size
    scale = rng.uniform(0.45, 0.9)
    crop_w, crop_h = max(8, int(width * scale)), max(8, int(height * scale))
    left, top = rng.randrange(width - crop_w + 1), rng.randrange(height - crop_h + 1)
    image = image.crop((left, top, left + crop_w, top + crop_h)).resize((64, 64), Image.Resampling.BICUBIC)
    image = image.rotate(rng.uniform(-30, 30), resample=Image.Resampling.BICUBIC)
    margin = rng.randint(1, 7)
    image = TF.perspective(
        image,
        [[0, 0], [63, 0], [63, 63], [0, 63]],
        [[rng.randint(0, margin), rng.randint(0, margin)],
         [63 - rng.randint(0, margin), rng.randint(0, margin)],
         [63 - rng.randint(0, margin), 63 - rng.randint(0, margin)],
         [rng.randint(0, margin), 63 - rng.randint(0, margin)]],
        interpolation=T.InterpolationMode.BILINEAR,
    )
    image = ImageEnhance.Brightness(image).enhance(rng.uniform(0.55, 1.45))
    image = ImageEnhance.Contrast(image).enhance(rng.uniform(0.5, 1.5))
    if rng.random() < 0.6:
        image = image.filter(ImageFilter.GaussianBlur(rng.uniform(0.4, 2.2)))
    if rng.random() < 0.55:
        generator = torch.Generator().manual_seed(seed + 17)
        pixels = TF.to_tensor(image)
        pixels = (pixels + torch.randn(pixels.shape, generator=generator) * rng.uniform(0.02, 0.12)).clamp(0, 1)
        image = TF.to_pil_image(pixels)
    if rng.random() < 0.5:
        small = rng.randint(18, 48)
        image = image.resize((small, small), Image.Resampling.BILINEAR).resize((64, 64), Image.Resampling.BILINEAR)
    if rng.random() < 0.45:
        x, y, size = rng.randrange(52), rng.randrange(52), rng.randrange(5, 13)
        image.paste(tuple(rng.randrange(256) for _ in range(3)), (x, y, x + size, y + size))
    buffer = io.BytesIO()
    image.save(buffer, "JPEG", quality=rng.randint(20, 75))
    buffer.seek(0)
    return Image.open(buffer).convert("RGB").copy()