
    3j4                       S SK Jr  S SKJr  S SKJr  S SKrS SKJr  SSS jjr	SS jr
SS jrS r " S	 S
\R                  5      r " S S\R                  5      r " S S\R                  5      rS r\S   r S S jr        S!         S"S jjr " S S\R                  5      r " S S\R                  5      r " S S\R                  5      rS#S jr       S$     S%S jjr        S&     S%S jjrg)'    )annotations)OrderedDict)LiteralNc                    U R                  5       n U S:X  a  [        R                  " U5      nU$ U S:X  a  [        R                  " X!5      nU$ U S:X  a  [        R                  " X2S9nU$ [        SU S S35      e)Nrelu	leakyreluprelu)num_parametersinitzactivation layer [s] is not found)lowernnReLU	LeakyReLUPReLUNotImplementedError)act_typeinplace	neg_slopen_prelulayers        e/home/wildlama/miniconda3/lib/python3.13/site-packages/spandrel/architectures/__arch_helpers/block.pyactr      s     ~~H6  L 
[	 Y0
 L	 
W	@ L "$6xl."QRR    c                    U R                  5       n U S:X  a  [        R                  " USS9nU$ U S:X  a  [        R                  " USS9nU$ [	        SU S S35      e)	NbatchT)affineinstanceFznormalization layer [r   r   )r   r   BatchNorm2dInstanceNorm2dr   )	norm_typencr   s      r   normr$       si    !IGr$/
 L	 
j	 !!"U3 L "$9)An"UVVr   c                    U R                  5       n US:X  a  g U S:X  a  [        R                  " U5      nU$ U S:X  a  [        R                  " U5      nU$ [	        SU S S35      e)Nr   reflect	replicatezpadding layer [r   z] is not implemented)r   r   ReflectionPad2dReplicationPad2dr   )pad_typepaddingr   s      r   padr,   ,   ss     ~~H!|9""7+
 L	 
[	 ##G, L "OHQ<?S"TUUr   c                0    X S-
  US-
  -  -   n U S-
  S-  nU$ )N       )kernel_sizedilationr+   s      r   get_valid_paddingr3   ;   s*    qX\ BBKQ1$GNr   c                  4   ^  \ rS rSrU 4S jrS rS rSrU =r$ )ConcatBlockA   c                .   > [         TU ]  5         Xl        g Nsuper__init__subself	submodule	__class__s     r   r;   ConcatBlock.__init__C       r   c                P    [         R                  " XR                  U5      4SS9nU$ )Nr.   )dim)torchcatr<   r>   xoutputs      r   forwardConcatBlock.forwardG   s"    Axx{+3r   c                f    SnU R                   R                  5       R                  SS5      nX-   nU$ )NzIdentity .. 
|

|r<   __repr__replacer>   tmpstrmodstrs      r   rP   ConcatBlock.__repr__K   s2    """$,,T59r   r<   	__name__
__module____qualname____firstlineno__r;   rJ   rP   __static_attributes____classcell__r@   s   @r   r5   r5   A        r   r5   c                  4   ^  \ rS rSrU 4S jrS rS rSrU =r$ )ShortcutBlockR   c                .   > [         TU ]  5         Xl        g r8   r9   r=   s     r   r;   ShortcutBlock.__init__T   rB   r   c                ,    XR                  U5      -   nU$ r8   rV   rG   s      r   rJ   ShortcutBlock.forwardX   s    XXa[r   c                f    SnU R                   R                  5       R                  SS5      nX-   nU$ NzIdentity + 
|rM   rN   rO   rR   s      r   rP   ShortcutBlock.__repr__\   2    !""$,,T59r   rV   rW   r^   s   @r   ra   ra   R   r_   r   ra   c                  4   ^  \ rS rSrU 4S jrS rS rSrU =r$ )ShortcutBlockSPSRc   c                .   > [         TU ]  5         Xl        g r8   r9   r=   s     r   r;   ShortcutBlockSPSR.__init__e   rB   r   c                    XR                   4$ r8   rV   )r>   rH   s     r   rJ   ShortcutBlockSPSR.forwardi   s    (({r   c                f    SnU R                   R                  5       R                  SS5      nX-   nU$ rh   rO   rR   s      r   rP   ShortcutBlockSPSR.__repr__l   rj   r   rV   rW   r^   s   @r   rl   rl   c   s     r   rl   c                    [        U 5      S:X  a(  [        U S   [        5      (       a  [        S5      eU S   $ / nU  H~  n[        U[        R
                  5      (       a*  UR                  5        H  nUR                  U5        M     ML  [        U[        R                  5      (       d  Mm  UR                  U5        M     [        R
                  " U6 $ )Nr.   r   z.sequential does not support OrderedDict input.)	len
isinstancer   r   r   
SequentialchildrenappendModule)argsmodulesmoduler?   s       r   
sequentialr~   s   s    
4yA~d1g{++%&VWWAwGfbmm,,#__.	y) /		**NN6"  =='""r   CNANACCNACc           
         [        [        R                  " XSSS9[        R                  " XSSS9U(       a  [        U5      5      $ S 5      $ )Nr/   r.   )r1   r+   r   )r~   r   Conv2dr   )in_ncout_ncr   s      r   conv_block_2c2r      sI    
 
		%Q:
		&a;!H  (, r   c                   U
S;   d   SU
S S35       e[        X$5      nU(       a  US:w  a  [        X{5      OSnUS:X  a  UOSn[        R                  " U UUUUUUUS9nU	(       a  [	        U	5      OSnU
S	;   a   U(       a  [        X5      OSn[        XX5      $ U
S
:X  a0  Uc  U	b
  [	        U	SS9nU(       a  [        X5      OSn[        XX5      $ [        SU
 35      e)z
Conv layer with padding, normalization, activation
mode:
- ``CNA`` --> Conv -> Norm -> Act
- ``NAC`` --> Norm -> Act --> Conv (Identity Mappings in Deep Residual Networks, ECCV16)
r   zWrong conv mode [r   ]zeroNr   )r1   strider+   r2   biasgroups)r   r   r   F)r   zInvalid conv mode )r3   r,   r   r   r   r$   r~   
ValueError)r   r   r1   r   r2   r   r   r*   r"   r   moder+   pcans                   r   
conv_blockr      s    ( ))H->tAha+HH)6G"*x6/AHtA!V+gG
				A "HtA'0D#d!%%	!5He,A
 '0D"T!%%-dV455r   c                  P   ^  \ rS rSrSr          S SU 4S jjjrS rSrU =r$ )ResNetBlock   z
ResNet Block, 3-3 style
with extra residual scaling used in EDSR
(Enhanced Deep Residual Networks for Single Image Super-Resolution, CVPRW 17)
c                   > [         TU ]  5         [        UUUUUUUU	U
UU5      nUS:X  a  S nUS:X  a  S nS n
[        UUUUUUUU	U
UU5      n[        X5      U l        Xl        g )Nr   r   )r:   r;   r   r~   res	res_scale)r>   r   mid_ncr   r1   r   r2   r   r   r*   r"   r   r   r   conv0conv1r@   s                   r   r;   ResNetBlock.__init__   s      	
 5=H6>HI
& e+"r   c                ^    U R                  U5      R                  U R                  5      nX-   $ r8   )r   mulr   )r>   rH   r   s      r   rJ   ResNetBlock.forward  s#    hhqkoodnn-wr   )r   r   )
   r.   r.   r.   Tr   Nr   r   r.   )r   ConvMode	rX   rY   rZ   r[   __doc__r;   rJ   r\   r]   r^   s   @r   r   r      sG     7# 7# 7#r r   r   c                  V   ^  \ rS rSrSr           S   SU 4S jjjrS rSrU =r$ )RRDBi  zf
Residual in Residual Dense Block
(ESRGAN: Enhanced Super-Resolution Generative Adversarial Networks)
c                   > [         TU ]  5         [        UUUUUUUUU	US9
U l        [        UUUUUUUUU	US9
U l        [        UUUUUUUUU	US9
U l        g )N)plus)r:   r;   ResidualDenseBlock_5CRDB1RDB2RDB3)r>   nfr1   gcr   r   r*   r"   r   r   	_convtype_spectral_normr   r@   s                r   r;   RRDB.__init__  s     	)
	 *
	 *
	r   c                x    U R                  U5      nU R                  U5      nU R                  U5      nUS-  U-   $ )N皙?r   r   r   )r>   rH   outs      r   rJ   RRDB.forwardL  s7    iiliiniinSy1}r   r   )r       r.   Tr   Nr   r   Conv2DFFr   boolr   r   r   r^   s   @r   r   r     sT     3
 3
 3
 3
j r   r   c                  T   ^  \ rS rSrSr          S   SU 4S jjjrS rSrU =r$ )r   iS  a  
Residual Dense Block
Style: 5 convs
The core module of paper: (Residual Dense Network for Image Super-Resolution, CVPR 18)
Modified options that can be used:

- "Partial Convolution based Padding" `arXiv:1811.11718`
- "Spectral normalization" `arXiv:1802.05957`
- "ICASSP 2020 - ESRGAN+ : Further Improving ESRGAN" N. C. {Rakotonirina} and A. {Rasoanaivo}

Args:
    nf (int): Channel number of intermediate features (num_feat).
    gc (int): Channels for each growth (num_grow_ch: growth channel, i.e. intermediate channels).
    convtype (str): the type of convolution to use. Default: 'Conv2D'
    gaussian_noise (bool): enable the ESRGAN+ gaussian noise (no new trainable parameters)
    plus (bool): enable the additional residual paths from ESRGAN+ (adds trainable parameters)
c                n  > [         TU ]  5         U
(       a  [        X5      OS U l        [        UUUUUUUUU	S9	U l        [        X-   UUUUUUUU	S9	U l        [        USU-  -   UUUUUUUU	S9	U l        [        USU-  -   UUUUUUUU	S9	U l        U	S:X  a  S nOUn[        USU-  -   USUUUUUU	S9	U l        g )N)r   r*   r"   r   r   r/   r   r      )	r:   r;   conv1x1r   r   conv2conv3conv4conv5)r>   r   r1   r   r   r   r*   r"   r   r   r   last_actr@   s               r   r;   ResidualDenseBlock_5C.__init__f  s    	 +/wrD  


  G


  RK


  RK


 5=HHRK


r   c           	        U R                  U5      nU R                  [        R                  " X4S5      5      nU R                  (       a  X0R	                  U5      -   nU R                  [        R                  " XU4S5      5      nU R                  [        R                  " XX44S5      5      nU R                  (       a  XS-   nU R                  [        R                  " XX4U4S5      5      nUS-  U-   $ )Nr.   r   )r   r   rE   rF   r   r   r   r   )r>   rH   x1x2x3x4x5s          r   rJ   ResidualDenseBlock_5C.forward  s    ZZ]ZZ		1'1-.<<ll1o%BZZ		1"+q12ZZ		1"/156<<BZZ		1"""5q9:Cx!|r   )r   r   r   r   r   r   )
@   r   r   r.   Tr   Nr   r   Fr   r   r^   s   @r   r   r   S  sU    ( M
 M
 M
 M
^ r   r   c                0    [         R                  " XSUSS9$ )Nr.   F)r1   r   r   )r   r   )	in_planes
out_planesr   s      r   r   r     s    99Y&uUUr   c	                    [        U XS-  -  UUUUSSS9n	[        R                  " U5      n
U(       a  [        Xq5      OSnU(       a  [	        U5      OSn[        XX5      $ )z
Pixel shuffle layer
(Real-Time Single Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolutional
Neural Network, CVPR17)
r/   Nr   r*   r"   r   )r   r   PixelShuffler$   r   r~   )r   r   upscale_factorr1   r   r   r*   r"   r   convpixel_shuffler   r   s                r   pixelshuffle_blockr     sf      !#$	D OON3M#,Y$A!HtAd100r   c
                `    [         R                  " X)S9n
[        U UUUUUUUS9n[        X5      $ )N)scale_factorr   r   )r   Upsampler   r~   )r   r   r   r1   r   r   r*   r"   r   r   upsampler   s               r   upconv_blockr     sA     {{BH	D h%%r   )Tr   r.   )r   str)r"   r   r#   int)r*   r   )r   )r.   r.   r.   Tr   Nr   r   )
r   r   r   r   r"   
str | Noner   r   r   r   )r.   )r/   r   r.   Tr   Nr   )r   r   r   r   r"   r   )r/   r   r.   Tr   Nr   nearest)
__future__r   collectionsr   typingr   rE   torch.nnr   r   r$   r,   r3   rz   r5   ra   rl   r~   r   r   r   r   r   r   r   r   r   r0   r   r   <module>r      sq   # #    	")) "BII "		  #  '( 	  	 !161616 16 16 16rB")) BJ?299 ?DmBII m`V 	 111 1H 	 	&&& &r   