a
    d                     @   s   d dl Z d dl mZ d dlmZ ddlmZmZ G dd dejZG dd	 d	ejZ	G d
d dejZ
e G dd dejZdS )    N)nn)ARCH_REGISTRY   )Upsample
make_layerc                       s*   e Zd ZdZd fdd	Zdd Z  ZS )ChannelAttentionzChannel attention used in RCAN.

    Args:
        num_feat (int): Channel number of intermediate features.
        squeeze_factor (int): Channel squeeze factor. Default: 16.
       c                    s\   t t|   ttdtj||| dddtjddtj|| |dddt | _	d S )Nr   r   )paddingT)Zinplace)
superr   __init__r   
SequentialZAdaptiveAvgPool2dConv2dReLUZSigmoid	attention)selfnum_featsqueeze_factor	__class__ `/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/basicsr/archs/rcan_arch.pyr      s
    $zChannelAttention.__init__c                 C   s   |  |}|| S N)r   )r   xyr   r   r   forward   s    
zChannelAttention.forward)r   __name__
__module____qualname____doc__r   r   __classcell__r   r   r   r   r      s   r   c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )	RCABa  Residual Channel Attention Block (RCAB) used in RCAN.

    Args:
        num_feat (int): Channel number of intermediate features.
        squeeze_factor (int): Channel squeeze factor. Default: 16.
        res_scale (float): Scale the residual. Default: 1.
    r   r   c                    sR   t t|   || _tt||dddtdt||dddt||| _	d S )N   r   T)
r
   r!   r   	res_scaler   r   r   r   r   rcab)r   r   r   r#   r   r   r   r   $   s    (zRCAB.__init__c                 C   s   |  || j }|| S r   )r$   r#   r   r   resr   r   r   r   ,   s    zRCAB.forward)r   r   r   r   r   r   r   r!      s   r!   c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )	ResidualGroupa!  Residual Group of RCAB.

    Args:
        num_feat (int): Channel number of intermediate features.
        num_block (int): Block number in the body network.
        squeeze_factor (int): Channel squeeze factor. Default: 16.
        res_scale (float): Scale the residual. Default: 1.
    r   r   c                    s:   t t|   tt||||d| _t||ddd| _d S )N)r   r   r#   r"   r   )	r
   r'   r   r   r!   residual_groupr   r   conv)r   r   	num_blockr   r#   r   r   r   r   ;   s
    
zResidualGroup.__init__c                 C   s   |  | |}|| S r   )r)   r(   r%   r   r   r   r   B   s    zResidualGroup.forward)r   r   r   r   r   r   r   r'   1   s   	r'   c                       s*   e Zd ZdZd fd	d
	Zdd Z  ZS )RCANa  Residual Channel Attention Networks.

    Paper: Image Super-Resolution Using Very Deep Residual Channel Attention
        Networks
    Ref git repo: https://github.com/yulunzhang/RCAN.

    Args:
        num_in_ch (int): Channel number of inputs.
        num_out_ch (int): Channel number of outputs.
        num_feat (int): Channel number of intermediate features.
            Default: 64.
        num_group (int): Number of ResidualGroup. Default: 10.
        num_block (int): Number of RCAB in ResidualGroup. Default: 16.
        squeeze_factor (int): Channel squeeze factor. Default: 16.
        upscale (int): Upsampling factor. Support 2^n and 3.
            Default: 4.
        res_scale (float): Used to scale the residual in residual block.
            Default: 1.
        img_range (float): Image range. Default: 255.
        rgb_mean (tuple[float]): Image mean in RGB orders.
            Default: (0.4488, 0.4371, 0.4040), calculated from DIV2K dataset.
    @   
   r      r        o@gw#?g8EGr?gB`"?c                    s   t t|   |	| _t|
dddd| _t	||ddd| _
tt|||||d| _t	||ddd| _t||| _t	||ddd| _d S )Nr   r"   )r   r*   r   r#   )r
   r+   r   	img_rangetorchZTensorviewmeanr   r   
conv_firstr   r'   bodyconv_after_bodyr   upsample	conv_last)r   Z	num_in_chZ
num_out_chr   Z	num_groupr*   r   Zupscaler#   r1   Zrgb_meanr   r   r   r   `   s    zRCAN.__init__c                 C   sd   | j || _ || j  | j }| |}| | |}||7 }| | |}|| j | j  }|S r   )r4   Ztype_asr1   r5   r7   r6   r9   r8   r%   r   r   r   r   |   s    
zRCAN.forward)r,   r-   r   r   r.   r   r/   r0   r   r   r   r   r   r+   G   s           r+   )r2   r   Zbasicsr.utils.registryr   Z	arch_utilr   r   Moduler   r!   r'   registerr+   r   r   r   r   <module>   s   