a
    d_                     @   s  d dl Z d dlmZmZ d dlmZ d dlmZmZm	Z	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Zd dlmZmZmZm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$ d dl%m&Z& d dl'm(Z( d dl)m*Z* d dl+m,Z, dee  ee  dddZ-G dd dedZ.G dd de.Z/G dd de/Z0G dd de/Z1G dd de/Z2G dd  d e2Z3G d!d" d"e3Z4G d#d$ d$e2Z5G d%d& d&Z6dS )'    N)ABCMetaabstractmethod)partial)
AnyCallableDictIteratorListOptionalTupleTypeUnioncast)Tensor)GeometricAugmentationBase2DMixAugmentationBase
RandomCropRandomErasing)MixAugmentationBaseV2)_AugmentationBase)	ParamItem)override_parameters)DataKey)transform_bbox)transform_points)KORNIA_UNWRAP_torch_inverse_cast#kornia.augmentation.ImageSequentialmoduleparamreturnc                 C   s@   |  |}g }t||D ]"\\}}}t|tfr|| q|S N)Zget_forward_sequencezip
isinstancer   append)r    r!   Znamed_modulesres_modp r+   l/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/kornia/augmentation/container/utils.py_get_geometric_only_param   s    
r-   c                   @   s   e Zd ZdZeei feee ej	e
eeef eeee f dddZeedi feej	ee
 eeef edddZdS )	ApplyInverseInterfacez>Abstract interface for applying and inversing transformations.inputlabelr    r!   
extra_argsr"   c                 C   s   t dS )_  Apply a transformation with respect to the parameters.

        Args:
            input: the input tensor.
            label: the optional label tensor.
            module: any torch Module but only kornia augmentation modules will count
                to apply transformations.
            param: the corresponding parameters to the module.
        NNotImplementedErrorclsr0   r1   r    r!   r2   r+   r+   r,   apply_trans$   s    z!ApplyInverseInterface.apply_transNr0   r    r!   r2   r"   c                 C   s   t dS )3  Inverse a transformation with respect to the parameters.

        Args:
            input: the input tensor.
            module: any torch Module but only kornia augmentation modules will count
                to apply transformations.
            param: the corresponding parameters to the module.
        Nr4   r7   r0   r    r!   r2   r+   r+   r,   inverse9   s    zApplyInverseInterface.inverse)__name__
__module____qualname____doc__classmethodr   r   r
   nnModuler   r   strr   r   r8   r<   r+   r+   r+   r,   r.   !   s&   
r.   )	metaclassc                   @   s   e Zd ZU dZeed< ei feee e	j
eeeef eeee f dddZedi fee	j
ee eeef eddd	Zedi fee	j
ee eeef ee d
ddZeeedddZdS )ApplyInverseImplz*Standard matrix apply and inverse methods.
apply_funcr/   c                 C   s   t |dr"|jdur"tt|j}n| j||||d}tj||j|jd}d}t	|t
r`|jd }t	|tjjrtj|jd |j|jd }|dur|dur| dkr| dkr| || || ||< ||fS )r3   transform_matrixNr2   devicedtypeZ
batch_probr   )hasattrrH   r   r   _get_transformationtorch	as_tensorrK   rL   r%   r   datakorniaaugmentationImageSequentialZonesshapeboolsumZnumelrG   )r7   r0   r1   r    r!   r2   matZto_applyr+   r+   r,   r8   N   s    

(zApplyInverseImpl.apply_transNr9   c                 C   sz   t |dr"|jdur"tt|j}n| j||||d}tj||j|jd}|durv| 	|}| 
tj||j|jd|}|S )r:   rH   NrI   rJ   )rM   rH   r   r   rN   rO   rP   rK   rL   _get_inverse_transformationrG   )r7   r0   r    r!   r2   rX   	transformr+   r+   r,   r<   r   s    
zApplyInverseImpl.inverse)r0   r    maybe_paramr2   r"   c           	      C   s   t |ttjjfr*|d u r*td| dd }t|t}t |trtt|jt	t
tf }t|j|}|j|||d}n:t |tjjr| st|jtt }|j||d|d}n |S )Nz(Parameters of transformation matrix for z has not been computed.)flagsF)Z	recomputer2   )r%   r   rR   rS   rT   
ValueErrorr   r   rQ   r   rD   r   r   r\   Zget_transformation_matrixis_intensity_onlyr	   )	r7   r0   r    r[   r2   Z	maybe_matr!   Z
param_datar\   r+   r+   r,   rN      s    

z$ApplyInverseImpl._get_transformation)rZ   r"   c                 C   s   t |S r#   r   )r7   rZ   r+   r+   r,   rY      s    z,ApplyInverseImpl._get_inverse_transformation)r=   r>   r?   r@   r   __annotations__rA   r   r
   rB   rC   r   r   rD   r   r   r8   r<   rN   rY   r+   r+   r+   r,   rF   I   s4   

#rF   c                   @   s~   e Zd ZdZejZei fee	e e
jeeeef eee	e f dddZedi fee
je	e eeef edddZdS )	InputApplyInversez<Apply and inverse transformations for (image) input tensors.r/   c                 C   s  t |tfr"||||jd\}}nt |ttfrH||fd|ji|}nt |tjjr|j}|j	}t
|_d|_	t |tjjr||||j| jgd\}}n||||j|d\}}||_||_	nP|jdurtd|j d| d	t |ttfr||d
 |d f}n||}||fS )r3   r1   paramsrb   T)r1   rb   Z	data_keys)r1   rb   r2   NzNon-augmentaion operation z require empty parameters. Got .r      )r%   r   rQ   r   r   rR   rS   rT   apply_inverse_funcreturn_labelr`   AugmentationSequentialdata_keyAssertionErrornametuplelist)r7   r0   r1   r    r!   r2   tempZtemp2r+   r+   r,   r8      s(    
zInputApplyInverse.apply_transNr9   c                 C   s   t |tr0|j||du rdn
tt|j|d}n|t |tjjr|j	}t
|_	t |tjjrtt|j||du rndn
tt|jd}n$|j||du rdn
tt|j|d}||_	|S )r:   N)rb   r2   rb   )r%   r   r<   r   r   rQ   rR   rS   rT   re   r`   rg   r   r	   r7   r0   r    r!   r2   rm   r+   r+   r,   r<      s    
*zInputApplyInverse.inverse)r=   r>   r?   r@   r   INPUTrh   rA   r   r
   rB   rC   r   r   rD   r   r   r8   r<   r+   r+   r+   r,   r`      s$   
*r`   c                   @   s   e Zd ZdZejZededddZ	edi fe
ee
 ejee eeef ee
ee
 f ddd	Zedi fe
ejee eeef e
d
ddZdS )MaskApplyInversez3Apply and inverse transformations for mask tensors.r   )r    r"   c                    s    fdd}|S )z@Disable all other additional inputs (e.g. ) for ImageSequential.c                     s$    j }d _  | i |}| _ |S )NF)rf   )argskwargsZif_return_labeloutr    r+   r,   f   s
    z6MaskApplyInverse.make_input_only_sequential.<locals>.fr+   )r7   r    rv   r+   ru   r,   make_input_only_sequential   s    z+MaskApplyInverse.make_input_only_sequentialNr/   c           	      C   s   |dur|j }nd}t|ttfrhttttf | }d|v rRt	
|d |d< ||fd|i|}nVt|tjjr| sttt |}|j}t|_t||}| ||d|d}||_n ||fS )r3   Nvaluesrb   ra   )rQ   r%   r   r   r   r   rD   r   copyrO   Z
zeros_likerR   rS   rT   r^   r	   r   re   rq   r-   rw   )	r7   r0   r1   r    r!   r2   _paramrm   Z	geo_paramr+   r+   r,   r8     s     
zMaskApplyInverse.apply_transr9   c                 C   s|   t |tr6|j|fd|du r dn
tt|ji|}nBt |tjjrx|j	}t
|_	|j||du rbdn
tt|jd}||_	|S )r:   rb   Nrn   )r%   r   r<   r   r   rQ   rR   rS   rT   re   rq   r	   ro   r+   r+   r,   r<   /  s    
,"zMaskApplyInverse.inverse)r=   r>   r?   r@   r   MASKrh   rA   r   rw   r   r
   rB   rC   r   r   rD   r   r   r8   r<   r+   r+   r+   r,   rq      s*   
(rq   c                       s   e Zd ZdZeejee ee	 dddZ
ee	e	e	dddZee	e	e	ddd	Zeed
ddZei fe	ee	 ejeeeef ee	ee	 f d fddZedi fe	ejee eeef e	d fddZ  ZS )BBoxApplyInversez}Apply and inverse transformations for bounding box tensors.

    This is for transform boxes in the format (B, N, 4, 2).
    r   c                 C   s,   t |tr(ttttf |j}|dS d S )Npadding_size)r%   r   r   r   rD   r   rQ   get)r7   r    r!   rz   r+   r+   r,   _get_padding_sizeL  s    

z"BBoxApplyInverse._get_padding_sizer0   r}   r"   c                 C   s   t |jdvrt|jt |jdkr0t|j| }| dkrT|dddf }|d  |ddddf 7  < |d  |ddddf 7  < | dkr|ddd	f }|S 
zX
        Args:
            input: (B, N, 4, 2)
            padding_size: (B, 4)
        )         r   N.r   .rd   .rd   r   lenrU   ri   cloneZdimr7   r0   r}   _inputr+   r+   r,   padS  s    

zBBoxApplyInverse.padc                 C   s   t |jdvrt|jt |jdkr0t|j| }| dkrT|dddf }|d  |ddddf 8  < |d  |ddddf 8  < | dkr|ddd	f }|S r   r   r   r+   r+   r,   unpadn  s    

zBBoxApplyInverse.unpadxyxyTmodeZrestore_coordinatesr/   c                    sP   |  }| ||}|dur.| |||}t j|||||d\}}||fS )az  Apply a transformation with respect to the parameters.

        Args:
            input: the input tensor, (B, N, 4, 2) or (B, 4, 2).
            label: the optional label tensor.
            module: any torch Module but only kornia augmentation modules will count
                to apply transformations.
            param: the corresponding parameters to the module.
        NrI   )r   r   r   tosuperr8   )r7   r0   r1   r    r!   r2   r   r}   	__class__r+   r,   r8     s    zBBoxApplyInverse.apply_transNr9   c                    sF   |  }t j||||d}| ||}|durB| |||}|S )r:   rI   N)r   r   r<   r   r   r   )r7   r0   r    r!   r2   r   r}   r   r+   r,   r<     s    zBBoxApplyInverse.inverse)r=   r>   r?   r@   rA   rB   rC   r
   r   r   r   r   r   r   r   rG   r   rD   r   r   r8   r<   __classcell__r+   r+   r   r,   r|   F  s0   
r|   c                       s   e Zd ZdZeedddZedd Zedd Z	ei fe
ee
 ejeeeef ee
ee
 f d	 fd
dZedi fe
ejee eeef e
d fddZ  ZS )BBoxXYXYApplyInversezApply and inverse transformations for bounding box tensors.

    This is for transform boxes in the format [xmin, ymin, xmax, ymax].
    r   Tr   c                 C   sp   | |}tt|D ]T}||d d dd df  || d 7  < ||d d dd df  || d 7  < q|S Nr   r   rd   r   ranger   r7   r0   r}   Z_padding_sizeir+   r+   r,   r     s
    
(*zBBoxXYXYApplyInverse.padc                 C   sp   | |}tt|D ]T}||d d dd df  || d 8  < ||d d dd df  || d 8  < q|S r   r   r   r+   r+   r,   r     s
    
(*zBBoxXYXYApplyInverse.unpadr/   c                    s    t d t j|||||dS )NRBBoxXYXYApplyInverse is no longer maintained. Please use BBoxApplyInverse instead.)r1   r    r!   r2   )warningswarnr   r8   r6   r   r+   r,   r8     s    	
z BBoxXYXYApplyInverse.apply_transNr9   c                    s   t d t j||||dS )Nr   r    r!   r2   )r   r   r   r<   r;   r   r+   r,   r<     s    
zBBoxXYXYApplyInverse.inverse)r=   r>   r?   r@   r   r   rG   rA   r   r   r   r
   rB   rC   r   r   rD   r   r   r8   r<   r   r+   r+   r   r,   r     s,   


r   c                   @   s6   e Zd ZdZeedddZedd Zedd Z	d	S )
BBoxXYWHApplyInversezApply and inverse transformations for bounding box tensors.

    This is for transform boxes in the format [xmin, ymin, width, height].
    ZxywhTr   c                 C   sd   | |}tt|D ]H}||d d df  || d 7  < ||d d df  || d 7  < q|S Nr   rd   r   r   r   r+   r+   r,   r     s
    
"$zBBoxXYWHApplyInverse.padc                 C   sd   | |}tt|D ]H}||d d df  || d 8  < ||d d df  || d 8  < q|S r   r   r   r+   r+   r,   r     s
    
"$zBBoxXYWHApplyInverse.unpadN)
r=   r>   r?   r@   r   r   rG   rA   r   r   r+   r+   r+   r,   r     s   
r   c                   @   sD   e Zd ZdZeeZeeeedddZ	eeeedddZ
dS )KeypointsApplyInversez{Apply and inverse transformations for keypoints tensors.

    This is for transform keypoints in the format (B, N, 2).
    r   c                 C   s   t |jdvrt|jt |jdkr0t|j| }| dkrT|d d d f }|d  |dd df 7  < |d  |dddf 7  < | dkr|d d df }|S 	N)r   r   r   r   .rd   r   r   r   r   r   r+   r+   r,   r     s    

zKeypointsApplyInverse.padc                 C   s   t |jdvrt|jt |jdkr0t|j| }| dkrT|d d d f }|d  |dd df 8  < |d  |dddf 8  < | dkr|d d df }|S r   r   r   r+   r+   r,   r   '  s    

zKeypointsApplyInverse.unpadN)r=   r>   r?   r@   r   r   rG   rA   r   r   r   r+   r+   r+   r,   r     s   r   c                   @   s   e Zd ZdZeeeeef e	e
 dddZeeji feee ejeeeeef eeef eeee f dddZedeji feejee eeeef eeef ed	d
dZdS )ApplyInversezKApply and inverse transformations for any tensors (e.g. mask, box, points).)dcater"   c                 C   sp   t |t jkrtS t |t jkr(tS t |t jt jt jfv rFt	S t |t j
fv r\tS td| dd S )Nzinput type of z is not implemented.)r   r~   rp   r`   r{   rq   ZBBOXZ	BBOX_XYXYZ	BBOX_XYWHr|   Z	KEYPOINTSr   r5   )r7   r   r+   r+   r,   _get_func_by_keyB  s    zApplyInverse._get_func_by_key)r0   r1   r    r!   r   r2   r"   c                 C   sP   |  |}t|tfr<||d ||||g|dd R S |j|||||dS )a  Apply a transformation with respect to the parameters.

        Args:
            input: the input tensor.
            label: the optional label tensor.
            module: any torch Module but only kornia augmentation modules will count
                to apply transformations.
            param: the corresponding parameters to the module.
            dcate: data category. 'input', 'mask', 'bbox', 'bbox_xyxy', 'bbox_xyhw', 'keypoints'.
                By default, it is set to 'input'.
        r   rd   Nr   )r   r%   rk   r8   )r7   r0   r1   r    r!   r   r2   funcr+   r+   r,   apply_by_keyO  s    
&zApplyInverse.apply_by_keyN)r0   r    r!   r   r2   r"   c                 C   s   |  |}|j||||dS )a  Inverse a transformation with respect to the parameters.

        Args:
            input: the input tensor.
            module: any torch Module but only kornia augmentation modules will count
                to apply transformations.
            param: the corresponding parameters to the module.
            dcate: data category. 'input', 'mask', 'bbox', 'bbox_xyxy', 'bbox_xyhw', 'keypoints'.
                By default, it is set to 'input'.
        rI   )r   r<   )r7   r0   r    r!   r   r2   r   r+   r+   r,   inverse_by_keyk  s    
zApplyInverse.inverse_by_key)r=   r>   r?   r@   rA   r   rD   intr   r   r.   r   rp   r   r
   rB   rC   r   r   r   r   r   r   r+   r+   r+   r,   r   ?  s6    

r   )7r   abcr   r   	functoolsr   typingr   r   r   r   r	   r
   r   r   r   r   rO   Ztorch.nnrB   r   rR   Zkornia.augmentationr   r   r   r   Z kornia.augmentation._2d.mix.baser   Zkornia.augmentation.baser   Z"kornia.augmentation.container.baser   Zkornia.augmentation.utilsr   Zkornia.constantsr   Zkornia.geometry.bboxr   Zkornia.geometry.linalgr   Zkornia.testingr   Zkornia.utils.helpersr   r-   r.   rF   r`   rq   r|   r   r   r   r   r+   r+   r+   r,   <module>   s6   0
(^MRz,8