22.02.2024 Views

Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

• Transformations based on Tensors

Obviously, there are conversion transforms to convert from tensor ToPILImage()

and from PIL image ToTensor().

Let’s start by using ToTensor() to convert a Numpy array (in PIL shape) to a

PyTorch tensor. We can create a "tensorizer" (for lack of a better name) and feed it

our example image (#7) in HWC shape:

tensorizer = ToTensor()

example_tensor = tensorizer(example_hwc)

example_tensor.shape

Output

torch.Size([1, 5, 5])

Cool, we got the expected CHW shape. So, its content should be easy to associate

with the underlying image:

example_tensor

Output

tensor([[[0., 1., 0., 0., 0.],

[0., 0., 1., 0., 0.],

[0., 0., 0., 1., 0.],

[0., 0., 0., 0., 1.],

[0., 0., 0., 0., 0.]]])

And indeed it is: Once again we can "see" the diagonal line. But, this time, its values

are not equal to 255; they are equal to 1.0 instead.

Torchvision | 275

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!