o
    oi                     @   s   d dl 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mZ d dlmZ d dlmZ d dlmZmZ d d	lmZmZmZ d d
lmZ G dd deeZ dS )    )AnyDictIteratorListOptionalTupleUnioncast)SizeN)OperationBase)ImageSequentialBaseTransformMatrixMinIn)InputSequentialOps)	ParamItem)_transform_inputoverride_parameters)ModuleTensor	as_tensoreye_likec                       s&  e Zd ZdZdeddf fddZdeddfdd	Zd  fd
dZdedd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
f
ddZdefddZd"deee  deeeef  f fddZde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  ZS )#PolicySequentialztPolicy tuple for applying multiple operations.

    Args:
        operations: a list of operations to perform.

    
operationsreturnNc                    s"   | j |  t j|  tf| _d S N)validate_operationssuper__init__r   $_valid_ops_for_transform_computation)selfr   	__class__ ^/home/ubuntu/.local/lib/python3.10/site-packages/kornia/augmentation/auto/operations/policy.pyr   (   s   
zPolicySequential.__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   z6PolicySequential._update_transform_matrix_for_valid_opc                    s   |    t  S r   )_reset_transform_matrix_stater   clear_state)r   r    r"   r#   r+   0   s   
zPolicySequential.clear_statec                 G   sB   g }|D ]}t |ts|| qt|dkrtd| dd S )Nr   z.All operations must be Kornia Operations. Got .)
isinstancer   r&   len
ValueError)r   r   invalid_opsopr"   r"   r#   r   4   s   

z$PolicySequential.validate_operationsinputc                 C   s
   t d|S )zReturn identity matrix.   r   )r   r2   r"   r"   r#   identity_matrix<   s   
z PolicySequential.identity_matrixFparams	recompute
extra_argsc                 C   s  |du rt d| |}| t|}t||dur|ng D ]g\\}}}	tt|}t|jt	j
frt|	jtr|j}
|j|}|rVt|jj|dd}|j||	j|}n|jjdurgt|j|j|jd}nt| d|| }|j||
}|jjr|
|jkr| }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.F)in_place)devicedtypez2.transform_matrix is None while `recompute=False`.)NotImplementedErrorget_forward_sequencer4   r   zipr	   r   r-   r1   KGeometricAugmentationBase2Ddatadictshapetransform_tensorr   flagsgenerate_transformation_matrix_transform_matrixr   r'   r9   r:   RuntimeErrortransform_output_tensorkeepdimsqueeze)r   r2   r5   r6   r7   named_modulesres_mat_r$   param	ori_shaperD   matr"   r"   r#   get_transformation_matrix@   s*   
"
z*PolicySequential.get_transformation_matrixc                 C   s2   |   D ]}tt|}t|jtjfr dS qdS )NFT)childrenr	   r   r-   r1   r>   r?   r(   r"   r"   r#   is_intensity_onlyj   s   
z"PolicySequential.is_intensity_onlyc                    s   |d ur
t  |S |  S r   )r   get_children_by_paramsnamed_children)r   r5   r    r"   r#   r<   q   s   z%PolicySequential.get_forward_sequencebatch_shapec                 C   sH   |   }g }|D ]\}}tt|}|j|}t||}|| q|S r   )r<   r	   r   r1   forward_parametersr   r&   )r   rV   rK   r5   namer$   	mod_paramrN   r"   r"   r#   rW   v   s   

z#PolicySequential.forward_parametersc                 C   s6   |D ]}|  |j}tj||||d}| | q|S )N)r$   rN   r7   )get_submodulerX   r   	transform"_update_transform_matrix_by_module)r   r2   r5   r7   rN   r$   r"   r"   r#   transform_inputs   s
   z!PolicySequential.transform_inputs)r   N)NFNr   )__name__
__module____qualname____doc__r   r   r   r)   r+   r   r   r4   r   r   r   boolr   strr   rQ   rS   r   r   r<   r
   rW   r]   __classcell__r"   r"   r    r#   r       sD    

*,r   )!typingr   r   r   r   r   r   r   r	   torchr
   kornia.augmentationaugmentationr>   #kornia.augmentation.auto.operationsr   "kornia.augmentation.container.baser   r   !kornia.augmentation.container.opsr   $kornia.augmentation.container.paramsr   kornia.augmentation.utilsr   r   kornia.corer   r   r   kornia.utilsr   r   r"   r"   r"   r#   <module>   s   (