image.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. from enum import Enum
  2. from warnings import warn
  3. import torch
  4. from ..extension import _load_library
  5. from ..utils import _log_api_usage_once
  6. try:
  7. _load_library("image")
  8. except (ImportError, OSError) as e:
  9. warn(
  10. f"Failed to load image Python extension: '{e}'"
  11. f"If you don't plan on using image functionality from `torchvision.io`, you can ignore this warning. "
  12. f"Otherwise, there might be something wrong with your environment. "
  13. f"Did you have `libjpeg` or `libpng` installed before building `torchvision` from source?"
  14. )
  15. class ImageReadMode(Enum):
  16. """
  17. Support for various modes while reading images.
  18. Use ``ImageReadMode.UNCHANGED`` for loading the image as-is,
  19. ``ImageReadMode.GRAY`` for converting to grayscale,
  20. ``ImageReadMode.GRAY_ALPHA`` for grayscale with transparency,
  21. ``ImageReadMode.RGB`` for RGB and ``ImageReadMode.RGB_ALPHA`` for
  22. RGB with transparency.
  23. """
  24. UNCHANGED = 0
  25. GRAY = 1
  26. GRAY_ALPHA = 2
  27. RGB = 3
  28. RGB_ALPHA = 4
  29. def read_file(path: str) -> torch.Tensor:
  30. """
  31. Reads and outputs the bytes contents of a file as a uint8 Tensor
  32. with one dimension.
  33. Args:
  34. path (str): the path to the file to be read
  35. Returns:
  36. data (Tensor)
  37. """
  38. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  39. _log_api_usage_once(read_file)
  40. data = torch.ops.image.read_file(path)
  41. return data
  42. def write_file(filename: str, data: torch.Tensor) -> None:
  43. """
  44. Writes the contents of an uint8 tensor with one dimension to a
  45. file.
  46. Args:
  47. filename (str): the path to the file to be written
  48. data (Tensor): the contents to be written to the output file
  49. """
  50. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  51. _log_api_usage_once(write_file)
  52. torch.ops.image.write_file(filename, data)
  53. def decode_png(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:
  54. """
  55. Decodes a PNG image into a 3 dimensional RGB or grayscale Tensor.
  56. Optionally converts the image to the desired format.
  57. The values of the output tensor are uint8 in [0, 255].
  58. Args:
  59. input (Tensor[1]): a one dimensional uint8 tensor containing
  60. the raw bytes of the PNG image.
  61. mode (ImageReadMode): the read mode used for optionally
  62. converting the image. Default: ``ImageReadMode.UNCHANGED``.
  63. See `ImageReadMode` class for more information on various
  64. available modes.
  65. Returns:
  66. output (Tensor[image_channels, image_height, image_width])
  67. """
  68. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  69. _log_api_usage_once(decode_png)
  70. output = torch.ops.image.decode_png(input, mode.value, False)
  71. return output
  72. def encode_png(input: torch.Tensor, compression_level: int = 6) -> torch.Tensor:
  73. """
  74. Takes an input tensor in CHW layout and returns a buffer with the contents
  75. of its corresponding PNG file.
  76. Args:
  77. input (Tensor[channels, image_height, image_width]): int8 image tensor of
  78. ``c`` channels, where ``c`` must 3 or 1.
  79. compression_level (int): Compression factor for the resulting file, it must be a number
  80. between 0 and 9. Default: 6
  81. Returns:
  82. Tensor[1]: A one dimensional int8 tensor that contains the raw bytes of the
  83. PNG file.
  84. """
  85. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  86. _log_api_usage_once(encode_png)
  87. output = torch.ops.image.encode_png(input, compression_level)
  88. return output
  89. def write_png(input: torch.Tensor, filename: str, compression_level: int = 6):
  90. """
  91. Takes an input tensor in CHW layout (or HW in the case of grayscale images)
  92. and saves it in a PNG file.
  93. Args:
  94. input (Tensor[channels, image_height, image_width]): int8 image tensor of
  95. ``c`` channels, where ``c`` must be 1 or 3.
  96. filename (str): Path to save the image.
  97. compression_level (int): Compression factor for the resulting file, it must be a number
  98. between 0 and 9. Default: 6
  99. """
  100. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  101. _log_api_usage_once(write_png)
  102. output = encode_png(input, compression_level)
  103. write_file(filename, output)
  104. def decode_jpeg(
  105. input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED, device: str = "cpu"
  106. ) -> torch.Tensor:
  107. """
  108. Decodes a JPEG image into a 3 dimensional RGB or grayscale Tensor.
  109. Optionally converts the image to the desired format.
  110. The values of the output tensor are uint8 between 0 and 255.
  111. Args:
  112. input (Tensor[1]): a one dimensional uint8 tensor containing
  113. the raw bytes of the JPEG image. This tensor must be on CPU,
  114. regardless of the ``device`` parameter.
  115. mode (ImageReadMode): the read mode used for optionally
  116. converting the image. The supported modes are: ``ImageReadMode.UNCHANGED``,
  117. ``ImageReadMode.GRAY`` and ``ImageReadMode.RGB``
  118. Default: ``ImageReadMode.UNCHANGED``.
  119. See ``ImageReadMode`` class for more information on various
  120. available modes.
  121. device (str or torch.device): The device on which the decoded image will
  122. be stored. If a cuda device is specified, the image will be decoded
  123. with `nvjpeg <https://developer.nvidia.com/nvjpeg>`_. This is only
  124. supported for CUDA version >= 10.1
  125. .. betastatus:: device parameter
  126. .. warning::
  127. There is a memory leak in the nvjpeg library for CUDA versions < 11.6.
  128. Make sure to rely on CUDA 11.6 or above before using ``device="cuda"``.
  129. Returns:
  130. output (Tensor[image_channels, image_height, image_width])
  131. """
  132. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  133. _log_api_usage_once(decode_jpeg)
  134. device = torch.device(device)
  135. if device.type == "cuda":
  136. output = torch.ops.image.decode_jpeg_cuda(input, mode.value, device)
  137. else:
  138. output = torch.ops.image.decode_jpeg(input, mode.value)
  139. return output
  140. def encode_jpeg(input: torch.Tensor, quality: int = 75) -> torch.Tensor:
  141. """
  142. Takes an input tensor in CHW layout and returns a buffer with the contents
  143. of its corresponding JPEG file.
  144. Args:
  145. input (Tensor[channels, image_height, image_width])): int8 image tensor of
  146. ``c`` channels, where ``c`` must be 1 or 3.
  147. quality (int): Quality of the resulting JPEG file, it must be a number between
  148. 1 and 100. Default: 75
  149. Returns:
  150. output (Tensor[1]): A one dimensional int8 tensor that contains the raw bytes of the
  151. JPEG file.
  152. """
  153. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  154. _log_api_usage_once(encode_jpeg)
  155. if quality < 1 or quality > 100:
  156. raise ValueError("Image quality should be a positive number between 1 and 100")
  157. output = torch.ops.image.encode_jpeg(input, quality)
  158. return output
  159. def write_jpeg(input: torch.Tensor, filename: str, quality: int = 75):
  160. """
  161. Takes an input tensor in CHW layout and saves it in a JPEG file.
  162. Args:
  163. input (Tensor[channels, image_height, image_width]): int8 image tensor of ``c``
  164. channels, where ``c`` must be 1 or 3.
  165. filename (str): Path to save the image.
  166. quality (int): Quality of the resulting JPEG file, it must be a number
  167. between 1 and 100. Default: 75
  168. """
  169. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  170. _log_api_usage_once(write_jpeg)
  171. output = encode_jpeg(input, quality)
  172. write_file(filename, output)
  173. def decode_image(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:
  174. """
  175. Detects whether an image is a JPEG or PNG and performs the appropriate
  176. operation to decode the image into a 3 dimensional RGB or grayscale Tensor.
  177. Optionally converts the image to the desired format.
  178. The values of the output tensor are uint8 in [0, 255].
  179. Args:
  180. input (Tensor): a one dimensional uint8 tensor containing the raw bytes of the
  181. PNG or JPEG image.
  182. mode (ImageReadMode): the read mode used for optionally converting the image.
  183. Default: ``ImageReadMode.UNCHANGED``.
  184. See ``ImageReadMode`` class for more information on various
  185. available modes.
  186. Returns:
  187. output (Tensor[image_channels, image_height, image_width])
  188. """
  189. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  190. _log_api_usage_once(decode_image)
  191. output = torch.ops.image.decode_image(input, mode.value)
  192. return output
  193. def read_image(path: str, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:
  194. """
  195. Reads a JPEG or PNG image into a 3 dimensional RGB or grayscale Tensor.
  196. Optionally converts the image to the desired format.
  197. The values of the output tensor are uint8 in [0, 255].
  198. Args:
  199. path (str): path of the JPEG or PNG image.
  200. mode (ImageReadMode): the read mode used for optionally converting the image.
  201. Default: ``ImageReadMode.UNCHANGED``.
  202. See ``ImageReadMode`` class for more information on various
  203. available modes.
  204. Returns:
  205. output (Tensor[image_channels, image_height, image_width])
  206. """
  207. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  208. _log_api_usage_once(read_image)
  209. data = read_file(path)
  210. return decode_image(data, mode)
  211. def _read_png_16(path: str, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:
  212. data = read_file(path)
  213. return torch.ops.image.decode_png(data, mode.value, True)