o
    oi(                     @   s   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
mZmZmZ d dlmZmZ d dlmZmZ d dlmZ d dlmZ G d	d
 d
eZdS )    )AnyDictListOptionalUnionN)_BasicAugmentationBase)_transform_input_transform_input_by_shape_transform_output_shape_validate_input_dtype)DataKeyDType)Tensortensor)KORNIA_UNWRAP)Boxesc                       sx  e Zd ZdZ			d;dededededeeee	e
ef   d	df fd
dZddddedee 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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eeef 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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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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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/d0Zdedee	ef dee	ef d	efd1d2Z ddd3dedeee	ef  deeee	e
ef   d	eeee f fd4d5Z!e"j#j$d6ed	ee fd7d8Z%e&d	ee fd9d:Z'  Z(S )<MixAugmentationBaseV2a  MixAugmentationBase base class for customized mix augmentation implementations.

    For any augmentation, the implementation of "generate_parameters" and "apply_transform" are required.
    "apply_transform" will need to handle the probabilities internally.

    Args:
        p: probability for applying an augmentation. This param controls if to apply the augmentation for the 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``.
        data_keys: the input type sequential for applying augmentations.
            Accepts "input", "image", "mask", "bbox", "bbox_xyxy", "bbox_xywh", "keypoints", "class", "label".

    FNpp_batchsame_on_batchkeepdim	data_keysreturnc                    s>   t  j||||d tjg| _|d urdd |D | _d S d S )N)r   r   r   c                 S      g | ]}t |qS  r   get.0inpr   r   T/home/ubuntu/.local/lib/python3.10/site-packages/kornia/augmentation/_2d/mix/base.py
<listcomp>@       z2MixAugmentationBaseV2.__init__.<locals>.<listcomp>)super__init__r   INPUTr   )selfr   r   r   r   r   	__class__r   r    r$   5   s
   
zMixAugmentationBaseV2.__init__T)shapematch_channelinputr)   r*   c                C   s6   t |tjtjtj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).)accepted_dtypesN)reference_shaper*   )r   torchfloat16float32float64r   r	   )r&   r+   r)   r*   r   r   r    transform_tensorB   s   z&MixAugmentationBaseV2.transform_tensorparamsflagsc                 C      t NNotImplementedErrorr&   r+   r3   r4   r   r   r    apply_transformK      z%MixAugmentationBaseV2.apply_transformc                 C      |S r6   r   r9   r   r   r    apply_non_transformO   r;   z)MixAugmentationBaseV2.apply_non_transformc           
      C   s   |d }|dk}|j }| |}|}t|t|kr!| |||}t|dkrA| |||}	| |||}||f| |	||}| jrKt||}|S |}|S N
batch_prob      ?r   )	r)   r2   sumlenr=   r:   	index_putr   r
   )
r&   r+   r3   r4   r?   to_apply	ori_shape	in_tensoroutputappliedr   r   r    transform_inputS   s   
z%MixAugmentationBaseV2.transform_inputc                 C   P   |d }|dk}|}t |t|kr| |||}t |dkr&| |||}|S r>   )rA   rB   apply_non_transform_maskapply_transform_maskr&   r+   r3   r4   r?   rD   rG   r   r   r    transform_maskb      z$MixAugmentationBaseV2.transform_maskc                 C   s   t |tr*t|jdkr|jdd  tddgks#td|j dt|ddd}|d }|d	k}|}t|t|krC| 	|||}t|d
krP| 
|||}|S )N      z&Only BxNx4x2 tensor is supported. Got .Fvertices_plus)moder?   r@   r   )
isinstancer   rB   r)   r.   SizeRuntimeErrorr   rA   apply_non_transform_boxesapply_transform_boxesrM   r   r   r    transform_boxesl   s   
*z%MixAugmentationBaseV2.transform_boxesc                 C   rJ   r>   )rA   rB   apply_non_transform_keypointapply_transform_keypointrM   r   r   r    transform_keypoint{   rO   z(MixAugmentationBaseV2.transform_keypointc                 C   rJ   r>   )rA   rB   apply_non_transform_classapply_transform_classrM   r   r   r    transform_class   rO   z%MixAugmentationBaseV2.transform_classc                 C   r5   r6   r7   r9   r   r   r    rK         z.MixAugmentationBaseV2.apply_non_transform_maskc                 C   r5   r6   r7   r9   r   r   r    rL      ra   z*MixAugmentationBaseV2.apply_transform_maskc                 C   r<   r6   r   r9   r   r   r    rX      ra   z/MixAugmentationBaseV2.apply_non_transform_boxesc                 C   r5   r6   r7   r9   r   r   r    rY      ra   z+MixAugmentationBaseV2.apply_transform_boxesc                 C   r<   r6   r   r9   r   r   r    r[      ra   z2MixAugmentationBaseV2.apply_non_transform_keypointc                 C   r5   r6   r7   r9   r   r   r    r\      ra   z.MixAugmentationBaseV2.apply_transform_keypointc                 C   r<   r6   r   r9   r   r   r    r^      ra   z/MixAugmentationBaseV2.apply_non_transform_classc                 C   r5   r6   r7   r9   r   r   r    r_      ra   z+MixAugmentationBaseV2.apply_transform_class)r3   r   c                G   s  |d u r| j }ndd |D }|d u r9|tj}|| }| |}| |j| _| jdt	t
|jji n|| _g }t||D ]\}}	|tjkrV| |	| j| j}
n|tjkre| |	| j| j}
n}|tjkrtj|	ddd}| || j| j}t|dt}
n^|tjkrtj|	ddd}| || j| j}t|dt}
n?|tjkrtj|	ddd}| || j| j}t|dt}
n |tjkr| |	| j| j}
n|tjkr| |	| j| j}
nt |!|
 qCt"|d	kr|d
 S |S )Nc                 S   r   r   r   r   r   r   r    r!      r"   z1MixAugmentationBaseV2.forward.<locals>.<listcomp>dtypeverticesF)rT   validate_boxesxyxyxywh   r   )#r   indexr   r%   r2   forward_parametersr)   _paramsupdater   r   r   rb   valueziprI   r4   MASKrN   BBOXr   from_tensorrZ   r   	to_tensorr   	BBOX_XYXY	BBOX_XYWH	KEYPOINTSr]   CLASSr`   r8   appendrB   )r&   r3   r   r+   keysin_tensor_idxrF   outputsdcate_inputrG   boxr   r   r    forward   sJ   
 






zMixAugmentationBaseV2.forwardkwargsc                 K      t d| jj d)NzInverse for  is not supported.rW   r(   __name__)r&   r~   r   r   r    inverse      zMixAugmentationBaseV2.inversec                 C   r   )NzTransformation matrices for r   r   )r&   r   r   r    transform_matrix   r   z&MixAugmentationBaseV2.transform_matrix)FFN))r   
__module____qualname____doc__floatboolr   r   r   strintr   r$   r   r2   r   r   r:   r=   rI   rN   r   rZ   r]   r`   rK   rL   rX   rY   r[   r\   r^   r_   r}   r.   jitignorer   propertyr   __classcell__r   r   r'   r    r   #   sb    &*	***2
**
*
*******
3r   )typingr   r   r   r   r   r.   kornia.augmentation.baser   kornia.augmentation.utilsr   r	   r
   r   kornia.constantsr   r   kornia.corer   r   kornia.core.checkr   kornia.geometry.boxesr   r   r   r   r   r    <module>   s   