-
Notifications
You must be signed in to change notification settings - Fork 13
/
BiSRNet.py
251 lines (202 loc) · 9.58 KB
/
BiSRNet.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import torch
import numpy as np
import torch.nn as nn
from torchvision import models
from torch.nn import functional as F
from utils.misc import initialize_weights
def conv1x1(in_planes, out_planes, stride=1):
return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)
def conv3x3(in_planes, out_planes, stride=1):
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False)
class FCN(nn.Module):
def __init__(self, in_channels=3, pretrained=True):
super(FCN, self).__init__()
resnet = models.resnet34(pretrained)
newconv1 = nn.Conv2d(in_channels, 64, kernel_size=7, stride=2, padding=3, bias=False)
newconv1.weight.data[:, 0:3, :, :].copy_(resnet.conv1.weight.data[:, 0:3, :, :])
if in_channels>3:
newconv1.weight.data[:, 3:in_channels, :, :].copy_(resnet.conv1.weight.data[:, 0:in_channels-3, :, :])
self.layer0 = nn.Sequential(newconv1, resnet.bn1, resnet.relu)
self.maxpool = resnet.maxpool
self.layer1 = resnet.layer1
self.layer2 = resnet.layer2
self.layer3 = resnet.layer3
self.layer4 = resnet.layer4
for n, m in self.layer3.named_modules():
if 'conv1' in n or 'downsample.0' in n:
m.stride = (1, 1)
for n, m in self.layer4.named_modules():
if 'conv1' in n or 'downsample.0' in n:
m.stride = (1, 1)
def _make_layer(self, block, inplanes, planes, blocks, stride=1):
downsample = None
if stride != 1 or inplanes != planes:
downsample = nn.Sequential(
conv1x1(inplanes, planes, stride),
nn.BatchNorm2d(planes) )
layers = []
layers.append(block(inplanes, planes, stride, downsample))
self.inplanes = planes * block.expansion
for _ in range(1, blocks):
layers.append(block(self.inplanes, planes))
return nn.Sequential(*layers)
class ResBlock(nn.Module):
expansion = 1
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(ResBlock, self).__init__()
self.conv1 = conv3x3(inplanes, planes, stride)
self.bn1 = nn.BatchNorm2d(planes)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv3x3(planes, planes)
self.bn2 = nn.BatchNorm2d(planes)
self.downsample = downsample
self.stride = stride
def forward(self, x):
identity = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
identity = self.downsample(x)
out += identity
out = self.relu(out)
return out
class DecoderBlock(nn.Module):
def __init__(self, in_channels, n_filters):
super(DecoderBlock,self).__init__()
self.conv1 = nn.Conv2d(in_channels, in_channels // 4, 1)
self.norm1 = nn.BatchNorm2d(in_channels // 4)
self.relu = nn.ReLU()
self.deconv2 = nn.ConvTranspose2d(in_channels // 4, in_channels // 4, 3, stride=2, padding=1, output_padding=1)
self.norm2 = nn.BatchNorm2d(in_channels // 4)
self.conv3 = nn.Conv2d(in_channels // 4, n_filters, 1)
self.norm3 = nn.BatchNorm2d(n_filters)
def forward(self, x):
x = self.conv1(x)
x = self.norm1(x)
x = self.relu(x)
x = self.deconv2(x)
x = self.norm2(x)
x = self.relu(x)
x = self.conv3(x)
x = self.norm3(x)
x = self.relu(x)
return x
class SR(nn.Module):
'''Spatial reasoning module'''
#codes from DANet 'Dual attention network for scene segmentation'
def __init__(self, in_dim):
super(SR, self).__init__()
self.chanel_in = 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 = nn.Parameter(torch.zeros(1))
self.softmax = nn.Softmax(dim=-1)
def forward(self, x):
''' inputs :
x : input feature 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(x).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 = x+self.gamma*out
return out
class CotSR(nn.Module):
#codes derived from DANet 'Dual attention network for scene segmentation'
def __init__(self, in_dim):
super(CotSR, self).__init__()
self.chanel_in = in_dim
self.query_conv1 = nn.Conv2d(in_channels=in_dim, out_channels=in_dim//8, kernel_size=1)
self.key_conv1 = nn.Conv2d(in_channels=in_dim, out_channels=in_dim//8, kernel_size=1)
self.value_conv1 = nn.Conv2d(in_channels=in_dim, out_channels=in_dim, kernel_size=1)
self.query_conv2 = nn.Conv2d(in_channels=in_dim, out_channels=in_dim//8, kernel_size=1)
self.key_conv2 = nn.Conv2d(in_channels=in_dim, out_channels=in_dim//8, kernel_size=1)
self.value_conv2 = nn.Conv2d(in_channels=in_dim, out_channels=in_dim, kernel_size=1)
self.gamma1 = nn.Parameter(torch.zeros(1))
self.gamma2 = nn.Parameter(torch.zeros(1))
self.softmax = nn.Softmax(dim=-1)
def forward(self, x1, x2):
''' inputs :
x1 : input feature maps( B X C X H X W)
x2 : input feature 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 = x1.size()
q1 = self.query_conv1(x1).view(m_batchsize, -1, width*height).permute(0, 2, 1)
k1 = self.key_conv1(x1).view(m_batchsize, -1, width*height)
v1 = self.value_conv1(x1).view(m_batchsize, -1, width*height)
q2 = self.query_conv2(x2).view(m_batchsize, -1, width*height).permute(0, 2, 1)
k2 = self.key_conv2(x2).view(m_batchsize, -1, width*height)
v2 = self.value_conv2(x2).view(m_batchsize, -1, width*height)
energy1 = torch.bmm(q1, k2)
attention1 = self.softmax(energy1)
out1 = torch.bmm(v2, attention1.permute(0, 2, 1))
out1 = out1.view(m_batchsize, C, height, width)
energy2 = torch.bmm(q2, k1)
attention2 = self.softmax(energy2)
out2 = torch.bmm(v1, attention2.permute(0, 2, 1))
out2 = out2.view(m_batchsize, C, height, width)
out1 = x1 + self.gamma1*out1
out2 = x2 + self.gamma2*out2
return out1, out2
class BiSRNet(nn.Module):
def __init__(self, in_channels=3, num_classes=7):
super(BiSRNet, self).__init__()
self.FCN = FCN(in_channels, pretrained=True)
self.SiamSR = SR(128)
self.CotSR = CotSR(128)
self.head = nn.Sequential(nn.Conv2d(512, 128, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(128), nn.ReLU())
self.res1 = self._make_layer(ResBlock, 256, 128, 6, stride=1)
self.classifier1 = nn.Conv2d(128, num_classes, kernel_size=1)
self.classifier2 = nn.Conv2d(128, num_classes, kernel_size=1)
self.CD = nn.Sequential(nn.Conv2d(128, 64, kernel_size=1), nn.BatchNorm2d(64), nn.ReLU(), nn.Conv2d(64, 1, kernel_size=1))
initialize_weights(self.head, self.SiamSR, self.res1, self.CD, self.CotSR, self.classifier1, self.classifier2)
def _make_layer(self, block, inplanes, planes, blocks, stride=1):
downsample = None
if stride != 1 or inplanes != planes:
downsample = nn.Sequential(
conv1x1(inplanes, planes, stride),
nn.BatchNorm2d(planes) )
layers = []
layers.append(block(inplanes, planes, stride, downsample))
self.inplanes = planes * block.expansion
for _ in range(1, blocks):
layers.append(block(self.inplanes, planes))
return nn.Sequential(*layers)
def base_forward(self, x):
x = self.FCN.layer0(x) #size:1/4
x = self.FCN.maxpool(x) #size:1/4
x = self.FCN.layer1(x) #size:1/4
x = self.FCN.layer2(x) #size:1/8
x = self.FCN.layer3(x) #size:1/16
x = self.FCN.layer4(x)
x = self.head(x)
x = self.SiamSR(x)
return x
def CD_forward(self, x1, x2):
b,c,h,w = x1.size()
x = torch.cat([x1,x2], 1)
x = self.res1(x)
change = self.CD(x)
return change
def forward(self, x1, x2):
x_size = x1.size()
x1 = self.base_forward(x1)
x2 = self.base_forward(x2)
change = self.CD_forward(x1, x2)
x1, x2 = self.CotSR(x1, x2)
out1 = self.classifier1(x1)
out2 = self.classifier2(x2)
return F.upsample(change, x_size[2:], mode='bilinear'), F.upsample(out1, x_size[2:], mode='bilinear'), F.upsample(out2, x_size[2:], mode='bilinear')