a
    d                  
   @   s   d dl mZmZmZ d dlZd dlmZ d dlmZ d dlm	Z	 eee
ee ee ee eeeef dddZee eeeee ef d	d
dZdeee
eeee eee ef f dddZdS )    )OptionalTupleUnionN)Tensor)_uniform_filter)_check_same_shape)predstargetwindow_sizermse_val_sumrmse_maptotal_imagesreturnc           
      C   s  | j |j kr&td| j  d|j  dt| | t| jdkrPtd| j dt|d |jd ks|t|d |jd krtdt|jd |jd  d	t|d  d|d
ur||jd 7 }ntj	|jd |j
d}||  d }t||}t|}t|d }|d
urH|d
d
d
d
|| || f }	||	d 7 }n.|d
d
d
d
|| || f d }|d
ur||d7 }n
|d}|||fS )a  Calculate the sum of RMSE values and RMSE map for the batch of examples and update intermediate states.

    Args:
        preds: Deformed image
        target: Ground truth image
        window_size: Sliding window used for rmse calculation
        rmse_val_sum: Sum of RMSE over all examples per individual channels
        rmse_map: Sum of RMSE map values over all examples
        total_images: Total number of images

    Return:
        (Optionally) Intermediate state of RMSE (using sliding window) over the accumulated examples.
        (Optionally) Intermediate state of RMSE map
        Updated total number of already processed images

    Raises:
        ValueError: If ``preds`` and ``target`` do not have the same data type.
        ValueError: If ``preds`` and ``target`` do not have ``BxCxWxH`` shape.
        ValueError: If ``round(window_size / 2)`` is greater or equal to width or height of the image.

    zBExpected `preds` and `target` to have the same data type. But got z and .   z=Expected `preds` and `target` to have BxCxHxW shape. But got       zBParameter `round(window_size / 2)` is expected to be smaller than z	 but got Nr   )device)Zdtype	TypeErrorr   lenshape
ValueErrorroundmintorchZtensorr   r   sqrtsummean)
r   r	   r
   r   r   r   errorZ	_rmse_mapZ
crop_slideZrmse_val r   n/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/functional/image/rmse_sw.py_rmse_sw_update   s8    
,



$.

r!   )r   r   r   r   c                 C   s,   | dur| | nd}|dur$|| }||fS )a\  Compute RMSE from the aggregated RMSE value. Optionally also computes the mean value for RMSE map.

    Args:
        rmse_val_sum: Sum of RMSE over all examples
        rmse_map: Sum of RMSE map values over all examples
        total_images: Total number of images

    Return:
        RMSE using sliding window
        (Optionally) RMSE map

    Nr   )r   r   r   rmser   r   r    _rmse_sw_computeZ   s    r#      F)r   r	   r
   return_rmse_mapr   c                 C   s^   t |trt |tr$|dk r$tdt| ||dddd\}}}t|||\}}|rZ||fS |S )a  Compute Root Mean Squared Error (RMSE) using sliding window.

    Args:
        preds: Deformed image
        target: Ground truth image
        window_size: Sliding window used for rmse calculation
        return_rmse_map: An indication whether the full rmse reduced image should be returned.

    Return:
        RMSE using sliding window
        (Optionally) RMSE map

    Example:
        >>> from torchmetrics.functional.image import root_mean_squared_error_using_sliding_window
        >>> g = torch.manual_seed(22)
        >>> preds = torch.rand(4, 3, 16, 16)
        >>> target = torch.rand(4, 3, 16, 16)
        >>> root_mean_squared_error_using_sliding_window(preds, target)
        tensor(0.3999)

    Raises:
        ValueError: If ``window_size`` is not a positive integer.

       z<Argument `window_size` is expected to be a positive integer.N)r   r   r   )
isinstanceintr   r!   r#   )r   r	   r
   r%   r   r   r   r"   r   r   r    ,root_mean_squared_error_using_sliding_windowo   s    r)   )r$   F)typingr   r   r   r   r   Z$torchmetrics.functional.image.helperr   Ztorchmetrics.utilities.checksr   r(   r!   r#   boolr)   r   r   r   r    <module>   s(   C
 