AI Grounds

Open AI Grounds on a desktop

These interactive lessons need a larger screen. Please continue on a desktop or laptop computer.

Guided discovery

Label-Mixing Image Transforms

Mix the pixels. Measure the patch. Weight the target.

Your mixing rule

MixUp uses this as A’s coefficient in every RGB channel and in the target. It is not an area or a model probability. Use arrow keys on the slider. Press Enter or leave the number field to apply an exact edit.

Your mixed example

Each full photo is resized to RGB 224×224, including its aspect ratio. The diagram displays rounded RGB bytes. Each corresponding channel is mixed as w·A+(1−w)·B. This arithmetic is on encoded RGB values, not a physical-light simulation.

Source A: cat

Loading prepared photo…

Source B: stop sign

Loading prepared photo…

MixUp result

Loading prepared photo…

w_A = 0.200000 · w_B = 0.800000 · every channel: w_A·A + w_B·B

Target class weights

y_A = [1.000000, 0.000000, 0.000000, 0.000000]
y_B = [0.000000, 0.000000, 1.000000, 0.000000]
y_mix = w_A·y_A + (1−w_A)·y_B

Each bar has a common 0–1 scale. Zero weight has zero width. This is a training target, not a prediction about objects in the mixed pixels.

catclass 00.200000
sneakerclass 10.000000
stop signclass 20.800000
leafclass 30.000000

Target vector [0.200000, 0.000000, 0.800000, 0.000000]

Two distinct classes: target is soft, with two positive entries. Target sums to 1. Label construction does not guarantee semantic suitability or better training.

Effective A weight

Coefficient of A in each RGB channel.

0.200000

Requested coefficient 0.20

Use this same weight to construct the target.

Fixed-model cross-entropy

Loss for the mixed target against fixed illustrative probabilities.

1.389715

nats · natural logarithm

No model inference or training occurs here.

What the loss sees

A fixed example prediction is p = [0.60,0.10,0.20,0.10] for cat, sneaker, stop sign, leaf. These probabilities stay unchanged as controls move. Cross-entropy is −Σ y_c ln(p_c); a nat is the unit when using natural logarithms.

A (cat): 0.200000 × −ln(0.60) = 0.102165

B (stop sign): 0.800000 × −ln(0.20) = 1.287550

L = term A + term B = 1.389715

Values are rounded to six decimals; totals use unrounded terms. Both class losses use this same fixed prediction for the mixed example. They are not predictions measured separately on the two unmixed photos. If A and B have the same class, the weights add and the loss reduces to that class’s negative log probability.

Controlled Python pair and random torchvision workflow

This manual two-image batch uses the exact displayed coefficient/box. Replace A.jpg and B.jpg with your source files. Python/PIL preparation and float32 arithmetic can differ from browser display bytes. Inspect item 0; the second batch item pairs in the other direction.

import torch
from torch.nn import functional as F
from PIL import Image
from torchvision import transforms as T

# Explicit two-image controlled example; not a random v2 transform call.
prepare = T.Compose([T.Resize((224, 224)), T.ToTensor()])
images = torch.stack([prepare(Image.open("A.jpg").convert("RGB")),
                      prepare(Image.open("B.jpg").convert("RGB"))])
class_ids = torch.tensor([0, 2])
labels = F.one_hot(class_ids, num_classes=4).float()
weight = 0.2
mixed_images = weight * images + (1 - weight) * images.roll(1, 0)
mixed_targets = weight * labels + (1 - weight) * labels.roll(1, 0)
# Fixed illustrative predictions, not trained or inferred from these photos.
p = torch.tensor([[0.6, 0.1, 0.2, 0.1]]).expand(2, -1)
loss = F.cross_entropy(p.log(), mixed_targets, reduction="none")
# Inspect mixed_images[0], mixed_targets[0], loss[0].

Random training workflow

from torchvision.transforms import v2
# images: float batch [N,3,H,W]; class_ids: integers [N]
transform = v2.MixUp(num_classes=4, alpha=1.0)
mixed_images, mixed_targets = transform(images, class_ids)

The actual v2 transform operates on a batch and samples a coefficient from Beta(alpha,alpha); alpha=1 gives a uniform coefficient. Alpha is not the displayed fixed weight. CutMix samples a center and corrects targets using actual area. This random workflow does not reproduce this browser sample. Pairing is implementation-specific; shuffle the training batch. This lesson demonstrates construction rules, not training accuracy.

mixup, A cat, B stop sign, requested 0.200000, effective A 0.200000, target [0.200000, 0.000000, 0.800000, 0.000000], fixed loss 1.389715 nats. Whole-image blend.