Albumentations

repository·main·Indexed 12 days ago

https://github.com/albumentations-team/albumentations

A fast and flexible Python library for image augmentation in deep learning, computer vision, and medical imaging. Version 2.0.8 provides a unified API for 2D and 3D data, including images, masks, bounding boxes, and keypoints, with over 70 high-quality transforms. It supports classification, segmentation, object detection, and pose estimation, and is compatible with PyTorch and TensorFlow.

Tokens
3.3K
Snippets
9
Records
13
Agent score
47%

What's inside Albumentations

  1. Overview of Albumentations features

    main

    Albumentations is a high-performance Python library for image augmentation designed for deep learning and computer vision tasks.

    Key features include:

    • Complete CV Support: Works with classification, segmentation (semantic & instance), object detection, and pose estimation.
    • Unified API: Consistent interface for RGB/grayscale/multispectral images, masks, bounding boxes, and keypoints.
    • Rich Library: Over 70 high-quality augmentations.
    • Fast Execution: Optimized for production use and benchmarked as one of the fastest augmentation libraries.
    • Framework Integration: Compatible with PyTorch, TensorFlow, and other major deep learning frameworks.
  2. Understand Pixel-level transforms

    main

    Pixel-level transforms modify only the input image. They do not affect additional targets like masks, bounding boxes, or keypoints.

    For volumetric data (volumes and 3D masks), these transforms are applied independently to each slice along the Z-axis (depth dimension) to maintain consistency across the volume.

  3. Understand Spatial-level transforms

    main

    Spatial-level transforms modify both the input image and any associated additional targets (e.g., masks, bounding boxes, and keypoints) simultaneously.

    For volumetric data, these transforms are applied independently to each slice along the Z-axis (depth dimension) to maintain consistency across the volume.

  4. Migrate to AlbumentationsX

    main

    The original albumentations repository is no longer actively maintained. For ongoing support, bug fixes, and performance improvements, you should migrate to AlbumentationsX.

    AlbumentationsX is a drop-in replacement with the same API. You can migrate by uninstalling the original package and installing albumentationsx. Your existing code using import albumentations as A will continue to work without changes.

    Licensing Note: AlbumentationsX uses dual licensing (AGPL-3.0 / Commercial). The AGPL-3.0 license is not compatible with permissive licenses like MIT, Apache 2.0, or BSD. If your project uses these licenses, a commercial license is required.

    # Uninstall original
    pip uninstall albumentations
    
    # Install AlbumentationsX
    pip install albumentationsx
  5. Use Albumentations for image augmentation

    main

    Albumentations provides a unified API to create augmentation pipelines using A.Compose. You can apply various transforms (like cropping, flipping, or brightness adjustments) to an image. The pipeline returns a dictionary containing the transformed image and any other augmented targets.

    import albumentations as A
    import cv2
    
    # Declare an augmentation pipeline
    transform = A.Compose([
        A.RandomCrop(width=256, height=256),
        A.HorizontalFlip(p=0.5),
        A.RandomBrightnessContrast(p=0.2),
    ])
    
    # Read an image with OpenCV and convert it to the RGB colorspace
    image = cv2.imread("image.jpg")
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    
    # Augment an image
    transformed = transform(image=image)
    transformed_image = transformed["image"]
  6. Reference: Spatial-level transforms and supported targets

    main

    Spatial-level transforms modify the image and its targets. The table below indicates which targets are supported by each transform.

    Target Definitions:

    • Volume: 3D array of shape (D, H, W) or (D, H, W, C).
    • Mask3D: Binary or multi-class 3D mask of shape (D, H, W).
    | Transform | Image | Mask | BBoxes | Keypoints | Volume | Mask3D |
    | :--- | :---: | :--: | :---: | :---: | :---: | :---: |
    | Affine | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | AtLeastOneBBoxRandomCrop | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | BBoxSafeRandomCrop | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | CenterCrop | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | CoarseDropout | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | ConstrainedCoarseDropout | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | Crop | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | CropAndPad | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | CropNonEmptyMaskIfExists | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | D4 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | ElasticTransform | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | Erasing | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | FrequencyMasking | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | GridDistortion | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | GridDropout | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | GridElasticDeform | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | HorizontalFlip | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | Lambda | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | LongestMaxSize | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | MaskDropout | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | Morphological | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | Mosaic | ✓ | ✓ | ✓ | ✓ | | |
    | NoOp | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | OpticalDistortion | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | OverlayElements | ✓ | ✓ | | | | |
    | Pad | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | PadIfNeeded | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | Perspective | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | PiecewiseAffine | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | PixelDropout | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | RandomCrop | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | RandomCropFromBorders | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | RandomCropNearBBox | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | RandomGridShuffle | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | RandomResizedCrop | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | RandomRotate90 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | RandomScale | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | RandomSizedBBoxSafeCrop | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | RandomSizedCrop | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | Resize | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | Rotate | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | SafeRotate | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | ShiftScaleRotate | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | SmallestMaxSize | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | SquareSymmetry | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | ThinPlateSpline | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | TimeMasking | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | TimeReverse | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | Transpose | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | VerticalFlip | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
    | XYMasking | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
  7. Reference: Pixel-level transforms

    main

    The following pixel-level transforms are available. These only change the input image and leave masks, bounding boxes, and keypoints unchanged.

    AdditiveNoise, AdvancedBlur, AutoContrast, Blur, CLAHE, ChannelDropout, ChannelShuffle, ChromaticAberration, ColorJitter, Defocus, Downscale, Emboss, Equalize, FDA, FancyPCA, FromFloat, GaussNoise, GaussianBlur, GlassBlur, HEStain, HistogramMatching, HueSaturationValue, ISONoise, Illumination, ImageCompression, InvertImg, MedianBlur, MotionBlur, MultiplicativeNoise, Normalize, PixelDistributionAdaptation, PlanckianJitter, PlasmaBrightnessContrast, PlasmaShadow, Posterize, RGBShift, RandomBrightnessContrast, RandomFog, RandomGamma, RandomGravel, RandomRain, RandomShadow, RandomSnow, RandomSunFlare, RandomToneCurve, RingingOvershoot, SaltAndPepper, Sharpen, ShotNoise, Solarize, Spatter, Superpixels, TextImage, ToFloat, ToGray, ToRGB, ToSepia, UnsharpMask, ZoomBlur
  8. Reference: 3D transforms and supported targets

    main

    3D transforms operate on volumetric data and can modify both the input volume and associated 3D mask.

    Target Definitions:

    • Volume: 3D array of shape (D, H, W) or (D, H, W, C).
    • Mask3D: Binary or multi-class 3D mask of shape (D, H, W).
    | Transform | Volume | Mask3D | Keypoints |
    | :--- | :---: | :---: | :---: |
    | CenterCrop3D | ✓ | ✓ | ✓ |
    | CoarseDropout3D | ✓ | ✓ | ✓ |
    | CubicSymmetry | ✓ | ✓ | ✓ |
    | Pad3D | ✓ | ✓ | ✓ |
    | PadIfNeeded3D | ✓ | ✓ | ✓ |
    | RandomCrop3D | ✓ | ✓ | ✓ |
  9. Cite Albumentations in research

    main

    If you use Albumentations in your research, please cite the following BibTeX entry:

    @Article{info11020125,
        AUTHOR = {Buslaev, Alexander and Iglovikov, Vladimir I. and Khvedchenya, Eugene and Parinov, Alex and Druzhinin, Mikhail and Kalinin, Alexandr A.},
        TITLE = {Albumentations: Fast and Flexible Image Augmentations},
        JOURNAL = {Information},
        VOLUME = {11},
        YEAR = {2020},
        NUMBER = {2},
        ARTICLE-NUMBER = {125},
        URL = {https://www.mdpi.com/2078-2489/11/2/125},
        ISSN = {2078-2489},
        DOI = {10.3390/info11020125}
    }