Ë
    ÃÓ^j›  ã                  óŒ   — d Z ddlmZ ddlmZmZ ddlZddlm	Z	 ddl
mZ ddlmZ ddlmZmZ d	d
lmZ dgZdZ G d„ de«      Zy)a�  Implementation of the Channel Dropout transform for multi-channel images.

This module provides the ChannelDropout transform, which randomly drops (sets to a fill value)
one or more channels in multi-channel images. This augmentation can help models become more
robust to missing or corrupted channel information and encourage learning from all available
channels rather than relying on a subset.
é    )Úannotations)Ú	AnnotatedÚAnyN)Úget_num_channels)ÚAfterValidator)Úcheck_range_bounds)ÚBaseTransformInitSchemaÚImageOnlyTransformé   )Úchannel_dropoutÚChannelDropouté   c                  ó\   ‡ — e Zd ZdZ G d„ de«      Z	 	 	 d	 	 	 	 	 dˆ fd„Zd	d„Zd
d„Zˆ xZ	S )r   a‹	  Randomly drop channels in the input image.

    This transform randomly selects a number of channels to drop from the input image
    and replaces them with a specified fill value. This can improve model robustness
    to missing or corrupted channels.

    The technique is conceptually similar to:
    - Dropout layers in neural networks, which randomly set input units to 0 during training.
    - CoarseDropout augmentation, which drops out regions in the spatial dimensions of the image.

    However, ChannelDropout operates on the channel dimension, effectively "dropping out"
    entire color channels or feature maps.

    Args:
        channel_drop_range (tuple[int, int]): Range from which to choose the number
            of channels to drop. The actual number will be randomly selected from
            the inclusive range [min, max]. Default: (1, 1).
        fill (float): Pixel value used to fill the dropped channels.
            Default: 0.
        p (float): Probability of applying the transform. Must be in the range
            [0, 1]. Default: 0.5.

    Raises:
        NotImplementedError: If the input image has only one channel.
        ValueError: If the upper bound of channel_drop_range is greater than or
            equal to the number of channels in the input image.

    Targets:
        image, volume

    Image types:
        uint8, float32

    Examples:
        >>> import numpy as np
        >>> import albumentations as A
        >>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
        >>> transform = A.ChannelDropout(channel_drop_range=(1, 2), fill=128, p=1.0)
        >>> result = transform(image=image)
        >>> dropped_image = result['image']
        >>> assert dropped_image.shape == image.shape
        >>> assert np.any(dropped_image != image)  # Some channels should be different

    Note:
        - The number of channels to drop is randomly chosen within the specified range.
        - Channels are randomly selected for dropping.
        - This transform is not applicable to single-channel (grayscale) images.
        - The transform will raise an error if it's not possible to drop the specified
          number of channels (e.g., trying to drop 3 channels from an RGB image).
        - This augmentation can be particularly useful for training models to be robust
          against missing or corrupted channel data in multi-spectral or hyperspectral imagery.

    c                  ó"   — e Zd ZU ded<   ded<   y)úChannelDropout.InitSchemazGAnnotated[tuple[int, int], AfterValidator(check_range_bounds(1, None))]Úchannel_drop_rangeÚfloatÚfillN)Ú__name__Ú
__module__Ú__qualname__Ú__annotations__© ó    ú/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/albumentations/augmentations/dropout/channel_dropout.pyÚ
InitSchemar   R   s   … ØcÓcØŒr   r   c                óB   •— t         ‰| �  |¬«       || _        || _        y )N)Úp)ÚsuperÚ__init__r   r   )Úselfr   r   r   Ú	__class__s       €r   r    zChannelDropout.__init__V   s$   ø€ ô 	‰Ñ˜1ÐÔà"4ˆÔØˆ�	r   c                ó0   — t        ||| j                  «      S )aB  Apply channel dropout to the image.

        Args:
            img (np.ndarray): Image to apply channel dropout to.
            channels_to_drop (list[int]): List of channel indices to drop.
            **params (Any): Additional parameters.

        Returns:
            np.ndarray: Image with dropped channels.

        )r   r   )r!   ÚimgÚchannels_to_dropÚparamss       r   ÚapplyzChannelDropout.applya   s   € ô ˜sÐ$4°d·i±iÓ@Ð@r   c                ó6  — d|v r|d   n|d   d   }t        |«      }|dk(  rd}t        |«      ‚| j                  d   |k\  rd}t        |«      ‚ | j                  j
                  | j                  Ž }| j                  j                  t        |«      |¬«      }d|iS )	zøGet parameters that depend on input data.

        Args:
            params (dict[str, Any]): Parameters.
            data (dict[str, Any]): Input data.

        Returns:
            dict[str, list[int]]: Dictionary with channels to drop.

        ÚimageÚimagesr   r   z6Images has one channel. ChannelDropout is not defined.z,Can not drop all channels in ChannelDropout.)Úkr%   )r   ÚNotImplementedErrorr   Ú
ValueErrorÚ	py_randomÚrandintÚsampleÚrange)r!   r&   Údatar)   Únum_channelsÚmsgÚnum_drop_channelsr%   s           r   Úget_params_dependent_on_dataz+ChannelDropout.get_params_dependent_on_datao   s«   € ð ")¨D¡��W’°d¸8±nÀQÑ6GˆÜ'¨Ó.ˆØ˜1ÒØJˆCÜ% cÓ*Ð*à×"Ñ" 1Ñ%¨Ò5Ø@ˆCÜ˜S“/Ð!Ø2˜DŸN™N×2Ñ2°D×4KÑ4KÐLÐØŸ>™>×0Ñ0´°|Ó1DÐHYÐ0ÓZÐà"Ð$4Ð5Ð5r   ))r   r   r   g      à?)r   ztuple[int, int]r   r   r   r   )r$   ú
np.ndarrayr%   z	list[int]r&   r   Úreturnr7   )r&   údict[str, Any]r2   r9   r8   zdict[str, list[int]])
r   r   r   Ú__doc__r	   r   r    r'   r6   Ú__classcell__)r"   s   @r   r   r      sM   ø„ ñ4ôlÐ,ô ð /5ØØð		à+ð	ð ð	ð õ		óA÷6r   )r:   Ú
__future__r   Útypingr   r   ÚnumpyÚnpÚalbucorer   Úpydanticr   Úalbumentations.core.pydanticr   Ú(albumentations.core.transforms_interfacer	   r
   Ú
functionalr   Ú__all__ÚMIN_DROPOUT_CHANNEL_LIST_LENGTHr   r   r   r   ú<module>rG      sA   ðñõ #ç !ã Ý %Ý #å ;ß `å 'àÐ
€à"#Ð ôk6Ð'õ k6r   