[Python] Why is Albumentations Adding Excessive Noise to Augmented Images?

Discussão em 'Python' iniciado por Stack, Fevereiro 14, 2025.

  1. Stack

    Stack Membro Participativo

    I am working on a pill detection project using YOLOv8 and applying Albumentations for data augmentation. However, some augmented images turn out with too much noise or distortion (example attached).

    What I Want to Achieve: Apply realistic augmentations (rotation, brightness, noise, etc.) Ensure the pills remain clear and recognizable

    Problem: Some images become too noisy or unrecognizable. The issue seems related to GaussNoise or color space handling.

    Some images generated: Image 1 & Image 2 I think they are overly zoomed.


    import cv2
    import os
    import albumentations as A

    # Set input and output directories
    INPUT_DIR = r"C:\Users\...\pills\01_01"
    OUTPUT_DIR = INPUT_DIR
    TARGET_COUNT = 350

    # Define Augmentations
    transform = A.Compose([
    A.RandomBrightnessContrast(p=0.3),
    A.HorizontalFlip(p=0.5),
    A.VerticalFlip(p=0.2),
    A.Rotate(limit=25, p=0.5),
    A.Blur(blur_limit=2, p=0.2),
    A.GaussNoise(var_limit=(5.0, 10.0), p=0.2), # Might be too strong?
    A.RandomCrop(height=256, width=256, p=0.1),
    A.HueSaturationValue(p=0.3)
    ])

    # Process images
    image_files = [f for f in os.listdir(INPUT_DIR) if f.endswith(('.jpg', '.png', '.jpeg'))]

    for i in range(1, TARGET_COUNT + 1):
    img_name = image_files[i % len(image_files)]
    img_path = os.path.join(INPUT_DIR, img_name)

    image = cv2.imread(img_path)
    if image is None:
    continue

    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Convert to RGB
    augmented = transform(image=image)['image']
    augmented = cv2.cvtColor(augmented, cv2.COLOR_RGB2BGR) # Convert back before saving

    new_filename = f"01_{i}.jpg"
    cv2.imwrite(os.path.join(OUTPUT_DIR, new_filename), augmented)

    print(f"Saved augmented images in {OUTPUT_DIR}")


    Troubleshooting Steps I've Tried: ✔ Reduced var_limit in GaussNoise → Still some noise issues. ✔ Checked if cv2 was handling color incorrectly. ✔ Reduced Blur intensity.

    Question: How can I fix the excessive noise issue while keeping useful augmentations? Are there better augmentation settings for pill detection? Should I use a different approach for ensuring annotation correctness in YOLOv8?

    Continue reading...

Compartilhe esta Página