o
    oi                     @   sh   d dl mZmZmZmZmZ d dlmZ d dl	m
Z
 d dlmZmZ d dlmZmZ G dd de
ZdS )	    )AnyDictOptionalTupleUnion)random_generator)IntensityAugmentationBase2D)Tensorwhere)bbox_generatorbbox_to_maskc                       s   e Zd ZdZ						ddeeeeef f deeeeef f d	ed
edededdf fddZ		ddede
eef de
eef dee def
ddZ	ddede
eef de
eef dee def
ddZ  ZS )RandomErasinga  Erase a random rectangle of a tensor image according to a probability p value.

    .. image:: _static/img/RandomErasing.png

    The operator removes image parts and fills them with zero values at a selected rectangle
    for each of the images in the batch.

    The rectangle will have an area equal to the original image area multiplied by a value uniformly
    sampled between the range [scale[0], scale[1]) and an aspect ratio sampled
    between [ratio[0], ratio[1])

    Args:
        scale: range of proportion of erased area against input image.
        ratio: range of aspect ratio of erased area.
        value: the value to fill the erased area.
        same_on_batch: apply the same transformation across the batch.
        p: probability that the random erasing operation will be performed.
        keepdim: whether to keep the output shape the same as input (True) or broadcast it
                        to the batch form (False).

    Shape:
        - Input: :math:`(C, H, W)` or :math:`(B, C, H, W)`, Optional: :math:`(B, 3, 3)`
        - Output: :math:`(B, C, H, W)`

    Note:
        Input tensor must be float and normalized into [0, 1] for the best differentiability support.
        Additionally, this function accepts another transformation tensor (:math:`(B, 3, 3)`), then the
        applied transformation will be merged int to the input transformation tensor and returned.

    Examples:
        >>> rng = torch.manual_seed(0)
        >>> inputs = torch.ones(1, 1, 3, 3)
        >>> aug = RandomErasing((.4, .8), (.3, 1/.3), p=0.5)
        >>> aug(inputs)
        tensor([[[[1., 0., 0.],
                  [1., 0., 0.],
                  [1., 0., 0.]]]])

    To apply the exact augmenation again, you may take the advantage of the previous parameter state:
        >>> input = torch.randn(1, 3, 32, 32)
        >>> aug = RandomErasing((.4, .8), (.3, 1/.3), p=1.)
        >>> (aug(input) == aug(input, params=aug._params)).all()
        tensor(True)

    g{Gz?gQ?g333333?gffffff
@        F      ?scaleratiovaluesame_on_batchpkeepdimreturnNc                    s8   t  j|||d || _|| _|| _t|||| _d S )N)r   r   r   )super__init__r   r   r   rgRectangleEraseGenerator_param_generator)selfr   r   r   r   r   r   	__class__ ]/home/ubuntu/.local/lib/python3.10/site-packages/kornia/augmentation/_2d/intensity/erasing.pyr   I   s
   	zRandomErasing.__init__inputparamsflags	transformc                 C   s   |  \}}}}|d dddjdg|jdd  R  |}	t|d |d |d |d }
t|
||}|dd|dd|}t|dk|	|}|S )	Nvalues   xsyswidthsheights      ?)size	unsqueezerepeatshapetor   r   r
   r   r#   r$   r%   r&   _chwr'   bboxesmasktransformedr!   r!   r"   apply_transformX   s   8zRandomErasing.apply_transformc                 C   s   |  \}}}}|d d jdg|jdd  R  |}	|	 }	t|d |d |d |d }
t|
||}|dd|dd|}t|dk|	|}|S )	Nr'   ).NNNr)   r*   r+   r,   r-   r.   )	r/   r1   r2   r3   zero_r   r   r0   r
   r4   r!   r!   r"   apply_transform_maskd   s   *z"RandomErasing.apply_transform_mask)r   r   r   Fr   F)N)__name__
__module____qualname____doc__r   r	   r   floatboolr   r   strr   r   r<   r>   __classcell__r!   r!   r   r"   r      s^    0




r   N)typingr   r   r   r   r   kornia.augmentationr   r   &kornia.augmentation._2d.intensity.baser   kornia.corer	   r
   kornia.geometry.bboxr   r   r   r!   r!   r!   r"   <module>   s   