
    3jO+                         S SK JrJrJrJrJr  S SKrS SKJr  S SKJ	r	  S SK
Jr  S SK
JrJr  S SKJr  S SKJr  S S	KJrJr  S S
KJr  S SKJr  / SQrSSSSS.r " S S\\5      r " S S5      r " S S5      rg)    )AnyListOptionalTupleUnionN)nn)kornia_config)PILImage)basicsronnx)ONNXExportMixin)	ModelBase)OutputRangePostProcessorResizePreProcessor)SmallSRNetWrapper)CachedDownloader)RRDBNetBuilderSmallSRBuilderSuperResolutionzUhttps://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pthzUhttps://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.1/RealESRNet_x4plus.pthz`https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pthzUhttps://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth)RealESRGAN_x4plusRealESRNet_x4plusRealESRGAN_x4plus_anime_6BRealESRGAN_x2plusc                      \ rS rSr% SrSr\\S'   \\	   \S'   \\	   \S'   \\	   \S'   \
R                  " 5       S\\
R                  \\
R                     4   S	\\
R                  \\
R                     4   4S
 j5       r  SS\\
R                  \\
R                     4   S\\\
R                  \\
R                     4      S\S	\\
R                  \\
R                     \S   4   4S jjr   SS\\
R                  \\
R                     4   S\\\
R                  \\
R                     4      S\\   S\S	S4
S jjr    SS\\   S\S\S\\\\\4         S\S	S4S jjrSrg)r   +   zASuperResolution is a module that wraps an super resolution model.super_resolutionnameinput_image_sizeoutput_image_sizepseudo_image_sizeimagesreturnc                 2   U R                  U5      n[        U[        [        -  5      (       a  US   nOUn[        U[        5      (       a'  U Vs/ s H  o0R	                  US   5      S   PM     nnOU R	                  U5      nU R                  U5      $ s  snf )zForward pass of the super resolution model.

Args:
    images: If list of RGB images. Each image is a torch.Tensor with shape :math:`(3, H, W)`.
        If torch.Tensor, a torch.Tensor with shape :math:`(B, 3, H, W)`.

Returns:
    output torch.Tensor.

r   N)pre_processor
isinstancelisttuplemodelpost_processor)selfr!   outputimage
out_imagess        Y/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/contrib/super_resolution.pyforwardSuperResolution.forward3   s     ##F+fdUl++AYFFfd##BHI&**U4[1!4&JIJF+J"":.. Js    BN	edge_mapsoutput_typezImage.Imagec                     Uc  U R                  U5      n/ nU H  nUR                  U5        M     U R                  XC[        U[        R
                  5      S9$ )zDraw the super resolution results.

Args:
    images: input torch.Tensor.
    edge_maps: detected edges.
    output_type: type of the output.

Returns:
    output torch.Tensor.

)is_batch)r/   append_tensor_to_typer%   torchTensor)r*   r!   r1   r2   r+   edge_maps         r.   	visualizeSuperResolution.visualizeJ   sY    " V,I!HMM(# " ##F*VUZUaUaBb#cc    	directoryc                 h    U R                  XU5      nU R                  XSS9  U R                  XSSS9  g)a  Save the super resolution results.

Args:
    images: input torch.Tensor.
    edge_maps: detected edges.
    output_type: type of the output.
    directory: where to save outputs.
    output_type: backend used to generate outputs.

Returns:
    output torch.Tensor.

_src)suffix_srN)r:   _save_outputs)r*   r!   r1   r=   r2   outputss         r.   saveSuperResolution.savec   s:    ( ..K@6V<7e<r<   	onnx_nameinclude_pre_and_post_processorrD   additional_metadatakwargszonnx.ModelProtoc           
         Uc  SU R                    S3n[        R                  " U U4SSU R                  =(       d    SU R                  =(       d    S/SSU R                  =(       d    SU R                  =(       d    S/SSU R
                  =(       d    SU R
                  =(       d    S/U(       a  U OU R                  UUS.UD6$ )a  Export the current super resolution model to an ONNX model file.

Args:
    onnx_name:
        The name of the output ONNX file. If not provided, a default name in the
        format "Kornia-<ClassName>.onnx" will be used.
    image_size:
        The size to which input images will be resized during preprocessing.
        If None, image_size will be dynamic. For DexiNed, recommended scale is 352.
    include_pre_and_post_processor:
        Whether to include the pre-processor and post-processor in the exported model.
    save:
        If to save the model or load it.
    additional_metadata:
        Additional metadata to add to the ONNX model.
    kwargs: Additional arguments for converting to onnx.

kornia_z.onnx      i`  )input_shapeoutput_shapepseudo_shaper(   rD   rH   )r   r   to_onnxr   r   r    r(   )r*   rF   rG   rD   rH   rI   s         r.   rR   SuperResolution.to_onnx{   s    4 !$))E2I&&

 Q 5 5 ;T=R=R=XVXYa!7!7!=2t?U?U?[Y[\Q 6 6 =#t?U?U?\Y\]8$djj 3

 

 
	
r<    )Nr7   )NNr7   )NTTN)__name__
__module____qualname____firstlineno____doc__r   str__annotations__r   intr7   inference_moder   r8   r   r/   r:   rD   boolr   r   rR   __static_attributes__rT   r<   r.   r   r   +   s   K"D#"sm#}$}$
/eELL$u||2D$DE /%PUP\P\^bchcoco^pPpJq / /2 HL"	dellD$667d E%,,U\\0B"BCDd 	d
 
u||T%,,/m1DD	Ed8 HL#'"=ellD$667= E%,,U\\0B"BCD= C=	=
 = 
=4 $(/3?C'
C='
 )-'
 	'

 &d5c?&;<'
 '
 
'
 '
r<   r   c                   <    \ rS rSrSr\S	S\S\S\4S jj5       r	Sr
g)
r      zSProvide a static factory to build Residual-in-Residual Dense Block (RRDB) networks.
model_name
pretrainedr"   c           	         U S:X  a-  [         R                  R                  R                  SSSSSSS9nOU S:X  a-  [         R                  R                  R                  SSSSSSS9nOuU S	:X  a-  [         R                  R                  R                  SSSS
SSS9nOBU S:X  a-  [         R                  R                  R                  SSSSSSS9nO[	        SU  S35      eSnU(       ah  [
        U    n[        R                  " X@SS[        R                  S9nUR                  [        R                  " U[        R                  " S5      S9S   SS9  UR                  5         [        U[         R"                  " 5       [%        SSS9U S9$ )a  Build a preconfigured RRDB (Residual-in-Residual Dense Block) model.

The returned object is a :class:`SuperResolution` wrapper configured for
inference. Internally, this factory selects architecture hyperparameters
based on ``model_name``, and optionally loads pretrained weights.

Args:
    model_name: Name of the RRDB variant to construct. Supported options are:
        ``"RealESRGAN_x4plus"``, ``"RealESRNet_x4plus"``,
        ``"RealESRGAN_x4plus_anime_6B"``, and ``"RealESRGAN_x2plus"``.
    pretrained: If ``True``, download and load pretrained weights for the
        selected model variant.

Returns:
    A :class:`SuperResolution` wrapper ready for inference.

Raises:
    ValueError: If ``model_name`` is not one of the supported variants.
r   rM   @             )	num_in_ch
num_out_chnum_feat	num_blocknum_grow_chscaler   r      r      Model z{ not found. Please choose from 'RealESRGAN_x4plus', 'RealESRNet_x4plus', 'RealESRGAN_x4plus_anime_6B', 'RealESRGAN_x2plus'.NTz.pth)downloadr@   	cache_dircpu)map_location
params_ema)strictg        g      ?)min_valmax_valr$   r)   r   )r   archsrrdbnet_archRRDBNet
ValueErrorURLsr   download_to_cacher	   hub_onnx_dirload_state_dictr7   loaddeviceevalr   r   Identityr   )rb   rc   r(   
model_pathurls        r.   buildRRDBNetBuilder.build   s   * ,,MM..66B"RT\] 7 E ..MM..66B"RT\] 7 E 77MM..66B!QS[\ 7 E ..MM..66B"RT\] 7 E  %o o 
 
z"C);;$vIcIcJ !!%**ZellSXFY"Z[g"hqu!v

++-3CM	
 	
r<   rT   N)r   T)rU   rV   rW   rX   rY   staticmethodrZ   r^   r   r   r_   rT   r<   r.   r   r      s-    ]8
# 8
 8
Q` 8
 8
r<   r   c                   L    \ rS rSrSr\ SS\S\S\S\	\   S\
4
S	 jj5       rS
rg)r      zFProvide a static factory to build lightweight Super-Resolution models.Nrb   rc   upscale_factor
image_sizer"   c                     U R                  5       S:X  a
  [        X!S9nO[        SU  S35      e[        U[	        SS5      [
        R                  " 5       U S9nUc	  SUl        U$ X5l        US-  Ul	        X5l        U$ )aa  Build a lightweight super-resolution (SR) model wrapper.

SR stands for *super-resolution*, which means generating a higher
resolution output from a lower resolution input.

Args:
    model_name: Name of the model variant to build. Currently only
        ``"small_sr"`` is supported.
    pretrained: If ``True``, load pretrained weights for the selected model.
    upscale_factor: Integer scale used by ``SmallSRNetWrapper``.
        For example, ``3`` maps an input image of size ``H x W`` to an
        output image of approximately ``3H x 3W``.
    image_size: Optional fixed input size. When provided, output metadata is
        configured as ``image_size * upscale_factor``.

Returns:
    A :class:`SuperResolution` instance configured with resizing pre-processing
    and identity post-processing.

Raises:
    ValueError: If ``model_name`` is not supported.
small_sr)rc   rq   z* not found. Please choose from 'small_sr'.   rz   rM   )
lowerr   r~   r   r   r   r   r    r   r   )rb   rc   r   r   r(   srs         r.   r   SmallSRBuilder.build   s    4 +%nLEvj\1[\]],S#6;;=	
 #&B 
 	 #-#->B #- 	r<   rT   )r   TrM   N)rU   rV   rW   rX   rY   r   rZ   r^   r\   r   r   r   r_   rT   r<   r.   r   r      sH    Ptx**26*OR*dlmpdq*	* *r<   r   )typingr   r   r   r   r   r7   r   kornia.configr	   kornia.core.externalr
   Imager   r   kornia.core.mixin.onnxr   kornia.models.baser   kornia.models.processorsr   r   kornia.models.small_srr   kornia.onnx.downloadr   __all__r   r   r   r   rT   r<   r.   <module>r      so   $ 5 4   ' 2 . 2 ( Q 4 1
A qp #Ep	w
i w
t<
 <
~. .r<   