|
7 | 7 | Try on `collab <https://colab.research.google.com/github/pytorch/vision/blob/gh-pages/main/_generated_ipynb_notebooks/plot_custom_tv_tensors.ipynb>`_
|
8 | 8 | or :ref:`go to the end <sphx_glr_download_auto_examples_transforms_plot_custom_tv_tensors.py>` to download the full example code.
|
9 | 9 |
|
10 |
| -In thie example, we explain the basic usgae of :func:`~torchvision.transforms.functional.to_tensor`, :func:`~torchvision.transforms.functional.pil_to_tensor` and :func:`~torchvision.transforms.functional.to_pil_image`. |
| 10 | +In thie example, we explain the basic usgae of :func:`~torchvision.transforms.functional.to_tensor`, :func:`~torchvision.transforms.functional.pil_to_tensor` and :func:`~torchvision.transforms.functional.to_pil_image`. |
11 | 11 | And the difference between :func:`~torchvision.transforms.functional.to_tensor` and :func:`~torchvision.transforms.functional.pil_to_tensor`.
|
12 | 12 | """
|
13 | 13 |
|
|
17 | 17 | # In this cell, we illustrate the different representation of a PIL image and tensor
|
18 | 18 | import PIL.Image as Image
|
19 | 19 | from torchvision.transforms.functional import to_tensor
|
20 |
| -from helpers import plot # use your favorite visualization library |
| 20 | +from helpers import plot # use your favorite visualization library |
21 | 21 |
|
22 |
| -img_pil = Image.open('../assets/person1.jpg') |
| 22 | +img_pil = Image.open("../assets/person1.jpg") |
23 | 23 | width, height = img_pil.size
|
24 | 24 | # There is no straight forward way to get channel information
|
25 | 25 | # Please read https://pillow.readthedocs.io/en/stable/handbook/concepts.html for more detail
|
26 |
| -num_channels = 3 # hardcoded since it's a color image. |
| 26 | +num_channels = 3 # hardcoded since it's a color image. |
27 | 27 | print("PIL image: width x height x num_channels:", width, height, num_channels)
|
28 | 28 |
|
29 | 29 | img_tensor = to_tensor(img_pil)
|
|
37 | 37 | # In this cell, we explain the difference between :func:`~torchvision.transforms.functional.pil_to_tensor` vs. :func:`~torchvision.transforms.functional.to_tensor`
|
38 | 38 | from torchvision.transforms.functional import pil_to_tensor
|
39 | 39 |
|
40 |
| -img_pil = Image.open('../assets/person1.jpg') |
| 40 | +img_pil = Image.open("../assets/person1.jpg") |
41 | 41 | img_to_tensor = to_tensor(img_pil)
|
42 | 42 | num_channels, height, width = img_to_tensor.shape
|
43 | 43 | print("Tensor image(to_tensor): num_channels x height x width:", num_channels, height, width)
|
|
52 | 52 | # The shape is the same but **data type** is different! The **tensor value** is also different!
|
53 | 53 |
|
54 | 54 | # %%
|
55 |
| -print(img_to_tensor) # tensor that is returned by calling to_tensor() |
56 |
| -print(img_pil_to_tensor) # tensor that is returned by calling pil_to_tensor() |
| 55 | +print(img_to_tensor) # tensor that is returned by calling to_tensor() |
| 56 | +print(img_pil_to_tensor) # tensor that is returned by calling pil_to_tensor() |
57 | 57 |
|
58 | 58 | # %%
|
59 | 59 | # Notice :func:`~torchvision.transforms.functional.to_tensor` automatically scale the image, but :func:`~torchvision.transforms.functional.pil_to_tensor` does not. To rescale the image back,
|
60 | 60 |
|
61 | 61 | import torch
|
| 62 | + |
62 | 63 | img_pil_to_tensor_2 = (img_to_tensor * 255).to(torch.uint8)
|
63 |
| -print((img_pil_to_tensor_2 == img_pil_to_tensor).all().item()) # check if two tensors are same |
| 64 | +print((img_pil_to_tensor_2 == img_pil_to_tensor).all().item()) # check if two tensors are same |
64 | 65 |
|
65 | 66 | # %%
|
66 | 67 | # **TLDR** it's recommended to use :func:`~torchvision.transforms.functional.pil_to_tensor` for visualization tasks since most visualization library
|
|
74 | 75 | # In this cell, we explain an example usage of :func:`~torchvision.transforms.functional.to_pil_image`
|
75 | 76 | from torchvision.transforms.functional import to_pil_image
|
76 | 77 |
|
77 |
| -img_pil = Image.open('../assets/person1.jpg') |
| 78 | +img_pil = Image.open("../assets/person1.jpg") |
78 | 79 |
|
79 | 80 | # convert to tensor using to_tensor() and pil_to_tensor()
|
80 | 81 | img_to_tensor = to_tensor(img_pil)
|
|
88 | 89 |
|
89 | 90 | # %%
|
90 | 91 | # Both tensor can be converted back to PIL image.
|
91 |
| - |
0 commit comments