Bird
Raised Fist0
PyTorchml~12 mins

Custom transforms in PyTorch - Model Pipeline Trace

Choose your learning style10 modes available
Model Pipeline - Custom transforms

This pipeline shows how custom data transforms prepare images for a PyTorch model. The transforms clean and change images before training, helping the model learn better.

Data Flow - 4 Stages
1Raw Image Data
1000 images x 3 channels x 256 height x 256 width→Original images loaded from disk→1000 images x 3 channels x 256 height x 256 width
An image of a cat with size 256x256 pixels and 3 color channels
↓
2Custom Transform: Resize and Normalize
1000 images x 3 channels x 256 height x 256 width→Resize images to 128x128 and normalize pixel values to 0-1 range→1000 images x 3 channels x 128 height x 128 width
Resized cat image now 128x128 pixels with pixel values scaled between 0 and 1
↓
3Custom Transform: Random Horizontal Flip
1000 images x 3 channels x 128 height x 128 width→Randomly flip images horizontally with 50% chance→1000 images x 3 channels x 128 height x 128 width
Some cat images flipped left to right, others unchanged
↓
4Tensor Conversion
1000 images x 3 channels x 128 height x 128 width→Convert images from numpy arrays to PyTorch tensors→1000 tensors x 3 channels x 128 height x 128 width
Cat image now a PyTorch tensor ready for model input
Training Trace - Epoch by Epoch
Loss
1.2 |****
0.9 |***
0.7 |**
0.55|*
0.45| 
Epochs -> 1 2 3 4 5
EpochLoss ↓Accuracy ↑Observation
11.20.45Model starts learning with high loss and low accuracy
20.90.60Loss decreases and accuracy improves as model learns
30.70.72Model continues to improve with better predictions
40.550.80Loss lowers further, accuracy nearing good performance
50.450.85Model converges with low loss and high accuracy
Prediction Trace - 5 Layers
Layer 1: Input Image
Layer 2: Normalize Pixels
Layer 3: Random Horizontal Flip
Layer 4: Convert to Tensor
Layer 5: Model Forward Pass
Model Quiz - 3 Questions
Test your understanding
Why do we resize images in the custom transform?
ATo increase image size for better detail
BTo reduce image size and speed up training
CTo change image colors
DTo convert images to grayscale
Key Insight
Custom transforms prepare raw images by resizing, normalizing, and augmenting them. This helps the model learn better by giving consistent and varied data, leading to improved accuracy and faster training.

Practice

(1/5)
1. What is the main purpose of creating a custom transform in PyTorch?
easy
A. To speed up model training by changing the optimizer
B. To save the model after training
C. To define a new neural network layer
D. To apply a specific data change not available in standard transforms

Solution

  1. Step 1: Understand what transforms do

    Transforms change input data before training to help the model learn better.
  2. Step 2: Identify the role of custom transforms

    Custom transforms let you add new ways to change data that standard tools don't have.
  3. Final Answer:

    To apply a specific data change not available in standard transforms -> Option D
  4. Quick Check:

    Custom transform = new data change [OK]
Hint: Custom transforms change data uniquely before training [OK]
Common Mistakes:
  • Confusing transforms with model layers
  • Thinking transforms speed up training directly
  • Mixing transforms with saving models
2. Which of the following is the correct way to define a custom transform class in PyTorch?
easy
A. class MyTransform: def transform(self, x): return x * 2
B. class MyTransform: def __call__(self, x): return x * 2
C. def MyTransform(x): return x * 2
D. def MyTransform: return x * 2

Solution

  1. Step 1: Recall custom transform structure

    Custom transforms are classes with a __call__ method to make them callable.
  2. Step 2: Check each option

    class MyTransform: def __call__(self, x): return x * 2 defines a class with __call__, correct syntax. Others miss class or __call__ or have wrong syntax.
  3. Final Answer:

    class MyTransform: def __call__(self, x): return x * 2 -> Option B
  4. Quick Check:

    Custom transform = class + __call__ method [OK]
Hint: Custom transform must be a class with __call__ method [OK]
Common Mistakes:
  • Defining transform as a function only
  • Using wrong method name like transform instead of __call__
  • Missing class keyword
3. Given this custom transform code, what will be the output of transform(3)?
class AddFive:
    def __call__(self, x):
        return x + 5

transform = AddFive()
medium
A. 8
B. 15
C. 3
D. Error

Solution

  1. Step 1: Understand the transform class behavior

    The __call__ method adds 5 to the input x.
  2. Step 2: Calculate transform(3)

    Input 3 plus 5 equals 8.
  3. Final Answer:

    8 -> Option A
  4. Quick Check:

    3 + 5 = 8 [OK]
Hint: Call __call__ method adds 5 to input [OK]
Common Mistakes:
  • Confusing addition with multiplication
  • Expecting the original input unchanged
  • Thinking transform is not callable
4. Identify the error in this custom transform code:
class Multiply:
    def call(self, x):
        return x * 3

transform = Multiply()
print(transform(4))
medium
A. Method should be named __call__, not call
B. Class should inherit from nn.Module
C. Missing return statement in method
D. transform(4) is invalid syntax

Solution

  1. Step 1: Check method name for callable class

    To make an instance callable, method must be __call__, not call.
  2. Step 2: Verify other parts

    Class inheritance is optional for transforms; return is present; calling instance is valid.
  3. Final Answer:

    Method should be named __call__, not call -> Option A
  4. Quick Check:

    Callable class needs __call__ method [OK]
Hint: Callable classes need __call__, not call method [OK]
Common Mistakes:
  • Using call instead of __call__
  • Thinking inheritance from nn.Module is required
  • Missing return statement
5. You want to create a custom transform that normalizes a tensor by subtracting its mean and dividing by its standard deviation. Which code correctly implements this?
hard
A. class Normalize: def __call__(self, x): return x + x.mean() + x.std()
B. class Normalize: def __call__(self, x): return x / x.mean() - x.std()
C. class Normalize: def __call__(self, x): return (x - x.mean()) / x.std()
D. class Normalize: def __call__(self, x): return x * x.mean() / x.std()

Solution

  1. Step 1: Recall normalization formula

    Normalization means subtract mean and divide by standard deviation.
  2. Step 2: Check each option's formula

    class Normalize: def __call__(self, x): return (x - x.mean()) / x.std() matches formula: (x - mean) / std. Others do incorrect math.
  3. Final Answer:

    class Normalize: def __call__(self, x): return (x - x.mean()) / x.std() -> Option C
  4. Quick Check:

    Normalize = (x - mean) / std [OK]
Hint: Normalize = subtract mean, then divide by std [OK]
Common Mistakes:
  • Dividing before subtracting mean
  • Adding mean and std instead of subtract/divide
  • Multiplying by mean/std incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard