Skip to main content

A unified PyTorch library for SOTA MixUp augmentations.

Project description

mixup

License: MIT Python 3.8+ PyTorch

A unified PyTorch library for SOTA MixUp augmentations. It keeps popular image and label mixing methods in one place, making it convenient for rapid experiments and training robust classification models.


Features

  • VanillaMixUp — classic linear mixing of two images and their labels.
  • CutMix — replaces a rectangular region of an image with a patch from another image.
  • ResizeMix — overlays a rescaled patch from another image.
  • FMix — blends images using a low-frequency noise mask generated via FFT.
  • SaliencyMix — pastes a patch centered on the most salient region of a partner image.
  • PuzzleMix — swaps spatial blocks where the partner image is more salient.
  • MixupPipeline — randomly selects one method from a provided list for multi-mode training.
  • Saliency utilities — heuristic and gradient-based saliency maps for custom workflows.
  • Fully compatible with PyTorch Automatic Mixed Precision (AMP) and soft-label loss functions.
  • Easily extensible architecture via the BaseMixup abstract class.

Installation

Install directly from PyPI:

pip install mixup

Or install in development mode from source:

git clone [https://github.com/your_username/mixup.git](https://github.com/your_username/mixup.git)
cd mixup
pip install -e .

Input Requirements

All augmentation methods strictly accept One-Hot encoded or soft-label target tensors:

  • Images (x): Tensor of shape [B, C, H, W]
  • Labels (y): One-Hot / Soft-label Tensor of shape [B, Num_Classes] (type float32 / float16)

Quick Start

import torch
import torch.nn.functional as F
from mixup import VanillaMixUp, CutMix, SaliencyMix, PuzzleMix, MixupPipeline

# Dummy batch of images [B, C, H, W] and integer labels [B]
images = torch.randn(8, 3, 32, 32)
labels = torch.randint(0, 100, (8,))

# Convert integer labels to one-hot encoding [B, Num_Classes]
labels_one_hot = F.one_hot(labels, num_classes=100).float()

# 1. Standard VanillaMixUp
mixup = VanillaMixUp(alpha=1.0, probability=0.5)
mixed_images, mixed_labels = mixup(images, labels_one_hot)

# 2. SaliencyMix with a fast heuristic saliency backend
saliencymix = SaliencyMix(alpha=1.0, mode='heuristic')
mixed_images, mixed_labels = saliencymix(images, labels_one_hot)

# 3. Multi-mode pipeline
pipeline = MixupPipeline([
    VanillaMixUp(alpha=1.0),
    CutMix(alpha=1.0),
    SaliencyMix(alpha=1.0, mode='heuristic'),
    PuzzleMix(alpha=1.0, mode='heuristic', block_size=4),
])
mixed_images, mixed_labels = pipeline(images, labels_one_hot)

Available Augmentations

VanillaMixUp

Blends two images and their corresponding labels using a mixing coefficient $\lambda \sim \text{Beta}(\alpha, \alpha)$.

from mixup import VanillaMixUp

mixup = VanillaMixUp(alpha=1.0, probability=1.0)

CutMix

Cuts a rectangular region from one image and pastes a patch from another. The mixing weight $\lambda$ is automatically recalculated based on the cropped area.

from mixup import CutMix

cutmix = CutMix(alpha=1.0, probability=0.5)

ResizeMix

Rescales a partner image and pastes the resulting patch into a random position on the original image.

from mixup import ResizeMix

resizemix = ResizeMix(alpha=1.0)

FMix

Generates a smooth binary mask via the inverse FFT of low-frequency noise and blends images according to this mask.

from mixup import FMix

fmix = FMix(alpha=1.0, decay_power=3.0)

SaliencyMix

Pastes a patch from a partner image centered around its most salient point. Supports two backends:

  • mode='heuristic' — fast, context-aware saliency with center prior (no model required).
  • mode='gradient' — input-gradient saliency from the training model.
from mixup import SaliencyMix

# Heuristic mode (model-free)
saliencymix = SaliencyMix(alpha=1.0, mode='heuristic')

# Gradient mode (requires training model)
saliencymix = SaliencyMix(alpha=1.0, mode='gradient', model=model)

PuzzleMix

Swaps spatial blocks where the partner image is more salient than the original image.

from mixup import PuzzleMix

puzzlemix = PuzzleMix(alpha=1.0, mode='heuristic', block_size=4)
puzzlemix = PuzzleMix(alpha=1.0, mode='gradient', model=model, block_size=4)

Low-level Saliency Helpers

from mixup import get_heuristic_saliency_map, get_gradient_saliency_map

sal_maps_heuristic = get_heuristic_saliency_map(images)
sal_maps_gradient = get_gradient_saliency_map(model, images, labels_one_hot)

Parameters

Common parameters across all augmentations inheriting from BaseMixup:

Parameter Type Default Description
alpha float 1.0 Beta distribution hyperparameter $\alpha$. If 0.0, $\lambda=1.0$.
probability float 1.0 Probability ($0.0 \dots 1.0$) of applying the augmentation.

Method-specific arguments:

Class Parameter Type Default Description
FMix decay_power float 3.0 Decay power in the frequency domain for FFT noise.
SaliencyMix mode str 'heuristic' Backend selection: 'heuristic' or 'gradient'.
SaliencyMix model nn.Module None Target model instance (required when mode='gradient').
PuzzleMix mode str 'heuristic' Backend selection: 'heuristic' or 'gradient'.
PuzzleMix model nn.Module None Target model instance (required when mode='gradient').
PuzzleMix block_size int 4 Block size in pixels for spatial tile swapping.

Integration into Training Loop

PyTorch's native nn.CrossEntropyLoss supports One-Hot / soft labels out of the box (PyTorch $\ge$ 1.10).

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from mixup import SaliencyMix, PuzzleMix

model = ...  # Your model
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.1, momentum=0.9)
NUM_CLASSES = 100

# Model-free SaliencyMix
saliencymix = SaliencyMix(alpha=1.0, probability=1.0, mode='heuristic')

# Gradient-based PuzzleMix using the active model
puzzlemix = PuzzleMix(alpha=1.0, mode='gradient', model=model, block_size=4)

model.train()
for images, labels in dataloader:
    images, labels = images.cuda(), labels.cuda()

    # Always convert labels to one-hot floats before applying mixup methods
    labels_one_hot = F.one_hot(labels, num_classes=NUM_CLASSES).float()

    mixed_images, mixed_labels = saliencymix(images, labels_one_hot)

    optimizer.zero_grad()
    outputs = model(mixed_images)
    loss = criterion(outputs, mixed_labels)
    loss.backward()
    optimizer.step()

References


License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mixup-0.2.0.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mixup-0.2.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file mixup-0.2.0.tar.gz.

File metadata

  • Download URL: mixup-0.2.0.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for mixup-0.2.0.tar.gz
Algorithm Hash digest
SHA256 fbc408d611726b5bc8eb069a1639ecafc1d73bb6d652ccfc3c27b797653690fb
MD5 9d32dae5c767cd098de6f4b200d71cac
BLAKE2b-256 3f287a9250156386dd8c756b8b2ec3eff0f2af1f08c522717c30fa89de947118

See more details on using hashes here.

File details

Details for the file mixup-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: mixup-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for mixup-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 60e93f7635cd25f77727116b9fdcb409888922c1a1c988dd2340d3a73cd5aae4
MD5 092ef66a39a3c4433e2d59286c042c86
BLAKE2b-256 02f332992d0949fbe8c898aeeb85697eceb9f695bcbb71417fa138435ae76f37

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page