o
    oi5                     @   s   d dl Z d dlmZmZmZ d dlZd dlmZ d dlmZm	Z	 d dl
mZ g dZG dd deZG d	d
 d
eZG dd deZdS )    N)ListTupleUnion)Tensor)Moduleconcatenate)resize)OutputRangePostProcessorResizePostProcessorResizePreProcessorc                	       sZ   e Zd ZdZddedededdf fdd	Zd
eee	e f de
eef fddZ  ZS )r   zResize a list of image tensors to the given size.

    Additionally, also returns the original image sizes for further post-processing.
    bilinearheightwidthinterpolation_modereturnNc                    s   t    ||f| _|| _dS )a?  Construct ResizePreprocessor module.

        Args:
        height: height of the resized image.
        width: width of the resized image.
        interpolation_mode: interpolation mode for image resizing. Supported values: ``nearest``, ``bilinear``,
            ``bicubic``, ``area``, and ``nearest-exact``.

        N)super__init__sizer   )selfr   r   r   	__class__ G/home/ubuntu/.local/lib/python3.10/site-packages/kornia/models/utils.pyr   $   s   



zResizePreProcessor.__init__imgsc                 C   s   g }t |trt|n|jd }|d |df}t|D ]&}|| }|jd ||df< |jd ||df< |t|d | j| j	d qt
||fS )Run forward.

        Returns:
        resized_imgs: resized images in a batch.
        original_sizes: the original image sizes of (height, width).

        r         Nr   interpolation)
isinstancelistlenshape	new_zerosrangeappendr   r   r   r   )r   r   resized_imgsitersoriginal_sizesiimgr   r   r   forward2   s   	zResizePreProcessor.forwardr   )__name__
__module____qualname____doc__intstrr   r   r   r   r   r-   __classcell__r   r   r   r   r      s     .r   c                	       sV   e Zd Zddeddf fddZdeeee f dedeeee f fd	d
Z  Z	S )r
   r   r   r   Nc                    s   t    || _d S N)r   r   r   )r   r   r   r   r   r   H   s   

zResizePostProcessor.__init__r   r*   c                 C   s   g }t j rtjddd |S t|trt|n|jd }t	|D ]}|| }|| }|
t|d |    | jd q"|S )r   znResizePostProcessor is not supported in ONNX export. The output will not be resized back to the original size.r   )
stacklevelr   Nr   )torchonnxis_in_onnx_exportwarningswarnr!   r"   r#   r$   r&   r'   r   cpulongnumpytolistr   )r   r   r*   r(   r)   r+   r,   r   r   r   r   r-   L   s   	
"zResizePostProcessor.forwardr.   )
r/   r0   r1   r4   r   r   r   r   r-   r5   r   r   r   r   r
   G   s    6r
   c                       sV   e Zd Zddededdf fddZd	eeee f deeee f fd
dZ  Z	S )r	                 ?min_valmax_valr   Nc                    s   t    || _|| _d S r6   )r   r   rC   rD   )r   rC   rD   r   r   r   r   j   s   

z!OutputRangePostProcessor.__init__r   c                    s.   t |trt| j jS  fdd|D S )Nc                    s   g | ]
}|  j jqS r   )clamp_rC   rD   ).0r,   r   r   r   
<listcomp>r   s    z4OutputRangePostProcessor.forward.<locals>.<listcomp>)r!   r   r8   clamprC   rD   )r   r   r   rG   r   r-   o   s   
z OutputRangePostProcessor.forward)rA   rB   )
r/   r0   r1   floatr   r   r   r   r-   r5   r   r   r   r   r	   i   s    2r	   )r;   typingr   r   r   r8   r   kornia.corer   r   kornia.geometry.transformr   __all__r   r
   r	   r   r   r   r   <module>   s   )"