o
    oiX                     @   s|  d dl mZmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ g dZG dd deZG d	d
 d
eZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd  d eZG d!d" d"eZG d#d$ d$eZG d%d& d&eZG d'd( d(eZG d)d* d*eZG d+d, d,eZdS )-    )OptionalTupleN)OperationBase)Tensor)STEFunction)AutoContrast
BrightnessContrastEqualizeGrayHorizontalFlipHueInvert	PosterizeRotateSaturate	SharpnessShearXShearYSolarizeSolarizeAdd
TranslateX
TranslateYVerticalFlipc                	       s6   e Zd ZdZ	ddedededd	f fd
dZ  ZS )r   a  Apply auto_contrast operation.

    Args:
        initial_probability: the initial probability. If None, the augmentation will be randomly
            applied according to he augmentation sampling range.
        temperature: temperature for RelaxedBernoulli distribution used during training.
        symmetric_megnitude: if to randomly assign the magnitude as negative or not.

          ?皙?Finitial_probabilitytemperaturesymmetric_megnitudereturnNc                    s"   t  jtjd|dd ||d d S NFsame_on_batchpinitial_magnituder   r   )super__init__KRandomAutoContrast)selfr   r   r   	__class__ [/home/ubuntu/.local/lib/python3.10/site-packages/kornia/augmentation/auto/operations/ops.pyr'   ;   s   
zAutoContrast.__init__)r   r   F)__name__
__module____qualname____doc__floatboolr'   __classcell__r-   r-   r+   r.   r   0   s    r   c                       R   e Zd ZdZ					ddee dedeeef d	ed
eddf fddZ  Z	S )r   a  Apply brightness operation.

    Args:
        initial_probability: the initial probability. If None, the augmentation will be randomly
            applied according to he augmentation sampling range.
        initial_magnitude: the initial magnitude.
        magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
        temperature: temperature for RelaxedBernoulli distribution used during training.
        symmetric_megnitude: if to randomly assign the magnitude as negative or not.

    r   皙?g?r   Fr%   r   magnitude_ranger   r   r   Nc                    *   t  jtj|d|dd|fg||d d S )NFr!   brightness_factorr$   )r&   r'   r(   RandomBrightnessr*   r%   r   r9   r   r   r+   r-   r.   r'   S      
zBrightness.__init__r   r   r7   r   F
r/   r0   r1   r2   r   r3   r   r4   r'   r5   r-   r-   r+   r.   r   F   (    
r   c                       r6   )r	   a  Apply contrast operation.

    Args:
        initial_probability: the initial probability. If None, the augmentation will be randomly
            applied according to he augmentation sampling range.
        initial_magnitude: the initial magnitude.
        magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
        temperature: temperature for RelaxedBernoulli distribution used during training.
        symmetric_megnitude: if to randomly assign the magnitude as negative or not.

    r   r7   r   Fr%   r   r9   r   r   r   Nc                    r:   )NFr!   contrast_factorr$   )r&   r'   r(   RandomContrastr=   r+   r-   r.   r'   p   r>   zContrast.__init__r?   r@   r-   r-   r+   r.   r	   c   rA   r	   c                       R   e Zd ZdZ					ddee ded	eeef d
ededdf fddZ  Z	S )r   a  Apply hue operation.

    Args:
        initial_probability: the initial probability. If None, the augmentation will be randomly
            applied according to he augmentation sampling range.
        initial_magnitude: the initial magnitude.
        magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
        temperature: temperature for RelaxedBernoulli distribution used during training.
        symmetric_megnitude: if to randomly assign the magnitude as negative or not.

            r   g      r   r   Fr%   r   r9   r   r   r   Nc                    r:   )NFr!   
hue_factorr$   )r&   r'   r(   	RandomHuer=   r+   r-   r.   r'      r>   zHue.__init__)rE   r   rF   r   Fr@   r-   r-   r+   r.   r      rA   r   c                       r6   )r   a  Apply saturation operation.

    Args:
        initial_probability: the initial probability. If None, the augmentation will be randomly
            applied according to he augmentation sampling range.
        initial_magnitude: the initial magnitude.
        magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
        temperature: temperature for RelaxedBernoulli distribution used during training.
        symmetric_megnitude: if to randomly assign the magnitude as negative or not.

    r   r7   r   Fr%   r   r9   r   r   r   Nc                    r:   )NFr!   saturation_factorr$   )r&   r'   r(   RandomSaturationr=   r+   r-   r.   r'      r>   zSaturate.__init__r?   r@   r-   r-   r+   r.   r      rA   r   c                       0   e Zd ZdZd
dededdf fdd	Z  ZS )r
   a  Apply equalize operation.

    Args:
        initial_probability: the initial probability. If None, the augmentation will be randomly
            applied according to he augmentation sampling range.
        temperature: temperature for RelaxedBernoulli distribution used during training.

    Note:
        Equalize cannot update probabilities yet.

    Note:
        STE gradient estimator applied for back propagation.

    r   r   r   r   r   Nc                    s$   t  jtjd|dd |dtd d S )NFr!   r%   r   r   gradient_estimator)r&   r'   r(   RandomEqualizer   r*   r   r   r+   r-   r.   r'      s   
zEqualize.__init__r   r   r/   r0   r1   r2   r3   r'   r5   r-   r-   r+   r.   r
      s    $r
   c                       rK   )r   a&  Apply grayscale operation.

    Args:
        initial_probability: the initial probability. If None, the augmentation will be randomly
            applied according to he augmentation sampling range.
        temperature: temperature for RelaxedBernoulli distribution used during training.

    r   r   r   r   r   Nc                    "   t  jtjd|dd |dd d S r    )r&   r'   r(   RandomGrayscalerO   r+   r-   r.   r'         
zGray.__init__rP   rQ   r-   r-   r+   r.   r          $	r   c                       rK   )r   a#  Apply invert operation.

    Args:
        initial_probability: the initial probability. If None, the augmentation will be randomly
            applied according to he augmentation sampling range.
        temperature: temperature for RelaxedBernoulli distribution used during training.

    r   r   r   r   r   Nc                    rR   r    )r&   r'   r(   RandomInvertrO   r+   r-   r.   r'      rT   zInvert.__init__rP   rQ   r-   r-   r+   r.   r      rU   r   c                       sh   e Zd ZdZededefddZ						
ddee dede	eef dede
ddf fddZ  ZS )r   a[  Apply posterize operation.

    Args:
        initial_magnitude: the initial magnitude.
        initial_probability: the initial probability. If None, the augmentation will be randomly
            applied according to he augmentation sampling range.
        magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
        temperature: temperature for RelaxedBernoulli distribution used during training.
        symmetric_megnitude: if to randomly assign the magnitude as negative or not.

    Note:
        STE gradient estimator applied for back propagation.

    	magnituder   c                 C   s   |   S )N)longrW   r-   r-   r.   _process_magnitude  s   zPosterize._process_magnitude      @r         ?g       @r   Fr%   r   r9   r   r   Nc                    s0   t  jtj|d|dd|fg||tjtd d S )NFr!   bits_factor)r%   r   r   magnitude_fnrM   )r&   r'   r(   RandomPosterizer   rZ   r   r=   r+   r-   r.   r'     s   
zPosterize.__init__)r[   r   r\   r   Fr/   r0   r1   r2   staticmethodr   rZ   r   r3   r   r4   r'   r5   r-   r-   r+   r.   r      s,    
r   c                       r6   )r   aY  Apply solarize operation.

    Args:
        initial_magnitude: the initial magnitude.
        initial_probability: the initial probability. If None, the augmentation will be randomly
            applied according to he augmentation sampling range.
        magnitude_range: the sampling range for random sampling and clamping the optimized
        symmetric_megnitude: if to randomly assign the magnitude as negative or not.magnitude.
        temperature: temperature for RelaxedBernoulli distribution used during training.

    Note:
        STE gradient estimator applied for back propagation.

    r   rE   r]   r   Fr%   r   r9   r   r   r   Nc                    s.   t  jtj|dd|dd|fg||td d S )NrE   F)	additionsr"   r#   
thresholdsrL   r&   r'   r(   RandomSolarizer   r=   r+   r-   r.   r'   1     
zSolarize.__init__)r   r   rc   r   Fr@   r-   r-   r+   r.   r   !  (    
r   c                       rD   )r   a  Apply solarize-addition operation with a fixed thresholds of 0.5.

    Args:
        initial_magnitude: the initial magnitude.
        initial_probability: the initial probability. If None, the augmentation will be randomly
            applied according to he augmentation sampling range.
        magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
        temperature: temperature for RelaxedBernoulli distribution used during training.
        symmetric_megnitude: if to randomly assign the magnitude as negative or not.

    Note:
        STE gradient estimator applied for back propagation.

    rE   r   g333333ӿ333333?r   Fr%   r   r9   r   r   r   Nc                    s.   t  jtjd|d|dd|fg||td d S )Nr   F)re   rd   r"   r#   rd   rL   rf   r=   r+   r-   r.   r'   R  rh   zSolarizeAdd.__init__)rE   r   rj   r   Fr@   r-   r-   r+   r.   r   B  ri   r   c                       r6   )r   a  Apply sharpness operation.

    Args:
        initial_magnitude: the initial magnitude.
        initial_probability: the initial probability. If None, the augmentation will be randomly
            applied according to he augmentation sampling range.
        magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
        temperature: temperature for RelaxedBernoulli distribution used during training.
        symmetric_megnitude: if to randomly assign the magnitude as negative or not.

    r   r   gffffff?r   Fr%   r   r9   r   r   r   Nc                    r:   )NFr!   	sharpnessr$   )r&   r'   r(   RandomSharpnessr=   r+   r-   r.   r'   p  r>   zSharpness.__init__)r   r   rl   r   Fr@   r-   r-   r+   r.   r   c  rA   r   c                       rK   )r   a,  Apply horizontal flip operation.

    Args:
        initial_probability: the initial probability. If None, the augmentation will be randomly
            applied according to he augmentation sampling range.
        temperature: temperature for RelaxedBernoulli distribution used during training.

    r   r   r   r   r   Nc                    rR   r    )r&   r'   r(   RandomHorizontalFliprO   r+   r-   r.   r'     rT   zHorizontalFlip.__init__rP   rQ   r-   r-   r+   r.   r     rU   r   c                       rK   )r   zApply vertical flip operation.

    Args:
        initial_magnitude: the initial magnitude.
        temperature: temperature for RelaxedBernoulli distribution used during training.

    r   r   r   r   r   Nc                    rR   r    )r&   r'   r(   RandomVerticalFliprO   r+   r-   r.   r'     rT   zVerticalFlip.__init__rP   rQ   r-   r-   r+   r.   r     s    $r   c                       rD   )r   a  Apply rotate operation.

    Args:
        initial_magnitude: the initial magnitude.
        initial_probability: the initial probability. If None, the augmentation will be randomly
            applied according to he augmentation sampling range.
        magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
        temperature: temperature for RelaxedBernoulli distribution used during training.
        symmetric_megnitude: if to randomly assign the magnitude as negative or not.

          .@r   rE   g      >@r   Tr%   r   r9   r   r   r   Nc                    sX   |r|d dk rt d| jj d|d  dt jtj|d|dd|fg||d d S )	Nr   Lower bound of = is a symmetric operation. The lower bound must above 0. Got .Fr!   degreesr$   )
ValueErrorr,   r/   r&   r'   r(   RandomRotationr=   r+   r-   r.   r'     s   
zRotate.__init__)rq   r   rr   r   Tr@   r-   r-   r+   r.   r     rA   r   c                       h   e Zd ZdZededefddZ						dd
ee dede	eef dede
ddf fddZ  ZS )r   a  Apply shear operation along x-axis.

    Args:
        initial_magnitude: the initial magnitude.
        initial_probability: the initial probability. If None, the augmentation will be randomly
            applied according to he augmentation sampling range.
        magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
        temperature: temperature for RelaxedBernoulli distribution used during training.
        symmetric_megnitude: if to randomly assign the magnitude as negative or not.

    rW   r   c                 C      | d S N   r-   rY   r-   r-   r.   rZ        zShearX._process_magnituder   r   rE   rk   Tr%   r   r9   r   r   Nc                    s^   |r|d dk rt d| jj d|d  dt jtj|d|ddd|fg||tjd	 d S )
Nr   rs   rt   ru   FTr"   r#   align_cornersshear_xr%   r   r   r_   )	rw   r,   r/   r&   r'   r(   RandomShearr   rZ   r=   r+   r-   r.   r'     s   
zShearX.__init__r   r   r~   r   Tra   r-   r-   r+   r.   r     ,    
r   c                       ry   )r   a  Apply shear operation along y-axis.

    Args:
        initial_magnitude: the initial magnitude.
        initial_probability: the initial probability. If None, the augmentation will be randomly
            applied according to he augmentation sampling range.
        magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
        temperature: temperature for RelaxedBernoulli distribution used during training.
        symmetric_megnitude: if to randomly assign the magnitude as negative or not.

    rW   r   c                 C   rz   r{   r-   rY   r-   r-   r.   rZ     r}   zShearY._process_magnituder   r   r~   Tr%   r   r9   r   r   Nc                    sn   |r|d dk rt d| jj d|d  dt jtjdd|d |d fd|dd	d
|fg||tjd d S )Nr   rs   rt   ru   rE      FTr   shear_yr   )	rw   r,   r/   r&   r'   r(   r   r   rZ   r=   r+   r-   r.   r'     s$   
zShearY.__init__r   ra   r-   r-   r+   r.   r     r   r   c                       rD   )r   a   Apply translate operation along x-axis.

    Args:
        initial_magnitude: the initial magnitude.
        initial_probability: the initial probability. If None, the augmentation will be randomly
            applied according to he augmentation sampling range.
        magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
        temperature: temperature for RelaxedBernoulli distribution used during training.
        symmetric_megnitude: if to randomly assign the magnitude as negative or not.

    r8   r   rE   r   r   Tr%   r   r9   r   r   r   Nc                    sZ   |r|d dk rt d| jj d|d  dt jtj|d|ddd|fg||d	 d S )
Nr   rs   rt   ru   FTr   translate_xr$   rw   r,   r/   r&   r'   r(   RandomTranslater=   r+   r-   r.   r'   )  s   
zTranslateX.__init__r8   r   r   r   Tr@   r-   r-   r+   r.   r     rA   r   c                       rD   )r   a   Apply translate operation along y-axis.

    Args:
        initial_magnitude: the initial magnitude.
        initial_probability: the initial probability. If None, the augmentation will be randomly
            applied according to he augmentation sampling range.
        magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
        temperature: temperature for RelaxedBernoulli distribution used during training.
        symmetric_megnitude: if to randomly assign the magnitude as negative or not.

    r8   r   r   r   Tr%   r   r9   r   r   r   Nc                    s\   |r|d dk rt d| jj d|d  dt jtjd |d|ddd|fg||d	 d S )
Nr   rs   rt   ru   FTr   translate_yr$   r   r=   r+   r-   r.   r'   K  s   
zTranslateY.__init__r   r@   r-   r-   r+   r.   r   >  rA   r   ) typingr   r   kornia.augmentationaugmentationr(   (kornia.augmentation.auto.operations.baser   kornia.corer   kornia.grad_estimatorr   __all__r   r   r	   r   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r-   r-   r-   r.   <module>   s2   &!!"(-"