-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathResNet2d.py
More file actions
553 lines (461 loc) · 19 KB
/
ResNet2d.py
File metadata and controls
553 lines (461 loc) · 19 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
# -*- coding: utf-8 -*-
from __future__ import division, print_function
import numpy as np
import torch
import torch.nn as nn
import pdb
from torch.nn import functional as F
from torch.distributions.uniform import Uniform
from networks.resnet import resnet34_2d
class ConvBlock(nn.Module):
"""two convolution layers with batch norm and leaky relu"""
def __init__(self, in_channels, out_channels, dropout_p):
super(ConvBlock, self).__init__()
self.conv_conv = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1),
nn.BatchNorm2d(out_channels),
nn.LeakyReLU(),
nn.Dropout(dropout_p),
nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1),
nn.BatchNorm2d(out_channels),
nn.LeakyReLU()
)
def forward(self, x):
return self.conv_conv(x)
class DownBlock(nn.Module):
"""Downsampling followed by ConvBlock"""
def __init__(self, in_channels, out_channels, dropout_p):
super(DownBlock, self).__init__()
self.maxpool_conv = nn.Sequential(
nn.MaxPool2d(2),
ConvBlock(in_channels, out_channels, dropout_p)
)
def forward(self, x):
return self.maxpool_conv(x)
class UpBlock(nn.Module):
"""Upsampling followed by ConvBlock"""
def __init__(self, in_channels1, in_channels2, out_channels, dropout_p):
super(UpBlock, self).__init__()
self.conv1x1 = nn.Conv2d(in_channels1, in_channels2, kernel_size=1)
self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
self.conv = ConvBlock(in_channels2 * 2, out_channels, dropout_p)
def forward(self, x1, x2):
x1 = self.conv1x1(x1)
x1 = self.up(x1)
x = torch.cat([x2, x1], dim=1)
return self.conv(x)
class Encoder(nn.Module):
def __init__(self, params):
super(Encoder, self).__init__()
self.params = params
self.in_chns = self.params['in_chns']
self.ft_chns = self.params['feature_chns']
self.n_class = self.params['class_num']
self.dropout = self.params['dropout']
assert (len(self.ft_chns) == 5)
self.in_conv = ConvBlock(
self.in_chns, self.ft_chns[0], self.dropout[0])
self.down1 = DownBlock(
self.ft_chns[0], self.ft_chns[1], self.dropout[1])
self.down2 = DownBlock(
self.ft_chns[1], self.ft_chns[2], self.dropout[2])
self.down3 = DownBlock(
self.ft_chns[2], self.ft_chns[3], self.dropout[3])
self.down4 = DownBlock(
self.ft_chns[3], self.ft_chns[4], self.dropout[4])
def forward(self, x):
x0 = self.in_conv(x)
x1 = self.down1(x0)
x2 = self.down2(x1)
x3 = self.down3(x2)
x4 = self.down4(x3)
return [x0, x1, x2, x3, x4]
class Decoder(nn.Module):
def __init__(self, params):
super(Decoder, self).__init__()
self.params = params
self.in_chns = self.params['in_chns']
self.ft_chns = self.params['feature_chns']
self.n_class = self.params['class_num']
assert (len(self.ft_chns) == 5)
self.up1 = UpBlock(self.ft_chns[4], self.ft_chns[3], self.ft_chns[3], dropout_p=0.0)
self.up2 = UpBlock(self.ft_chns[3], self.ft_chns[2], self.ft_chns[2], dropout_p=0.0)
self.up3 = UpBlock(self.ft_chns[2], self.ft_chns[1], self.ft_chns[1], dropout_p=0.0)
self.up4 = UpBlock(self.ft_chns[1], self.ft_chns[0], self.ft_chns[0], dropout_p=0.0)
self.out_conv = nn.Conv2d(self.ft_chns[0], self.n_class, kernel_size=3, padding=1)
def forward(self, feature):
x0 = feature[0]
x1 = feature[1]
x2 = feature[2]
x3 = feature[3]
x4 = feature[4]
x = self.up1(x4, x3)
x = self.up2(x, x2)
x = self.up3(x, x1)
x_last = self.up4(x, x0)
output = self.out_conv(x_last)
return output, x_last
class Decoder_tsne(nn.Module):
def __init__(self, params):
super(Decoder_tsne, self).__init__()
self.params = params
self.in_chns = self.params['in_chns']
self.ft_chns = self.params['feature_chns']
self.n_class = self.params['class_num']
assert (len(self.ft_chns) == 5)
self.up1 = UpBlock(self.ft_chns[4], self.ft_chns[3], self.ft_chns[3], dropout_p=0.0)
self.up2 = UpBlock(self.ft_chns[3], self.ft_chns[2], self.ft_chns[2], dropout_p=0.0)
self.up3 = UpBlock(self.ft_chns[2], self.ft_chns[1], self.ft_chns[1], dropout_p=0.0)
self.up4 = UpBlock(self.ft_chns[1], self.ft_chns[0], self.ft_chns[0], dropout_p=0.0)
self.out_conv = nn.Conv2d(self.ft_chns[0], self.n_class, kernel_size=3, padding=1)
def forward(self, feature):
x0 = feature[0]
x1 = feature[1]
x2 = feature[2]
x3 = feature[3]
x4 = feature[4]
x5 = self.up1(x4, x3) # 1, 128, 32, 32
x6 = self.up2(x5, x2) # 1, 64, 64, 64
x7 = self.up3(x6, x1) # 1, 32, 128, 128
x_last = self.up4(x7, x0) # 1, 16, 256, 256
output = self.out_conv(x_last)
return output, x_last
class UNet(nn.Module):
def __init__(self, in_chns, class_num):
super(UNet, self).__init__()
params = {'in_chns': in_chns,
'feature_chns': [16, 32, 64, 128, 256],
'dropout': [0.05, 0.1, 0.2, 0.3, 0.5],
'class_num': class_num,
'acti_func': 'relu'}
self.encoder = Encoder(params)
self.decoder = Decoder(params)
dim_in = 16
feat_dim = 32
self.projection_head = nn.Sequential(
nn.Linear(dim_in, feat_dim),
nn.BatchNorm1d(feat_dim),
nn.ReLU(inplace=True),
nn.Linear(feat_dim, feat_dim)
)
self.prediction_head = nn.Sequential(
nn.Linear(feat_dim, feat_dim),
nn.BatchNorm1d(feat_dim),
nn.ReLU(inplace=True),
nn.Linear(feat_dim, feat_dim)
)
for class_c in range(4):
selector = nn.Sequential(
nn.Linear(feat_dim, feat_dim),
nn.BatchNorm1d(feat_dim),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Linear(feat_dim, 1)
)
self.__setattr__('contrastive_class_selector_' + str(class_c), selector)
for class_c in range(4):
selector = nn.Sequential(
nn.Linear(feat_dim, feat_dim),
nn.BatchNorm1d(feat_dim),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Linear(feat_dim, 1)
)
self.__setattr__('contrastive_class_selector_memory' + str(class_c), selector)
def forward_projection_head(self, features):
return self.projection_head(features)
def forward_prediction_head(self, features):
return self.prediction_head(features)
def forward(self, x):
feature = self.encoder(x)
output, features = self.decoder(feature)
return output, features
class ResUNet_2d(nn.Module):
def __init__(self, in_chns, class_num):
super(ResUNet_2d, self).__init__()
params = {'in_chns': in_chns,
'feature_chns': [16, 32, 64, 128, 256],
'dropout': [0.05, 0.1, 0.2, 0.3, 0.5],
'class_num': class_num,
'acti_func': 'relu'}
self.encoder = resnet34_2d()
self.decoder = Decoder(params)
dim_in = 16
feat_dim = 32
self.projection_head = nn.Sequential(
nn.Linear(dim_in, feat_dim),
nn.BatchNorm1d(feat_dim),
nn.ReLU(inplace=True),
nn.Linear(feat_dim, feat_dim)
)
self.prediction_head = nn.Sequential(
nn.Linear(feat_dim, feat_dim),
nn.BatchNorm1d(feat_dim),
nn.ReLU(inplace=True),
nn.Linear(feat_dim, feat_dim)
)
for class_c in range(4):
selector = nn.Sequential(
nn.Linear(feat_dim, feat_dim),
nn.BatchNorm1d(feat_dim),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Linear(feat_dim, 1)
)
self.__setattr__('contrastive_class_selector_' + str(class_c), selector)
for class_c in range(4):
selector = nn.Sequential(
nn.Linear(feat_dim, feat_dim),
nn.BatchNorm1d(feat_dim),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Linear(feat_dim, 1)
)
self.__setattr__('contrastive_class_selector_memory' + str(class_c), selector)
for m in self.modules():
if isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d):
torch.nn.init.kaiming_normal_(m.weight)
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def forward_projection_head(self, features):
return self.projection_head(features)
# return self.decoder(features)
def forward_prediction_head(self, features):
return self.prediction_head(features)
def forward(self, x):
feature = self.encoder(x)
output, features = self.decoder(feature)
return output
class Sep_UNet_2d(nn.Module):
def __init__(self, in_chns, class_num):
super(Sep_UNet_2d, self).__init__()
params = {'in_chns': in_chns,
'feature_chns': [16, 32, 64, 128, 256],
'dropout': [0.05, 0.1, 0.2, 0.3, 0.5],
'class_num': class_num,
'acti_func': 'relu'}
self.encoder = Encoder(params)
self.decoder = Decoder(params)
self.pool = nn.MaxPool2d(3, stride=2)
dim_in = 16
feat_dim = 32
self.projection_head = nn.Sequential(
nn.Linear(dim_in, feat_dim),
nn.BatchNorm1d(feat_dim),
nn.ReLU(inplace=True),
nn.Linear(feat_dim, feat_dim)
)
self.prediction_head = nn.Sequential(
nn.Linear(feat_dim, feat_dim),
nn.BatchNorm1d(feat_dim),
nn.ReLU(inplace=True),
nn.Linear(feat_dim, feat_dim)
)
for class_c in range(4):
selector = nn.Sequential(
nn.Linear(feat_dim, feat_dim),
nn.BatchNorm1d(feat_dim),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Linear(feat_dim, 1)
)
self.__setattr__('contrastive_class_selector_' + str(class_c), selector)
for class_c in range(4):
selector = nn.Sequential(
nn.Linear(feat_dim, feat_dim),
nn.BatchNorm1d(feat_dim),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Linear(feat_dim, 1)
)
self.__setattr__('contrastive_class_selector_memory' + str(class_c), selector)
def forward_projection_head(self, features):
return self.projection_head(features)
# return self.decoder(features)
def forward_prediction_head(self, features):
return self.prediction_head(features)
def forward(self, x):
feature = self.encoder(x)
output, features = self.decoder(feature)
feature = self.pool(feature[4])
feature = self.pool(feature)
return feature, output
class GradReverse(torch.autograd.Function):
def __init__(self):
super(GradReverse, self).__init__()
@staticmethod
def forward(ctx, x, lambda_):
ctx.save_for_backward(lambda_)
return x.view_as(x)
@staticmethod
def backward(ctx, grad_output):
lambda_, = ctx.saved_tensors
grad_input = grad_output.clone()
return - lambda_ * grad_input, None
class GradReverseLayer(torch.nn.Module):
def __init__(self, lambd):
super(GradReverseLayer, self).__init__()
self.lambd = lambd
def forward(self, x):
lam = torch.tensor(self.lambd)
return GradReverse.apply(x, lam)
class net_D(nn.Module):
def __init__(self, b_size):
super(net_D, self).__init__()
# self.total_dim = int(total_dim)
self.b_size = b_size
self.total_dim = self.b_size * 256 * 3 * 3
self.model = nn.Sequential(
nn.Linear(self.total_dim, int(self.total_dim / 2)),
nn.Tanh(),
nn.Linear(int(self.total_dim / 2), int(self.total_dim / 4)),
nn.Tanh(),
nn.Linear(int(self.total_dim / 4), 1),
nn.Sigmoid()
)
# self.GRL = GradReverseLayer(1)
def forward(self, x):
# x = self.GRL(x)
x = x.view(1, -1)
x = self.model(x)
return x
class UNet_2dBCP(nn.Module):
def __init__(self, in_chns, class_num):
super(UNet_2dBCP, self).__init__()
params = {'in_chns': in_chns,
'feature_chns': [16, 32, 64, 128, 256],
'dropout': [0.05, 0.1, 0.2, 0.3, 0.5],
'class_num': class_num,
'acti_func': 'relu'}
self.encoder = Encoder(params)
self.decoder = Decoder(params)
def forward(self, x):
feature = self.encoder(x)
output = self.decoder(feature)
return output
class UNet_tsne(nn.Module):
def __init__(self, in_chns, class_num):
super(UNet_tsne, self).__init__()
params = {'in_chns': in_chns,
'feature_chns': [16, 32, 64, 128, 256],
'dropout': [0.05, 0.1, 0.2, 0.3, 0.5],
'class_num': class_num,
'acti_func': 'relu'}
self.encoder = Encoder(params)
self.decoder = Decoder_tsne(params)
dim_in = 16
feat_dim = 32
self.projection_head = nn.Sequential(
nn.Linear(dim_in, feat_dim),
nn.BatchNorm1d(feat_dim),
nn.ReLU(inplace=True),
nn.Linear(feat_dim, feat_dim)
)
self.prediction_head = nn.Sequential(
nn.Linear(feat_dim, feat_dim),
nn.BatchNorm1d(feat_dim),
nn.ReLU(inplace=True),
nn.Linear(feat_dim, feat_dim)
)
for class_c in range(4):
selector = nn.Sequential(
nn.Linear(feat_dim, feat_dim),
nn.BatchNorm1d(feat_dim),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Linear(feat_dim, 1)
)
self.__setattr__('contrastive_class_selector_' + str(class_c), selector)
for class_c in range(4):
selector = nn.Sequential(
nn.Linear(feat_dim, feat_dim),
nn.BatchNorm1d(feat_dim),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Linear(feat_dim, 1)
)
self.__setattr__('contrastive_class_selector_memory' + str(class_c), selector)
def forward_projection_head(self, features):
return self.projection_head(features)
def forward_prediction_head(self, features):
return self.prediction_head(features)
def forward(self, x):
feature = self.encoder(x)
output, features = self.decoder(feature)
return output, features
class UNet_3D(nn.Module):
def __init__(self, in_channel=1, out_channel=2, training=False):
super(UNet_3D, self).__init__()
self.training = training
self.encoder1 = nn.Conv3d(in_channel, 32, 3, stride=1, padding=1) # b, 16, 10, 10
self.encoder2 = nn.Conv3d(32, 64, 3, stride=1, padding=1) # b, 8, 3, 3
self.encoder3 = nn.Conv3d(64, 128, 3, stride=1, padding=1)
self.encoder4 = nn.Conv3d(128, 256, 3, stride=1, padding=1)
# self.encoder5= nn.Conv3d(256, 512, 3, stride=1, padding=1)
# self.decoder1 = nn.Conv3d(512, 256, 3, stride=1,padding=1) # b, 16, 5, 5
self.decoder2 = nn.Conv3d(256, 128, 3, stride=1, padding=1) # b, 8, 15, 1
self.decoder3 = nn.Conv3d(128, 64, 3, stride=1, padding=1) # b, 1, 28, 28
self.decoder4 = nn.Conv3d(64, 32, 3, stride=1, padding=1)
self.decoder5 = nn.Conv3d(32, 2, 3, stride=1, padding=1)
self.map4 = nn.Sequential(
nn.Conv3d(2, out_channel, 1, 1),
nn.Upsample(scale_factor=(1, 2, 2), mode='trilinear'),
nn.Softmax(dim=1)
)
# 128*128 尺度下的映射
self.map3 = nn.Sequential(
nn.Conv3d(64, out_channel, 1, 1),
nn.Upsample(scale_factor=(4, 8, 8), mode='trilinear'),
nn.Softmax(dim=1)
)
# 64*64 尺度下的映射
self.map2 = nn.Sequential(
nn.Conv3d(128, out_channel, 1, 1),
nn.Upsample(scale_factor=(8, 16, 16), mode='trilinear'),
nn.Softmax(dim=1)
)
# 32*32 尺度下的映射
self.map1 = nn.Sequential(
nn.Conv3d(256, out_channel, 1, 1),
nn.Upsample(scale_factor=(16, 32, 32), mode='trilinear'),
nn.Softmax(dim=1)
)
def forward(self, x):
out = F.relu(F.max_pool3d(self.encoder1(x), 2, 2))
t1 = out
out = F.relu(F.max_pool3d(self.encoder2(out), 2, 2))
t2 = out
out = F.relu(F.max_pool3d(self.encoder3(out), 2, 2))
t3 = out
out = F.relu(F.max_pool3d(self.encoder4(out), 2, 2))
# t4 = out
# out = F.relu(F.max_pool3d(self.encoder5(out),2,2))
# t2 = out
# out = F.relu(F.interpolate(self.decoder1(out),scale_factor=(2,2,2),mode ='trilinear'))
# print(out.shape,t4.shape)
output1 = self.map1(out)
out = F.relu(F.interpolate(self.decoder2(out), scale_factor=(2, 2, 2), mode='trilinear'))
out = torch.add(out, t3)
output2 = self.map2(out)
out = F.relu(F.interpolate(self.decoder3(out), scale_factor=(2, 2, 2), mode='trilinear'))
out = torch.add(out, t2)
output3 = self.map3(out)
out = F.relu(F.interpolate(self.decoder4(out), scale_factor=(2, 2, 2), mode='trilinear'))
out = torch.add(out, t1)
out = F.relu(F.interpolate(self.decoder5(out), scale_factor=(2, 2, 2), mode='trilinear'))
output4 = self.map4(out)
# print(out.shape)
# print(output1.shape,output2.shape,output3.shape,output4.shape)
if self.training is True:
return output1, output2, output3, output4
else:
return output4
if __name__ == '__main__':
# compute FLOPS & PARAMETERS
from thop import profile
from thop import clever_format
model = UNet_3D(in_channel=1, out_channel=2)
input = torch.randn(1, 1, 112, 112, 80)
flops, params = profile(model, inputs=(input,))
macs, params = clever_format([flops, params], "%.3f")
print(macs, params)
from ptflops import get_model_complexity_info
with torch.cuda.device(0):
macs, params = get_model_complexity_info(model, (1, 112, 112, 80), as_strings=True,
print_per_layer_stat=True, verbose=True)
print('{:<30} {:<8}'.format('Computational complexity: ', macs))
print('{:<30} {:<8}'.format('Number of parameters: ', params))
# import pdb; pdb.set_trace()