55 lines
1.8 KiB
Python
55 lines
1.8 KiB
Python
import math
|
|
import torch.nn as nn
|
|
import models.basicblock as B
|
|
|
|
|
|
"""
|
|
# --------------------------------------------
|
|
# SR network with Residual in Residual Dense Block (RRDB)
|
|
# "ESRGAN: Enhanced Super-Resolution Generative Adversarial Networks"
|
|
# --------------------------------------------
|
|
"""
|
|
|
|
|
|
class RRDB(nn.Module):
|
|
"""
|
|
gc: number of growth channels
|
|
nb: number of RRDB
|
|
"""
|
|
def __init__(self, in_nc=3, out_nc=3, nc=64, nb=23, gc=32, upscale=4, act_mode='L', upsample_mode='upconv'):
|
|
super(RRDB, self).__init__()
|
|
assert 'R' in act_mode or 'L' in act_mode, 'Examples of activation function: R, L, BR, BL, IR, IL'
|
|
|
|
n_upscale = int(math.log(upscale, 2))
|
|
if upscale == 3:
|
|
n_upscale = 1
|
|
|
|
m_head = B.conv(in_nc, nc, mode='C')
|
|
|
|
m_body = [B.RRDB(nc, gc=32, mode='C'+act_mode) for _ in range(nb)]
|
|
m_body.append(B.conv(nc, nc, mode='C'))
|
|
|
|
if upsample_mode == 'upconv':
|
|
upsample_block = B.upsample_upconv
|
|
elif upsample_mode == 'pixelshuffle':
|
|
upsample_block = B.upsample_pixelshuffle
|
|
elif upsample_mode == 'convtranspose':
|
|
upsample_block = B.upsample_convtranspose
|
|
else:
|
|
raise NotImplementedError('upsample mode [{:s}] is not found'.format(upsample_mode))
|
|
|
|
if upscale == 3:
|
|
m_uper = upsample_block(nc, nc, mode='3'+act_mode)
|
|
else:
|
|
m_uper = [upsample_block(nc, nc, mode='2'+act_mode) for _ in range(n_upscale)]
|
|
|
|
H_conv0 = B.conv(nc, nc, mode='C'+act_mode)
|
|
H_conv1 = B.conv(nc, out_nc, mode='C')
|
|
m_tail = B.sequential(H_conv0, H_conv1)
|
|
|
|
self.model = B.sequential(m_head, B.ShortcutBlock(B.sequential(*m_body)), *m_uper, m_tail)
|
|
|
|
def forward(self, x):
|
|
x = self.model(x)
|
|
return x
|