What if a few photos could teach a computer to recognize millions of different scenes?
Why Data augmentation in PyTorch? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you want to teach a computer to recognize cats in photos. You only have a few pictures, so you try to draw new ones by hand or copy and paste parts. This takes forever and the new images don't look natural.
Manually creating more images is slow, tiring, and often leads to mistakes. It's hard to cover all the ways a cat might appear, like different angles or lighting. This means the computer learns poorly and makes many errors.
Data augmentation automatically creates many new, varied images by slightly changing the originals. It flips, rotates, or changes colors so the computer sees many versions of cats. This helps the model learn better without extra photos.
new_image = draw_new_cat_image_by_hand(original_image)
augmented_image = transforms.RandomHorizontalFlip()(original_image)
Data augmentation lets models learn from limited data by showing many realistic variations, improving accuracy and robustness.
In medical imaging, doctors have few X-rays of rare diseases. Data augmentation creates varied images so AI can better detect those diseases, helping save lives.
Manual data creation is slow and limited.
Data augmentation automatically generates diverse training data.
This improves model learning and prediction quality.
Practice
Solution
Step 1: Understand data augmentation concept
Data augmentation means making new training examples by changing existing ones, like flipping or rotating images.Step 2: Identify the purpose in training
This helps the model see more variety and avoid memorizing only the original data, improving learning.Final Answer:
To create new training data by modifying existing data -> Option BQuick Check:
Data augmentation = create new data [OK]
- Thinking it reduces dataset size
- Confusing augmentation with speeding training
- Believing it changes file formats
Solution
Step 1: Recall torchvision transform syntax
The correct transform for horizontal flip is RandomHorizontalFlip with a probability parameter p.Step 2: Match correct syntax
transforms.RandomHorizontalFlip(p=0.5) uses transforms.RandomHorizontalFlip(p=0.5), which is the exact PyTorch syntax.Final Answer:
transforms.RandomHorizontalFlip(p=0.5) -> Option AQuick Check:
Correct transform name and parameter = C [OK]
- Using wrong transform names
- Using 'prob' instead of 'p'
- Incorrect parameter names or missing parentheses
transform = transforms.Compose([
transforms.RandomRotation(30),
transforms.ToTensor()
])
image = Image.open('sample.jpg')
tensor_image = transform(image)
print(tensor_image.shape)Solution
Step 1: Understand transforms.Compose and RandomRotation
RandomRotation rotates the image but keeps the original size (height and width). ToTensor converts the image to a tensor with shape [channels, height, width].Step 2: Determine output tensor shape
Since the image is color (3 channels), the tensor shape will be [3, H, W], where H and W are original height and width.Final Answer:
[3, H, W] where H and W are original image height and width -> Option AQuick Check:
Rotation keeps size, ToTensor outputs [3, H, W] [OK]
- Confusing channel order as last dimension
- Assuming rotation changes image size
- Thinking output is grayscale shape
transform = transforms.Compose([
transforms.RandomHorizontalFlip(prob=0.5),
transforms.RandomRotation(degrees=45),
transforms.ToTensor()
])Solution
Step 1: Check RandomHorizontalFlip usage
RandomHorizontalFlip requires the probability argument as p=0.5, not prob=0.5.Step 2: Verify other transforms
RandomRotation accepts a single number for degrees, ToTensor can come last, and Compose supports multiple transforms.Final Answer:
RandomHorizontalFlip should use keyword argument p=0.5 -> Option CQuick Check:
Correct argument name = p [OK]
- Passing positional argument instead of keyword
- Thinking degrees must be tuple
- Misordering transforms in Compose
Options: A) RandomHorizontalFlip(p=0.5) + RandomRotation(15) + ColorJitter(brightness=0.2) B) RandomResizedCrop(size=224) + Grayscale(num_output_channels=1) C) RandomVerticalFlip(p=1.0) + RandomRotation(90) + ToTensor() D) Resize(128) + RandomCrop(64) + RandomHorizontalFlip(p=0.5)
Solution
Step 1: Analyze each option's effect on size and channels
RandomHorizontalFlip, small RandomRotation, and ColorJitter to vary brightness flips, rotates slightly, and changes brightness without resizing or changing channels. RandomResizedCrop and converting to grayscale (changes size and channels) changes size and converts to grayscale. Vertical flip and 90-degree rotation (may change orientation drastically) rotates 90 degrees and flips vertically, which changes orientation drastically. Resize and crop to smaller size (changes image size) resizes and crops, changing size.Step 2: Choose the option that keeps size and channels but increases variety
RandomHorizontalFlip, small RandomRotation, and ColorJitter to vary brightness best fits the requirement by augmenting with flips, small rotations, and brightness changes without altering size or channels.Final Answer:
RandomHorizontalFlip, small RandomRotation, and ColorJitter to vary brightness -> Option DQuick Check:
Keep size and channels, add mild augmentations = A [OK]
- Choosing transforms that resize images
- Converting images to grayscale unintentionally
- Using large rotations that distort orientation
