o
    oi                     @   s   d dl mZmZmZ d dlmZmZmZ d dlm	Z	 d dl
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mZ G d	d
 d
e	ZG dd deZdS )    )AnyDictOptional)float16float32float64)_AugmentationBase)_transform_input_transform_input_by_shape_validate_input_dtype)Tensor)Boxes)	Keypoints)eye_likeis_autocast_enabledc                	   @   sH   e Zd ZdZdeddfddZdddded	ee d
edefddZdS )AugmentationBase2Da$  AugmentationBase2D base class for customized augmentation implementations.

    AugmentationBase2D aims at offering a generic base class for a greater level of customization.
    If the subclass contains routined matrix-based transformations, `RigidAffineAugmentationBase2D`
    might be a better fit.

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

    inputreturnNc                 C   s6   t |tttgd t|jdkrtd|j ddS )z3Check if the input tensor is formatted as expected.accepted_dtypes   zExpect (B, C, H, W). Got .N)r   r   r   r   lenshapeRuntimeErrorselfr    r   P/home/ubuntu/.local/lib/python3.10/site-packages/kornia/augmentation/_2d/base.pyvalidate_tensor0   s   z"AugmentationBase2D.validate_tensorT)r   match_channelr   r    c                C   s0   t |tttgd |du rt|S t|||dS )zJConvert any incoming (H, W), (C, H, W) and (B, C, H, W) into (B, C, H, W).r   N)reference_shaper    )r   r   r   r   r	   r
   )r   r   r   r    r   r   r   transform_tensor6   s   z#AugmentationBase2D.transform_tensor)	__name__
__module____qualname____doc__r   r   r   boolr"   r   r   r   r   r      s    *r   c                   @   s  e Zd ZU dZee ed< edee fddZdedefddZ	ded	e
eef d
e
eef defddZded	e
eef d
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ef  defddZdS )RigidAffineAugmentationBase2Da  AugmentationBase2D base class for rigid/affine augmentation implementations.

    RigidAffineAugmentationBase2D enables routined transformation with given transformation matrices
    for different data types like masks, boxes, and keypoints.

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

    _transform_matrixr   c                 C   s   | j S N)r)   )r   r   r   r   transform_matrixS   s   z.RigidAffineAugmentationBase2D.transform_matrixr   c                 C   s
   t d|S )zReturn 3x3 identity matrix.   )r   r   r   r   r   identity_matrixW   s   
z-RigidAffineAugmentationBase2D.identity_matrixparamsflagsc                 C      t r*   NotImplementedError)r   r   r.   r/   r   r   r   compute_transformation[   s   z4RigidAffineAugmentationBase2D.compute_transformationc           
      C   s   |d }|dk}|  |}| s| |}|S | r&| j|||d}|S | |}| j|| ||d}	t rD||j}|	|j}	||f|	}|S )zIGenerate transformation matrices with the given input and param settings.
batch_probg      ?)r.   r/   )	r"   anyr-   allr3   r   typedtype	index_put)
r   r   r.   r/   r4   to_apply	in_tensortrans_matrixtrans_matrix_Atrans_matrix_Br   r   r   generate_transformation_matrix^   s    


z<RigidAffineAugmentationBase2D.generate_transformation_matrixN	transformc                 C   r0   r*   r1   r   r   r.   r/   r@   r   r   r   inverse_inputst      z,RigidAffineAugmentationBase2D.inverse_inputsc                 C   r0   r*   r1   rA   r   r   r   inverse_masksy   rC   z+RigidAffineAugmentationBase2D.inverse_masksc                 C   r0   r*   r1   rA   r   r   r   inverse_boxes~   rC   z+RigidAffineAugmentationBase2D.inverse_boxesc                 C   r0   r*   r1   rA   r   r   r   inverse_keypoints   rC   z/RigidAffineAugmentationBase2D.inverse_keypointsc                 C   r0   r*   r1   rA   r   r   r   inverse_classes   rC   z-RigidAffineAugmentationBase2D.inverse_classesr;   c                 C   s6   |d u r| j }| |||}| ||||}|| _|S r*   )r/   r?   transform_inputsr)   )r   r;   r.   r/   r<   outputr   r   r   
apply_func   s   z(RigidAffineAugmentationBase2D.apply_funcr*   )r#   r$   r%   r&   r   r   __annotations__propertyr+   r-   r   strr   r3   r?   rB   rD   r   rE   r   rF   rG   rJ   r   r   r   r   r(   @   s   
 **















r(   N)typingr   r   r   torchr   r   r   kornia.augmentation.baser   kornia.augmentation.utilsr	   r
   r   kornia.corer   kornia.geometry.boxesr   kornia.geometry.keypointsr   kornia.utilsr   r   r   r(   r   r   r   r   <module>   s   "