a
    d#                     @   s   d dl Zd dlZd dlZd dlmZ d dlmZm	Z	 d dl
mZ d dlmZ d dlmZmZ d dlmZ d dlmZ ejd	d
G dd deZdS )    N)
functional)random_add_gaussian_noise_ptrandom_add_poisson_noise_pt)paired_random_crop)SRModel)DiffJPEGUSMSharp)filter2D)MODEL_REGISTRYZbasicsr)suffixc                       sL   e Zd ZdZ fddZe dd Ze dd Z fdd	Z	  Z
S )
RealESRNetModela  RealESRNet Model for Real-ESRGAN: Training Real-World Blind Super-Resolution with Pure Synthetic Data.

    It is trained without GAN losses.
    It mainly performs:
    1. randomly synthesize LQ images in GPU tensors
    2. optimize the networks with GAN training.
    c                    s>   t t| | tdd | _t  | _|dd| _	d S )NF)Zdifferentiable
queue_size   )
superr   __init__r   cudajpegerr   usm_sharpenergetr   )selfopt	__class__ h/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/basicsr/models/realesrnet_model.pyr      s    zRealESRNetModel.__init__c           	      C   s  | j  \}}}}t| ds| j| dks@J d| j d| t| j||| | _| j \}}}}t| j||| | _	d| _
| j
| jkrdt| j}| j| | _| j	| | _	| jd|ddddddf  }| j	d|ddddddf  }| j  | jd|ddddddf< | j | j	d|ddddddf< || _ || _nl| j  | j| j
| j
| ddddddf< | j | j	| j
| j
| ddddddf< | j
| | _
dS )ac  It is the training pair pool for increasing the diversity in a batch.

        Batch processing limits the diversity of synthetic degradations in a batch. For example, samples in a
        batch could not have different resize scaling factors. Therefore, we employ this training pair pool
        to increase the degradation diversity in a batch.
        queue_lrr   zqueue size z# should be divisible by batch size N)lqsizehasattrr   torchzerosr   r   gtZqueue_gtZ	queue_ptrZrandpermclone)	r   bchw_idxZ
lq_dequeueZ
gt_dequeuer   r   r   _dequeue_and_enqueue   s(    	
$&&((00z$RealESRNetModel._dequeue_and_enqueuec                 C   sl  | j r,| jddr,|d | j| _| jd du rF| | j| _|d | j| _|d | j| _|d | j| _	| j
 dd	 \}}t| j| j}tg d
| jd d }|dkrtjd| jd d }n&|dkrtj| jd d d}nd}tg d}tj|||d}| jd }tj | jd k rZt|| jd dd|d}nt|| jd |ddd}||
dj| jd  }	t|dd}| j||	d}tj | jd k rt|| j}tg d
| jd d }|dkr
tjd| jd d }n(|dkr.tj| jd d d}nd}tg d}tj|t|| jd  | t|| jd  | f|d }| jd! }tj | jd" k rt|| jd# dd|d}nt|| jd$ |ddd}tj d%k rVtg d}tj||| jd  || jd  f|d }t|| j	}||
dj| jd&  }	t|dd}| j||	d}nz||
dj| jd&  }	t|dd}| j||	d}tg d}tj||| jd  || jd  f|d }t|| j	}t|d'  dd(d' | _| jd) }
t| j| j|
| jd \| _| _|   | j | _n<|d* | j| _d|v rh|d | j| _| | j| _ d+S ),z^Accept data from dataloader, and then add two-order degradations to obtain LQ images.
        Zhigh_order_degradationTr!   gt_usmkernel1kernel2sinc_kernel      )updownZkeepZresize_probr   r0      Zresize_ranger1   )areaZbilinearZbicubic)Zscale_factormodegray_noise_probZgaussian_noise_probZnoise_rangeF)Zsigma_rangecliprounds	gray_probZpoisson_scale_range)Zscale_ranger8   r6   r7   Z
jpeg_range)ZqualityZsecond_blur_probZresize_prob2Zresize_range2scale)r   r4   Zgray_noise_prob2Zgaussian_noise_prob2Znoise_range2Zpoisson_scale_range2g      ?Zjpeg_range2g     o@   gt_sizer   N)!is_trainr   r   toZdevicer!   r   r+   r,   r-   r   r	   randomchoicesnpuniformchoiceFZinterpolater   r   Z	new_zerosZuniform_r   clampr   introundr   r   r)   
contiguousr*   )r   dataZori_hZori_woutZupdown_typer9   r4   r5   Zjpeg_pr;   r   r   r   	feed_dataC   s    


.
((
 
zRealESRNetModel.feed_datac                    s&   d| _ tt| |||| d| _ d S )NFT)r<   r   r   nondist_validation)r   Z
dataloaderZcurrent_iterZ	tb_loggerZsave_imgr   r   r   rK      s    z"RealESRNetModel.nondist_validation)__name__
__module____qualname____doc__r   r   Zno_gradr)   rJ   rK   __classcell__r   r   r   r   r      s   
$
ur   )numpyr@   r>   r   Ztorch.nnr   rC   Zbasicsr.data.degradationsr   r   Zbasicsr.data.transformsr   Zbasicsr.models.sr_modelr   Zbasicsr.utilsr   r   Zbasicsr.utils.img_process_utilr	   Zbasicsr.utils.registryr
   registerr   r   r   r   r   <module>   s   
