o
    oiE                     @   s  d dl mZ d dlmZmZmZmZmZmZm	Z	 d dl
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 d dlmZ d	ed
ef ded
ef fddZd	ed
ef ded
ef fddZde	eeeef f de
jfddZde	eeeef f de
jfddZdVdedededefddZdVdedededefddZdedefddZdedefddZ ded ee
j! ddfd!d"Z"dd#d$ed%ee#d
f dee defd&d'Z$dWd%e	ee#d
f e
jf d)ee%d
f ddfd*d+Z&ded,e#d-e#defd.d/Z'	0dXd%e	ee#d
f e
jf d1e
j(j)d2ee defd3d4Z*	0dXd%e	ee#d
f e
jf d1e
j(j)d2ee defd5d6Z+	0dXd%e	ee#d
f e
jf d7e	e,ef d8e	e,ef d2edef
d9d:Z-	0dXd%e	ee#d
f e
jf d;e	e,ef d<e	e,ef d2edef
d=d>Z.d?ed%e	ee#d
f ee# f d@e%ddfdAdBZ/dCee%ef dee%ef fdDdEZ0		F	0dYdCee%ef dGeee%ef  dHe%dIedee%ef f
dJdKZ1dZde	eef dMe%defdNdOZ2de	eef defdPdQZ3dedefdRdSZ4G dTdU dUZ5dS )[    )wraps)AnyCallableDictListOptionalTupleUnionN)BetaUniform)Tensor	as_tensor)Boxes)	Keypoints)_extract_device_dtypef.returnc              	      *   t  dtdtdtdtf fdd}|S )zValidate the 2D input of the wrapped function.

    Args:
        f: a function that takes the first argument as tensor.

    Returns:
        the wrapped function after input is validated.

    inputargskwargsr   c                    sX   t | stdt|  t| jdd t| t jt jt j	gd  | g|R i |S )N Input type is not a Tensor. Got BCHW)required_shapesaccepted_dtypes)
torch	is_tensor	TypeErrortype_validate_shapeshape_validate_input_dtypefloat16float32float64)r   r   r   r    U/home/ubuntu/.local/lib/python3.10/site-packages/kornia/augmentation/utils/helpers.pywrapper)   s
   
z _validate_input.<locals>.wrapperr   r   r   r   r*   r(   r'   r)   _validate_input   s    	r-   c              	      r   )zValidate the 3D input of the wrapped function.

    Args:
        f: a function that takes the first argument as tensor.

    Returns:
        the wrapped function after input is validated.

    r   r   r   r   c                    sl   t | stdt|  t| j}|dkrtd| dt| t jt j	t j
gd  | g|R i |S )Nr      z"Expect input of 5 dimensions, got z insteadr   )r   r   r   r    lenr"   AssertionErrorr#   r$   r%   r&   )r   r   r   input_shaper'   r(   r)   r*   A   s   

z"_validate_input3d.<locals>.wrapperr+   r,   r(   r'   r)   _validate_input3d6   s    r2   r   c                 C   *   t | trt| d }|jS t| }|jS zXInfer input shape.

    Input may be either (tensor,) or (tensor, transform_matrix)
    r   )
isinstancetuple_transform_inputr"   r   tensorr(   r(   r)   _infer_batch_shapeP   
   
r:   c                 C   r3   r4   )r5   r6   _transform_input3dr"   r8   r(   r(   r)   _infer_batch_shape3d\   r;   r=   Treference_shapematch_channelc                 C   s   t |dkr
|d nd}t |dkr|d nd}t | jdkr$| d} t | jdkr<|| jd kr7| dn| d} |rM|rM| jd |ksKtd	| S |rW|du rWtd
| S )  Reshape an input tensor to have the same dimensions as the reference_shape.

    Arguments:
        input: tensor to be transformed
        reference_shape: shape used as reference
        match_channel: if True, C_{src} == C_{ref}. otherwise, no constrain. C =1 by default

       N      r      BThe C dimension of tensor did not match with the reference tensor./The reference tensor do not have a C dimension!r/   r"   	unsqueeze
ValueErrorr   r>   r?   BCr(   r(   r)   _transform_input_by_shapeh   s   	
"rO   c                 C   s   t |dkr
|d nd}t |dkr|d nd}t | jdkr$| d} t | jdkr7|| jd kr7| d} |rH|rH| jd |ksFtd	| S |rR|du rRtd
| S )r@   r.   NrA   rB   rC   r   rE   rG   rH   rI   rL   r(   r(   r)   _transform_input3d_by_shape   s   	

rQ   c                 C   n   t | stdt|  t| jdvrtd| j t| jdkr)| d} t| jdkr5| d} | S )zReshape an input tensor to be (*, C, H, W). Accept either (H, W), (C, H, W) or (*, C, H, W).

    Args:
        input: Tensor

    Returns:
        Tensor

    r   )rE   rC   rA   zNInput size must have a shape of either (H, W), (C, H, W) or (*, C, H, W). Got rE   r   rC   r   r   r   r    r/   r"   rK   rJ   r   r(   r(   r)   r7      s   



r7   c                 C   rR   )zReshape an input tensor to be (*, C, D, H, W). Accept either (D, H, W), (C, D, H, W) or (*, C, D, H, W).

    Args:
        input: Tensor

    Returns:
        Tensor

    r   )rC   rA   r.   zWInput size must have a shape of either (D, H, W), (C, D, H, W) or (*, C, D, H, W). Got rC   r   rA   rS   rT   r(   r(   r)   r<      s   




r<   r   c                 C   s$   | j |vrtd| d| j  dS )zCheck if the dtype of the input tensor is in the range of accepted_dtypes.

    Args:
        input: Tensor
        accepted_dtypes: List. e.g. [torch.float32, torch.float64]
    zExpected input of . Got N)dtyper   )r   r   r(   r(   r)   r#      s   
r#   )r>   outputr"   c                C   s   |   }tt|jt| D ]:}d}|dur0|jd |d   kr&dkr0n nt|dkr0d}|j| dkrDtd| d|j|  ||}q|S )a  Collapse the broadcasted batch dimensions an input tensor to be the specified shape.

    Args:
        output: Tensor
        shape: List/tuple of int
        reference_shape: Tensor representation of shape to control which dimensions are collapsed.

    Returns:
        Tensor

    r   NrF   rE   z
Dimension z# of input is expected to be 1, got )cloneranger/   r"   r0   squeeze)rW   r"   r>   
out_tensordimidxr(   r(   r)   _transform_output_shape   s   6r^   r   r   c                 C   sD   d}|D ]}t | t |krd} nq|s td| d|  ddS )zCheck if the dtype of the input tensor is in the range of accepted_dtypes.

    Args:
        shape: tensor shape
        required_shapes: List. e.g. ["BCHW", "BCDHW"]
    FTzExpected input shape in rU   .N)r/   r   )r"   r   passedrequired_shaper(   r(   r)   r!      s   r!   channel_indexnumberc                 C   s   | j | |kS )aT  Validate if an input has the right shape.

    e.g. to check if an input is channel first.
    If channel first, the second channel of an RGB input shall be fixed to 3. To verify using:
        _validate_input_shape(input, 1, 3)

    Args:
        input: Tensor
        channel_index: int
        number: int
    Returns:
        bool

    )r"   )r   rb   rc   r(   r(   r)   _validate_input_shape  s   rd   Fdistsame_on_batchc                 C   sl   t | tr
t| } |r1tdg| dd R }||}|j| d gdgt|jd  R  S || S )zSample from a uniform reparameterized sampling function that accepts 'same_on_batch'.

    If same_on_batch is True, all values generated will be exactly same given a batch_size (shape[0]). By default,
    same_on_batch is set to False.
    rF   Nr   )r5   r6   r   Sizersamplerepeatr/   r"   )r"   re   rf   rsample_sizerh   r(   r(   r)   _adapted_rsampling  s   



&
rk   c                 C   sb   t | tr
t| } |r,|tdg| dd R j| d gdgt| d  R  S || S )zSample from a uniform sampling function that accepts 'same_on_batch'.

    If same_on_batch is True, all values generated will be exactly same given a batch_size (shape[0]). By default,
    same_on_batch is set to False.
    rF   Nr   )r5   r6   r   rg   sampleri   r/   )r"   re   rf   r(   r(   r)   _adapted_sampling2  s
   


@
rm   lowhighc                 C   b   t t|tr|ndt|tr|ndg\}}t|||d}t|||d}t||dd}t| ||S )a  Sample from a uniform sampling function that accepts 'same_on_batch'.

    If same_on_batch is True, all values generated will be exactly same given a batch_size (shape[0]). By default,
    same_on_batch is set to False.

    By default, sampling happens on the default device and dtype. If low/high is a tensor, sampling will happen in the
    same device/dtype as low/high tensor.
    NdevicerV   Fvalidate_args)r   r5   r   r   r   rk   )r"   rn   ro   rf   rr   rV   re   r(   r(   r)   _adapted_uniformD  s   "ru   abc                 C   rp   )av  Sample from a beta sampling function that accepts 'same_on_batch'.

    If same_on_batch is True, all values generated will be exactly same given a batch_size (shape[0]). By default,
    same_on_batch is set to False.

    By default, sampling happens on the default device and dtype. If a/b is a tensor, sampling will happen in the same
    device/dtype as a/b tensor.
    Nrq   Frs   )r   r5   r   r   r
   rk   )r"   rv   rw   rf   rr   rV   re   r(   r(   r)   _adapted_beta]  s
   ,rx   paramnamec                 C   s0   | j t|krtd| d| d| j  d S )NzInvalid shape for z. Expected rU   )r"   r   rg   r0   )ry   r"   rz   r(   r(   r)   _shape_validationr  s   r{   paramsc                 C   sF   i }|   D ]\}}t|tr||| i q|||i q|S )zEPerform deep copy on any dict.

    Support tensor copying here.
    )itemsr5   r   updaterX   )r|   outkvr(   r(   r)   deepcopy_dictw  s   
r   ignoreparams_overrideif_none_existin_placec                 C   sz   |du r| S |r
| nt | }| D ](\}}||v r|||< q|dkr$q|dkr3td| d| dtd| d|S )	aU  Override params dict w.r.t params_override.

    Args:
        params: source parameters.
        params_override: key-values to override the source parameters.
        if_none_exist: behaviour if the key in `params_override` does not exist in `params`.
            'raise' | 'ignore'.
        in_place: if to override in-place or not.

    Nr   raisezParam `z` not existed in `z`.`z` is not a valid option.)r   r}   RuntimeErrorrK   )r|   r   r   r   r   r   r   r(   r(   r)   override_parameters  s   
r   vertices_plusmodec                 C   sv   t | tr*t| jdkr| jdd tddgks#td| j dtj| |d} t | ts9tdt	|  d| S )a=  Preprocess input boxes.

    Args:
        input: 2D boxes, shape of :math:`(N, 4, 2)`, :math:`(B, N, 4, 2)` or a list of :math:`(N, 4, 2)`.
            See below for more details.
        mode: The format in which the boxes are provided.

            * 'xyxy': boxes are assumed to be in the format ``xmin, ymin, xmax, ymax`` where ``width = xmax - xmin``
                and ``height = ymax - ymin``. With shape :math:`(N, 4)`, :math:`(B, N, 4)`.
            * 'xyxy_plus': similar to 'xyxy' mode but where box width and length are defined as
                ``width = xmax - xmin + 1`` and ``height = ymax - ymin + 1``.
                With shape :math:`(N, 4)`, :math:`(B, N, 4)`.
            * 'xywh': boxes are assumed to be in the format ``xmin, ymin, width, height`` where
                ``width = xmax - xmin`` and ``height = ymax - ymin``. With shape :math:`(N, 4)`, :math:`(B, N, 4)`.
            * 'vertices': boxes are defined by their vertices points in the following ``clockwise`` order:
                *top-left, top-right, bottom-right, bottom-left*. Vertices coordinates are in (x,y) order. Finally,
                box width and height are defined as ``width = xmax - xmin`` and ``height = ymax - ymin``.
                With shape :math:`(N, 4, 2)` or :math:`(B, N, 4, 2)`.
            * 'vertices_plus': similar to 'vertices' mode but where box width and length are defined as
                ``width = xmax - xmin + 1`` and ``height = ymax - ymin + 1``. ymin + 1``.
                With shape :math:`(N, 4, 2)` or :math:`(B, N, 4, 2)`.

    Note:
        **2D boxes format** is defined as a floating data type tensor of shape ``Nx4x2`` or ``BxNx4x2``
        where each box is a `quadrilateral <https://en.wikipedia.org/wiki/Quadrilateral>`_ defined by it's 4 vertices
        coordinates (A, B, C, D). Coordinates must be in ``x, y`` order. The height and width of a box is defined as
        ``width = xmax - xmin + 1`` and ``height = ymax - ymin + 1``. Examples of
        `quadrilaterals <https://en.wikipedia.org/wiki/Quadrilateral>`_ are rectangles, rhombus and trapezoids.

    rA   rE   Nz&Only BxNx4x2 tensor is supported. Got r_   )r   zExpect `Boxes` type. Got )
r5   r   r/   r"   r   rg   r   r   from_tensorr    )r   r   r(   r(   r)   preprocess_boxes  s   
!*
r   c                 C   sp   t | tr't| jdkr| jdd tdgks"td| j dt| d} t | tr6tdt|  d| S )	zPreprocess input keypoints.rC   rF   NrE   z$Only BxNx2 tensor is supported. Got r_   FzExpect `Keypoints` type. Got )	r5   r   r/   r"   r   rg   r   r   r    rT   r(   r(   r)   preprocess_keypoints  s   
(

r   c                 C   s   | S )zPreprocess input class tags.r(   rT   r(   r(   r)   preprocess_classes  s   r   c                       s.   e Zd ZdZdededdf fddZ  ZS )MultiprocessWrapperzYWhen used as a base class, makes the class work with the 'spawn' multiprocessing context.r   r   r   Nc                    s:   t dd |D }dd | D }t j|i | d S )Nc                 s   s(    | ]}t |tjr| n|V  qd S )Nr5   r   r   rX   ).0argr(   r(   r)   	<genexpr>  s   & z/MultiprocessWrapper.__init__.<locals>.<genexpr>c                 S   s*   i | ]\}}|t |tjr| n|qS r(   r   )r   keyvalr(   r(   r)   
<dictcomp>  s   * z0MultiprocessWrapper.__init__.<locals>.<dictcomp>)r6   r}   super__init__)selfr   r   	__class__r(   r)   r     s   zMultiprocessWrapper.__init__)__name__
__module____qualname____doc__r   r   __classcell__r(   r(   r   r)   r     s    "r   )T)r   )F)Nr   F)r   )6	functoolsr   typingr   r   r   r   r   r   r	   r   torch.distributionsr
   r   kornia.corer   r   kornia.geometry.boxesr   kornia.geometry.keypointsr   kornia.utilsr   r-   r2   rg   r:   r=   boolrO   rQ   r7   r<   rV   r#   intr^   strr!   rd   distributionsDistributionrk   rm   floatru   rx   r{   r   r   r   r   r   r   r(   r(   r(   r)   <module>   s   $""$$

2







."


 *