o
    oi                     @   s   d dl mZmZmZmZmZmZmZ d dlZd dlm	Z	 d dl
mZ d dlmZmZ d dlmZ d dlmZ d dlmZmZ ed	d
dZG dd
 d
eZdS )    )CallableDictListOptionalTupleTypeTypeVarN)nn)Function)	BernoulliRelaxedBernoulli)Self)_AugmentationBase)ModuleTensorTOperationBase)boundc                       s  e Zd ZdZ						d*dedeeeeee	 f   de	de
d	eeegef  d
eee  de
ddf fddZd	eeegef  deegef fddZdeeeeee	 f   ddfddZde
ddfddZd+de
def fddZdefddZd,dejdee deeef fddZd,d ed!eeeef  defd"d#Zedee fd$d%Zedee fd&d'Zedefd(d)Z  ZS )-r   a  Base class of differentiable augmentation operations.

    Args:
        operation: Kornia augmentation module.
        initial_magnitude: targeted magnitude parameter name and its initial magnitude value.
            The magnitude parameter name shall align with the attribute inside the random_generator
            in each augmentation. If None, the augmentation will be randomly applied according to
            the augmentation sampling range.
        temperature: temperature for RelaxedBernoulli distribution used during training.
        is_batch_operation: determine if to obtain the probability from `p` or `p_batch`.
            Set to True for most non-shape-persistent operations (e.g. cropping).

    N皙?F	operationinitial_magnitudetemperatureis_batch_operationmagnitude_fngradient_estimatorsymmetric_megnitudereturnc                    s   t    t|tstd| d|| _| | d| _|| _|r1t	
td| jj| _nt	
td| jj| _|dk rKtd| d| dtd| || _| || _|| _d S )Nz)Only Kornia augmentations supported. Got .)gHz>gP?   r   z-Expect temperature value greater than 0. Got r   )super__init__
isinstancer   
ValueErrorop_init_magnitudeprobability_range_is_batch_operationr	   	Parametertorchemptyfill_p_batch_probabilitypregister_bufferr   _init_magnitude_fn_magnitude_fn_gradient_estimator)selfr   r   r   r   r   r   r   	__class__ \/home/ubuntu/.local/lib/python3.10/site-packages/kornia/augmentation/auto/operations/base.pyr    /   s    




zOperationBase.__init__c                 C   sV   dt dt fdd}dtt gt f dtt gt f fdd}|d u r"|}| jr)||S |S )Nxr   c                 S   s   | S Nr5   )r7   r5   r5   r6   	_identityR   s   z3OperationBase._init_magnitude_fn.<locals>._identityfnc                    s   dt dt f fdd}|S )Nr7   r   c                    s(   t j| jd f| jddk} | | S )Nr   deviceg      ?)r(   randshaper<   )r7   flipr:   r5   r6   fV   s   zAOperationBase._init_magnitude_fn.<locals>._random_flip.<locals>.f)r   )r:   rA   r5   r@   r6   _random_flipU   s   z6OperationBase._init_magnitude_fn.<locals>._random_flip)r   r   r   )r2   r   r9   rB   r5   r5   r6   r/   Q   s   &z OperationBase._init_magnitude_fnc                 C   s   t |ttfr!tdd |D std| t|dkr!td|d u r0d | _d | _d | _	d S |d d | _| j
jd urGt| j
j| j| _	ntd| j d| j
j d	d | _|d d d urrttd|d d | _d S d S )
Nc                 s   s*    | ]}t |ttfot|d kV  qdS )   N)r!   listtuplelen).0ini_magr5   r5   r6   	<genexpr>f   s   ( z0OperationBase._init_magnitude.<locals>.<genexpr>z=`initial_magnitude` shall be a list of 2-element tuples. Got r   z2Multi magnitudes operations are not yet supported.r   zNo valid magnitude `z` found in `z`.)r!   rD   rE   allr"   rF   NotImplementedError_factor_name
_magnitudemagnitude_ranger#   _param_generatorgetattrr	   r'   r(   r)   r*   )r2   r   r5   r5   r6   r$   d   s"   
$zOperationBase._init_magnitude
relaxationc                 C   s`   |r| j rt| j| j| j_d S t| j| j| j_d S | j r't| j| j_d S t| j| j_d S r8   )r&   r   r   probabilityr#   _p_batch_gen_p_genr   )r2   rQ   r5   r5   r6   _update_probability_genz   s   z%OperationBase._update_probability_genTmodec                    s   | j |d t j|dS )NrQ   )rV   )rU   r   train)r2   rV   r3   r5   r6   rX      s   zOperationBase.trainc                 C   s
   |  dS )NF)rX   r2   r5   r5   r6   eval   s   
zOperationBase.evalbatch_shapemagc                 C   s|   |d u r| j }| jdd | j|}|d ur,| jd u r td|| j  | || j< | jd ur<| || j || j< |S )NTrW   z6No factor found in the params while `mag` is provided.)	magnituderU   r#   forward_parametersrL   RuntimeErrorzero_r0   )r2   r[   r\   paramsr5   r5   r6   r^      s   

z OperationBase.forward_parametersinputra   c                 C   s   |d u r
|  |j}|d ddt|jd    j|jd}| jd ur\t  | j||d}W d    n1 s9w   Y  || d| |  }| j	d u rT| j
||S | j
| j	|S || j||d d| |  S )N
batch_prob).r8   r   r;   )ra   )r^   r>   rF   tor<   r1   r(   no_gradr#   r]   apply)r2   rb   ra   rc   outputr5   r5   r6   forward   s   (


zOperationBase.forwardc                 C   s   t | jdr
| jjS d S )Ntransform_matrix)hasattrr#   ri   rY   r5   r5   r6   ri      s   zOperationBase.transform_matrixc                 C   s.   | j d u rd S | j }| jd ur|j| j S |S r8   )rM   rN   clamp)r2   r\   r5   r5   r6   r]      s   

zOperationBase.magnitudec                 C   s   | j j| j }|S r8   )r,   rk   r%   )r2   r-   r5   r5   r6   rR      s   zOperationBase.probability)Nr   FNNF)Tr8   ) __name__
__module____qualname____doc__r   r   r   r   strfloatboolr   r   r   r
   r    r/   r$   rU   r   rX   rZ   r(   Sizer   r^   rh   propertyri   r]   rR   __classcell__r5   r5   r3   r6   r       sL    
	*"&&$)typingr   r   r   r   r   r   r   r(   r	   torch.autogradr
   torch.distributionsr   r   typing_extensionsr   kornia.augmentation.baser   kornia.corer   r   r   r   r5   r5   r5   r6   <module>   s   $