a
    d+                     @   s   d dl mZ d dl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 d dlmZ d dlmZmZmZ eeeeef f ZG dd	 d	e
jZG d
d deZdS )    )Enum)AnyDictOptionalTupleUnionN)Tensor)	Bernoulli)RandomGeneratorBase)_adapted_sampling_transform_output_shapeoverride_parametersc                       sp  e Zd ZdZd*eeeedd fddZed	d
dZe	e	dddZ
e	e	dddZe	ee	dddZejeee	f dddZe	eee	f eeef e	dddZejejddddZejeeee	dddZeee	f eeef eeee	f eeef f d d!d"Zeee	f d	d#d$Ze	eee	f eeef e	dd%d&Zd+e	eeee	f  e	d'd(d)Z  ZS ),_BasicAugmentationBasea  _BasicAugmentationBase base class for customized augmentation implementations.

    Plain augmentation base class without the functionality of transformation matrix calculations.
    By default, the random computations will be happened on CPU with ``torch.get_default_dtype()``.
    To change this behaviour, please use ``set_rng_device_and_dtype``.

    For automatically generating the corresponding ``__repr__`` with full customized parameters, you may need to
    implement ``_param_generator`` by inheriting ``RandomGeneratorBase`` for generating random parameters and
    put all static parameters inside ``self.flags``. You may take the advantage of ``PlainUniformGenerator`` to
    generate simple uniform parameters with less boilerplate code.

    Args:
        p: probability for applying an augmentation. This param controls the augmentation probabilities element-wise.
        p_batch: probability for applying an augmentation to a batch. This param controls the augmentation
          probabilities batch-wise.
        same_on_batch: apply the same transformation across the batch.
        keepdim: whether to keep the output shape the same as input ``True`` or broadcast it to
          the batch form ``False``.
          ?      ?FN)pp_batchsame_on_batchkeepdimreturnc                    s   t    || _|| _|| _|| _i | _|dks8|dkrDt| j| _|dksT|dkr`t| j| _	d | _
i | _| tdt  d S )Ng        r   cpu)super__init__r   r   r   r   _paramsr	   _p_gen_p_batch_gen_param_generatorflagsset_rng_device_and_dtypetorchdeviceZget_default_dtype)selfr   r   r   r   	__class__ a/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/kornia/augmentation/base.pyr   $   s    
z_BasicAugmentationBase.__init__r   c                 C   s   d| j  d| j d| j }t| jtr<t| j d| }| j D ]B\}}t|t	rt|d| d|j
  7 }qF|d| d| 7 }qF| jj d| dS )Nzp=z
, p_batch=z, same_on_batch=z, =())r   r   r   
isinstancer   r
   strr   itemsr   namelowerr#   __name__)r!   txtkvr$   r$   r%   __repr__5   s    
z_BasicAugmentationBase.__repr__inputr   c                 C   s   |S Nr$   r!   r5   r$   r$   r%   __unpack_input__@   s    z'_BasicAugmentationBase.__unpack_input__c                 C   s   t dS )zStandardize input tensors.NNotImplementedErrorr7   r$   r$   r%   transform_tensorC   s    z'_BasicAugmentationBase.transform_tensor)outputoutput_shaper   c                 C   s   | j rt||S |S )zStandardize output tensors.)r   r   )r!   r<   r=   r$   r$   r%   transform_output_tensorG   s    z._BasicAugmentationBase.transform_output_tensor)batch_shaper   c                 C   s   | j d ur|  || jS i S r6   )r   r   )r!   r?   r$   r$   r%   generate_parametersK   s    
z*_BasicAugmentationBase.generate_parametersr5   paramsr   r   c                 C   s   t d S r6   r9   r!   r5   rB   r   r$   r$   r%   apply_transformP   s    z&_BasicAugmentationBase.apply_transform)r    dtyper   c                 C   s(   || _ || _| jdur$| j|| dS )zChange the random generation device and dtype.

        Note:
            The generated random numbers are not reproducible across different devices and dtypes.
        N)r    rE   r   r   )r!   r    rE   r$   r$   r%   r   S   s    
z/_BasicAugmentationBase.set_rng_device_and_dtype)r?   r   r   r   r   c                 C   s   |dkrt dg}n(|dkr,t dg}ntd| j| }|  dkr|dkrlt dg|d  }n6|dkrt dg|d  }nt|d f| j| }|| }n||d }|S )N   Tr   F)rF   )	r   tensorr   r   boolsumitemr   repeat)r!   r?   r   r   r   
batch_probZ	elem_probr$   r$   r%   __batch_prob_generator__^   s    
z/_BasicAugmentationBase.__batch_prob_generator__)rB   r   r   c                 K   sZ   d|v r|d nd}|r.t ||dd}|| _n|| _t ||dd}t | j|dd}||fS )Nsave_kwargsFT)Zin_place)r   r   r   )r!   rB   r   kwargsrN   r$   r$   r%   #_process_kwargs_to_params_and_flagsv   s    z:_BasicAugmentationBase._process_kwargs_to_params_and_flagsc                 C   sx   |  || j| j| j}| tt| 	 g|dd  R }|d u rNi }||d< tj
|tjd}|d|i |S )NrF   rL   )rE   Zforward_input_shape)rM   r   r   r   r@   r   SizeintrI   rJ   rG   longupdate)r!   r?   to_applyr   Z
input_sizer$   r$   r%   forward_parameters   s    ,z)_BasicAugmentationBase.forward_parametersc                 C   s   |  |||S r6   )rD   rC   r$   r$   r%   
apply_func   s    z!_BasicAugmentationBase.apply_func)r5   rB   r   c           	      K   s   |  |}|j}| |}|j}|du r2| |}d|vrRtdg|d  |d< | j|| jfi |\}}| |||}| j	r| 
||S |S )a  Perform forward operations.

        Args:
            input: the input tensor.
            params: the corresponding parameters for an operation.
                If None, a new parameter suite will be generated.
            **kwargs: key-value pairs to override the parameters and flags.

        Note:
            By default, all the overwriting parameters in kwargs will not be recorded
            as in ``self._params``. If you wish it to be recorded, you may pass
            ``save_kwargs=True`` additionally.
        NrL   Tr   )r8   shaper;   rV   r   rG   rP   r   rW   r   r>   )	r!   r5   rB   rO   	in_tensorZinput_shaper?   r   r<   r$   r$   r%   forward   s    


z_BasicAugmentationBase.forward)r   r   FF)N)r/   
__module____qualname____doc__floatrH   r   r+   r3   r   r8   r;   r   r>   r   rQ   r   r@   r   rD   r    rE   r   rM   rP   rV   rW   r   rZ   __classcell__r$   r$   r"   r%   r      s,    $
$r   c                       s   e Zd ZdZdee eeeedd fddZee	d	d
dZ
ed	 fddZe	e	dddZe	eee	f eeef e	dddZde	eee	f eeef ee	 e	dddZde	eee	f eeeef  e	dddZ  ZS )_AugmentationBasea  _AugmentationBase base class for customized augmentation implementations.

    Advanced augmentation base class with the functionality of transformation matrix calculations.

    Args:
        p: probability for applying an augmentation. This param controls the augmentation probabilities
          element-wise for a batch.
        p_batch: probability for applying an augmentation to a batch. This param controls the augmentation
          probabilities batch-wise.
        same_on_batch: apply the same transformation across the batch.
        keepdim: whether to keep the output shape the same as input ``True`` or broadcast it
          to the batch form ``False``.
    NFr   r   )return_transformr   r   r   r   r   c                    s>   t  j||||d || _|| _|| _|  |d ur:tdd S )N)r   r   r   z`return_transform` is deprecated. Please access the transformation matrix with `.transform_matrix`. For chained matrices, please use `AugmentationSequential`.)r   r   r   r   ra   
ValueError)r!   ra   r   r   r   r   r"   r$   r%   r      s    z_AugmentationBase.__init__r&   c                 C   s   | j S r6   )_transform_matrixr!   r$   r$   r%   transform_matrix   s    z"_AugmentationBase.transform_matrixc                    s   | j jdt   d S )Nr(   r)   )r#   r/   r   r3   rd   r"   r$   r%   r3      s    z_AugmentationBase.__repr__r4   c                 C   s   t d S r6   r9   r7   r$   r$   r%   identity_matrix   s    z!_AugmentationBase.identity_matrixrA   c                 C   s   t d S r6   r9   rC   r$   r$   r%   compute_transformation   s    z(_AugmentationBase.compute_transformation)r5   rB   r   	transformr   c                 C   s   t d S r6   r9   )r!   r5   rB   r   rh   r$   r$   r%   rD      s    z!_AugmentationBase.apply_transform)rY   rB   r   r   c                 C   s   |d u r| j }|d }| s.|}| |}nt| rZ| j|||d}| j||||d}nH| }| |}| j|| ||d||< | j|| |||| d||< || _|S )NrL   )rB   r   )rB   r   rh   )r   anyrf   allrg   rD   clonerc   )r!   rY   rB   r   rU   r<   Ztrans_matrixr$   r$   r%   rW      s"    

z_AugmentationBase.apply_func)NFr   r   F)N)N)r/   r[   r\   r]   r   rH   r^   r   propertyr   re   r+   r3   rf   r   r   rg   rD   rW   r_   r$   r$   r"   r%   r`      s:        $  r`   )enumr   typingr   r   r   r   r   r   Ztorch.nnnnr   Ztorch.distributionsr	   Z$kornia.augmentation.random_generatorr
   Zkornia.augmentation.utilsr   r   r   ZTensorWithTransformMatModuler   r`   r$   r$   r$   r%   <module>   s    &