forked from lqz2/SFDFusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules.py
168 lines (140 loc) · 5.64 KB
/
modules.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
def fft(input):
'''
input: tensor of shape (batch_size, 1, height, width)
mask: tensor of shape (height, width)
'''
# 执行2D FFT
img_fft = torch.fft.rfftn(input, dim=(-2, -1))
amp = torch.abs(img_fft)
pha = torch.angle(img_fft)
return amp, pha
class Att_Block(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.att = nn.Sequential(nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1), nn.Sigmoid())
def forward(self, x):
att = self.att(x)
x = x * att
return x
class Sobelxy(nn.Module):
def __init__(self, channels, kernel_size=3, padding=1, stride=1, dilation=1, groups=1):
super(Sobelxy, self).__init__()
sobel_filter = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]])
self.convx = nn.Conv2d(
channels, channels, kernel_size=kernel_size, padding=padding, stride=stride, dilation=dilation, groups=channels, bias=False
)
self.convx.weight.data.copy_(torch.from_numpy(sobel_filter))
self.convy = nn.Conv2d(
channels, channels, kernel_size=kernel_size, padding=padding, stride=stride, dilation=dilation, groups=channels, bias=False
)
self.convy.weight.data.copy_(torch.from_numpy(sobel_filter.T))
def forward(self, x):
sobelx = self.convx(x)
sobely = self.convy(x)
x = torch.abs(sobelx) + torch.abs(sobely)
return x
class DMRM(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.ir_embed = nn.Sequential(nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1), nn.ReLU())
self.vi_embed = nn.Sequential(nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1), nn.ReLU())
self.ir_att1 = Att_Block(out_channels, out_channels)
self.ir_att2 = Att_Block(out_channels, out_channels)
self.vi_att1 = Att_Block(out_channels, out_channels)
self.vi_att2 = Att_Block(out_channels, out_channels)
self.grad_ir = Sobelxy(out_channels)
self.grad_vi = Sobelxy(out_channels)
def forward(self, x, y):
x = self.ir_embed(x)
y = self.vi_embed(y)
# return x, y
t = x + y
x1 = self.ir_att1(x)
y1 = self.vi_att1(y)
x2 = self.ir_att2(t)
y2 = self.vi_att2(t)
ir_grad = self.grad_ir(x)
vi_grad = self.grad_vi(y)
return x1 + x2 + ir_grad, y1 + y2 + vi_grad
class Fuse_block(nn.Module):
def __init__(self, dim, channels=32):
super().__init__()
self.encoder = nn.Sequential(nn.Conv2d(dim, channels, kernel_size=3, stride=1, padding=1), nn.ReLU())
self.down_conv = nn.Sequential(
nn.Sequential(nn.Conv2d(channels, channels * 4, kernel_size=3, stride=1, padding=1), nn.ReLU()),
nn.Sequential(nn.Conv2d(channels * 4, channels * 2, kernel_size=3, stride=1, padding=1), nn.ReLU()),
nn.Sequential(nn.Conv2d(channels * 2, channels, kernel_size=3, stride=1, padding=1), nn.ReLU()),
nn.Sequential(nn.Conv2d(channels, 1, kernel_size=3, stride=1, padding=1), nn.Tanh()),
)
def forward(self, ir, vi, frefus):
x = torch.cat([ir, vi, frefus], dim=1) # n,c,h,w
x = self.encoder(x)
x = self.down_conv(x)
return x
class IFFT(nn.Module):
def __init__(self, out_channels=8):
super().__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(2, out_channels // 2, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(out_channels // 2, out_channels, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
)
def forward(self, amp, pha):
real = amp * torch.cos(pha) + 1e-8
imag = amp * torch.sin(pha) + 1e-8
x = torch.complex(real, imag)
x = torch.abs(torch.fft.irfftn(x, dim=(-2, -1)))
x = torch.cat((torch.max(x, 1)[0].unsqueeze(1), torch.mean(x, 1).unsqueeze(1)), dim=1)
x = self.conv1(x)
return x
class AmpFuse(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(2, 1, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(0.1),
nn.Conv2d(1, 1, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(0.1),
)
def forward(self, f1, f2):
x = torch.cat([f1, f2], dim=1)
x = self.conv1(x)
return x
class PhaFuse(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(2, 1, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(0.1),
nn.Conv2d(1, 1, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(0.1),
)
def forward(self, f1, f2):
x = torch.cat([f1, f2], dim=1)
x = self.conv1(x)
return x
class Fuse(nn.Module):
def __init__(self):
super().__init__()
self.channel = 8
self.dmrm = DMRM(1, self.channel)
self.ff1 = AmpFuse()
self.ff2 = PhaFuse()
self.ifft = IFFT(self.channel)
self.fus_block = Fuse_block(self.channel * 3)
def forward(self, ir, vi):
ir_amp, ir_pha = fft(ir)
vi_amp, vi_pha = fft(vi)
amp = self.ff1(ir_amp, vi_amp)
pha = self.ff2(ir_pha, vi_pha)
frefus = self.ifft(amp, pha)
ir, vi = self.dmrm(ir, vi)
fus = self.fus_block(ir, vi, frefus)
fus = (fus - torch.min(fus)) / (torch.max(fus) - torch.min(fus))
return fus, amp, pha
# return fus, fus, fus