Where

  C is number of Channels.   

     3  is for RGB 

     1  is Black and white channel


 W  is   width of image 

 H  is   height of image 

 N is number of images in a given Batch.


Read Image


from PIL import Image

img = Image.open(r"jk.jpg"

Image Resize


import torchvision.transforms as T

# define transformt o resize 

#  the image with given size

transform = T.Resize(size = (250,450))

# apply the transform on the input image

img = transform(img)

Convert to Tensor


convert_tensor = transforms.ToTensor()

convert_tensor(img)

Tensor is ready to use with PyTorch

( DataClass and DataLoader)


import torchvision.transforms as transforms

from PIL import Image

# load image in RGB mode (png files contains additional alpha channel)

img = Image.open('golf.png').convert('RGB')

# set up transformation to resize the image

resize = transforms.Resize([224, 224])

img = resize(img)

to_tensor = transforms.ToTensor()

# apply transformation and convert to Pytorch tensor

tensor = to_tensor(img)

# torch.Size([3, 224, 224])

# add another dimension at the front to get NCHW shape

tensor = tensor.unsqueeze(0)

# torch.Size([1, 3, 224, 224])