o
    oif                     @   s  d dl Z d dlmZmZ d dlmZmZmZmZ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 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 eee	e eef Z ee	e e	e	e  e	e e	e f Z!edZ"G dd dee" edZ#G dd dZ$edZ%dddee%ef fddZ&ddde	e de	e fddZ'G dd de#e Z(G dd de#e Z)G dd  d e#e Z*G d!d" d"e#e Z+G d#d$ d$e#e Z,dS )%    N)ABCMetaabstractmethod)	AnyCallableDictGenericListOptionalTypeTypeVarUnion)	ParamSpec)_AugmentationBase)DataKey)ModuleTensor)Boxes)	Keypoints   )	ParamItemTc                   @   s   e Zd ZdZededeeef fddZ	edede
e fddZeedd	ed
ededeeeef  def
ddZeedd	ed
ededeeeef  def
ddZdS )SequentialOpsInterfacez>Abstract interface for applying and inversing transformations.paramreturnc                 C   0   t |trt |jtr|j}|S td| d)Nz8Expected param (ParamItem.data) be a dictionary. Gotcha .)
isinstancer   datadict	TypeErrorclsr   _params r#   U/home/ubuntu/.local/lib/python3.10/site-packages/kornia/augmentation/container/ops.pyget_instance_module_param,      z0SequentialOpsInterface.get_instance_module_paramc                 C   r   )Nz2Expected param (ParamItem.data) be a list. Gotcha r   )r   r   r   listr   r    r#   r#   r$   get_sequential_module_param4   r&   z2SequentialOpsInterface.get_sequential_module_paramNinputmodule
extra_argsc                 C      t )  Apply 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.
            extra_args: Optional dictionary of extra arguments with specific options for different input types.
        NotImplementedErrorr!   r)   r*   r   r+   r#   r#   r$   	transform<      z SequentialOpsInterface.transformc                 C   r,   )  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.
            extra_args: Optional dictionary of extra arguments with specific options for different input types.
        r.   r0   r#   r#   r$   inverseJ   r2   zSequentialOpsInterface.inverseN)__name__
__module____qualname____doc__classmethodr   r   strr   r%   r   r(   r   r   r   r	   r   r1   r4   r#   r#   r#   r$   r   )   s    04r   )	metaclassc                   @   sr  e Zd Zdeee  ddfddZedeee  fddZej	dee
ee ee ee f  ddfddZddee
ee ee ee f  dee fd	d
Zdedeee  fddZdddedededeeeeef f dee
ee ee ee f  de
eef fddZdddedededeeeeef f dee
ee ee ee f  de
eef fddZdS )AugmentationSequentialOps	data_keysr   Nc                 C   s
   || _ d S r5   
_data_keysselfr>   r#   r#   r$   __init__Z   s   
z"AugmentationSequentialOps.__init__c                 C   s   | j S r5   r?   )rB   r#   r#   r$   r>   ]   s   z#AugmentationSequentialOps.data_keysc                 C   s"   |rdd |D | _ d S d | _ d S )Nc                 S      g | ]}t |qS r#   r   get.0inpr#   r#   r$   
<listcomp>d       z7AugmentationSequentialOps.data_keys.<locals>.<listcomp>r?   rA   r#   r#   r$   r>   a   s   
c                 C   s0   |d u rt | jtr| jS tddd |D S )Nz5Sequential ops needs data keys to be able to process.c                 S   rD   r#   rE   rG   r#   r#   r$   rJ   n   rK   z>AugmentationSequentialOps.preproc_datakeys.<locals>.<listcomp>)r   r>   r'   
ValueErrorrA   r#   r#   r$   preproc_datakeysh   s
   z*AugmentationSequentialOps.preproc_datakeysdata_keyc                 C   sb   |t jkrtS |t jkrtS |t jt jt jhv rtS |t j	kr!t
S |t jkr(tS td|j d)z4Return the corresponding operation given a data key.zOperation for `z` is not found.)r   INPUTInputSequentialOpsMASKMaskSequentialOpsBBOX	BBOX_XYWH	BBOX_XYXYBoxSequentialOps	KEYPOINTSKeypointSequentialOpsCLASSClassSequentialOpsRuntimeErrorname)rB   rN   r#   r#   r$   _get_opp   s   



z!AugmentationSequentialOps._get_op)r>   argr*   r   r+   c             	   G   s   |  |}t|tjrt|j|j|||j|dd}g }t||D ]2\}}	| 	|	}
|
|	i }|	jdkrHt|trH|tj||||d q"||
j||||d q"t|dkrft|ttfrf|d S |S )N)r>   paramsr+   )r\   r   rQ   r   r+   r   r   )rM   r   KRandomTransplantationr   r\   params_from_inputr   zipr]   rF   r'   appendrR   transform_listr1   lentuplerB   r*   r   r+   r>   r^   r@   outputsrI   dcateop	extra_argr#   r#   r$   r1   ~   s*   


z#AugmentationSequentialOps.transformc             	   G   s~   |  |}g }t||D ]\}}	| |	}
|	|v r||	 ni }||
j||||d qt|dkr=t|ttfr=|d S |S )Nr`   r   r   )	rM   rd   r]   re   r4   rg   r   r'   rh   ri   r#   r#   r$   r4      s   

z!AugmentationSequentialOps.inverser5   )r6   r7   r8   r	   r   r   rC   propertyr>   setterr   r;   intrM   r
   r   r   r]   DataTyper   r   r   SequenceDataTyper1   r4   r#   r#   r#   r$   r=   Y   sH    .2

(
r=   Pr*   zK.container.ImageSequentialBaser   c                    s"   dt jdt jdtf fdd}|S )z@Disable all other additional inputs (e.g. ) for ImageSequential.argskwargsr   c                     s    | i |S r5   r#   )rt   ru   r*   r#   r$   f   s   z%make_input_only_sequential.<locals>.f)rs   rt   ru   r   )r*   rw   r#   rv   r$   make_input_only_sequential   s   rx   r   c                 C   sF   |  |}g }t||D ]\\}}}t|tjtjfr || q|S )zReturn geometry param.)get_forward_sequencerd   r   ra   GeometricAugmentationBase2DGeometricAugmentationBase3Dre   )r*   r   named_modulesres_modpr#   r#   r$   get_geometric_only_param   s   

r   c                   @   sp   e Zd Ze	ddedededeee	e
f  def
ddZe	ddedededeee	e
f  def
d	d
ZdS )rP   Nr)   r*   r   r+   r   c                 C   s   |d u ri }t |ttjfr ||f| |tjgd|}|S t |tjjfr5|j	|| 
||d}|S t |tjjjfrI||| |d}|S |jd urZtd|j d| d||}|S )Nr_   r>   r_   r+   )r_   zNon-augmentaion operation z require empty parameters. Got r   )r   r   ra   MixAugmentationBaseV2r%   r   rO   	containerImageSequentialBasetransform_inputsr(   auto
operationsOperationBaser   AssertionErrorr\   r0   r#   r#   r$   r1      s    	
zInputSequentialOps.transformc                 C   s   |d u ri }t |tjr|j|fd| |i|}|S t |tjfr'tdt |tjjj	fr:t
j||j||dS t |tjrQ| sQ|j|| ||d}|S t |tjjrc|j|| ||d}|S )Nr_   zfThe support for 3d inverse operations are not yet supported. You are welcome to file a PR in our repo.r*   r   r+   r   )r   ra   rz   r4   r%   r{   r/   r   r   r   rP   rl   ImageSequentialis_intensity_onlyinverse_inputsr(   r   r   r0   r#   r#   r$   r4      s"   zInputSequentialOps.inverser5   )r6   r7   r8   r:   r   r   r   r	   r   r;   r   r1   r4   r#   r#   r#   r$   rP      s6    rP   c                   @   t   e Zd ZdZe	ddedededee	e
ef  def
dd	Ze	ddedededee	e
ef  def
d
dZdS )rZ   z=Apply and inverse transformations for class labels if needed.Nr)   r*   r   r+   r   c                 C   s   t |tjr
td|S )NzdThe support for class labels for mix augmentations that change the class label is not yet supported.)r   ra   r   r/   r0   r#   r#   r$   r1      s
   zClassSequentialOps.transformc                 C   s   |S r5   r#   r0   r#   r#   r$   r4      s   zClassSequentialOps.inverser5   )r6   r7   r8   r9   r:   r   r   r   r	   r   r;   r   r1   r4   r#   r#   r#   r$   rZ      s8    	rZ   c                   @   s   e Zd ZdZe	ddedededee	e
ef  def
dd	Ze	ddee dededee	e
ef  dee f
d
dZe	ddedededee	e
ef  def
ddZdS )rR   z3Apply and inverse transformations for mask tensors.Nr)   r*   r   r+   r   c                 C   s6  |du ri }t |tjfr!|j|f| ||j|jd|}|S t |tjfr,tdt |tj	rD||f| |t
jgd|}|S t |tr[|j|f| ||jd|}|S t |tjrr| sr|j|| ||d}|S t |tjjr|j|| ||d}|S t |tjjjfrtj||j||d}|S )r-   Nr_   flagsr1   cThe support for 3d mask operations are not yet supported. You are welcome to file a PR in our repo.r   r_   r   r   r   )r   ra   rz   transform_masksr%   r   transform_matrixr{   r/   rb   r   rQ   r   r   r   r(   r   r   r   r   r   rR   r1   rl   r0   r#   r#   r$   r1   	  sL    
zMaskSequentialOps.transformc                 C   s  |du ri }t |tjfrBg }| |}t|}t|D ] \}}	|d | |d< |j|	f||j|j	d|}
|
|
 q|}|S t |tjfrMtdt |trg }| |}t|}t|D ]\}}	|d | |d< |j|	f||jd|}
|
|
 qb|}|S t |tjr| sg }| |}|D ]}	|j|	||d}
|
|
 q|}|S t |tjjrg }| |}|D ]}	|j|	||d}
|
|
 q|}|S t |tjjjfrtd|S )a  Apply a transformation with respect to the parameters.

        Args:
            input: list of input tensors.
            module: any torch Module but only kornia augmentation modules will count
                to apply transformations.
            param: the corresponding parameters to the module.
            extra_args: Optional dictionary of extra arguments with specific options for different input types.
        N
batch_probr   r   r   r   ztThe support for list of masks under auto operations are not yet supported. You are welcome to file a PR in our repo.)r   ra   rz   r%   copydeepcopy	enumerater   r   r   re   r{   r/   r   r   r   r(   r   r   r   r   r   )r!   r)   r*   r   r+   	tfm_inputr_   params_iirI   tfm_inp
seq_paramsr#   r#   r$   rf   :  sj   


&




z MaskSequentialOps.transform_listc                 C   s   |du ri }t |tjfr4|jdu rtd|j d||j}|j|f| ||j	|d|}|S t |tj
fr?tdt |tjjrS|j|| ||d}|S t |tjjjfrftj||j||d}|S )r3   N(No valid transformation matrix found in r   r   r   r   r   )r   ra   rz   r   rL   	__class__compute_inverse_transformationinverse_masksr%   r   r{   r/   r   r   r(   r   r   r   rR   r4   rl   )r!   r)   r*   r   r+   r1   r#   r#   r$   r4   {  s4   
zMaskSequentialOps.inverser5   )r6   r7   r8   r9   r:   r   r   r   r	   r   r;   r   r1   r   rf   r4   r#   r#   r#   r$   rR     sR    0@rR   c                   @   r   )rV   z}Apply and inverse transformations for bounding box tensors.

    This is for transform boxes in the format (B, N, 4, 2).
    Nr)   r*   r   r+   r   c                 C      |du ri }|  }t|tjfr%|j|| ||jfd|ji|}|S t|tjfr0t	dt|tj
rG| sG|j|| ||d}|S t|tjjr[|j|| ||d}|S t|tjjjfrntj||j||dS |S )  Apply a transformation with respect to the parameters.

        Args:
            input: the input tensor, (B, N, 4, 2) or (B, 4, 2).
            module: any torch Module but only kornia augmentation modules will count
                to apply transformations.
            param: the corresponding parameters to the module.
            extra_args: Optional dictionary of extra arguments with specific options for different input types.
        Nr1   bThe support for 3d box operations are not yet supported. You are welcome to file a PR in our repo.r   r   )cloner   ra   rz   transform_boxesr%   r   r   r{   r/   r   r   r(   r   r   r   r   r   rV   r1   rl   r!   r)   r*   r   r+   _inputr#   r#   r$   r1     s>   zBoxSequentialOps.transformc                 C   s  |du ri }|  }t|tjfr6|jdu rtd|j d||j}|j||j	|j
fd|i|}|S t|tjfrAtdt|tjrX| sX|j|| ||d}|S t|tjjrl|j|| ||d}|S t|tjjjfrtj||j||dS |S )r3   Nr   r   r1   r   r   r   )r   r   ra   rz   r   rL   r   r   inverse_boxesr   r   r{   r/   r   r   r(   r   r   r   r   r   rV   r4   rl   r!   r)   r*   r   r+   r   r1   r#   r#   r$   r4     s<   
zBoxSequentialOps.inverser5   )r6   r7   r8   r9   r:   r   r   r   r	   r   r;   r   r1   r4   r#   r#   r#   r$   rV     s8    -rV   c                   @   r   )rX   z{Apply and inverse transformations for keypoints tensors.

    This is for transform keypoints in the format (B, N, 2).
    Nr)   r*   r   r+   r   c                 C   r   )r   Nr1   gThe support for 3d keypoint operations are not yet supported. You are welcome to file a PR in our repo.r   r   )r   r   ra   rz   transform_keypointsr%   r   r   r{   r/   r   r   r(   r   r   r   r   r   rX   r1   rl   r   r#   r#   r$   r1     s>   zKeypointSequentialOps.transformc                 C   s  |du ri }|  }t|tjfr8|jdu rtd|j d||j}|j|| 	||j
fd|i|}|S t|tjfrCtdt|tjrZ| sZ|j|| ||d}|S t|tjjrn|j|| ||d}|S t|tjjjfrtj||j||dS |S )r3   Nr   r   r1   r   r   r   )r   r   ra   rz   r   rL   r   r   inverse_keypointsr%   r   r{   r/   r   r   r(   r   r   r   r   r   rX   r4   rl   r   r#   r#   r$   r4   ;  s@   
zKeypointSequentialOps.inverser5   )r6   r7   r8   r9   r:   r   r   r   r	   r   r;   r   r1   r4   r#   r#   r#   r$   rX     s8    .rX   )-r   abcr   r   typingr   r   r   r   r   r	   r
   r   r   typing_extensionsr   kornia.augmentationaugmentationra   kornia.augmentation.baser   kornia.constantsr   kornia.corer   r   kornia.geometry.boxesr   kornia.geometry.keypointsr   r_   r   rq   rr   r   r   r=   rs   rx   r   rP   rZ   rR   rV   rX   r#   r#   r#   r$   <module>   s2   ,$0Z	(  a