a
    d}                     @   sj   d dl Z d dlmZ e je jdddZe je jdddZe je je jddd	ZG d
d dejZdS )    N)img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   c/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/kornia/losses/depth_smooth.py_gradient_x   s    
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   s    
r   idepthimager   c           
      C   s  t | tjstdt|  t |tjs<tdt| t| jdksZtd| j t|jdksxtd|j | jdd |jdd kstd| j d	|j | j|jkstd
| j d	|j | j	|j	kst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)ZdimZkeepdim)
isinstancetorchTensor	TypeErrortyper	   r
   
ValueErrorZdeviceZdtyper   r   expmeanabs)
r   r   Z	idepth_dxZ	idepth_dyZimage_dxZimage_dyZ	weights_xZ	weights_yZsmoothness_xZsmoothness_yr   r   r   inverse_depth_smoothness_loss   s.    r   c                   @   s(   e Zd ZdZejejejdddZdS )InverseDepthSmoothnessLossaS  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   c                 C   s
   t ||S )N)r   )selfr   r   r   r   r   forwardd   s    z"InverseDepthSmoothnessLoss.forwardN)__name__
__module____qualname____doc__r   r   r!   r   r   r   r   r   O   s   r   )	r   Ztorch.nnnnr   r   r   r   Moduler   r   r   r   r   <module>   s
   ;