From d3782e2a00c2e4e303da6bfbd65346b4f3e406ee Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Fri, 18 Aug 2023 11:22:58 +0100 Subject: [PATCH 1/3] Remove import-time warning for v2 namespaces --- docs/source/conf.py | 3 -- test/conftest.py | 4 --- test/test_transforms_v2.py | 52 +-------------------------- torchvision/__init__.py | 17 --------- torchvision/datapoints/__init__.py | 6 ---- torchvision/datasets/__init__.py | 18 ++-------- torchvision/transforms/v2/__init__.py | 7 ---- 7 files changed, 4 insertions(+), 103 deletions(-) diff --git a/docs/source/conf.py b/docs/source/conf.py index 4a331b6cd75..de2adaa81c6 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -34,9 +34,6 @@ sys.path.append(os.path.abspath(".")) -torchvision.disable_beta_transforms_warning() -import torchvision.datapoints # Don't remove, otherwise the docs for datapoints aren't linked properly - # -- General configuration ------------------------------------------------ # Required version of sphinx is set from docs/requirements.txt diff --git a/test/conftest.py b/test/conftest.py index a54028bc70d..ea73b09b906 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -3,10 +3,6 @@ import numpy as np import pytest import torch -import torchvision - - -torchvision.disable_beta_transforms_warning() from common_utils import ( CUDA_NOT_AVAILABLE_MSG, diff --git a/test/test_transforms_v2.py b/test/test_transforms_v2.py index 5752b323f79..9630132e271 100644 --- a/test/test_transforms_v2.py +++ b/test/test_transforms_v2.py @@ -1,7 +1,6 @@ import itertools import pathlib import random -import textwrap import warnings import numpy as np @@ -11,7 +10,7 @@ import torch import torchvision.transforms.v2 as transforms -from common_utils import assert_equal, assert_run_python_script, cpu_and_cuda +from common_utils import assert_equal, cpu_and_cuda from torch.utils._pytree import tree_flatten, tree_unflatten from torchvision import datapoints from torchvision.ops.boxes import box_iou @@ -1279,55 +1278,6 @@ def test_sanitize_bounding_boxes_errors(): transforms.SanitizeBoundingBoxes()(different_sizes) -@pytest.mark.parametrize( - "import_statement", - ( - "from torchvision.transforms import v2", - "import torchvision.transforms.v2", - "from torchvision.transforms.v2 import Resize", - "import torchvision.transforms.v2.functional", - "from torchvision.transforms.v2.functional import resize", - "from torchvision import datapoints", - "from torchvision.datapoints import Image", - "from torchvision.datasets import wrap_dataset_for_transforms_v2", - ), -) -@pytest.mark.parametrize("call_disable_warning", (True, False)) -def test_warnings_v2_namespaces(import_statement, call_disable_warning): - if call_disable_warning: - source = f""" - import warnings - import torchvision - torchvision.disable_beta_transforms_warning() - with warnings.catch_warnings(): - warnings.simplefilter("error") - {import_statement} - """ - else: - source = f""" - import pytest - with pytest.warns(UserWarning, match="v2 namespaces are still Beta"): - {import_statement} - """ - assert_run_python_script(textwrap.dedent(source)) - - -def test_no_warnings_v1_namespace(): - source = """ - import warnings - with warnings.catch_warnings(): - warnings.simplefilter("error") - import torchvision.transforms - from torchvision import transforms - import torchvision.transforms.functional - from torchvision.transforms import Resize - from torchvision.transforms.functional import resize - from torchvision import datasets - from torchvision.datasets import ImageNet - """ - assert_run_python_script(textwrap.dedent(source)) - - class TestLambda: inputs = pytest.mark.parametrize("input", [object(), torch.empty(()), np.empty(()), "string", 1, 0.0]) diff --git a/torchvision/__init__.py b/torchvision/__init__.py index 3ce050e4d85..e44301c1a04 100644 --- a/torchvision/__init__.py +++ b/torchvision/__init__.py @@ -95,20 +95,3 @@ def get_video_backend(): def _is_tracing(): return torch._C._get_tracing_state() - - -_WARN_ABOUT_BETA_TRANSFORMS = True -_BETA_TRANSFORMS_WARNING = ( - "The torchvision.datapoints and torchvision.transforms.v2 namespaces are still Beta. " - "While we do not expect major breaking changes, some APIs may still change " - "according to user feedback. Please submit any feedback you may have in " - "this issue: https://github.com/pytorch/vision/issues/6753, and you can also " - "check out https://github.com/pytorch/vision/issues/7319 to learn more about " - "the APIs that we suspect might involve future changes. " - "You can silence this warning by calling torchvision.disable_beta_transforms_warning()." -) - - -def disable_beta_transforms_warning(): - global _WARN_ABOUT_BETA_TRANSFORMS - _WARN_ABOUT_BETA_TRANSFORMS = False diff --git a/torchvision/datapoints/__init__.py b/torchvision/datapoints/__init__.py index dcb110f74a7..512a8d606f2 100644 --- a/torchvision/datapoints/__init__.py +++ b/torchvision/datapoints/__init__.py @@ -1,5 +1,4 @@ import torch -from torchvision import _BETA_TRANSFORMS_WARNING, _WARN_ABOUT_BETA_TRANSFORMS from ._bounding_box import BoundingBoxes, BoundingBoxFormat from ._datapoint import Datapoint @@ -8,11 +7,6 @@ from ._torch_function_helpers import set_return_type from ._video import Video -if _WARN_ABOUT_BETA_TRANSFORMS: - import warnings - - warnings.warn(_BETA_TRANSFORMS_WARNING) - def wrap(wrappee, *, like, **kwargs): """[BETA] Convert a :class:`torch.Tensor` (``wrappee``) into the same :class:`~torchvision.datapoints.Datapoint` subclass as ``like``. diff --git a/torchvision/datasets/__init__.py b/torchvision/datasets/__init__.py index 7d3357e3dc2..1957d0db43a 100644 --- a/torchvision/datasets/__init__.py +++ b/torchvision/datasets/__init__.py @@ -1,3 +1,5 @@ +from torchvision.datapoints._dataset_wrapper import wrap_dataset_for_transforms_v2 + from ._optical_flow import FlyingChairs, FlyingThings3D, HD1K, KittiFlow, Sintel from ._stereo_matching import ( CarlaStereo, @@ -127,19 +129,5 @@ "SintelStereo", "InStereo2k", "ETH3DStereo", + "wrap_dataset_for_transforms_v2", ) - - -# We override current module's attributes to handle the import: -# from torchvision.datasets import wrap_dataset_for_transforms_v2 -# with beta state v2 warning from torchvision.datapoints -# We also want to avoid raising the warning when importing other attributes -# from torchvision.datasets -# Ref: https://peps.python.org/pep-0562/ -def __getattr__(name): - if name in ("wrap_dataset_for_transforms_v2",): - from torchvision.datapoints._dataset_wrapper import wrap_dataset_for_transforms_v2 - - return wrap_dataset_for_transforms_v2 - - raise AttributeError(f"module {__name__!r} has no attribute {name!r}") diff --git a/torchvision/transforms/v2/__init__.py b/torchvision/transforms/v2/__init__.py index bc15c96b51b..b60962748d1 100644 --- a/torchvision/transforms/v2/__init__.py +++ b/torchvision/transforms/v2/__init__.py @@ -55,10 +55,3 @@ from ._type_conversion import PILToTensor, ToImage, ToPILImage, ToPureTensor from ._deprecated import ToTensor # usort: skip - -from torchvision import _BETA_TRANSFORMS_WARNING, _WARN_ABOUT_BETA_TRANSFORMS - -if _WARN_ABOUT_BETA_TRANSFORMS: - import warnings - - warnings.warn(_BETA_TRANSFORMS_WARNING) From 7f8ed881cca6986fdc4afb4a283d2426a5ad38cb Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Fri, 18 Aug 2023 11:34:21 +0100 Subject: [PATCH 2/3] Fixes --- torchvision/datasets/__init__.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/torchvision/datasets/__init__.py b/torchvision/datasets/__init__.py index 1957d0db43a..43b0801d495 100644 --- a/torchvision/datasets/__init__.py +++ b/torchvision/datasets/__init__.py @@ -1,5 +1,3 @@ -from torchvision.datapoints._dataset_wrapper import wrap_dataset_for_transforms_v2 - from ._optical_flow import FlyingChairs, FlyingThings3D, HD1K, KittiFlow, Sintel from ._stereo_matching import ( CarlaStereo, @@ -131,3 +129,16 @@ "ETH3DStereo", "wrap_dataset_for_transforms_v2", ) + + +# We override current module's attributes to handle the import: +# from torchvision.datasets import wrap_dataset_for_transforms_v2 +# without a cyclic error. +# Ref: https://peps.python.org/pep-0562/ +def __getattr__(name): + if name in ("wrap_dataset_for_transforms_v2",): + from torchvision.datapoints._dataset_wrapper import wrap_dataset_for_transforms_v2 + + return wrap_dataset_for_transforms_v2 + + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") From 3fad5be5a4774c514c68eeef19b9ef9eae04bdc1 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Fri, 18 Aug 2023 12:13:14 +0100 Subject: [PATCH 3/3] Oops --- gallery/v2_transforms/plot_custom_datapoints.py | 6 ------ gallery/v2_transforms/plot_custom_transforms.py | 6 ------ gallery/v2_transforms/plot_cutmix_mixup.py | 6 ------ gallery/v2_transforms/plot_datapoints.py | 6 ------ gallery/v2_transforms/plot_transforms_v2.py | 4 ---- gallery/v2_transforms/plot_transforms_v2_e2e.py | 12 ++---------- 6 files changed, 2 insertions(+), 38 deletions(-) diff --git a/gallery/v2_transforms/plot_custom_datapoints.py b/gallery/v2_transforms/plot_custom_datapoints.py index dcad5f0a406..fb72222ba10 100644 --- a/gallery/v2_transforms/plot_custom_datapoints.py +++ b/gallery/v2_transforms/plot_custom_datapoints.py @@ -11,12 +11,6 @@ # %% import torch -import torchvision - -# We are using BETA APIs, so we deactivate the associated warning, thereby acknowledging that -# some APIs may slightly change in the future -torchvision.disable_beta_transforms_warning() - from torchvision import datapoints from torchvision.transforms import v2 diff --git a/gallery/v2_transforms/plot_custom_transforms.py b/gallery/v2_transforms/plot_custom_transforms.py index eba8e91faf4..18b6c82b286 100644 --- a/gallery/v2_transforms/plot_custom_transforms.py +++ b/gallery/v2_transforms/plot_custom_transforms.py @@ -9,12 +9,6 @@ # %% import torch -import torchvision - -# We are using BETA APIs, so we deactivate the associated warning, thereby acknowledging that -# some APIs may slightly change in the future -torchvision.disable_beta_transforms_warning() - from torchvision import datapoints from torchvision.transforms import v2 diff --git a/gallery/v2_transforms/plot_cutmix_mixup.py b/gallery/v2_transforms/plot_cutmix_mixup.py index 932ce325b56..55799b1788e 100644 --- a/gallery/v2_transforms/plot_cutmix_mixup.py +++ b/gallery/v2_transforms/plot_cutmix_mixup.py @@ -17,13 +17,7 @@ # %% import torch -import torchvision from torchvision.datasets import FakeData - -# We are using BETA APIs, so we deactivate the associated warning, thereby acknowledging that -# some APIs may slightly change in the future -torchvision.disable_beta_transforms_warning() - from torchvision.transforms import v2 diff --git a/gallery/v2_transforms/plot_datapoints.py b/gallery/v2_transforms/plot_datapoints.py index 0bab2d34088..1e696af8e01 100644 --- a/gallery/v2_transforms/plot_datapoints.py +++ b/gallery/v2_transforms/plot_datapoints.py @@ -24,12 +24,6 @@ import PIL.Image import torch -import torchvision - -# We are using BETA APIs, so we deactivate the associated warning, thereby acknowledging that -# some APIs may slightly change in the future -torchvision.disable_beta_transforms_warning() - from torchvision import datapoints from torchvision.transforms.v2 import functional as F diff --git a/gallery/v2_transforms/plot_transforms_v2.py b/gallery/v2_transforms/plot_transforms_v2.py index e6c8b3ffdb5..1449402862b 100644 --- a/gallery/v2_transforms/plot_transforms_v2.py +++ b/gallery/v2_transforms/plot_transforms_v2.py @@ -11,7 +11,6 @@ import pathlib import torch -import torchvision def load_data(): @@ -42,9 +41,6 @@ def load_data(): # detection or instance and semantic segmentation. Still, the interface is the same, making # :mod:`torchvision.transforms.v2` a drop-in replacement for the existing :mod:`torchvision.transforms` API, aka v1. -# We are using BETA APIs, so we deactivate the associated warning, thereby acknowledging that -# some APIs may slightly change in the future -torchvision.disable_beta_transforms_warning() import torchvision.transforms.v2 as transforms transform = transforms.Compose( diff --git a/gallery/v2_transforms/plot_transforms_v2_e2e.py b/gallery/v2_transforms/plot_transforms_v2_e2e.py index e6a36ebbf58..51e3baff118 100644 --- a/gallery/v2_transforms/plot_transforms_v2_e2e.py +++ b/gallery/v2_transforms/plot_transforms_v2_e2e.py @@ -16,7 +16,8 @@ import torch import torch.utils.data -import torchvision +from torchvision import models, datasets +import torchvision.transforms.v2 as transforms def show(sample): @@ -39,19 +40,10 @@ def show(sample): fig.show() -# We are using BETA APIs, so we deactivate the associated warning, thereby acknowledging that -# some APIs may slightly change in the future -torchvision.disable_beta_transforms_warning() - -from torchvision import models, datasets -import torchvision.transforms.v2 as transforms - - # %% # We start off by loading the :class:`~torchvision.datasets.CocoDetection` dataset to have a look at what it currently # returns, and we'll see how to convert it to a format that is compatible with our new transforms. - def load_example_coco_detection_dataset(**kwargs): # This loads fake data for illustration purposes of this example. In practice, you'll have # to replace this with the proper data