DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

ToImage in PyTorch

Buy Me a Coffee

*Memos:

ToImage() can convert a PIL(Pillow library) Image([H, W, C]), tensor or ndarray to an Image ([..., C, H, W]) and doesn't scale its values to [0.0, 1.0] as shown below:
*Memos:

  • The 1st argument is img(Required-Type:PIL Image or tensor/ndarray(int/float/complex/bool)): *Memos:
    • A tensor must be 2D or more D.
    • A ndarray must be 0D to 3D.
    • Don't use img=.
  • v2 is recommended to use according to V1 or V2? Which one should I use?.
from torchvision.datasets import OxfordIIITPet
from torchvision.transforms.v2 import ToImage
import torch
import numpy as np

ToImage()
# ToImage()

PILImage_data = OxfordIIITPet(
    root="data",
    transform=None
)

Image_data = OxfordIIITPet(
    root="data",
    transform=ToImage()
)

Image_data
# Dataset OxfordIIITPet
#     Number of datapoints: 3680
#     Root location: data
#     StandardTransform
# Transform: ToImage()

Image_data[0]
# (Image([[[37, 35, 36, ..., 247, 249, 249],
#          [35, 35, 37, ..., 246, 248, 249],
#          ...,
#          [28, 28, 27, ..., 59, 65, 76]],
#         [[20, 18, 19, ..., 248, 248, 248],
#          [18, 18, 20, ..., 247, 247, 248],
#          ...,
#          [27, 27, 27, ..., 94, 106, 117]], 
#         [[12, 10, 11, ..., 253, 253, 253],
#          [10, 10, 12, ..., 251, 252, 253],
#          ...,
#          [35, 35, 35, ..., 214, 232, 223]]], dtype=torch.uint8,), 0)

Image_data[0][0].size()
# torch.Size([3, 500, 394])

Image_data[0][0]
# Image([[[37, 35, 36, ..., 247, 249, 249],
#         [35, 35, 37, ..., 246, 248, 249],
#         ...,
#         [28, 28, 27, ...,  59, 65, 76]],
#        [[20, 18, 19, ..., 248, 248, 248],
#         [18, 18, 20, ..., 247, 247, 248],
#         ...,
#         [27, 27, 27, ...,  94, 106, 117]],
#        [[12, 10, 11, ..., 253, 253, 253],
#         [10, 10, 12, ..., 251, 252, 253],
#         ...,
#         [35, 35, 35,  ..., 214, 232, 223]]], dtype=torch.uint8,)

Image_data[0][1]
# 0

import matplotlib.pyplot as plt

plt.imshow(X=Image_data[0][0])
# TypeError: Invalid shape (3, 500, 394) for image data

ti = ToImage()

ti(PILImage_data) # It's still PIL Image.
# Dataset OxfordIIITPet
#     Number of datapoints: 3680
#     Root location: data

ti(PILImage_data[0])
# (Image([[[37, 35, 36, ..., 247, 249, 249],
#          [35, 35, 37, ..., 246, 248, 249],
#          ...,
#          [28, 28, 27, ..., 59, 65, 76]],
#         [[20, 18, 19, ..., 248, 248, 248],
#          [18, 18, 20, ..., 247, 247, 248],
#         ...,
#          [27, 27, 27, ..., 94, 106, 117]],
#         [[12, 10, 11, ..., 253, 253, 253],
#          [10, 10, 12, ..., 251, 252, 253],
#          ...,
#          [35, 35, 35, ..., 214, 232, 223]]], dtype=torch.uint8,), 0)

ti(PILImage_data[0][0])
# Image([[[37, 35, 36, ..., 247, 249, 249],
#         [35, 35, 37, ..., 246, 248, 249],
#         ...,
#         [28, 28, 27, ...,  59, 65, 76]],
#        [[20, 18, 19, ..., 248, 248, 248],
#         [18, 18, 20, ..., 247, 247, 248],
#         ...,
#         [27, 27, 27, ...,  94, 106, 117]],
#        [[12, 10, 11, ..., 253, 253, 253],
#         [10, 10, 12, ..., 251, 252, 253],
#         ...,
#         [35, 35, 35,  ..., 214, 232, 223]]], dtype=torch.uint8,)

plt.imshow(X=ti(PILImage_data[0][0]))
# TypeError: Invalid shape (3, 500, 394) for image data

ti((torch.tensor([[0, 1, 2, 3]]), 0)) # int64
ti((torch.tensor([[0, 1, 2, 3]], dtype=torch.int64), 0))
ti((torch.tensor([[[0, 1, 2, 3]]]), 0))
# (Image([[[0, 1, 2, 3]]],), 0)

ti(torch.tensor([[0, 1, 2, 3]]))
ti(torch.tensor([[[0, 1, 2, 3]]]))
# Image([[[0, 1, 2, 3]]],)

ti((torch.tensor([[[[0, 1, 2, 3]]]]), 0))
# (Image([[[[0, 1, 2, 3]]]],), 0)

ti(torch.tensor([[[[0, 1, 2, 3]]]]))
# Image([[[[0, 1, 2, 3]]]],)

ti((torch.tensor([[[[[0, 1, 2, 3]]]]]), 0))
# (Image([[[[[0, 1, 2, 3]]]]],), 0)

ti(torch.tensor([[[[[0, 1, 2, 3]]]]]))
# Image([[[[[0, 1, 2, 3]]]]],)

ti((torch.tensor([[0, 1, 2, 3]], dtype=torch.int32), 0))
# (Image([[[0, 1, 2, 3]]], dtype=torch.int32,), 0)

ti((torch.tensor([[0., 1., 2., 3.]]), 0)) # float32
ti((torch.tensor([[0., 1., 2., 3.]], dtype=torch.float32), 0))
# (Image([[[0., 1., 2., 3.]]],), 0)

ti((torch.tensor([[0., 1., 2., 3.]], dtype=torch.float64), 0))
# (Image([[[0., 1., 2., 3.]]], dtype=torch.float64,), 0)

ti((torch.tensor([[0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j]]), 0)) # complex64
ti((torch.tensor([[0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j]],
    dtype=torch.complex64), 0))
# (Image([[[0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j]]],), 0)

ti((torch.tensor([[0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j]],
    dtype=torch.complex32), 0))
# (Image([[[0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j]]], dtype=torch.complex32,), 0)

ti((torch.tensor([[True, False, True, False]]), 0)) # bool
ti((torch.tensor([[True, False, True, False]], dtype=torch.bool), 0))
# (Image([[[True, False, True, False]]],), 0)

ti((np.array(3), 0)) # int32
ti((np.array(3, dtype=np.int32), 0))
# (Image([[[3]]], dtype=torch.int32,), 0)

ti(np.array(3))
# Image([[[3]]], dtype=torch.int32,)

ti((np.array([0, 1, 2, 3]), 0))
ti((np.array([[0, 1, 2, 3]]), 0))
# (Image([[[0, 1, 2, 3]]], dtype=torch.int32,), 0)

ti(np.array([0, 1, 2, 3]))
ti(np.array([[0, 1, 2, 3]]))
# Image([[[0, 1, 2, 3]]], dtype=torch.int32,)

ti((np.array([[[0, 1, 2, 3]]]), 0))
# (Image([[[0]], [[1]], [[2]], [[3]]], dtype=torch.int32,), 0)

ti(np.array([[[0, 1, 2, 3]]]))
# Image([[[0]], [[1]], [[2]], [[3]]], dtype=torch.int32,)

ti((np.array([[0, 1, 2, 3]], dtype=np.int64), 0))
# (Image([[[0, 1, 2, 3]]],), 0)

ti((np.array([[0., 1., 2., 3.]]), 0)) # float64
ti((np.array([[0., 1., 2., 3.]], dtype=np.float64), 0))
# (Image([[[0., 1., 2., 3.]]], dtype=torch.float64,), 0)

ti((np.array([[0., 1., 2., 3.]], dtype=np.float32), 0))
# (Image([[[0., 1., 2., 3.]]],), 0)

ti((np.array([[0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j]]), 0)) # complex128
ti((np.array([[0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j]], dtype=np.complex128), 0))
# (Image([[[0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j]]], dtype=torch.complex128,), 0)

ti((np.array([[0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j]], dtype=np.complex64), 0))
# (Image([[[0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j]]],), 0)

ti((np.array([[True, False, True, False]]), 0)) # bool
ti((np.array([[True, False, True, False]], dtype=bool), 0))
# (Image([[[True, False, True, False]]],), 0)
Enter fullscreen mode Exit fullscreen mode

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

AWS Security LIVE! Stream

Go beyond the firewall

Watch AWS Security LIVE! to uncover how today’s cybersecurity teams secure what matters most.

Learn More

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay