Folder of images to a tensor

Hi, I made algorithm that loads images from a folder as numpy arrays or PIL images. Now I am not sure how to fit these images to a tensor of a shape

torch.Size([64, 1, 28, 28])

by the way they are MNIST images, I want to make my own loader

Right now i have my 64 images as numpy arrays forms. In array.

How to fit them into torch.Size([64, 1, 28, 28]) ?

EDIT: For clear info, i created normal python array and every image as numpy array form iam apending to this array.
Now i want to make from this torch.Size([64, 1, 28, 28])

EDIT2: My images have shape - (28x28)

You can use,

torchvision.transforms.ToTensor()

1 Like

You can also convert each image before it goes to the array to a tensor via imgs.append(torch.from_numpy(img)), then use torch.stack(imgs) to turn the array into a tensor.

1 Like