-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathteam22_rep_rfdn.py
165 lines (145 loc) · 6.63 KB
/
team22_rep_rfdn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import torch
from torch import nn as nn
from torch.nn import functional as F
from collections import OrderedDict
def sequential(*args):
if len(args) == 1:
if isinstance(args[0], OrderedDict):
raise NotImplementedError('sequential does not support OrderedDict input.')
return args[0]
modules = []
for module in args:
if isinstance(module, nn.Sequential):
for submodule in module.children():
modules.append(submodule)
elif isinstance(module, nn.Module):
modules.append(module)
return nn.Sequential(*modules)
def pixelshuffle_block(in_channels, out_channels, upscale_factor=2, kernel_size=3, stride=1):
conv = conv_layer(in_channels, out_channels * (upscale_factor ** 2), kernel_size, stride)
pixel_shuffle = nn.PixelShuffle(upscale_factor)
return sequential(conv, pixel_shuffle)
def conv_layer(in_channels, out_channels, kernel_size, stride=1, dilation=1, groups=1):
padding = int((kernel_size - 1) / 2) * dilation
return nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding=padding, bias=True, dilation=dilation,
groups=groups)
def activation(act_type, inplace=True, neg_slope=0.05, n_prelu=1):
act_type = act_type.lower()
if act_type == 'relu':
layer = nn.ReLU(inplace)
elif act_type == 'lrelu':
layer = nn.LeakyReLU(neg_slope, inplace)
elif act_type == 'prelu':
layer = nn.PReLU(num_parameters=n_prelu, init=neg_slope)
else:
raise NotImplementedError('activation layer [{:s}] is not found'.format(act_type))
return layer
def norm(norm_type, nc):
norm_type = norm_type.lower()
if norm_type == 'batch':
layer = nn.BatchNorm2d(nc, affine=True)
elif norm_type == 'instance':
layer = nn.InstanceNorm2d(nc, affine=False)
else:
raise NotImplementedError('normalization layer [{:s}] is not found'.format(norm_type))
return layer
def pad(pad_type, padding):
pad_type = pad_type.lower()
if padding == 0:
return None
if pad_type == 'reflect':
layer = nn.ReflectionPad2d(padding)
elif pad_type == 'replicate':
layer = nn.ReplicationPad2d(padding)
else:
raise NotImplementedError('padding layer [{:s}] is not implemented'.format(pad_type))
return layer
def get_valid_padding(kernel_size, dilation):
kernel_size = kernel_size + (kernel_size - 1) * (dilation - 1)
padding = (kernel_size - 1) // 2
return padding
def conv_block(in_nc, out_nc, kernel_size, stride=1, dilation=1, groups=1, bias=True,
pad_type='zero', norm_type=None, act_type='relu'):
padding = get_valid_padding(kernel_size, dilation)
p = pad(pad_type, padding) if pad_type and pad_type != 'zero' else None
padding = padding if pad_type == 'zero' else 0
c = nn.Conv2d(in_nc, out_nc, kernel_size=kernel_size, stride=stride, padding=padding,
dilation=dilation, bias=bias, groups=groups)
a = activation(act_type) if act_type else None
n = norm(norm_type, out_nc) if norm_type else None
return sequential(p, c, n, a)
class ESA(nn.Module):
def __init__(self, n_feats, conv=nn.Conv2d):
super(ESA, self).__init__()
f = n_feats // 4
self.conv1 = conv(n_feats, f, kernel_size=1)
self.conv_f = conv(f, f, kernel_size=1)
self.conv_max = conv(f, f, kernel_size=3, padding=1)
self.conv2 = conv(f, f, kernel_size=3, stride=2, padding=0)
self.conv3 = conv(f, f, kernel_size=3, padding=1)
self.conv3_ = conv(f, f, kernel_size=3, padding=1)
self.conv4 = conv(f, n_feats, kernel_size=1)
self.sigmoid = nn.Sigmoid()
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
c1_ = (self.conv1(x))
c1 = self.conv2(c1_)
v_max = F.max_pool2d(c1, kernel_size=7, stride=3)
v_range = self.relu(self.conv_max(v_max))
c3 = self.relu(self.conv3(v_range))
c3 = self.conv3_(c3)
c3 = F.interpolate(c3, (x.size(2), x.size(3)), mode='bilinear', align_corners=False)
cf = self.conv_f(c1_)
c4 = self.conv4(c3 + cf)
m = self.sigmoid(c4)
return x * m
class RFDB(nn.Module):
def __init__(self, in_channels):
super(RFDB, self).__init__()
self.dc = self.distilled_channels = in_channels // 2
self.rc = self.remaining_channels = in_channels
self.c1_d = conv_layer(in_channels, self.dc, 1)
self.c1_r = conv_layer(in_channels, self.rc, 3)
self.c2_d = conv_layer(self.remaining_channels, self.dc, 1)
self.c2_r = conv_layer(self.remaining_channels, self.rc, 3)
self.c3_d = conv_layer(self.remaining_channels, self.dc, 1)
self.c3_r = conv_layer(self.remaining_channels, self.rc, 3)
self.c4 = conv_layer(self.remaining_channels, self.dc, 3)
self.act = activation('lrelu', neg_slope=0.05)
self.c5 = conv_layer(self.dc * 4, in_channels, 1)
self.esa = ESA(in_channels, nn.Conv2d)
def forward(self, input):
distilled_c1 = self.act(self.c1_d(input))
r_c1 = (self.c1_r(input))
r_c1 = self.act(r_c1 + input)
distilled_c2 = self.act(self.c2_d(r_c1))
r_c2 = (self.c2_r(r_c1))
r_c2 = self.act(r_c2 + r_c1)
distilled_c3 = self.act(self.c3_d(r_c2))
r_c3 = (self.c3_r(r_c2))
r_c3 = self.act(r_c3 + r_c2)
r_c4 = self.act(self.c4(r_c3))
out = torch.cat([distilled_c1, distilled_c2, distilled_c3, r_c4], dim=1)
out_fused = self.esa(self.c5(out))
return out_fused
class RFDN40(nn.Module):
def __init__(self, in_nc=3, nf=40, num_modules=4, out_nc=3, upscale=4):
super(RFDN40, self).__init__()
self.fea_conv = conv_layer(in_nc, nf, kernel_size=3)
self.B1 = RFDB(in_channels=nf)
self.B2 = RFDB(in_channels=nf)
self.B3 = RFDB(in_channels=nf)
self.B4 = RFDB(in_channels=nf)
self.c = conv_block(nf * num_modules, nf, kernel_size=1, act_type='lrelu')
self.LR_conv = conv_layer(nf, nf, kernel_size=3)
self.upsampler = pixelshuffle_block(nf, out_nc, upscale_factor=upscale)
def forward(self, input):
out_fea = self.fea_conv(input)
out_B1 = self.B1(out_fea)
out_B2 = self.B2(out_B1)
out_B3 = self.B3(out_B2)
out_B4 = self.B4(out_B3)
out_B = self.c(torch.cat([out_B1, out_B2, out_B3, out_B4], dim=1))
out_lr = self.LR_conv(out_B) + out_fea
output = self.upsampler(out_lr)
return output