Data Loading Best Practices
Rule of thumb for num_workers:
- Start with
num_workers = min(4, num_cpus)
- Profile and tune (diminishing returns after ~8)
- Too many workers → memory overhead
Optimization checklist:
DataLoader(
dataset,
batch_size=32,
num_workers=4,
pin_memory=True,
persistent_workers=True,
prefetch_factor=2,
drop_last=True
)
Advanced: GPU preprocessing:
import kornia.augmentation as K
augment = K.AugmentationSequential(
K.RandomHorizontalFlip(p=0.5),
K.RandomRotation(degrees=15),
).cuda()