-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathteam03_fmen.py
134 lines (93 loc) · 3.2 KB
/
team03_fmen.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
import torch
import torch.nn as nn
import torch.nn.functional as F
lrelu_value = 0.1
act = nn.LeakyReLU(lrelu_value)
class RepConv(nn.Module):
def __init__(self, n_feats):
super(RepConv, self).__init__()
self.rep_conv = nn.Conv2d(n_feats, n_feats, 3, 1, 1)
def forward(self, x):
out = self.rep_conv(x)
return out
class BasicBlock(nn.Module):
""" Basic block for building HFAN
Args:
n_feats (int): The number of feature maps.
Diagram:
--RepConv--LeakyReLU--RepConv--
"""
def __init__(self, n_feats):
super(BasicBlock, self).__init__()
self.conv1 = RepConv(n_feats)
self.conv2 = RepConv(n_feats)
def forward(self, x):
res = self.conv1(x)
res = act(res)
res = self.conv2(res)
return res
class HFAB(nn.Module):
""" High-Frequency Attention Block
args:
n_feats (int): The number of input feature maps.
up_blocks (int): The number of RepConv in this HFAB.
mid_feats (int): Input feature map numbers of RepConv.
Diagram:
--Reduce_dimension--[RepConv]*up_blocks--Expand_dimension--Sigmoid--
"""
def __init__(self, n_feats, up_blocks, mid_feats):
super(HFAB, self).__init__()
self.squeeze = nn.Conv2d(n_feats, mid_feats, 3, 1, 1)
convs = [BasicBlock(mid_feats) for _ in range(up_blocks)]
self.convs = nn.Sequential(*convs)
self.excitate = nn.Conv2d(mid_feats, n_feats, 3, 1, 1)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
out = act(self.squeeze(x))
out = act(self.convs(out))
out = self.excitate(out)
out = self.sigmoid(out)
out *= x
return out
class FMEN(nn.Module):
""" Fast and Memory-Efficient Network
Diagram:
--Conv--Conv-HFAB-[BasicBlock-HFAB]*down_blocks-Conv-+-Upsample--
|_____________________________________________|
"""
def __init__(self):
super(FMEN, self).__init__()
self.down_blocks = 4
up_blocks = [2, 1, 1, 1, 1]
mid_feats = 16
n_feats = 50
n_colors = 3
scale = 4
# define head module
self.head = nn.Conv2d(n_colors, n_feats, 3, 1, 1)
# warm up
self.warmup = nn.Sequential(
nn.Conv2d(n_feats, n_feats, 3, 1, 1),
HFAB(n_feats, up_blocks[0], mid_feats-4)
)
# define body module
basic_blocks = [BasicBlock(n_feats) for _ in range(self.down_blocks)]
hfabs = [HFAB(n_feats, up_blocks[i+1], mid_feats) for i in range(self.down_blocks)]
self.basic_blocks = nn.ModuleList(basic_blocks)
self.hfabs = nn.ModuleList(hfabs)
self.lr_conv = nn.Conv2d(n_feats, n_feats, 3, 1, 1)
# define tail module
self.tail = nn.Sequential(
nn.Conv2d(n_feats, n_colors*(scale**2), 3, 1, 1),
nn.PixelShuffle(scale)
)
def forward(self, x):
x = self.head(x)
h = self.warmup(x)
for i in range(self.down_blocks):
h = self.basic_blocks[i](h)
h = self.hfabs[i](h)
h = self.lr_conv(h)
h += x
x = self.tail(h)
return x