PyTorch: Image dimension issue
I am working on an image classifier dataset. There are 31 classes in my dataset and there is a folder for each class. For training, I am loading data in the following manner:

from torchvision import datasets, transforms
import torch

def load_training(root_path, dir, batch_size, kwargs):
    transform = transforms.Compose(
        [transforms.Resize([256, 256]),
         transforms.RandomCrop(224),
         transforms.RandomHorizontalFlip(),
         transforms.ToTensor()])
    data = datasets.ImageFolder(root=root_path + dir, transform=transform)
    train_loader = torch.utils.data.DataLoader(data, batch_size=batch_size, shuffle=False, drop_last=True, **kwargs)
    return train_loader


Now for a batch size 32, each batch dim are : [32,3,224,224]. I know Pytorch uses PIL for loading images. But when I'm trying to load images from a list named images of size 32 using PIL using the following code snippet:

for i in images:
    im = Image.open(root_dir + dir + i)
    im = im.convert('RGB')
    im.load()
    im = im.resize((224, 224))
    im = np.asarray( im, dtype="int32" )
    print(im.shape)
    temp_im_2 = torch.from_numpy(np.array(temp_im)).float()


It says im.shape is (32,224,224,3). What am I doing wrong? What should I do to get the batches to be of the same shape in both cases say [32,3,224,224]?

How about just swapping axes? Like im.transpose(0, 3, 1, 2) if im has four dimensions. However, im.shape should return (224, 224, 3) as you've loaded only one image, so that im.transpose(2, 0, 1) should give you the image with the channels in the first dimension which you can then stack together to form a batch.
