Skip to content

Commit

Permalink
Remove import-time warning for v2 namespaces (#7853)
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasHug authored Aug 18, 2023
1 parent 87d54c4 commit 90e0b79
Show file tree
Hide file tree
Showing 13 changed files with 5 additions and 129 deletions.
3 changes: 0 additions & 3 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions gallery/v2_transforms/plot_custom_datapoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 0 additions & 6 deletions gallery/v2_transforms/plot_custom_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 0 additions & 6 deletions gallery/v2_transforms/plot_cutmix_mixup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
6 changes: 0 additions & 6 deletions gallery/v2_transforms/plot_datapoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 0 additions & 4 deletions gallery/v2_transforms/plot_transforms_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import pathlib

import torch
import torchvision


def load_data():
Expand Down Expand Up @@ -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(
Expand Down
12 changes: 2 additions & 10 deletions gallery/v2_transforms/plot_transforms_v2_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down
4 changes: 0 additions & 4 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
52 changes: 1 addition & 51 deletions test/test_transforms_v2.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import itertools
import pathlib
import random
import textwrap
import warnings

import numpy as np
Expand All @@ -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
Expand Down Expand Up @@ -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])

Expand Down
17 changes: 0 additions & 17 deletions torchvision/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 0 additions & 6 deletions torchvision/datapoints/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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``.
Expand Down
5 changes: 2 additions & 3 deletions torchvision/datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,13 @@
"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
# without a cyclic error.
# Ref: https://peps.python.org/pep-0562/
def __getattr__(name):
if name in ("wrap_dataset_for_transforms_v2",):
Expand Down
7 changes: 0 additions & 7 deletions torchvision/transforms/v2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 90e0b79

Please sign in to comment.