o
    oi                  	   @   sh   d dl Z d dl mZmZ d dlmZmZmZ dde jde jdede jfd	d
ZG dd dej	Z
eZdS )    N)Tensornn)KORNIA_CHECKKORNIA_CHECK_IS_TENSORKORNIA_CHECK_SHAPEmeaninputtarget	reductionreturnc                 C   s   t |  t | t| ddg t|ddg t| j|jkd| j d|j  | d |d  d | d |d  d   }|dkrG| }|S |d	krQ| }|S |d
krX	 |S td)a  Create a function that calculates the average endpoint error (AEPE) between 2 flow maps.

    AEPE is the endpoint error between two 2D vectors (e.g., optical flow).
    Given a h x w x 2 optical flow map, the AEPE is:

    .. math::

        \text{AEPE}=\frac{1}{hw}\sum_{i=1, j=1}^{h, w}\sqrt{(I_{i,j,1}-T_{i,j,1})^{2}+(I_{i,j,2}-T_{i,j,2})^{2}}

    Args:
        input: the input flow map with shape :math:`(*, 2)`.
        target: the target flow map with shape :math:`(*, 2)`.
        reduction : Specifies the reduction to apply to the
         output: ``'none'`` | ``'mean'`` | ``'sum'``. ``'none'``: no reduction will be applied,
         ``'mean'``: the sum of the output will be divided by the number of elements
         in the output, ``'sum'``: the output will be summed.

    Return:
        the computed AEPE as a scalar.

    Examples:
        >>> ones = torch.ones(4, 4, 2)
        >>> aepe(ones, 1.2 * ones)
        tensor(0.2828)

    Reference:
        https://link.springer.com/content/pdf/10.1007/s11263-010-0390-2.pdf

    *2z/input and target shapes must be the same. Got: z and ).r      ).   r   sumnonezInvalid reduction option.)r   r   r   shapesqrtr   r   NotImplementedError)r   r	   r
   epe r   Q/home/ubuntu/.local/lib/python3.10/site-packages/kornia/metrics/endpoint_error.pyaepe   s$   ,r   c                       sH   e Zd ZdZddeddf fddZdejd	ejdejfd
dZ  Z	S )AEPEa  Computes the average endpoint error (AEPE) between 2 flow maps.

    EPE is the endpoint error between two 2D vectors (e.g., optical flow).
    Given a h x w x 2 optical flow map, the AEPE is:

    .. math::

        \text{AEPE}=\frac{1}{hw}\sum_{i=1, j=1}^{h, w}\sqrt{(I_{i,j,1}-T_{i,j,1})^{2}+(I_{i,j,2}-T_{i,j,2})^{2}}

    Args:
        reduction : Specifies the reduction to apply to the
         output: ``'none'`` | ``'mean'`` | ``'sum'``. ``'none'``: no reduction will be applied,
         ``'mean'``: the sum of the output will be divided by the number of elements
         in the output, ``'sum'``: the output will be summed.

    Shape:
        - input: :math:`(*, 2)`.
        - target :math:`(*, 2)`.
        - output: :math:`(1)`.

    Examples:
        >>> input1 = torch.rand(1, 4, 5, 2)
        >>> input2 = torch.rand(1, 4, 5, 2)
        >>> epe = AEPE(reduction="mean")
        >>> epe = epe(input1, input2)

    r   r
   r   Nc                    s   t    || _d S N)super__init__r
   )selfr
   	__class__r   r   r   i   s   

zAEPE.__init__r   r	   c                 C   s   t ||| jS r   )r   r
   )r   r   r	   r   r   r   forwardm   s   zAEPE.forwardr   )
__name__
__module____qualname____doc__strr   torchr   r    __classcell__r   r   r   r   r   L   s    $r   r!   )r'   r   r   kornia.core.checkr   r   r   r&   r   Moduler   average_endpoint_errorr   r   r   r   <module>   s   "4%