from torchvision import datasets, transforms transform = transforms.ToTensor() dataset = datasets.ImageFolder(root='data/train', transform=transform) image, label = dataset[0] print(image.shape)
The ToTensor() transform converts PIL images to tensors with shape (channels, height, width). For color images, channels = 3 (RGB).
from torchvision import datasets dataset = datasets.ImageFolder(root='data/train') print(dataset.classes)
ImageFolder sorts the folder names alphabetically to create the classes list. So the order is ['cats', 'dogs', 'rabbits'].
from torchvision import datasets dataset = datasets.ImageFolder(root='data/train') image, label = dataset[0]
If a file is not a valid image, PIL will raise UnidentifiedImageError when ImageFolder tries to open it.
The correct order is Resize, then ToTensor, then Normalize. Resize needs a tuple for size. Normalize expects 3 values for RGB channels.
ImageFolder reads images from subfolders and assigns labels based on folder names, simplifying dataset creation.
