o
    oid                     @   s   d dl 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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 eeef Zeeeee f Zee ZG d
d deeZ dS )    )AnyDictIteratorListOptionalTupleUnioncastN)OperationBase)PolicySequential)ImageSequentialBaseTransformMatrixMinIn)InputSequentialOps)	ParamItem)ModuleTensoreye_likec                       sd  e Zd ZdZd%dee deddf fddZd	eddfd
dZ	d& fddZ
dee dee fddZdedefddZdedefddZ			d'dedeee  dedeeeef  dee f
ddZd(deee  defddZdejdee fdd Z	d(dedee deeeef  defd!d"Z	d)dedeee  deeeef  defd#d$Z  ZS )*PolicyAugmentBasez Policy-based image augmentation.silencepolicytransformation_matrix_modereturnNc                    s,   |  |}t j|  | | tf| _d S N)compose_policysuper__init__!_parse_transformation_matrix_moder   $_valid_ops_for_transform_computation)selfr   r   policies	__class__ Q/home/ubuntu/.local/lib/python3.10/site-packages/kornia/augmentation/auto/base.pyr   &   s   

zPolicyAugmentBase.__init__modulec                 C   s   | j |j d S r   )_transform_matricesappendtransform_matrix)r   r%   r#   r#   r$   %_update_transform_matrix_for_valid_op,   s   z7PolicyAugmentBase._update_transform_matrix_for_valid_opc                    s   |    t  S r   )_reset_transform_matrix_stater   clear_stater   r!   r#   r$   r+   /   s   
zPolicyAugmentBase.clear_statec                    s    fdd|D S )z-Compose policy by the provided policy config.c                    s   g | ]}  |qS r#   )compose_subpolicy_sequential).0	subpolicyr,   r#   r$   
<listcomp>5   s    z4PolicyAugmentBase.compose_policy.<locals>.<listcomp>r#   )r   r   r#   r,   r$   r   3   s   z PolicyAugmentBase.compose_policyr/   c                 C   s   t r   )NotImplementedError)r   r/   r#   r#   r$   r-   7   s   z.PolicyAugmentBase.compose_subpolicy_sequentialinputc                 C   s
   t d|S )zReturn identity matrix.   r   )r   r2   r#   r#   r$   identity_matrix:   s   
z!PolicyAugmentBase.identity_matrixFparams	recompute
extra_argsc                 C   s   |du rt d| |}d}t||dur|ng D ]&\\}}}	tt|}|j|tttt  |	j	||d}
|du r<|
n|
| }q|S )a  Compute the transformation matrix according to the provided parameters.

        Args:
            input: the input tensor.
            params: params for the sequence.
            recompute: if to recompute the transformation matrix according to the params.
                default: False.
            extra_args: Optional dictionary of extra arguments with specific options for different input types.

        Nzrequires params to be provided.)r5   r6   r7   )
r1   get_forward_sequencezipr	   r   get_transformation_matrixr   r   r   data)r   r2   r5   r6   r7   named_modulesres_mat_r%   parammatr#   r#   r$   r:   >   s   
"
z+PolicyAugmentBase.get_transformation_matrixc                 C   s4   |  |}|D ]\}}tt|}| s dS qdS )NFT)r8   r	   r   is_intensity_only)r   r5   r<   r>   r%   r#   r#   r$   rA   ]   s   

z#PolicyAugmentBase.is_intensity_onlybatch_shapec                 C   sF   |   }g }|D ]\}}tt|}||}t||}|| q|S r   )r8   r	   r
   forward_parametersr   r'   )r   rB   r<   r5   namer%   	mod_paramr?   r#   r#   r$   rC   e   s   


z$PolicyAugmentBase.forward_parametersc                 C   s,   |D ]}|  |j}tj||||d}q|S )Nr%   r?   r7   )get_submodulerD   r   	transform)r   r2   r5   r7   r?   r%   r#   r#   r$   transform_inputsq   s   z"PolicyAugmentBase.transform_inputsc           	      C   sl   |    |d u r|}| j|dd\}}| |}|D ]}| |j}tj||||d}| | q|| _|S )N)      )	dim_rangerF   )	r+   autofill_dimrC   rG   rD   r   rH   "_update_transform_matrix_by_module_params)	r   r2   r5   r7   inpr>   	out_shaper?   r%   r#   r#   r$   forwardy   s   
zPolicyAugmentBase.forward)r   )r   N)NFNr   )NN)__name__
__module____qualname____doc__r   SUBPOLICY_CONFIGstrr   r   r)   r+   r   r-   r   r4   r   r   boolr   r   r:   rA   torchSizerC   rI   rR   __classcell__r#   r#   r!   r$   r   #   sX     


	
r   )!typingr   r   r   r   r   r   r   r	   rZ   (kornia.augmentation.auto.operations.baser
   *kornia.augmentation.auto.operations.policyr   "kornia.augmentation.container.baser   r   !kornia.augmentation.container.opsr   $kornia.augmentation.container.paramsr   kornia.corer   r   kornia.utilsr   floatintNUMBERrX   	OP_CONFIGrW   r   r#   r#   r#   r$   <module>   s   (