|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import torch |
| 9 | +import torchvision.transforms.functional as TF |
| 10 | + |
| 11 | + |
| 12 | +class Grayscale: |
| 13 | + def __call__(self, img): |
| 14 | + if isinstance(img, torch.Tensor): |
| 15 | + if img.ndim == 3 and img.shape[0] == 3: |
| 16 | + return TF.rgb_to_grayscale(img, num_output_channels=1) |
| 17 | + return img |
| 18 | + if isinstance(img, np.ndarray) and img.ndim == 3 and img.shape[-1] == 3: |
| 19 | + return img.mean(axis=-1, keepdims=True) |
| 20 | + return img |
| 21 | + |
| 22 | + |
| 23 | +class Resize: |
| 24 | + def __init__(self, size: int = 32): |
| 25 | + self.size = size |
| 26 | + |
| 27 | + def __call__(self, img): |
| 28 | + if isinstance(img, torch.Tensor): |
| 29 | + return TF.resize(img, [self.size, self.size], antialias=True) |
| 30 | + if isinstance(img, np.ndarray): |
| 31 | + tensor = torch.as_tensor(img).permute(2, 0, 1) if img.ndim == 3 else torch.as_tensor(img)[None] |
| 32 | + tensor = TF.resize(tensor, [self.size, self.size], antialias=True) |
| 33 | + return tensor.squeeze(0).numpy() if img.ndim == 2 else tensor.permute(1, 2, 0).numpy() |
| 34 | + return img |
| 35 | + |
| 36 | + |
| 37 | +class ToFloat: |
| 38 | + def __init__(self, normalise: bool = False): |
| 39 | + self.normalise = normalise |
| 40 | + |
| 41 | + def __call__(self, img): |
| 42 | + if isinstance(img, torch.Tensor): |
| 43 | + img = img.float() |
| 44 | + else: |
| 45 | + img = np.asarray(img, dtype=np.float32) |
| 46 | + if self.normalise: |
| 47 | + img = img / 255.0 |
| 48 | + return img |
| 49 | + |
| 50 | + |
| 51 | +class ToTensor: |
| 52 | + def __call__(self, img): |
| 53 | + if isinstance(img, torch.Tensor): |
| 54 | + return img |
| 55 | + arr = np.asarray(img) |
| 56 | + if arr.ndim == 2: |
| 57 | + arr = arr[None, ...] |
| 58 | + elif arr.ndim == 3: |
| 59 | + arr = arr.transpose(2, 0, 1) |
| 60 | + return torch.as_tensor(arr).float() |
| 61 | + |
| 62 | + |
| 63 | +class RandomFlipUDSet: |
| 64 | + def __init__(self, p: float = 0.5): |
| 65 | + self.p = p |
| 66 | + |
| 67 | + def __call__(self, sample): |
| 68 | + if torch.rand(1).item() >= self.p: |
| 69 | + return sample |
| 70 | + if isinstance(sample, torch.Tensor): |
| 71 | + return torch.flip(sample, dims=[-2]) |
| 72 | + return np.flip(sample, axis=-3 if sample.ndim >= 3 else 0).copy() |
| 73 | + |
| 74 | + |
| 75 | +class RandomRotateSet: |
| 76 | + def __init__(self, angles=(0, 90, 180, 270)): |
| 77 | + self.angles = list(angles) |
| 78 | + |
| 79 | + def __call__(self, sample): |
| 80 | + angle = float(self.angles[torch.randint(0, len(self.angles), (1,)).item()]) |
| 81 | + if isinstance(sample, torch.Tensor): |
| 82 | + return TF.rotate(sample, angle) |
| 83 | + tensor = torch.as_tensor(sample) |
| 84 | + if tensor.ndim == 3 and tensor.shape[-1] in (1, 3): |
| 85 | + tensor = tensor.permute(2, 0, 1) |
| 86 | + tensor = TF.rotate(tensor, angle) |
| 87 | + return tensor.permute(1, 2, 0).numpy() |
| 88 | + return TF.rotate(tensor.unsqueeze(0), angle).squeeze(0).numpy() |
0 commit comments