Overview
Thetransforms module provides functions to create torchvision transform pipelines for training and validation. It supports flexible augmentation presets and normalization strategies optimized for malware image classification.
Functions
create_train_transforms
Creates a transform pipeline for training data with optional augmentation.Dataset configuration dictionary with keys:
preprocessing: Preprocessing settingstarget_size: Tuple (height, width) for resizing (default: (224, 224))normalization: Normalization strategy (“[0,1]”, ”[-1,1]”, “ImageNet Mean/Std”)color_mode: Color mode (“RGB”, “Grayscale”)
augmentation: Augmentation settingspreset: Augmentation preset (“None”, “Light”, “Moderate”, “Heavy”, “Custom”)custom: Custom augmentation parameters (if preset=“Custom”)
- Resize to target size
- Convert to RGB (if needed)
- Apply augmentations
- Convert to tensor
- Apply normalization
Augmentation Presets
None
No augmentation, only basic preprocessing.Light
- Random horizontal flip (p=0.5)
- Random 90° rotation (0°, 90°, 180°, 270°)
Moderate
- Random horizontal flip (p=0.5)
- Random vertical flip (p=0.5)
- Random 90° rotation (0°, 90°, 180°, 270°)
- Color jitter (brightness±10%, contrast±10%)
Heavy
- Random horizontal flip (p=0.5)
- Random vertical flip (p=0.5)
- Random 90° rotation (0°, 90°, 180°, 270°)
- Color jitter (brightness±20%, contrast±20%)
- Gaussian blur (kernel=3, sigma=0.1-0.5)
Custom
Configure individual augmentations:horizontal_flip: Enable horizontal flipsvertical_flip: Enable vertical flipsrotation: Enable rotationsrotation_angles: List of rotation angles (default: [90, 180, 270])brightness_range: Brightness variation percentage (0-100)contrast_range: Contrast variation percentage (0-100)gaussian_noise: Enable Gaussian blur
Normalization Strategies
| Strategy | Mean | Std | Use Case |
|---|---|---|---|
| [0,1] | - | - | Default, no normalization after ToTensor() |
| [-1,1] | [0.5, 0.5, 0.5] | [0.5, 0.5, 0.5] | Neural networks with tanh activation |
| ImageNet Mean/Std | [0.485, 0.456, 0.406] | [0.229, 0.224, 0.225] | Transfer learning from ImageNet models |
Example
create_val_transforms
Creates a transform pipeline for validation/test data without augmentation.Dataset configuration dictionary with keys:
preprocessing: Preprocessing settingstarget_size: Tuple (height, width) for resizing (default: (224, 224))normalization: Normalization strategy (“[0,1]”, ”[-1,1]”, “ImageNet Mean/Std”)color_mode: Color mode (“RGB”, “Grayscale”)
- Resize to target size
- Convert to RGB (if needed)
- Convert to tensor
- Apply normalization
Example
Usage with Dataset
Visualizing Augmentations
Best Practices
Choosing Augmentation Strength
Choosing Normalization
Consistent Preprocessing
Related
- Dataset - Dataset and DataLoader creation
- Training Engine - Core training loop