|
1 |
| -from __future__ import annotations |
2 |
| - |
3 | 1 | import warnings
|
4 |
| -from typing import Any, Dict |
| 2 | +from typing import Any, Dict, Optional |
5 | 3 |
|
| 4 | +import numpy as np |
| 5 | +import PIL.Image |
| 6 | +from torchvision.prototype import features |
6 | 7 | from torchvision.prototype.features import ColorSpace
|
7 | 8 | from torchvision.prototype.transforms import Transform
|
| 9 | +from torchvision.transforms import functional as _F |
8 | 10 | from typing_extensions import Literal
|
9 | 11 |
|
10 | 12 | from ._meta import ConvertImageColorSpace
|
11 | 13 | from ._transform import _RandomApplyTransform
|
| 14 | +from ._utils import is_simple_tensor |
| 15 | + |
| 16 | + |
| 17 | +class ToTensor(Transform): |
| 18 | + def __init__(self) -> None: |
| 19 | + warnings.warn( |
| 20 | + "The transform `ToTensor()` is deprecated and will be removed in a future release. " |
| 21 | + "Instead, please use `transforms.ToImageTensor()`." |
| 22 | + ) |
| 23 | + super().__init__() |
| 24 | + |
| 25 | + def _transform(self, input: Any, params: Dict[str, Any]) -> Any: |
| 26 | + if isinstance(input, (PIL.Image.Image, np.ndarray)): |
| 27 | + return _F.to_tensor(input) |
| 28 | + else: |
| 29 | + return input |
| 30 | + |
| 31 | + |
| 32 | +class PILToTensor(Transform): |
| 33 | + def __init__(self) -> None: |
| 34 | + warnings.warn( |
| 35 | + "The transform `PILToTensor()` is deprecated and will be removed in a future release. " |
| 36 | + "Instead, please use `transforms.ToImageTensor()`." |
| 37 | + ) |
| 38 | + super().__init__() |
| 39 | + |
| 40 | + def _transform(self, input: Any, params: Dict[str, Any]) -> Any: |
| 41 | + if isinstance(input, PIL.Image.Image): |
| 42 | + return _F.pil_to_tensor(input) |
| 43 | + else: |
| 44 | + return input |
| 45 | + |
| 46 | + |
| 47 | +class ToPILImage(Transform): |
| 48 | + def __init__(self, mode: Optional[str] = None) -> None: |
| 49 | + warnings.warn( |
| 50 | + "The transform `ToPILImage()` is deprecated and will be removed in a future release. " |
| 51 | + "Instead, please use `transforms.ToImagePIL()`." |
| 52 | + ) |
| 53 | + super().__init__() |
| 54 | + self.mode = mode |
| 55 | + |
| 56 | + def _transform(self, input: Any, params: Dict[str, Any]) -> Any: |
| 57 | + if is_simple_tensor(input) or isinstance(input, (features.Image, np.ndarray)): |
| 58 | + return _F.to_pil_image(input, mode=self.mode) |
| 59 | + else: |
| 60 | + return input |
12 | 61 |
|
13 | 62 |
|
14 | 63 | class Grayscale(Transform):
|
|
0 commit comments