= Image.open('images/cat.jpeg')
img img
Image data augmentation
pytorch
data augmentation
This notebook will show the common methods used for preparing image data for vision models in PyTorch.
Load an original image
Convert the image to Tensor
= torchvision.transforms.ToTensor()
toTensor toTensor(img).shape
torch.Size([3, 1199, 1200])
From above, we know that the image size is 1199 * 1200
.
Resize the image
Here we resize the image to size 224 \(\times\) 224.
= torchvision.transforms.Resize(224)
resize = resize(img)
img_rs img_rs
Flip an image
1. Flip horizontally
= torchvision.transforms.RandomHorizontalFlip(p=1.0)
flip flip(img_rs)
2. Flip vertically
= torchvision.transforms.RandomVerticalFlip(p=1.0)
flip flip(img_rs)
Change brightness, contrast, saturation and hue of an image
= torchvision.transforms.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.3)
colorjitter colorjitter(img_rs)
Turn an image grayscale
= torchvision.transforms.Grayscale()
grayscale grayscale(img_rs)
Crop an image
= torchvision.transforms.CenterCrop(128)
crop crop(img_rs)