o
    oi                     @   s|   d dl 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mZ d dlmZmZmZ d dlmZmZ G dd de
Zd	S )
    )AnyDictOptionalTupleUnion)random_generator)GeometricAugmentationBase2D)ResampleSamplePadding)Tensor	as_tensorstack)get_translation_matrix2dwarp_affinec                       s8  e Zd ZdZddejjddejjddfde	e
eeeef f  de	e
eeeef f  de
eeef ded	ed
e
eeef dededdf 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 de	eeef  def
ddZ  ZS )RandomTranslatea  Apply a random 2D affine transformation to a tensor image.

    Args:
        translate_x: tuple of maximum absolute fraction for horizontal translations.
            For example translate_x=(a, b), then horizontal shift
            is randomly sampled in the range img_width * a < dx < img_width * b
        translate_y: tuple of maximum absolute fraction for vertical translations.
            For example translate_y=(a, b), then vertical shift
            is randomly sampled in the range img_height * a < dy < img_height * b.
        resample: resample mode from "nearest" (0) or "bilinear" (1).
        padding_mode: padding mode from "zeros" (0), "border" (1) or "reflection" (2).
        same_on_batch: apply the same transformation across the batch.
        align_corners: interpolation flag.
        p: probability of applying the transformation.
        keepdim: whether to keep the output shape the same as input (True) or broadcast it to the batch form (False).

    Shape:
        - Input: :math:`(C, H, W)` or :math:`(B, C, H, W)`
        - Output: :math:`(B, C, H, W)`

    .. note::
        This function internally uses :func:`kornia.geometry.transform.warp_affine`.

    Examples:
        >>> import torch
        >>> rng = torch.manual_seed(0)
        >>> input = torch.rand(1, 1, 3, 3)
        >>> aug = RandomTranslate((-0.2, 0.2), (-0.1, 0.1), p=1.)
        >>> out = aug(input)
        >>> out, aug.transform_matrix
        (tensor([[[[0.3403, 0.6439, 0.2920],
                  [0.1377, 0.3383, 0.5569],
                  [0.3226, 0.6909, 0.4844]]]]), tensor([[[ 1.0000,  0.0000,  0.1588],
                 [ 0.0000,  1.0000, -0.0907],
                 [ 0.0000,  0.0000,  1.0000]]]))
        >>> aug.inverse(out)
        tensor([[[[0.3565, 0.4839, 0.1922],
                  [0.2164, 0.4134, 0.3968],
                  [0.3797, 0.6075, 0.3765]]]])

    To apply the exact augmenation again, you may take the advantage of the previous parameter state:
        >>> input = torch.randn(1, 3, 32, 32)
        >>> aug = RandomTranslate((-0.2, 0.2), (-0.1, 0.1), p=1.)
        >>> (aug(input) == aug(input, params=aug._params)).all()
        tensor(True)

    NFg      ?translate_xtranslate_yresamplesame_on_batchalign_cornerspadding_modepkeepdimreturnc	           	         s>   t  j|||d t||| _t|t||d| _d S )N)r   r   r   )r   r   r   )	super__init__rgTranslateGenerator_param_generatorr	   getr
   flags)	selfr   r   r   r   r   r   r   r   	__class__ _/home/ubuntu/.local/lib/python3.10/site-packages/kornia/augmentation/_2d/geometric/translate.pyr   L   s   zRandomTranslate.__init__inputparamsr    c                 C   s.   t |d |d gdd}tt||j|jdS )Nr   r   )dimdevicedtype)r   r   r   r+   r,   )r!   r&   r'   r    translationsr$   r$   r%   compute_transformation_   s   z&RandomTranslate.compute_transformation	transformc                 C   sr   |j \}}}}t|tstdt| dt||d d d dd d f ||f|d j |d |d j dS )N*Expected the `transform` be a Tensor. Got .   r   r   r   )r   r   )shape
isinstancer   	TypeErrortyper   namelower)r!   r&   r'   r    r/   _heightwidthr$   r$   r%   apply_transformc   s   
zRandomTranslate.apply_transformsizec                 C   s@   t |tstdt| d| j|| jt||j|jd|dS )Nr0   r1   r*   )r'   r/   r    )	r4   r   r5   r6   r<   _paramsr   r+   r,   )r!   r&   r    r/   r=   r$   r$   r%   inverse_transforms   s   
z!RandomTranslate.inverse_transform)N)NN)__name__
__module____qualname____doc__r	   BILINEARr7   r
   ZEROSr   r   r   r   floatstrintboolr   r   r   r.   r<   r?   __classcell__r$   r$   r"   r%   r      sn    2	
*



r   N)typingr   r   r   r   r   kornia.augmentationr   r   &kornia.augmentation._2d.geometric.baser   kornia.constantsr	   r
   kornia.corer   r   r   kornia.geometry.transformr   r   r   r$   r$   r$   r%   <module>   s   