From 64e043fa85c34d70ec01226a6535b8f1b677fe0f Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Wed, 15 Jun 2022 17:51:09 +0100 Subject: [PATCH 1/3] Add remove-able warnings for Beta modules or functions --- torchvision/io/__init__.py | 8 ++++++++ torchvision/io/video_reader.py | 11 +++++++++++ torchvision/models/__init__.py | 8 +++++++- torchvision/models/detection/__init__.py | 12 ++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/torchvision/io/__init__.py b/torchvision/io/__init__.py index 22788cef71e..7a117f011a3 100644 --- a/torchvision/io/__init__.py +++ b/torchvision/io/__init__.py @@ -40,6 +40,14 @@ from .video_reader import VideoReader +_BETA_VIDEO_API_IS_ENABLED = False + + +def enable_beta_video_api(): + global _BETA_VIDEO_API_IS_ENABLED + _BETA_VIDEO_API_IS_ENABLED = True + + __all__ = [ "write_video", "read_video", diff --git a/torchvision/io/video_reader.py b/torchvision/io/video_reader.py index 881b9d75bd4..bfa326bbda3 100644 --- a/torchvision/io/video_reader.py +++ b/torchvision/io/video_reader.py @@ -1,3 +1,4 @@ +import warnings from typing import Any, Dict, Iterator import torch @@ -92,6 +93,16 @@ class VideoReader: def __init__(self, path: str, stream: str = "video", num_threads: int = 0, device: str = "cpu") -> None: _log_api_usage_once(self) + from . import _BETA_VIDEO_API_IS_ENABLED # import here to avoid circular import + + if not _BETA_VIDEO_API_IS_ENABLED: + warnings.warn( + "The VideoReader class is still in Beta stage, which means " + "that backward compatibility isn't fully guaranteed. " + "Please visit to learn more about what we are planning to change in future versions. " + "To silence this warning, please call torchvision.io.enable_beta_video_api." + ) + self.is_cuda = False device = torch.device(device) if device.type == "cuda": diff --git a/torchvision/models/__init__.py b/torchvision/models/__init__.py index 00b5ebefe55..b56eb2c2b47 100644 --- a/torchvision/models/__init__.py +++ b/torchvision/models/__init__.py @@ -13,9 +13,15 @@ from .vgg import * from .vision_transformer import * from .swin_transformer import * -from . import detection from . import optical_flow from . import quantization from . import segmentation from . import video from ._api import get_weight + +_BETA_DETECTION_IS_ENABLED = False + + +def enable_beta_detection(): + global _BETA_DETECTION_IS_ENABLED + _BETA_DETECTION_IS_ENABLED = True diff --git a/torchvision/models/detection/__init__.py b/torchvision/models/detection/__init__.py index 4146651c737..909fc62face 100644 --- a/torchvision/models/detection/__init__.py +++ b/torchvision/models/detection/__init__.py @@ -5,3 +5,15 @@ from .retinanet import * from .ssd import * from .ssdlite import * + +import warnings + +from .. import _BETA_DETECTION_IS_ENABLED + +if not _BETA_DETECTION_IS_ENABLED: + warnings.warn( + "The torchvision.models.detection module is still in Beta stage, which means " + "that backward compatibility isn't fully guaranteed. " + "Please visit to learn more about what we are planning to change in future versions. " + "To silence this warning, please call torchvision.models.enable_beta_detection." + ) From ecc3e249022d533f3201223360edbf46ef3686da Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Thu, 16 Jun 2022 10:31:36 +0100 Subject: [PATCH 2/3] Keep detection attribute --- test/test_models.py | 6 +++++- torchvision/models/__init__.py | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/test/test_models.py b/test/test_models.py index 0acef4dcef6..dcf11d2ce04 100644 --- a/test/test_models.py +++ b/test/test_models.py @@ -26,7 +26,11 @@ def get_models_from_module(module): return [ v for k, v in module.__dict__.items() - if callable(v) and k[0].lower() == k[0] and k[0] != "_" and k != "get_weight" + if callable(v) + and k[0].lower() == k[0] + and k[0] != "_" + and k != "get_weight" + and not k.startswith("enable_beta") ] diff --git a/torchvision/models/__init__.py b/torchvision/models/__init__.py index b56eb2c2b47..35526a56526 100644 --- a/torchvision/models/__init__.py +++ b/torchvision/models/__init__.py @@ -1,3 +1,5 @@ +import importlib + from .alexnet import * from .convnext import * from .densenet import * @@ -19,6 +21,14 @@ from . import video from ._api import get_weight + +def __getattr__(name): + if name == "detection": + return importlib.import_module("." + name, __name__) + else: + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") + + _BETA_DETECTION_IS_ENABLED = False From a8a58fa0605759f975ca9535c5c09ce03b9d9c65 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Fri, 17 Jun 2022 13:36:59 +0100 Subject: [PATCH 3/3] Fix other tests --- test/test_backbone_utils.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/test_backbone_utils.py b/test/test_backbone_utils.py index a2b2406441e..0c8ae971929 100644 --- a/test/test_backbone_utils.py +++ b/test/test_backbone_utils.py @@ -16,7 +16,11 @@ def get_available_models(): return [ k for k, v in models.__dict__.items() - if callable(v) and k[0].lower() == k[0] and k[0] != "_" and k != "get_weight" + if callable(v) + and k[0].lower() == k[0] + and k[0] != "_" + and k != "get_weight" + and not k.startswith("enable_beta") ]