__init__.py 2.84 KB
Newer Older
1
import os
2
import warnings
3

4
import torch
5
from torchvision import datasets, io, models, ops, transforms, utils
6

7
from .extension import _HAS_OPS
8

9
10
try:
    from .version import __version__  # noqa: F401
Soumith Chintala's avatar
Soumith Chintala committed
11
except ImportError:
12
    pass
13

14
# Check if torchvision is being imported within the root folder
15
16
17
18
19
20
21
22
if not _HAS_OPS and os.path.dirname(os.path.realpath(__file__)) == os.path.join(
    os.path.realpath(os.getcwd()), "torchvision"
):
    message = (
        "You are importing torchvision within its own root folder ({}). "
        "This is not expected to work and may give errors. Please exit the "
        "torchvision project source and relaunch your python interpreter."
    )
23
24
    warnings.warn(message.format(os.getcwd()))

25
_image_backend = "PIL"
26

27
28
_video_backend = "pyav"

29
30
31
32
33
34

def set_image_backend(backend):
    """
    Specifies the package used to load images.

    Args:
35
36
37
        backend (string): Name of the image backend. one of {'PIL', 'accimage'}.
            The :mod:`accimage` package uses the Intel IPP library. It is
            generally faster than PIL, but does not support as many operations.
38
39
    """
    global _image_backend
40
    if backend not in ["PIL", "accimage"]:
41
        raise ValueError(f"Invalid backend '{backend}'. Options are 'PIL' and 'accimage'")
42
43
44
45
46
47
48
49
    _image_backend = backend


def get_image_backend():
    """
    Gets the name of the package used to load images
    """
    return _image_backend
50
51


52
53
54
55
56
57
58
def set_video_backend(backend):
    """
    Specifies the package used to decode videos.

    Args:
        backend (string): Name of the video backend. one of {'pyav', 'video_reader'}.
            The :mod:`pyav` package uses the 3rd party PyAv library. It is a Pythonic
59
60
61
            binding for the FFmpeg libraries.
            The :mod:`video_reader` package includes a native C++ implementation on
            top of FFMPEG libraries, and a python API of TorchScript custom operator.
62
            It generally decodes faster than :mod:`pyav`, but is perhaps less robust.
63
64

    .. note::
65
        Building with FFMPEG is disabled by default in the latest `main`. If you want to use the 'video_reader'
66
        backend, please compile torchvision from source.
67
68
69
    """
    global _video_backend
    if backend not in ["pyav", "video_reader"]:
70
        raise ValueError("Invalid video backend '%s'. Options are 'pyav' and 'video_reader'" % backend)
71
    if backend == "video_reader" and not io._HAS_VIDEO_OPT:
72
        message = "video_reader video backend is not available. Please compile torchvision from source and try again"
73
        warnings.warn(message)
74
75
    else:
        _video_backend = backend
76
77
78


def get_video_backend():
79
80
81
82
83
84
85
    """
    Returns the currently active video backend used to decode videos.

    Returns:
        str: Name of the video backend. one of {'pyav', 'video_reader'}.
    """

86
87
88
    return _video_backend


89
90
def _is_tracing():
    return torch._C._get_tracing_state()