__init__.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import os
  2. import warnings
  3. from modulefinder import Module
  4. import torch
  5. # from torchvision import _meta_registrations, datasets, io, models, ops, transforms, utils
  6. from torchvision import datasets, io, models, ops, transforms, utils
  7. from .extension import _HAS_OPS
  8. try:
  9. from .version import __version__ # noqa: F401
  10. except ImportError:
  11. pass
  12. # Check if torchvision is being imported within the root folder
  13. if not _HAS_OPS and os.path.dirname(os.path.realpath(__file__)) == os.path.join(
  14. os.path.realpath(os.getcwd()), "torchvision"
  15. ):
  16. message = (
  17. "You are importing torchvision within its own root folder ({}). "
  18. "This is not expected to work and may give errors. Please exit the "
  19. "torchvision project source and relaunch your python interpreter."
  20. )
  21. warnings.warn(message.format(os.getcwd()))
  22. _image_backend = "PIL"
  23. _video_backend = "pyav"
  24. def set_image_backend(backend):
  25. """
  26. Specifies the package used to load images.
  27. Args:
  28. backend (string): Name of the image backend. one of {'PIL', 'accimage'}.
  29. The :mod:`accimage` package uses the Intel IPP library. It is
  30. generally faster than PIL, but does not support as many operations.
  31. """
  32. global _image_backend
  33. if backend not in ["PIL", "accimage"]:
  34. raise ValueError(f"Invalid backend '{backend}'. Options are 'PIL' and 'accimage'")
  35. _image_backend = backend
  36. def get_image_backend():
  37. """
  38. Gets the name of the package used to load images
  39. """
  40. return _image_backend
  41. def set_video_backend(backend):
  42. """
  43. Specifies the package used to decode videos.
  44. Args:
  45. backend (string): Name of the video backend. one of {'pyav', 'video_reader'}.
  46. The :mod:`pyav` package uses the 3rd party PyAv library. It is a Pythonic
  47. binding for the FFmpeg libraries.
  48. The :mod:`video_reader` package includes a native C++ implementation on
  49. top of FFMPEG libraries, and a python API of TorchScript custom operator.
  50. It generally decodes faster than :mod:`pyav`, but is perhaps less robust.
  51. .. note::
  52. Building with FFMPEG is disabled by default in the latest `main`. If you want to use the 'video_reader'
  53. backend, please compile torchvision from source.
  54. """
  55. global _video_backend
  56. if backend not in ["pyav", "video_reader", "cuda"]:
  57. raise ValueError("Invalid video backend '%s'. Options are 'pyav', 'video_reader' and 'cuda'" % backend)
  58. if backend == "video_reader" and not io._HAS_VIDEO_OPT:
  59. # TODO: better messages
  60. message = "video_reader video backend is not available. Please compile torchvision from source and try again"
  61. raise RuntimeError(message)
  62. elif backend == "cuda" and not io._HAS_GPU_VIDEO_DECODER:
  63. # TODO: better messages
  64. message = "cuda video backend is not available."
  65. raise RuntimeError(message)
  66. else:
  67. _video_backend = backend
  68. def get_video_backend():
  69. """
  70. Returns the currently active video backend used to decode videos.
  71. Returns:
  72. str: Name of the video backend. one of {'pyav', 'video_reader'}.
  73. """
  74. return _video_backend
  75. def _is_tracing():
  76. return torch._C._get_tracing_state()
  77. def disable_beta_transforms_warning():
  78. # Noop, only exists to avoid breaking existing code.
  79. # See https://github.com/pytorch/vision/issues/7896
  80. pass