Skip to content
TheChatArchiveThe Chat Archive
Geminigemini-2.5-flash· GoogleHugging Face LMArena Human Preference 140K

What are good values of sigma and alpha to use in Kornia RandomElasticTransform?

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-pro’s answer.
  1. User

    What are good values of sigma and alpha to use in Kornia RandomElasticTransform?

  2. Gemini Assistant

    The "good" values for sigma and alpha in Kornia's RandomElasticTransform are highly dependent on your specific use case, the type of images you're working with, and the desired level of distortion. However, we can break down what each parameter controls and provide common ranges for different scenarios.

    Understanding alpha and sigma

    The elastic transform is based on applying random displacement fields, which are generated by convolving random noise with a Gaussian kernel.

    1. alpha (Magnitude of Displacement):

      • What it controls: The intensity or magnitude of the elastic deformation. It's essentially how "strong" the distortion is.
      • Effect:
        • Small alpha: Subtle, barely noticeable distortions. The image will look almost unchanged.
        • Large alpha: Strong, highly visible distortions, potentially making objects in the image look very warped or unrecognizable if too high.
      • Units: It's typically in pixels, representing the maximum displacement of a pixel.
    2. sigma (Smoothness of Displacement):

      • What it controls: The standard deviation of the Gaussian kernel used to smooth the random displacement field. This determines the "smoothness" or "locality" of the deformation.
      • Effect:
        • Small sigma: Leads to more local, jagged, or "bumpy" deformations. The displacement can change rapidly over short distances.
        • Large sigma: Leads to smoother, more global-like deformations. The displacement changes gradually, creating "wavy" or "stretched" effects across larger regions.
      • Units: It's also in pixels, representing the spread of the Gaussian filter.

    Kornia's RandomElasticTransform Defaults:

    By default, Kornia's RandomElasticTransform uses:

    • alpha=(32.0, 35.0)
    • sigma=(4.0, 5.0)

    These defaults are often suitable for tasks like digit recognition (e.g., MNIST), where significant deformation can still preserve the digit's identity.

    Recommended Values for Different Scenarios:

    You'll typically provide alpha and sigma as a tuple (min_val, max_val) to allow for random variation during training, which is beneficial for robust augmentation.

    1. General Data Augmentation (e.g., natural images, objects):

      • Goal: Make your model robust to slight non-rigid variations without fundamentally altering the object's identity.
      • alpha: (15.0, 30.0)
        • 15-20: Moderate, usually safe.
        • 20-30: More aggressive, but still often works for many datasets.
      • sigma: (3.0, 7.0)
        • 3-5: Good for preserving some local detail while smoothing.
        • 5-7: For very smooth, "wavy" deformations over larger areas.
      • Example: RandomElasticTransform(alpha=(20.0, 25.0), sigma=(4.0, 5.0), p=0.5)
    2. Subtle / Medical Image Augmentation:

      • Goal: Simulate very slight tissue deformations, where major distortions are unrealistic or harmful to the data's integrity.
      • alpha: (1.0, 10.0)
        • 1-5: Very subtle, almost imperceptible.
        • 5-10: Slightly more visible but still mild.
      • sigma: (1.0, 4.0)
        • 1-2: Allows for some local variation but still smooth.
        • 2-4: Smoother effects for larger structures.
      • Example: RandomElasticTransform(alpha=(5.0, 8.0), sigma=(2.0, 3.0), p=0.7)
    3. Aggressive / Artistic / Handwritten Digit Augmentation (like MNIST):

      • Goal: Create highly varied, distorted images, often where the object's identity can withstand significant changes.
      • alpha: (30.0, 50.0) or even higher (50.0, 70.0)
        • Kornia's defaults (32, 35) fit well here.
      • sigma: (4.0, 8.0) or even higher (8.0, 10.0) for extremely smooth "liquid" effects.
      • Example: RandomElasticTransform(alpha=(35.0, 45.0), sigma=(6.0, 8.0), p=0.9)

    Key Considerations and Best Practices:

    1. Visualize, Visualize, Visualize! This is the most crucial step. Apply the transform to a batch of your images and inspect the results. Do the transformed images still look like valid examples of your data? Do they introduce artifacts? This is the only way to truly determine "good" values for your specific task.

    2. Start with Kornia Defaults: See how they look on your data. They are often a reasonable starting point.

    3. Iterate and Tune: Adjust alpha and sigma incrementally based on your visualizations. If it's too subtle, increase alpha. If it's too noisy, increase sigma. If it's too smooth, decrease sigma.

    4. Use Ranges: Always prefer providing tuples (min_val, max_val) for alpha and sigma when using RandomElasticTransform to add variability to your augmentation process.

    5. Probability (p): Don't forget the p parameter, which determines the probability of applying the transform. For elastic transforms, a p value of 0.5 to 0.8 is common, meaning it won't be applied to every image, introducing more diversity.

    6. Input Image Format: Remember that Kornia typically expects floating-point tensors, usually normalized to [0, 1] or [-1, 1].

    Here's a small example of how to use it and visualize:

    import torch
    import kornia.augmentation as K
    import matplotlib.pyplot as plt
    import torchvision.transforms as T
    from PIL import Image
    
    # 1. Load an example image
    # You can replace this with your own image loading
    try:
        from kornia.utils.io import load_image
        img_path = "https://kornia.github.io/tutorials/assets/jp_goku.png"
        img_orig = load_image(img_path, True).float() / 255.
    except Exception:
        print("Could not load image from URL, using a dummy image.")
        # Create a dummy image if URL loading fails
        img_orig = torch.rand(1, 3, 256, 256)
    
    # Ensure batch dimension
    if img_orig.ndim == 3:
        img_orig = img_orig.unsqueeze(0)
    
    # 2. Define the augmentation pipeline
    # Scenario 1: Moderate augmentation
    aug_moderate = K.RandomElasticTransform(
        alpha=(20.0, 25.0),
        sigma=(4.0, 5.0),
        p=1.0, # Apply to every image for demonstration
        padding_mode='reflection', # or 'zeros', 'border'
        #interpolation='bilinear' # default
    )
    
    # Scenario 2: Subtle augmentation (like medical imaging)
    aug_subtle = K.RandomElasticTransform(
        alpha=(5.0, 8.0),
        sigma=(2.0, 3.0),
        p=1.0,
        padding_mode='reflection',
    )
    
    # Scenario 3: Aggressive augmentation (like MNIST)
    aug_aggressive = K.RandomElasticTransform(
        alpha=(35.0, 45.0),
        sigma=(6.0, 8.0),
        p=1.0,
        padding_mode='reflection',
    )
    
    # 3. Apply and visualize
    fig, axes = plt.subplots(1, 4, figsize=(15, 5))
    
    # Original Image
    axes[0].imshow(img_orig.squeeze(0).permute(1, 2, 0).cpu().numpy())
    axes[0].set_title("Original")
    axes[0].axis('off')
    
    # Moderate
    img_moderate = aug_moderate(img_orig)
    axes[1].imshow(img_moderate.squeeze(0).permute(1, 2, 0).cpu().numpy())
    axes[1].set_title(f"Moderate\nalpha={aug_moderate.alpha}\nsigma={aug_moderate.sigma}")
    axes[1].axis('off')
    
    # Subtle
    img_subtle = aug_subtle(img_orig)
    axes[2].imshow(img_subtle.squeeze(0).permute(1, 2, 0).cpu().numpy())
    axes[2].set_title(f"Subtle\nalpha={aug_subtle.alpha}\nsigma={aug_subtle.sigma}")
    axes[2].axis('off')
    
    # Aggressive
    img_aggressive = aug_aggressive(img_orig)
    axes[3].imshow(img_aggressive.squeeze(0).permute(1, 2, 0).cpu().numpy())
    axes[3].set_title(f"Aggressive\nalpha={aug_aggressive.alpha}\nsigma={aug_aggressive.sigma}")
    axes[3].axis('off')
    
    plt.tight_layout()
    plt.show()
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.