o
    oi;.                     @   sx   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
mZ d dlmZ d dlmZ d dlmZ G dd	 d	eZd
S )    )AnyDictOptionalTuple)RigidAffineAugmentationBase2D)Resample)Tensor	as_tensor)Boxes)	Keypoints_torch_inverse_castc                   @   s  e Zd ZdZ		d,dedeeef dee dee	e
e
f  def
dd	Zdedefd
dZ	d,dedeeeef  deeeef  defddZ	d-dedeeef deeef dee def
ddZ	d-dedeeef deeef dee def
ddZ	d-dedeeef deeef dee def
ddZ	d-dedeeef deeef dee def
ddZ	d-dedeeef deeef dee def
ddZ	d-dedeeef deeef dee def
ddZ	d-dedeeef deeef dee def
ddZ	d-dedeeef deeef dee def
ddZ	d-dedeeef deeef dee dedefd d!Z	d-dedeeef deeef dee dedefd"d#Z	d-dedeeef deeef dee dedefd$d%Z	d-dedeeef deeef dee dedefd&d'Z	d-dedeeef deeef dee dedefd(d)Zd-dedeeeef  dedefd*d+ZdS ).GeometricAugmentationBase2DaT  GeometricAugmentationBase2D base class for customized geometric augmentation implementations.

    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``.

    Ninputflags	transformsizereturnc                 C   s   t )zIBy default, the exact transformation as ``apply_transform`` will be used.)NotImplementedError)selfr   r   r   r    r   Z/home/ubuntu/.local/lib/python3.10/site-packages/kornia/augmentation/_2d/geometric/base.pyinverse_transform*      z-GeometricAugmentationBase2D.inverse_transformc                 C   s   t |S )z?Compute the inverse transform of given transformation matrices.r   )r   r   r   r   r   compute_inverse_transformation4   s   z:GeometricAugmentationBase2D.compute_inverse_transformationparamsc                 C   sh   |du r| j n|}|dur| |||}n| jdu r(| |j}| |||}n| j}t||j|jdS )zObtain transformation matrices.

        Return the current transformation matrix if existed. Generate a new one, otherwise.
        N)devicedtype)r   generate_transformation_matrixtransform_matrixforward_parametersshaper	   r   r   r   r   r   r   r   r   r   r   get_transformation_matrix8   s   
z5GeometricAugmentationBase2D.get_transformation_matrixc                 C      |S )zMProcess masks corresponding to the inputs that are no transformation applied.r   r"   r   r   r   apply_non_transform_maskI      z4GeometricAugmentationBase2D.apply_non_transform_maskc                 C   sB   d|v r|d }t d|d< | ||||}|dur||d< |S )zProcess masks corresponding to the inputs that are transformed.

        Note:
            Convert "resample" arguments to "nearest" by default.

        resamplenearestN)r   getapply_transform)r   r   r   r   r   resample_methodoutputr   r   r   apply_transform_maskO   s   
z0GeometricAugmentationBase2D.apply_transform_maskc                 C   r$   )zMProcess boxes corresponding to the inputs that are no transformation applied.r   r"   r   r   r   apply_non_transform_boxa   r&   z3GeometricAugmentationBase2D.apply_non_transform_boxc                 C   :   |du r| j du rtd| j }| ||||}||S )z?Process boxes corresponding to the inputs that are transformed.NRNo valid transformation matrix found. Please either pass one or forward one first.)r   RuntimeErrorr.   transform_boxes_r"   r   r   r   apply_transform_boxg      

z/GeometricAugmentationBase2D.apply_transform_boxc                 C   r$   )zQProcess keypoints corresponding to the inputs that are no transformation applied.r   r"   r   r   r   apply_non_transform_keypointr   r&   z8GeometricAugmentationBase2D.apply_non_transform_keypointc                 C   r/   )zCProcess keypoints corresponding to the inputs that are transformed.Nr0   )r   r1   r5   transform_keypoints_r"   r   r   r   apply_transform_keypointx   r4   z4GeometricAugmentationBase2D.apply_transform_keypointc                 C   r$   )zRProcess class tags corresponding to the inputs that are no transformation applied.r   r"   r   r   r   apply_non_transform_class   r&   z5GeometricAugmentationBase2D.apply_non_transform_classc                 C   r$   )zDProcess class tags corresponding to the inputs that are transformed.r   r"   r   r   r   apply_transform_class   r&   z1GeometricAugmentationBase2D.apply_transform_classkwargsc                 K   s   |  |}| }|d }|dk}	| j|d u r| jn||fi |\}}d }
d|v r8|d  }|d |d f}
|	 s@|}|S |	 rO| j||||
d}|S | j||	 |d ur\||	 n||
|d||	< |S )N
batch_prob      ?forward_input_shape)r   r   r   )r   r   r   )transform_tensorclone#_process_kwargs_to_params_and_flags_paramstolistanyallr   )r   r   r   r   r   r:   	in_tensorr,   r;   to_applyr   _sizer   r   r   inverse_inputs   s4   


z*GeometricAugmentationBase2D.inverse_inputsc                 K   sN   d }d|v r|d }t d|d< | j||||fi |}|d ur%||d< |S )Nr'   r(   )r   r)   rJ   )r   r   r   r   r   r:   r+   r,   r   r   r   inverse_masks   s   z)GeometricAugmentationBase2D.inverse_masksc           	      K   s   |  }|d }|dk}|d u rtd| j|d u r| jn||fi |\}}| s/|}|S | r:||}|S || || ||< |S )Nr;   r<   )`transform` has to be a tensor. Got None.)rA   r1   rB   rC   rE   rF   r2   	r   r   r   r   r   r:   r,   r;   rH   r   r   r   inverse_boxes   s$   

z)GeometricAugmentationBase2D.inverse_boxesc           	      K   s   |  }|d }|dk}|du rtd| j|du r| jn||fi |\}}| s/|}|S | r:||}|S || || ||< |S )aA  Inverse the transformation on keypoints.

        Args:
            input: input keypoints tensor or object.
            params: the corresponding parameters for an operation.
            flags: static parameters.
            transform: the inverse transformation matrix
            kwargs: additional arguments

        r;   r<   NrL   )rA   r1   rB   rC   rE   rF   r6   rM   r   r   r   inverse_keypoints   s$   

z-GeometricAugmentationBase2D.inverse_keypointsc                 K   r$   Nr   )r   r   r   r   r   r:   r   r   r   inverse_classes  r   z+GeometricAugmentationBase2D.inverse_classesc           	      K   s   |j }| |}| j|du r| jn|| jfi |\}}|du r#| j}| j|||d}| |}| ||||}| jrA| 	||S |S )a*  Perform inverse 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.

        N)r   r   )
r!   r@   rB   rC   r   r#   r   rJ   keepdimtransform_output_tensor)	r   r   r   r:   input_shaperG   r   r   r,   r   r   r   inverse  s   



z#GeometricAugmentationBase2D.inverse)NNrP   )__name__
__module____qualname____doc__r   r   strr   r   r   intr   r   r#   r%   r-   r
   r.   r3   r   r5   r7   r8   r9   rJ   rK   rN   rO   rQ   rU   r   r   r   r   r      s   






























+





#


-


,
r   N)typingr   r   r   r   kornia.augmentation._2d.baser   kornia.constantsr   kornia.corer   r	   kornia.geometry.boxesr
   kornia.geometry.keypointsr   kornia.utils.helpersr   r   r   r   r   r   <module>   s   