From 11bc45327acd38c1df626de0d48bad88983b8c26 Mon Sep 17 00:00:00 2001 From: Lionel Date: Tue, 5 Nov 2024 17:25:08 +0100 Subject: [PATCH 1/5] v1 and v2 independent transform import --- lightly/transforms/torchvision_transforms.py | 35 ++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 lightly/transforms/torchvision_transforms.py diff --git a/lightly/transforms/torchvision_transforms.py b/lightly/transforms/torchvision_transforms.py new file mode 100644 index 000000000..20e5d8f60 --- /dev/null +++ b/lightly/transforms/torchvision_transforms.py @@ -0,0 +1,35 @@ +# +# Copyright (c) Lightly AG and affiliates. +# All rights reserved. +# +# This source code is licensed under the license found in the +# LICENSE file in the root directory of this source tree. +# +from typing import Callable, Generic, Protocol, TypeVar, Union + +import torch +from PIL.Image import Image +from torch import Tensor +from torchvision.transforms import ToTensor as ToTensorV1 + +try: + from torchvision.transforms import v2 as torchvision_transforms + + _TRANSFORMS_V2 = True + +except ImportError: + from torchvision import transforms as torchvision_transforms + + _TRANSFORMS_V2 = False + + +def ToTensor() -> Union[torchvision_transforms.Compose, ToTensorV1]: + T = torchvision_transforms + if _TRANSFORMS_V2 and hasattr(T, "ToImage") and hasattr(T, "ToDtype"): + # v2.transforms.ToTensor is deprecated and will be removed in the future. + # This is the new recommended way to convert a PIL Image to a tensor since + # torchvision v0.16. + # See also https://github.com/pytorch/vision/blame/33e47d88265b2d57c2644aad1425be4fccd64605/torchvision/transforms/v2/_deprecated.py#L19 + return T.Compose([T.ToImage(), T.ToDtype(dtype=torch.float32, scale=True)]) + else: + return T.ToTensor() From 923565875e9a5757f7c6a55967edef231347d721 Mon Sep 17 00:00:00 2001 From: Lionel Date: Tue, 5 Nov 2024 17:25:29 +0100 Subject: [PATCH 2/5] make v1/v2 independent transforms available --- lightly/transforms/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lightly/transforms/__init__.py b/lightly/transforms/__init__.py index 6794c146f..81acff0fc 100644 --- a/lightly/transforms/__init__.py +++ b/lightly/transforms/__init__.py @@ -46,6 +46,7 @@ TiCoView1Transform, TiCoView2Transform, ) +from lightly.transforms.torchvision_transforms import torchvision_transforms from lightly.transforms.vicreg_transform import VICRegTransform, VICRegViewTransform from lightly.transforms.vicregl_transform import VICRegLTransform, VICRegLViewTransform from lightly.transforms.wmse_transform import WMSETransform From aaeb94ff14ad5efe44125fa0262d7d75c6ab9b5e Mon Sep 17 00:00:00 2001 From: Lionel Date: Tue, 5 Nov 2024 17:40:50 +0100 Subject: [PATCH 3/5] remove not needed types --- lightly/transforms/torchvision_transforms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lightly/transforms/torchvision_transforms.py b/lightly/transforms/torchvision_transforms.py index 20e5d8f60..6e26f571b 100644 --- a/lightly/transforms/torchvision_transforms.py +++ b/lightly/transforms/torchvision_transforms.py @@ -5,7 +5,7 @@ # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # -from typing import Callable, Generic, Protocol, TypeVar, Union +from typing import Union import torch from PIL.Image import Image From 70af7151f64630d118f95af3189555dc7dcf0d41 Mon Sep 17 00:00:00 2001 From: Lionel Date: Tue, 5 Nov 2024 17:48:40 +0100 Subject: [PATCH 4/5] remove ToTensor() for later --- lightly/transforms/torchvision_transforms.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/lightly/transforms/torchvision_transforms.py b/lightly/transforms/torchvision_transforms.py index 6e26f571b..e225d1d37 100644 --- a/lightly/transforms/torchvision_transforms.py +++ b/lightly/transforms/torchvision_transforms.py @@ -21,15 +21,3 @@ from torchvision import transforms as torchvision_transforms _TRANSFORMS_V2 = False - - -def ToTensor() -> Union[torchvision_transforms.Compose, ToTensorV1]: - T = torchvision_transforms - if _TRANSFORMS_V2 and hasattr(T, "ToImage") and hasattr(T, "ToDtype"): - # v2.transforms.ToTensor is deprecated and will be removed in the future. - # This is the new recommended way to convert a PIL Image to a tensor since - # torchvision v0.16. - # See also https://github.com/pytorch/vision/blame/33e47d88265b2d57c2644aad1425be4fccd64605/torchvision/transforms/v2/_deprecated.py#L19 - return T.Compose([T.ToImage(), T.ToDtype(dtype=torch.float32, scale=True)]) - else: - return T.ToTensor() From 4f3795c45f818acb931bac5916909704c06a19d1 Mon Sep 17 00:00:00 2001 From: Lionel Date: Wed, 6 Nov 2024 08:43:03 +0100 Subject: [PATCH 5/5] remove unused import and save for later --- lightly/transforms/torchvision_transforms.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lightly/transforms/torchvision_transforms.py b/lightly/transforms/torchvision_transforms.py index e225d1d37..061061e22 100644 --- a/lightly/transforms/torchvision_transforms.py +++ b/lightly/transforms/torchvision_transforms.py @@ -10,7 +10,6 @@ import torch from PIL.Image import Image from torch import Tensor -from torchvision.transforms import ToTensor as ToTensorV1 try: from torchvision.transforms import v2 as torchvision_transforms