"gpu/types.go" did not exist on "f8ef4439e9673c7df2314fafb5975aeab856c51f"
__init__.py 3.14 KB
Newer Older
1
import warnings
2
import os
3

4
5
from .extension import _HAS_OPS

6
7
from torchvision import models
from torchvision import datasets
8
from torchvision import ops
9
10
from torchvision import transforms
from torchvision import utils
11
from torchvision import io
12

eellison's avatar
eellison committed
13
import torch
14

15
16
try:
    from .version import __version__  # noqa: F401
Soumith Chintala's avatar
Soumith Chintala committed
17
except ImportError:
18
    pass
panning's avatar
panning committed
19
20
21
22
try:
    from .version import __dcu_version__  # noqa: F401
except ImportError:
    pass
23
24
25
26
27
28
29
30
# Check if torchvision is being imported within the root folder
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.')
    warnings.warn(message.format(os.getcwd()))

31
32
_image_backend = 'PIL'

33
34
_video_backend = "pyav"

35
36
37
38
39
40

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

    Args:
41
42
43
        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.
44
45
46
47
48
49
50
51
52
53
54
55
56
    """
    global _image_backend
    if backend not in ['PIL', 'accimage']:
        raise ValueError("Invalid backend '{}'. Options are 'PIL' and 'accimage'"
                         .format(backend))
    _image_backend = backend


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


59
60
61
62
63
64
65
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
66
67
68
69
            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.
            It is generally decoding faster than :mod:`pyav`, but perhaps is less robust.
70
71
72
73

    .. note::
        Building with FFMPEG is disabled by default in the latest master. If you want to use the 'video_reader'
        backend, please compile torchvision from source.
74
75
76
77
78
79
    """
    global _video_backend
    if backend not in ["pyav", "video_reader"]:
        raise ValueError(
            "Invalid video backend '%s'. Options are 'pyav' and 'video_reader'" % backend
        )
80
    if backend == "video_reader" and not io._HAS_VIDEO_OPT:
81
82
83
84
85
        message = (
            "video_reader video backend is not available."
            " Please compile torchvision from source and try again"
        )
        warnings.warn(message)
86
87
    else:
        _video_backend = backend
88
89
90


def get_video_backend():
91
92
93
94
95
96
97
    """
    Returns the currently active video backend used to decode videos.

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

98
99
100
    return _video_backend


101
102
def _is_tracing():
    return torch._C._get_tracing_state()