-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmlegn.py
176 lines (144 loc) · 6.04 KB
/
mlegn.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
166
167
168
169
170
171
172
173
174
175
176
from model import common
import torch
import torch.nn as nn
from torch.nn import Parameter, Softmax
import torch.nn.functional as F
def make_model(args, parent=False):
return MLEGN(args)
class RDB_Conv(nn.Module):
def __init__(self, inChannels, growRate, kernel_size = 3):
super(RDB_Conv, self).__init__()
n_feats = inChannels
rate = growRate
self.conv = nn.Sequential(*[
nn.Conv2d(n_feats, rate, kernel_size, padding=(kernel_size-1)//2, stride=1),
nn.ReLU()
])
def forward(self, x):
out = self.conv(x)
return torch.cat((x, out), 1)
class FEB(nn.Module):
def __init__(self, args, n_layer):
super(FEB, self).__init__()
n_feats = args.n_feats
rate = args.rate
kernel_size = args.kernel_size
convs = []
for n in range(n_layer):
convs.append(RDB_Conv(n_feats + n * rate, rate))
self.convs = nn.Sequential(*convs)
self.LFF = nn.Conv2d(n_feats + n_layer * rate, n_feats, 1, padding=0, stride=1)
def forward(self, x):
out = self.LFF(self.convs(x)) + x
return out
class EA_Module(nn.Module):
""" Edge Attention Module"""
def __init__(self, args):
super(EA_Module, self).__init__()
in_dim = args.in_dim
self.query_conv = nn.Conv2d(in_channels=in_dim, out_channels=in_dim//8, kernel_size=1)
self.key_conv = nn.Conv2d(in_channels=in_dim, out_channels=in_dim//8, kernel_size=1)
self.value_conv = nn.Conv2d(in_channels=in_dim, out_channels=in_dim, kernel_size=1)
self.gamma = Parameter(torch.zeros(1))
self.softmax = Softmax(dim=-1)
def forward(self, x, edge):
"""
inputs :
x : input image feature maps( B X C X H X W)
edge : input edge maps( B X C X H X W)
returns :
out : attention value + input feature
attention: B X (HxW) X (HxW)
"""
m_batchsize, C, height, width = x.size()
proj_query = self.query_conv(edge).view(m_batchsize, -1, width*height).permute(0, 2, 1)
proj_key = self.key_conv(x).view(m_batchsize, -1, width*height)
energy = torch.bmm(proj_query, proj_key)
attention = self.softmax(energy)
proj_value = self.value_conv(x).view(m_batchsize, -1, width*height)
out = torch.bmm(proj_value, attention.permute(0, 2, 1))
out = out.view(m_batchsize, C, height, width)
out = self.gamma*out + x
return out
class RG(nn.Module):
""" Residual Group"""
def __init__(self, n_feats, block, kernel_size=3, conv=common.default_conv):
super(RG, self).__init__()
n_resblock = block
residual_group = []
residual_group = [
common.ResBlock(
conv, n_feats, kernel_size
) for _ in range(n_resblock)]
self.body = nn.Sequential(*residual_group)
def forward(self, x):
res = self.body(x)
out = res + x
return out
class Edge_Net(nn.Module):
def __init__(self, args, n_feats, n_layer, kernel_size=3, conv=common.default_conv):
super(Edge_Net, self).__init__()
self.trans = conv(args.n_colors, n_feats, kernel_size)
self.head = common.ResBlock(conv, n_feats, kernel_size)
self.rdb = FEB(args, n_layer)
self.tail = common.ResBlock(conv, n_feats, kernel_size)
self.rebuilt = conv(n_feats, args.n_colors, kernel_size)
def forward(self, x):
out = self.trans(x)
out = self.head(out)
out = self.rdb(out)
out = self.tail(out)
out = self.rebuilt(out)
out = x - out
return out
class MLEGN(nn.Module):
def __init__(self, args, conv=common.default_conv):
super(MLEGN, self).__init__()
n_feats = args.n_feats
kernel_size = args.kernel_size
block = args.block
n_layer = args.n_layer
self.noise_head = conv(args.n_colors, n_feats, kernel_size)
self.edge_head = conv(args.n_colors, n_feats, kernel_size)
self.Edge_Net = Edge_Net(args, n_feats, n_layer)
self.image_feature = FEB(args, n_layer)
self.edge_feature = FEB(args, n_layer)
self.image_rg_1 = RG(n_feats, block)
self.image_rg_2 = RG(n_feats, block)
self.edge_rg_1 = RG(n_feats, block)
self.edge_rg_2 = RG(n_feats, block)
self.cat_rg_1 = RG(n_feats, block)
self.cat_rg_2 = RG(n_feats, block)
self.cat_rg_3 = RG(n_feats, block)
self.fusion_1 = nn.Conv2d(n_feats * 2, n_feats, 1, padding=0, stride=1)
self.fusion_2 = nn.Conv2d(n_feats * 2, n_feats, 1, padding=0, stride=1)
self.fusion_3 = nn.Conv2d(n_feats * 2, n_feats, 1, padding=0, stride=1)
self.tail = conv(n_feats, args.n_colors, kernel_size)
def forward(self, x):
noise_map = self.noise_head(x)
edge = self.Edge_Net(x)
edge_map = self.edge_head(edge)
##特征编码
image_feature_1 = self.image_feature(noise_map)
edge_feature_1 = self.edge_feature(edge_map)
##第一次edge attention
leve_l = image_feature_1 + edge_feature_1
image_feature_2 = self.image_rg_1(image_feature_1)
edge_feature_2 = self.edge_rg_1(edge_feature_1)
##第二次edge guided
leve_2 = image_feature_2 + edge_feature_2
image_feature_3 = self.image_rg_2(edge_feature_2)
edge_feature_3 = self.edge_rg_2(edge_feature_2)
##第三次edge guided
leve_3 = image_feature_3 + edge_feature_3
cat_1 = torch.cat([leve_l, leve_2], 1)
cat_1 = self.fusion_1(cat_1)
cat_1 = self.cat_rg_1(cat_1)
cat_2 = torch.cat([leve_2, leve_3], 1)
cat_2 = self.fusion_2(cat_2)
cat_2 = self.cat_rg_2(cat_2)
cat_3 = torch.cat([cat_1, cat_2], 1)
cat_3 = self.fusion_3(cat_3)
out = self.cat_rg_3(cat_3)
out = self.tail(out)
return edge, out