diff options
author | aktersnurra <grydholm@kth.se> | 2020-12-02 23:48:52 +0100 |
---|---|---|
committer | aktersnurra <grydholm@kth.se> | 2020-12-02 23:48:52 +0100 |
commit | 5529e0fc9ca39e81fe0f08a54f257d32f0afe120 (patch) | |
tree | f2be992554e278857db7d56786dba54a76d439c7 /src/text_recognizer/datasets | |
parent | e3b039c9adb4bce42ede4cb682a3ae71e797539a (diff) | |
parent | 8e3985c9cde6666e4314973312135ec1c7a025b9 (diff) |
Merge branch 'master' of github.com:aktersnurra/text-recognizer
Diffstat (limited to 'src/text_recognizer/datasets')
-rw-r--r-- | src/text_recognizer/datasets/transforms.py | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/src/text_recognizer/datasets/transforms.py b/src/text_recognizer/datasets/transforms.py index 1105f23..1ec23dc 100644 --- a/src/text_recognizer/datasets/transforms.py +++ b/src/text_recognizer/datasets/transforms.py @@ -4,7 +4,7 @@ from PIL import Image import torch from torch import Tensor import torch.nn.functional as F -from torchvision.transforms import Compose, ToPILImage, ToTensor +from torchvision.transforms import Compose, RandomAffine, ToTensor from text_recognizer.datasets.util import EmnistMapper @@ -64,3 +64,16 @@ class AddTokens: target = torch.cat([sos, target], dim=0) return target + + +class ApplyContrast: + """Sets everything below a threshold to zero, i.e. increase contrast.""" + + def __init__(self, low: float = 0.0, high: float = 0.25) -> None: + self.low = low + self.high = high + + def __call__(self, x: Tensor) -> Tensor: + """Apply mask binary mask to input tensor.""" + mask = x > np.random.RandomState().uniform(low=self.low, high=self.high) + return x * mask |