kornia/kornia

[Bug]: same_on_batch=True does not enforce batch-consistent randomness for several intensity augmentations (e.g., RandomGaussianNoise)

Closed

#3,624 创建于 2026年3月17日

在 GitHub 查看
 (6 评论) (0 反应) (1 负责人)Python (8,677 star) (892 fork)batch import
help wantedtriage

描述

🐛 Describe the bug

With same_on_batch=True, several kornia.augmentation intensity transforms still produce different outputs across batch elements for identical input images.

This seems to violate the documented expectation that same_on_batch applies the same transformation across the batch.

Affected (reproduced):

  • RandomGaussianNoise (large mismatch)
  • RandomChannelShuffle
  • RandomRain
  • RandomPlasmaBrightness
  • RandomPlasmaContrast
  • RandomPlasmaShadow

Not affected in my checks:

  • RandomGamma, RandomContrast, RandomBrightness, RandomSaturation, RandomHue, RandomSharpness, RandomMotionBlur, RandomGaussianBlur (with explicit args)

RandomJPEG had only tiny fp-level differences (~2e-7).

🔄 Steps to Reproduce

  1. Create a batch where all images are identical.
  2. Apply a transform with same_on_batch=True and p=1.0.
  3. Compare output[0] vs output[i] for i>0.
  4. Observe non-zero max differences for some transforms.

💻 Minimal Code Example

import torch
import kornia.augmentation as K

torch.manual_seed(0)

# identical batch
x = torch.rand(4, 3, 64, 64)
x[:] = x[0:1]

ops = {
    "RandomGaussianNoise": K.RandomGaussianNoise(mean=0.0, std=0.02, p=1.0, same_on_batch=True),
    "RandomChannelShuffle": K.RandomChannelShuffle(p=1.0, same_on_batch=True),
    "RandomRain": K.RandomRain(p=1.0, same_on_batch=True),
    "RandomPlasmaBrightness": K.RandomPlasmaBrightness(p=1.0, same_on_batch=True),
    "RandomPlasmaContrast": K.RandomPlasmaContrast(p=1.0, same_on_batch=True),
    "RandomPlasmaShadow": K.RandomPlasmaShadow(p=1.0, same_on_batch=True),
    "RandomGamma": K.RandomGamma((0.6, 1.2), p=1.0, same_on_batch=True),
}

for name, aug in ops.items():
    y = aug(x)
    max_diff = max((y[0] - y[i]).abs().max().item() for i in range(1, y.shape[0]))
    print(f"{name}: max_diff={max_diff:.9f}")

✅ Expected behavior

For identical batch inputs and same_on_batch=True, outputs should be identical across batch elements (up to tiny floating-point tolerance).

❌ Actual behavior

Some transforms still apply per-sample randomness even when same_on_batch=True.

🔧 Environment

- Kornia version: 0.8.2
- PyTorch version: 2.8.0+cu129
- Python version: 3.12.12
- OS: Linux (WSL2, kernel 5.15.167.4-microsoft-standard-WSL2)
- Installation method: pip
- CUDA/cuDNN version: CUDA 12.9, cuDNN 91002
- GPU model: NVIDIA GeForce RTX 3080 Ti Laptop GPU

You can also run:

wget https://raw.githubusercontent.com/pytorch/pytorch/main/torch/utils/collect_env.py
# For security purposes, please check the contents of collect_env.py before running it.
python collect_env.py

📝 Additional context

  • RandomGaussianNoise appears to generate noise directly in apply_transform via torch.randn_like(input) instead of shared sampled params.
  • RandomChannelShuffle appears to sample per-item channel permutations in generate_parameters.
  • RandomRain and RandomPlasma* also appear to have per-item random map behavior. If this is intended semantics (i.e., same_on_batch only means shared scalar params, not identical stochastic fields), docs might need clarification because current wording suggests full batch-consistent randomness.

🤝 Contribution Intent

  • I plan to submit a PR to fix this bug
  • I'm reporting this bug but not planning to fix it

贡献者指南