o
    oi                     @  sT   d dl mZ d dlZd dlmZ dddZdd	d
ZdddZG dd dejZdS )    )annotationsN)nnimgtorch.Tensorreturnc                 C  sX   t | jdkrt| j| d d d d d d d df | d d d d d d dd f  S N      lenshapeAssertionErrorr    r   N/home/ubuntu/.local/lib/python3.10/site-packages/kornia/losses/depth_smooth.py_gradient_x      
@r   c                 C  sX   t | jdkrt| j| d d d d d dd d f | d d d d dd d d f  S r   r   r   r   r   r   _gradient_y!   r   r   idepthimagec           
      C  s  t | tjstdt|  t |tjstdt| t| jdks-td| j t|jdks<td|j | jdd |jdd ksVtd| j d	|j | j|jkshtd
| j d	|j | j	|j	ksztd| j	 d	|j	 t
| }t| }t
|}t|}ttjt|ddd }ttjt|ddd }t|| }t|| }	t|t|	 S )a  Criterion that computes image-aware inverse depth smoothness loss.

    .. math::

        \text{loss} = \left | \partial_x d_{ij} \right | e^{-\left \|
        \partial_x I_{ij} \right \|} + \left |
        \partial_y d_{ij} \right | e^{-\left \| \partial_y I_{ij} \right \|}

    Args:
        idepth: tensor with the inverse depth with shape :math:`(N, 1, H, W)`.
        image: tensor with the input image with shape :math:`(N, 3, H, W)`.

    Return:
        a scalar with the computed loss.

    Examples:
        >>> idepth = torch.rand(1, 1, 4, 5)
        >>> image = torch.rand(1, 3, 4, 5)
        >>> loss = inverse_depth_smoothness_loss(idepth, image)

    z-Input idepth type is not a torch.Tensor. Got z,Input image type is not a torch.Tensor. Got r   z.Invalid idepth shape, we expect BxCxHxW. Got: z-Invalid image shape, we expect BxCxHxW. Got: Nz/idepth and image shapes must be the same. Got: z and z2idepth and image must be in the same device. Got: z1idepth and image must be in the same dtype. Got: r
   T)dimkeepdim)
isinstancetorchTensor	TypeErrortyper   r   
ValueErrordevicedtyper   r   expmeanabs)
r   r   	idepth_dx	idepth_dyimage_dximage_dy	weights_x	weights_ysmoothness_xsmoothness_yr   r   r   inverse_depth_smoothness_loss'   s.   r-   c                   @  s   e Zd ZdZd	ddZdS )
InverseDepthSmoothnessLossaT  Criterion that computes image-aware inverse depth smoothness loss.

    .. math::

        \text{loss} = \left | \partial_x d_{ij} \right | e^{-\left \|
        \partial_x I_{ij} \right \|} + \left |
        \partial_y d_{ij} \right | e^{-\left \| \partial_y I_{ij} \right \|}

    Shape:
        - Inverse Depth: :math:`(N, 1, H, W)`
        - Image: :math:`(N, 3, H, W)`
        - Output: scalar

    Examples:
        >>> idepth = torch.rand(1, 1, 4, 5)
        >>> image = torch.rand(1, 3, 4, 5)
        >>> smooth = InverseDepthSmoothnessLoss()
        >>> loss = smooth(idepth, image)

    r   r   r   r   c                 C  s
   t ||S )N)r-   )selfr   r   r   r   r   forwardy   s   
z"InverseDepthSmoothnessLoss.forwardNr   r   r   r   r   r   )__name__
__module____qualname____doc__r0   r   r   r   r   r.   c   s    r.   )r   r   r   r   r1   )	
__future__r   r   r   r   r   r-   Moduler.   r   r   r   r   <module>   s   


<