-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiscriminator.py
316 lines (289 loc) · 13.9 KB
/
discriminator.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
from torch import nn
import torch
# from __future__ import absolute_import
# from __future__ import division
# from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import math
import torchvision.models as models
import pdb
def conv3x3(in_planes, out_planes, pad, dilation, stride=1):
"3x3 convolution with padding"
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=pad, dilation=dilation, bias=True)
def conv1x1(in_planes, out_planes, stride=1):
"3x3 convolution with padding"
return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride,
padding=0, bias=False)
class DDCPP(nn.Module):
def __init__(self, input_channel):
super(DDCPP, self).__init__()
self.reduced_conv = conv3x3(input_channel, 256, 1, 1)
self.reduced_bn = nn.BatchNorm2d(256)
self.ddc_x1 = conv3x3(256, 256, 2, 2)
self.ddc_bn1 = nn.BatchNorm2d(256)
self.ddc_x2 = conv3x3(512, 256, 4, 4)
self.ddc_bn2 = nn.BatchNorm2d(256)
self.ddc_x3 = conv3x3(768, 256, 8, 8)
self.ddc_bn3 = nn.BatchNorm2d(256)
self.post_conv = conv1x1(1024, 512)
self.post_bn = nn.BatchNorm2d(512)
self.pool1_conv = conv1x1(512, 128)
self.pool1_bn = nn.BatchNorm2d(128)
self.pool2_conv = conv1x1(512, 128)
self.pool2_bn = nn.BatchNorm2d(128)
self.pool3_conv = conv1x1(512, 128)
self.pool3_bn = nn.BatchNorm2d(128)
self.pool4_conv = conv1x1(512, 128)
self.pool4_bn = nn.BatchNorm2d(128)
self.conv2 = conv1x1(1024, 512)
self.bn2 = nn.BatchNorm2d(512)
self.conv_cls = conv1x1(512, 2)
#self.fc = nn.Linear(128, 2)
def forward(self, x):
# reduced_x
x_r = F.relu(self.reduced_bn(self.reduced_conv(x)))
#ddc x1
x1 = F.relu(self.ddc_bn1(self.ddc_x1(x_r)))
x1_c = torch.cat((x_r, x1), 1)
# ddc x2
x2 = F.relu(self.ddc_bn2(self.ddc_x2(x1_c)))
x2_c = torch.cat((x1_c, x2), 1)
# ddc x3
x3 = F.relu(self.ddc_bn3(self.ddc_x3(x2_c)))
#all concat
x1_p = torch.cat((x_r, x1), 1)
x2_p = torch.cat((x1_p, x2), 1)
x3_p = torch.cat((x2_p, x3), 1)
#post layers
x_post = F.relu(self.post_bn(self.post_conv(x3_p)))
#First level
x_b_1 = F.avg_pool2d(x_post, (x_post.size(2), x_post.size(3)))
x_b_1 = F.relu(self.pool1_bn(self.pool1_conv(x_b_1)))
#Second level
x_b_2 = F.avg_pool2d(x_post, (x_post.size(2) // 2, x_post.size(3) // 2))
x_b_2 = F.relu(self.pool2_bn(self.pool2_conv(x_b_2)))
# Third level
x_b_3 = F.avg_pool2d(x_post, (x_post.size(2) // 4, x_post.size(3) // 4))
x_b_3 = F.relu(self.pool3_bn(self.pool3_conv(x_b_3)))
# Fourth level
x_b_4 = F.avg_pool2d(x_post, (x_post.size(2) // 8, x_post.size(3) // 8))
x_b_4 = F.relu(self.pool4_bn(self.pool4_conv(x_b_4)))
#unsampling layer
x_b_1_u = F.upsample(input=x_b_1, size=(x_post.size(2), x_post.size(3)), mode='bilinear',align_corners=True)
x_b_2_u = F.upsample(input=x_b_2, size=(x_post.size(2), x_post.size(3)), mode='bilinear',align_corners=True)
x_b_3_u = F.upsample(input=x_b_3, size=(x_post.size(2), x_post.size(3)), mode='bilinear',align_corners=True)
x_b_4_u = F.upsample(input=x_b_4, size=(x_post.size(2), x_post.size(3)), mode='bilinear',align_corners=True)
#concat layer
x_c_1 = torch.cat((x_post,x_b_4_u),1)
x_c_2 = torch.cat((x_c_1, x_b_3_u), 1)
x_c_3 = torch.cat((x_c_2, x_b_2_u), 1)
x_c_4 = torch.cat((x_c_3, x_b_1_u), 1)
#domain classifier
x_p = F.relu(self.bn2(self.conv2(x_c_4)))
#x = F.avg_pool2d(x_p, (x_p.size(2), x_p.size(3)))
#x = x.view(-1, 128)
x = self.conv_cls(x_p)
return x
class ConvBNReLU(nn.Module):#CBR块
def __init__(self, in_chan, out_chan, ks=3, stride=1, padding=1, *args, **kwargs):
super(ConvBNReLU, self).__init__()
self.conv = nn.Conv2d(in_chan,
out_chan,
kernel_size = ks,
stride = stride,
padding = padding,
bias = False)
self.bn = nn.BatchNorm2d(out_chan)
self.relu = nn.ReLU(inplace=True)
self.init_weight()
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
x = self.relu(x)
return x
def init_weight(self):
for ly in self.children():
if isinstance(ly, nn.Conv2d):
nn.init.kaiming_normal_(ly.weight, a=1)
if not ly.bias is None: nn.init.constant_(ly.bias, 0)
class FeatureFusionModule(nn.Module):
def __init__(self, in_chan, out_chan, *args, **kwargs):#256,256
super(FeatureFusionModule, self).__init__()
self.convblk = ConvBNReLU(in_chan, out_chan, ks=1, stride=1, padding=0)
self.conv1 = nn.Conv2d(out_chan,
# out_chan//4,
out_chan//2,
kernel_size = 1,
stride = 1,
padding = 0,
bias = False)
self.conv2 = nn.Conv2d(out_chan//2,
out_chan,
kernel_size = 1,
stride = 1,
padding = 0,
bias = False)
self.relu = nn.ReLU(inplace=True)
self.sigmoid = nn.Sigmoid()
self.init_weight()
def forward(self, fsp, fcp):
fcat = torch.cat([fsp, fcp], dim=1)#torch.Size([14, 256, 128, 256])
feat = self.convblk(fcat)#torch.Size([14, 256, 128, 256])
atten = torch.mean(feat, dim=(2, 3), keepdim=True)
atten = self.conv1(atten)
atten = self.relu(atten)
atten = self.conv2(atten)
atten = self.sigmoid(atten)
feat_atten = torch.mul(feat, atten)
feat_out = feat_atten + feat
return feat_out
def init_weight(self):
for ly in self.children():
if isinstance(ly, nn.Conv2d):
nn.init.kaiming_normal_(ly.weight, a=1)
if not ly.bias is None: nn.init.constant_(ly.bias, 0)
class fc_discriminator(nn.Module):
def __init__(self, num_classes, ndf=128):
super(fc_discriminator, self).__init__()
self.branch1 = nn.Sequential(
nn.Conv2d(num_classes, ndf, kernel_size=4, stride=2, padding=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(ndf, ndf * 2, kernel_size=4, stride=2, padding=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(ndf * 2, ndf * 4, kernel_size=4, stride=2, padding=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(ndf * 4, ndf * 8, kernel_size=4, stride=2, padding=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(ndf * 8, 1, kernel_size=4, stride=2, padding=1),
)
self.branch2 = nn.Sequential(
nn.Conv2d(num_classes, ndf, kernel_size=4, stride=2, padding=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(ndf, ndf * 2, kernel_size=4, stride=2, padding=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(ndf * 2, ndf * 4, kernel_size=4, stride=2, padding=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(ndf * 4, ndf * 4, kernel_size=3, stride=1, padding=1, dilation=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(ndf * 4, ndf * 8, kernel_size=3, stride=1, padding=1, dilation=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),#new
nn.Conv2d(ndf * 8, 1, kernel_size=3, stride=1, padding=1, dilation=1),
)
self.upsample = nn.Upsample(size=5)
self.downsample = nn.MaxPool2d(3, 2, 1)
self.ffm = FeatureFusionModule(2, 2)
self.final = nn.Conv2d(2,1,kernel_size=3,stride=1,padding=1)
self.init_weight()
def forward(self, x):
x=torch.cat([self.upsample(self.branch1(x)), self.downsample(self.branch2(x))], dim=1)
# x_32=self.upsample(self.branch1(x))
# x_8=self.downsample(self.branch2(x))
# x=self.ffm(x_8,x_32)
# x=self.final(x)
return x
def init_weight(self):
for ly in self.children():
if isinstance(ly, nn.Conv2d):
nn.init.kaiming_normal_(ly.weight, a=1)
if not ly.bias is None: nn.init.constant_(ly.bias, 0)
#--------Deeplab-v2--------------
def get_fc_discriminator_add(num_classes, ndf=128):
return nn.Sequential(
nn.Conv2d(num_classes, ndf, kernel_size=4, stride=2, padding=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(ndf, ndf * 2, kernel_size=4, stride=2, padding=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(ndf * 2, ndf * 4, kernel_size=4, stride=2, padding=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(ndf * 4, ndf * 8, kernel_size=4, stride=2, padding=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(ndf * 8, 1, kernel_size=4, stride=2, padding=1),
)
# def get_fc_discriminator_add(num_classes, ndf=128):
# return nn.Sequential(
# nn.Conv2d(num_classes, ndf, kernel_size=4, stride=2, padding=1),
# nn.LeakyReLU(negative_slope=0.2, inplace=True),
# nn.Conv2d(ndf, ndf * 2, kernel_size=4, stride=2, padding=1),
# nn.LeakyReLU(negative_slope=0.2, inplace=True),
# nn.Conv2d(ndf * 2, ndf * 4, kernel_size=4, stride=2, padding=1),
# nn.LeakyReLU(negative_slope=0.2, inplace=True),
# nn.Conv2d(ndf * 4, ndf * 8, kernel_size=4, stride=2, padding=1),
# nn.LeakyReLU(negative_slope=0.2, inplace=True),
# nn.Conv2d(ndf * 8, 1, kernel_size=3, stride=1, padding=1),
# )
#--------UNET--------------D1--------------------
# def get_fc_discriminator(num_classes, ndf=128):
# return nn.Sequential(
# nn.Conv2d(num_classes, ndf, kernel_size=4, stride=2, padding=1),
# nn.LeakyReLU(negative_slope=0.2, inplace=True),
# nn.Conv2d(ndf, ndf * 2, kernel_size=4, stride=2, padding=1),
# nn.LeakyReLU(negative_slope=0.2, inplace=True),
# nn.Conv2d(ndf * 2, ndf * 4, kernel_size=4, stride=2, padding=1),
# nn.LeakyReLU(negative_slope=0.2, inplace=True),
# nn.Conv2d(ndf * 4, ndf * 8, kernel_size=3, stride=1, padding=2, dilation=2),
# nn.LeakyReLU(negative_slope=0.2, inplace=True),
# nn.Conv2d(ndf * 8, 1, kernel_size=3, stride=1, padding=2, dilation=2),
# )
#--------UNET--------------D2--------------------
def get_fc_discriminator(num_classes, ndf=128):#8倍下采样
return nn.Sequential(
nn.Conv2d(num_classes, ndf, kernel_size=4, stride=2, padding=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(ndf, ndf * 2, kernel_size=4, stride=2, padding=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(ndf * 2, ndf * 4, kernel_size=4, stride=2, padding=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(ndf * 4, ndf * 4, kernel_size=3, stride=1, padding=1, dilation=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(ndf * 4, ndf * 8, kernel_size=3, stride=1, padding=1, dilation=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),#new
nn.Conv2d(ndf * 8, 1, kernel_size=3, stride=1, padding=1, dilation=1),
)
# --------UNET--------------D3--------------------
# def get_fc_discriminator(num_classes, ndf=128):#4倍下采样
# return nn.Sequential(
# nn.Conv2d(num_classes, ndf, kernel_size=4, stride=2, padding=1),
# nn.LeakyReLU(negative_slope=0.2, inplace=True),
# nn.Conv2d(ndf, ndf * 2, kernel_size=4, stride=2, padding=1),
# nn.LeakyReLU(negative_slope=0.2, inplace=True),
# nn.Conv2d(ndf * 2, ndf * 4, kernel_size=3, stride=1, padding=1),
# nn.LeakyReLU(negative_slope=0.2, inplace=True),
# nn.Conv2d(ndf * 4, ndf * 4, kernel_size=3, stride=1, padding=1, dilation=1),
# nn.LeakyReLU(negative_slope=0.2, inplace=True),
# nn.Conv2d(ndf * 4, ndf * 8, kernel_size=3, stride=1, padding=1, dilation=1),
# nn.LeakyReLU(negative_slope=0.2, inplace=True),#new
# nn.Conv2d(ndf * 8, 1, kernel_size=3, stride=1, padding=1, dilation=1),
# )
#--------UNET--------------D4--------------------
# def get_fc_discriminator(num_classes, ndf=128):
# return nn.Sequential(
# nn.Conv2d(num_classes, ndf, kernel_size=3, stride=1, padding=1),
# nn.LeakyReLU(negative_slope=0.2, inplace=True),
# nn.Conv2d(ndf, ndf * 2, kernel_size=3, stride=1, padding=1),
# nn.LeakyReLU(negative_slope=0.2, inplace=True),
# nn.Conv2d(ndf * 2, ndf * 4, kernel_size=3, stride=1, padding=1),
# nn.LeakyReLU(negative_slope=0.2, inplace=True),
# nn.Conv2d(ndf * 4, ndf * 8, kernel_size=3, stride=1, padding=2, dilation=2),
# nn.LeakyReLU(negative_slope=0.2, inplace=True),
# nn.Conv2d(ndf * 8, 1, kernel_size=3, stride=1, padding=2, dilation=2),
# )
# def get_fc_discriminator(num_classes, ndf=128):
# return nn.Sequential(
# nn.Conv2d(num_classes, ndf, kernel_size=3, stride=1, padding=1),
# nn.LeakyReLU(negative_slope=0.2, inplace=True),
# nn.Conv2d(ndf, ndf * 2, kernel_size=3, stride=1, padding=1),
# nn.LeakyReLU(negative_slope=0.2, inplace=True),
# nn.Conv2d(ndf * 2, ndf * 4, kernel_size=3, stride=1, padding=1),
# nn.LeakyReLU(negative_slope=0.2, inplace=True),
# nn.Conv2d(ndf * 4, ndf * 8, kernel_size=3, stride=1, padding=2, dilation=2),
# nn.LeakyReLU(negative_slope=0.2, inplace=True),
# nn.Conv2d(ndf * 8, 1, kernel_size=3, stride=1, padding=2, dilation=2),
# )
if __name__ == "__main__":
a=torch.randn([12,192,80,80])
dis_model = fc_discriminator(num_classes=192)
x=dis_model(a)