Skip to content

Refactor autoaugment.py: extract _AutoAugmentBase to eliminate duplicated fill logic #9446

@sunnycho100

Description

@sunnycho100

Description

There is a FIXME comment at line 103 of torchvision/transforms/autoaugment.py:

# FIXME: Eliminate copy-pasted code for fill standardization and _augmentation_space() by moving stuff on a base class

Four classes (AutoAugment, RandAugment, TrivialAugmentWide, AugMix) all independently repeat the same fill-standardization logic in their forward() methods:

fill = self.fill
channels, height, width = F.get_dimensions(img)
if isinstance(img, Tensor):
    if isinstance(fill, (int, float)):
        fill = [float(fill)] * channels
    elif fill is not None:
        fill = [float(f) for f in fill]

They also independently inherit from torch.nn.Module and define interpolation and fill in their __init__.

Proposal

Create an _AutoAugmentBase class (like v2 already did in torchvision/transforms/v2/_auto_augment.py) that:

  1. Inherits from torch.nn.Module
  2. Holds common __init__ params: interpolation and fill
  3. Provides a _get_fill() helper to eliminate the duplicated fill-standardization logic

This is a pure internal refactor — no public API changes, no behavior changes. All existing tests should pass as-is.

Note: _augmentation_space() is intentionally not unified since each class uses different signatures and contents.

cc @pmeier

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions