Class MixUp<T>
- Namespace
- AiDotNet.Augmentation.Image
- Assembly
- AiDotNet.dll
Blends two images together by weighted averaging (MixUp augmentation).
public class MixUp<T> : LabelMixingAugmentationBase<T, ImageTensor<T>>, ILabelMixingAugmentation<T, ImageTensor<T>>, IAugmentation<T, ImageTensor<T>>
Type Parameters
TThe numeric type for calculations.
- Inheritance
-
AugmentationBase<T, ImageTensor<T>>MixUp<T>
- Implements
-
IAugmentation<T, ImageTensor<T>>
- Inherited Members
Remarks
MixUp is a powerful regularization technique that creates virtual training examples by taking linear combinations of two training samples and their labels. Given two images (x₁, y₁) and (x₂, y₂), MixUp creates a new training sample: x' = λx₁ + (1-λ)x₂ y' = λy₁ + (1-λ)y₂ where λ is sampled from a Beta distribution.
For Beginners: Imagine blending two photos together like a double exposure. If you blend a photo of a cat with a photo of a dog, you get something that's partly cat and partly dog. The label becomes a mix too: maybe 70% cat and 30% dog. This teaches the model that the world isn't black-and-white, and improves generalization.
When to use:
- Image classification with sufficient training data
- When you want smoother decision boundaries
- As a regularization alternative or complement to dropout
When NOT to use:
- Object detection (bounding boxes can't be meaningfully mixed)
- Semantic segmentation (mixed masks don't make sense)
- When you need hard labels for downstream processing
Constructors
MixUp(double, double)
Creates a new MixUp augmentation.
public MixUp(double alpha = 1, double probability = 1)
Parameters
alphadoubleThe alpha parameter for the Beta distribution that samples the mixing ratio. Industry standard default is 1.0 (uniform distribution over [0, 1]). Lower values (like 0.2) concentrate samples near 0 and 1 (less mixing). Higher values (above 1.0) concentrate samples near 0.5 (stronger mixing).
probabilitydoubleThe probability of applying this augmentation (0.0 to 1.0). Industry standard default is 1.0 (always applied during training).
Remarks
For Beginners: The alpha parameter controls how much mixing happens. With alpha=1.0, you might get 50% of one image and 50% of another. With alpha=0.2, you'll mostly get one image with just a tiny bit of the other mixed in. Most papers use alpha=1.0 or alpha=0.4 for good results.
Methods
ApplyAugmentation(ImageTensor<T>, AugmentationContext<T>)
Applies MixUp augmentation (single image version - requires external pairing).
protected override ImageTensor<T> ApplyAugmentation(ImageTensor<T> data, AugmentationContext<T> context)
Parameters
dataImageTensor<T>contextAugmentationContext<T>
Returns
- ImageTensor<T>
Remarks
MixUp requires two images to blend together. This method stores the current image for potential pairing. In practice, MixUp is typically applied at the batch level where all pairs can be formed at once using ApplyMixUp.
ApplyMixUp(ImageTensor<T>, ImageTensor<T>, Vector<T>?, Vector<T>?, AugmentationContext<T>)
Applies MixUp to blend two images together.
public ImageTensor<T> ApplyMixUp(ImageTensor<T> image1, ImageTensor<T> image2, Vector<T>? labels1, Vector<T>? labels2, AugmentationContext<T> context)
Parameters
image1ImageTensor<T>The first image.
image2ImageTensor<T>The second image to mix with the first.
labels1Vector<T>The labels for the first image.
labels2Vector<T>The labels for the second image.
contextAugmentationContext<T>The augmentation context.
Returns
- ImageTensor<T>
The mixed image.
Remarks
This method blends the two images pixel-by-pixel using the formula: mixed_pixel = λ * pixel1 + (1 - λ) * pixel2 The labels should be mixed the same way in your training loop.
GetParameters()
Gets the parameters of this augmentation.
public override IDictionary<string, object> GetParameters()
Returns
- IDictionary<string, object>
A dictionary of parameter names to values.