-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
151 lines (114 loc) · 5.05 KB
/
model.py
File metadata and controls
151 lines (114 loc) · 5.05 KB
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
from torch import nn
import torch
from torch.nn import Module, Conv2d, Parameter
def l2_norm(x):
return torch.einsum("bcn, bn->bcn", x, 1 / torch.norm(x, p=2, dim=-2))
def conv3otherRelu(in_planes, out_planes, kernel_size=None, stride=None, padding=None):
# 3x3 convolution with padding and relu
if kernel_size is None:
kernel_size = 3
assert isinstance(kernel_size, (int, tuple)), 'kernel_size is not in (int, tuple)!'
if stride is None:
stride = 1
assert isinstance(stride, (int, tuple)), 'stride is not in (int, tuple)!'
if padding is None:
padding = 1
assert isinstance(padding, (int, tuple)), 'padding is not in (int, tuple)!'
return nn.Sequential(
nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=padding, bias=True),
nn.BatchNorm2d(out_planes),
)
class FeatureExtractionModule(Module):
def __init__(self, in_places):
super(FeatureExtractionModule, self).__init__()
self.conv1 = nn.Conv2d(in_places, in_places, (1, 1))
self.attention = Attention(in_places)
self.conv2 = conv3otherRelu(in_places, in_places, kernel_size=(3, 3), padding=(1, 1))
def forward(self, x):
conv1 = self.conv1(x)
conv1 = self.attention(conv1)
conv2 = self.conv2(x)
return conv1 + conv2
class Attention(Module):
def __init__(self, in_places, scale=4, eps=1e-6):
super(Attention, self).__init__()
self.gamma = Parameter(torch.zeros(1))
self.in_places = in_places
self.l2_norm = l2_norm
self.eps = eps
self.query_conv = Conv2d(in_channels=in_places, out_channels=in_places // scale, kernel_size=(1, 1))
self.key_conv = Conv2d(in_channels=in_places, out_channels=in_places // scale, kernel_size=(1, 1))
self.value_conv = Conv2d(in_channels=in_places, out_channels=in_places, kernel_size=(1, 1))
def forward(self, x):
batch_size, chnnels, width, height = x.shape
Q = self.query_conv(x).view(batch_size, -1, width * height)
K = self.key_conv(x).view(batch_size, -1, width * height)
V = self.value_conv(x).view(batch_size, -1, width * height)
Q = self.l2_norm(Q).permute(-3, -1, -2)
K = self.l2_norm(K)
tailor_sum = 1 / (width * height + torch.einsum("bnc, bc->bn", Q, torch.sum(K, dim=-1) + self.eps))
value_sum = torch.einsum("bcn->bc", V).unsqueeze(-1)
value_sum = value_sum.expand(-1, chnnels, width * height)
matrix = torch.einsum('bmn, bcn->bmc', K, V)
matrix_sum = value_sum + torch.einsum("bnm, bmc->bcn", Q, matrix)
weight_value = torch.einsum("bcn, bn->bcn", matrix_sum, tailor_sum)
weight_value = weight_value.view(batch_size, chnnels, width, height)
return (self.gamma * weight_value).contiguous()
class FeatureFusionModule(nn.Module):
def __init__(self, in_chan, out_chan):
super(FeatureFusionModule, self).__init__()
self.convblk = conv3otherRelu(in_chan, out_chan, 1, 1, 0)
self.conv_atten = Attention(out_chan)
def forward(self, x):
fcat = torch.cat(x, dim=1)
feat = self.convblk(fcat)
atten = self.conv_atten(feat)
feat_atten = torch.mul(feat, atten)
feat_out = feat_atten + feat
return feat_out
class SFNet(nn.Module):
def __init__(self, band_num=1, resolution=None):
super(SFNet, self).__init__()
if resolution is None:
resolution = [30, 50]
self.band_num = band_num
self.name = 'SFNet'
self.resolution = resolution
channels = 32
self.conv_input = nn.Sequential(
conv3otherRelu(self.band_num, channels),
conv3otherRelu(channels, channels),
)
self.FEM1 = FeatureExtractionModule(channels)
self.FEM2 = FeatureExtractionModule(channels)
self.FEM3 = FeatureExtractionModule(channels)
self.FFM = FeatureFusionModule(channels * 3, channels * 3)
self.conv_dim = nn.Sequential(
conv3otherRelu(channels * 3, channels),
)
self.conv_output = nn.Sequential(
nn.Conv2d(channels, channels // 4, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
nn.Conv2d(channels // 4, self.band_num, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
)
self.linear = nn.Sequential(
nn.Linear(in_features=1500,
out_features=1500)
)
def forward(self, x):
batch, channel, height, width = x.shape
conv = self.conv_input(x)
fem1 = self.FEM1(conv)
fem2 = self.FEM2(fem1)
fem3 = self.FEM3(fem2)
ffm = self.FFM([fem1, fem2, fem3])
ffm = self.conv_dim(ffm)
output = self.conv_output(ffm)
output = output.reshape(batch, channel, -1)
output = self.linear(output)
return output.reshape(batch, channel, height, width)
if __name__ == '__main__':
input = torch.randn(8, 1, 30, 50)
yaw = torch.randn(8, 1, 1)
net = SFNet().eval()
y = net(input).squeeze()
print(y.shape)