Prerequisites:

In [ ]:
import torch
from PIL import Image
import numpy as np
In [ ]:
torch.zeros(10)
In [ ]:
torch.ones(10)
In [ ]:
torch.ones([4,5])
In [ ]:
v = torch.ones(5)
print( v.dtype, v.shape )
In [ ]:
v = torch.arange(100)
v
In [ ]:
v.shape
In [ ]:
m = v.view((10,10))
m
In [ ]:
m.shape
In [ ]:
I = Image.open('cat.jpg')
I
In [ ]:
np.array(I)
In [ ]:
np.array(I).shape
In [ ]:
from torchvision import transforms
image_to_tensor = transforms.ToTensor()
image_tensor = image_to_tensor(I)
image_tensor
In [ ]:
image_tensor.shape
In [ ]:
tensor_to_image = transforms.ToPILImage()
tensor_to_image(image_tensor)
In [ ]: