Introduction
Image augmentation transforms help create more varied pictures from a few images. This makes machine learning models better at understanding new pictures.
Jump into concepts and practice - no test required
transform = torchvision.transforms.Compose([
torchvision.transforms.RandomHorizontalFlip(p=0.5),
torchvision.transforms.RandomRotation(degrees=30),
torchvision.transforms.ColorJitter(brightness=0.2, contrast=0.2),
torchvision.transforms.ToTensor()
])transform = torchvision.transforms.RandomHorizontalFlip(p=1.0)transform = torchvision.transforms.RandomRotation(degrees=45)transform = torchvision.transforms.ColorJitter(brightness=0.5)transform = torchvision.transforms.Compose([
torchvision.transforms.RandomResizedCrop(224),
torchvision.transforms.ToTensor()
])import torch from torchvision import transforms from PIL import Image import requests from io import BytesIO # Load an example image from the web url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/640px-PNG_transparency_demonstration_1.png' response = requests.get(url) img = Image.open(BytesIO(response.content)) # Define augmentation transforms transform = transforms.Compose([ transforms.RandomHorizontalFlip(p=1.0), transforms.RandomRotation(degrees=30), transforms.ColorJitter(brightness=0.3, contrast=0.3), transforms.ToTensor() ]) # Apply transforms augmented_img = transform(img) # Show shape and type print(f"Augmented image tensor shape: {augmented_img.shape}") print(f"Tensor type: {augmented_img.dtype}")
image augmentation in training machine learning models?transforms.RandomHorizontalFlip(p=probability) to flip images horizontally.transform = transforms.Compose([
transforms.Resize((128, 128)),
transforms.RandomCrop(100),
transforms.ToTensor()
])
image = Image.open('sample.jpg')
output = transform(image)
print(output.shape)ToTensor() converts it to a tensor with shape [channels, height, width] = [3, 100, 100].transform = transforms.Compose([
transforms.Rotate(45),
transforms.ToTensor()
])
image = Image.open('sample.jpg')
output = transform(image)transforms.Rotate class. Rotation is done with transforms.RandomRotation or using functional API.transforms.RandomRotation([45, 45]) or transforms.functional.rotate. The code as is will cause an AttributeError.