-
Notifications
You must be signed in to change notification settings - Fork 3
/
models.py
626 lines (518 loc) · 23.1 KB
/
models.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
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
from torch.nn.parameter import Parameter
import torch
import torch.nn as nn
import torch.nn.functional as F
from lib.pvtv2 import *
import torchvision.ops as ops
def conv3x3(in_, out):
return nn.Conv2d(in_, out, 3, padding=1)
class Conv3BN(nn.Module):
def __init__(self, in_: int, out: int, bn=True):
super().__init__()
self.conv = conv3x3(in_, out)
self.bn = nn.BatchNorm2d(out) if bn else None
self.activation = nn.ReLU(inplace=True)
def forward(self, x):
x = self.conv(x)
if self.bn is not None:
x = self.bn(x)
x = self.activation(x)
return x
class NetModule(nn.Module):
def __init__(self, in_: int, out: int):
super().__init__()
self.l1 = Conv3BN(in_, out)
self.l2 = Conv3BN(out, out)
def forward(self, x):
x = self.l1(x)
x = self.l2(x)
return x
#SE注意力机制
class SELayer(nn.Module):
def __init__(self, channel, reduction=16):
super(SELayer, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Sequential(
nn.Linear(channel, channel // reduction, bias=False),
nn.ReLU(inplace=True),
nn.Linear(channel // reduction, channel, bias=False),
nn.Sigmoid()
)
def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x).view(b, c)
y = self.fc(y).view(b, c, 1, 1)
return x * y.expand_as(x)
#scce注意力模块
class cSE(nn.Module): # noqa: N801
"""
The channel-wise SE (Squeeze and Excitation) block from the
`Squeeze-and-Excitation Networks`__ paper.
Adapted from
https://www.kaggle.com/c/tgs-salt-identification-challenge/discussion/65939
and
https://www.kaggle.com/c/tgs-salt-identification-challenge/discussion/66178
Shape:
- Input: (batch, channels, height, width)
- Output: (batch, channels, height, width) (same shape as input)
__ https://arxiv.org/abs/1709.01507
"""
def __init__(self, in_channels: int, r: int = 16):
"""
Args:
in_channels: The number of channels
in the feature map of the input.
r: The reduction ratio of the intermediate channels.
Default: 16.
"""
super().__init__()
self.linear1 = nn.Linear(in_channels, in_channels // r)
self.linear2 = nn.Linear(in_channels // r, in_channels)
def forward(self, x: torch.Tensor):
"""Forward call."""
input_x = x
x = x.view(*(x.shape[:-2]), -1).mean(-1)
x = F.relu(self.linear1(x), inplace=True)
x = self.linear2(x)
x = x.unsqueeze(-1).unsqueeze(-1)
x = torch.sigmoid(x)
x = torch.mul(input_x, x)
return x
class sSE(nn.Module): # noqa: N801
"""
The sSE (Channel Squeeze and Spatial Excitation) block from the
`Concurrent Spatial and Channel ‘Squeeze & Excitation’
in Fully Convolutional Networks`__ paper.
Adapted from
https://www.kaggle.com/c/tgs-salt-identification-challenge/discussion/66178
Shape:
- Input: (batch, channels, height, width)
- Output: (batch, channels, height, width) (same shape as input)
__ https://arxiv.org/abs/1803.02579
"""
def __init__(self, in_channels: int):
"""
Args:
in_channels: The number of channels
in the feature map of the input.
"""
super().__init__()
self.conv = nn.Conv2d(in_channels, 1, kernel_size=1, stride=1)
def forward(self, x: torch.Tensor):
"""Forward call."""
input_x = x
x = self.conv(x)
x = torch.sigmoid(x)
x = torch.mul(input_x, x)
return x
class scSE(nn.Module): # noqa: N801
"""
The scSE (Concurrent Spatial and Channel Squeeze and Channel Excitation)
block from the `Concurrent Spatial and Channel ‘Squeeze & Excitation’
in Fully Convolutional Networks`__ paper.
Adapted from
https://www.kaggle.com/c/tgs-salt-identification-challenge/discussion/66178
Shape:
- Input: (batch, channels, height, width)
- Output: (batch, channels, height, width) (same shape as input)
__ https://arxiv.org/abs/1803.02579
"""
def __init__(self, in_channels: int, r: int = 16):
"""
Args:
in_channels: The number of channels
in the feature map of the input.
r: The reduction ratio of the intermediate channels.
Default: 16.
"""
super().__init__()
self.cse_block = cSE(in_channels, r)
self.sse_block = sSE(in_channels)
def forward(self, x: torch.Tensor):
"""Forward call."""
cse = self.cse_block(x)
sse = self.sse_block(x)
x = torch.add(cse, sse)
return x
######CBAM
class CBAM(nn.Module):
def __init__(self, channel, reduction=16, spatial_kernel=7):
super(CBAM, self).__init__()
# channel attention 压缩H,W为1
self.max_pool = nn.AdaptiveMaxPool2d(1)
self.avg_pool = nn.AdaptiveAvgPool2d(1)
# shared MLP
self.mlp = nn.Sequential(
nn.Conv2d(channel, channel // reduction, 1, bias=False),
nn.ReLU(inplace=True),
nn.Conv2d(channel // reduction, channel, 1, bias=False)
)
# spatial attention
self.conv = nn.Conv2d(2, 1, kernel_size=spatial_kernel,
padding=spatial_kernel // 2, bias=False)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
max_out = self.mlp(self.max_pool(x))
avg_out = self.mlp(self.avg_pool(x))
channel_out = self.sigmoid(max_out + avg_out)
x = channel_out * x
max_out, _ = torch.max(x, dim=1, keepdim=True)
avg_out = torch.mean(x, dim=1, keepdim=True)
spatial_out = self.sigmoid(self.conv(torch.cat([max_out, avg_out], dim=1)))
x = spatial_out * x
return x
class Residual(nn.Module):
def __init__(self, input_dim, output_dim, stride=1, padding=1):
super(Residual, self).__init__()
self.conv_block = nn.Sequential(
nn.BatchNorm2d(input_dim),
nn.ReLU(),
nn.Conv2d(
input_dim, output_dim, kernel_size=3, stride=stride, padding=padding
),
nn.BatchNorm2d(output_dim),
nn.ReLU(),
nn.Conv2d(output_dim, output_dim, kernel_size=3, padding=1),
)
self.conv_skip = nn.Sequential(
nn.Conv2d(input_dim, output_dim, kernel_size=3, stride=stride, padding=1),
nn.BatchNorm2d(output_dim),
)
def forward(self, x):
return self.conv_block(x) + self.conv_skip(x)
class Conv(nn.Module):
def __init__(self, inp_dim, out_dim, kernel_size=3, stride=1, bn=True, padding=1,relu=True, bias=True):
super(Conv, self).__init__()
self.inp_dim = inp_dim
self.conv = nn.Conv2d(inp_dim, out_dim, kernel_size, stride, padding, bias=bias)
self.relu = relu
self.bn = bn
if relu:
self.relu = nn.ReLU(inplace=True)
if bn:
self.bn = nn.BatchNorm2d(out_dim)
def forward(self, x):
assert x.size()[1] == self.inp_dim, "{} {}".format(x.size()[1], self.inp_dim)
x = self.conv(x)
if self.bn is not None:
x = self.bn(x)
if self.relu is not None:
x = self.relu(x)
return x
class BasicConv2(nn.Module):
def __init__(self, in_planes, out_planes, kernel_size, stride=1, padding=0, dilation=1):
super(BasicConv2, self).__init__()
self.conv = nn.Conv2d(in_planes, out_planes,
kernel_size=kernel_size, stride=stride,
padding=padding, dilation=dilation, bias=False)
self.bn = nn.BatchNorm2d(out_planes)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
x = self.relu(x) #加的
return x
###
class boundary_enhance_module(nn.Module):
def __init__(self, out_channel):
super(boundary_enhance_module, self).__init__()
self.sigmoid = nn.Sigmoid()
self.conv2 = BasicConv2(out_channel, out_channel, 3, padding=1)
def forward(self,low_level,high_level):
##edge semantics
edge = self.sigmoid(low_level)
x = edge*high_level
x = high_level + self.conv2(x)
return x
class cross_module(nn.Module):
# codes derived from DANet 'Dual attention network for scene segmentation'
def __init__(self, in_dim):
super(cross_module, self).__init__()
self.chanel_in = in_dim
self.query_conv1 = nn.Conv2d(in_channels=in_dim, out_channels=in_dim // 2, kernel_size=1)
self.key_conv1 = nn.Conv2d(in_channels=in_dim, out_channels=in_dim // 2, kernel_size=1)
self.value_conv1 = nn.Conv2d(in_channels=in_dim, out_channels=in_dim//2, kernel_size=1)
self.conv1 = nn.Conv2d(in_channels=in_dim// 2, out_channels=in_dim, kernel_size=1)
self.query_conv2 = nn.Conv2d(in_channels=in_dim, out_channels=in_dim // 2, kernel_size=1)
self.key_conv2 = nn.Conv2d(in_channels=in_dim, out_channels=in_dim // 2, kernel_size=1)
self.value_conv2 = nn.Conv2d(in_channels=in_dim, out_channels=in_dim//2, kernel_size=1)
self.conv2 = nn.Conv2d(in_channels=in_dim // 2, out_channels=in_dim, kernel_size=1)
# self.priors = nn.AdaptiveAvgPool2d(output_size=(6, 6))
# self.priors1 = nn.AdaptiveMaxPool2d(output_size=(6, 6))
self.gamma1 = nn.Parameter(torch.zeros(1))
self.gamma2 = nn.Parameter(torch.zeros(1))
self.sigmoid = nn.Sigmoid()
def forward(self, x1, x2):
''' inputs :
x1 : input feature maps( B X C X H X W), boundary semantics
x2 : input feature maps( B X C X H X W), field semantics
returns :
out : attention value + input feature
attention: B X (HxW) X (HxW) '''
m_batchsize, C, height, width = x1.size()
# x1, _ = torch.max(edge, dim=1, keepdim=True)
q1, _ = torch.max(self.query_conv1(x1), dim=1, keepdim=True)
q1 = q1.reshape(m_batchsize, -1, width * height).permute(0, 2, 1)
# q1 = self.query_conv1(x1).reshape(m_batchsize, self.chanel_in//2, -1).permute(0, 2, 1)
k1 = self.key_conv1(x1).view(m_batchsize, -1, width * height)
k1, _ = torch.max(k1, dim=1, keepdim=True)
v1 = self.value_conv1(x1).view(m_batchsize, -1, width * height)
q2 = torch.mean(self.query_conv2(x2), dim=1, keepdim=True).reshape(m_batchsize, -1, width * height).permute(0, 2, 1)
# q2 = self.query_conv2(x2).reshape(m_batchsize, self.chanel_in//2, -1).permute(0, 2, 1)
k2 = self.key_conv2(x2).view(m_batchsize, -1, width * height)
k2 = torch.mean(k2, dim=1, keepdim=True)
v2 = self.value_conv2(x2).view(m_batchsize, -1, width * height)
energy2 = torch.bmm(q1, k2)
attention2 = 1 - self.sigmoid(energy2)
out1 = torch.bmm(v1, attention2.permute(0, 2, 1))
out1 = out1.view(m_batchsize, C//2, height, width)
out1 = self.conv2(out1)
energy1 = torch.bmm(q2, k1)
attention1 = self.sigmoid(energy1)
out2 = torch.bmm(v2, attention1.permute(0, 2, 1))
out2 = out2.view(m_batchsize, C//2, height, width)
out2 = self.conv1(out2)
out1 = x1 + self.gamma1 * out1
out2 = x2 + self.gamma2 * out2
return out1, out2
class DeformableConv2d(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, padding):
super(DeformableConv2d, self).__init__()
self.offset_conv = nn.Conv2d(in_channels, 18, kernel_size=3, padding=1, bias=False)
self.conv = ops.DeformConv2d(in_channels, out_channels, kernel_size=kernel_size, padding=padding)
def forward(self, x):
offset = self.offset_conv(x)
return self.conv(x, offset)
class Multi_BranchesModule(nn.Module):
def __init__(self, in_channels, out_channels):
super(Multi_BranchesModule, self).__init__()
# 分支一:池化后加3x3卷积
self.seq1 = nn.Sequential(
nn.AdaptiveAvgPool2d((6, 6)),
nn.Conv2d(in_channels=in_channels, out_channels=in_channels, kernel_size=1,bias=False),
nn.BatchNorm2d(in_channels),
nn.ReLU(),
nn.Upsample(size=(64, 64), mode='bilinear')
)
# 分支二:3x3、5x5、7x7卷积
self.seq2 = nn.Sequential(
nn.Conv2d(in_channels=in_channels, out_channels=in_channels, kernel_size=3, padding=1,bias=False),
nn.BatchNorm2d(in_channels),
nn.ReLU(),
nn.Conv2d(in_channels=in_channels, out_channels=in_channels, kernel_size=5, padding=2,bias=False),
nn.BatchNorm2d(in_channels),
nn.ReLU(),
nn.Conv2d(in_channels=in_channels, out_channels=in_channels, kernel_size=7, padding=3,bias=False),
nn.BatchNorm2d(in_channels),
nn.ReLU(),
)
# 分支三:膨胀卷积
self.seq3 = nn.Sequential(
nn.Conv2d(in_channels, in_channels, kernel_size=3, padding=1, dilation=1,bias=False), # 膨胀率为1的卷积
nn.BatchNorm2d(in_channels),
nn.ReLU(),
nn.Conv2d(in_channels, in_channels, kernel_size=3, padding=1, dilation=3,bias=False), # 膨胀率为3的卷积
nn.BatchNorm2d(in_channels),
nn.ReLU(),
nn.Conv2d(in_channels, in_channels, kernel_size=3, padding=7, dilation=5,bias=False), # 膨胀率为5的卷积
nn.BatchNorm2d(in_channels),
nn.ReLU()
)
# 分支四:3x3卷积加可变形卷积
self.seq4 = nn.Sequential(
nn.Conv2d(in_channels, in_channels, kernel_size=3, padding=1,bias=False),
nn.BatchNorm2d(in_channels),
nn.ReLU(),
DeformableConv2d(in_channels, in_channels, kernel_size=3, padding=1) # 使用自定义的可变形卷积
)
# 分支五:1x1卷积
self.seq5 = BasicConv2(in_channels, out_channels, kernel_size=1)
###
self.conc1 = nn.Conv2d(in_channels*5, out_channels, kernel_size=1, bias=False)
def forward(self, x):
out1 = self.seq1(x)
out2 = self.seq2(x)
out3 = self.seq3(x)
out4 = self.seq4(x)
out5 = self.seq5(x)
out = torch.concat((out1+out5, out2+out5, out3+out5, out4+out5, out5), dim=1)
out = self.conc1(out)
return out
class PyramidVisionTransformerImpr(nn.Module):
def __init__(self, img_size=224, patch_size=16, in_chans=3, num_classes=1000, embed_dims=[64, 128, 256, 512],
num_heads=[1, 2, 4, 8], mlp_ratios=[4, 4, 4, 4], qkv_bias=False, qk_scale=None, drop_rate=0.,
attn_drop_rate=0., drop_path_rate=0., norm_layer=nn.LayerNorm,
depths=[3, 4, 6, 3], sr_ratios=[8, 4, 2, 1]):
super().__init__()
self.num_classes = num_classes
self.depths = depths
# patch_embed
self.patch_embed1 = OverlapPatchEmbed(img_size=img_size, patch_size=7, stride=4, in_chans=in_chans,
embed_dim=embed_dims[0])
self.patch_embed2 = OverlapPatchEmbed(img_size=img_size // 4, patch_size=3, stride=2, in_chans=embed_dims[0],
embed_dim=embed_dims[1])
self.patch_embed3 = OverlapPatchEmbed(img_size=img_size // 8, patch_size=3, stride=2, in_chans=embed_dims[1],
embed_dim=embed_dims[2])
self.patch_embed4 = OverlapPatchEmbed(img_size=img_size // 16, patch_size=3, stride=2, in_chans=embed_dims[2],
embed_dim=embed_dims[3])
# transformer encoder
dpr = [x.item() for x in torch.linspace(0, drop_path_rate, sum(depths))] # stochastic depth decay rule
cur = 0
self.block1 = nn.ModuleList([Block(
dim=embed_dims[0], num_heads=num_heads[0], mlp_ratio=mlp_ratios[0], qkv_bias=qkv_bias, qk_scale=qk_scale,
drop=drop_rate, attn_drop=attn_drop_rate, drop_path=dpr[cur + i], norm_layer=norm_layer,
sr_ratio=sr_ratios[0])
for i in range(depths[0])])
self.norm1 = norm_layer(embed_dims[0])
cur += depths[0]
self.block2 = nn.ModuleList([Block(
dim=embed_dims[1], num_heads=num_heads[1], mlp_ratio=mlp_ratios[1], qkv_bias=qkv_bias, qk_scale=qk_scale,
drop=drop_rate, attn_drop=attn_drop_rate, drop_path=dpr[cur + i], norm_layer=norm_layer,
sr_ratio=sr_ratios[1])
for i in range(depths[1])])
self.norm2 = norm_layer(embed_dims[1])
cur += depths[1]
self.block3 = nn.ModuleList([Block(
dim=embed_dims[2], num_heads=num_heads[2], mlp_ratio=mlp_ratios[2], qkv_bias=qkv_bias, qk_scale=qk_scale,
drop=drop_rate, attn_drop=attn_drop_rate, drop_path=dpr[cur + i], norm_layer=norm_layer,
sr_ratio=sr_ratios[2])
for i in range(depths[2])])
self.norm3 = norm_layer(embed_dims[2])
cur += depths[2]
self.block4 = nn.ModuleList([Block(
dim=embed_dims[3], num_heads=num_heads[3], mlp_ratio=mlp_ratios[3], qkv_bias=qkv_bias, qk_scale=qk_scale,
drop=drop_rate, attn_drop=attn_drop_rate, drop_path=dpr[cur + i], norm_layer=norm_layer,
sr_ratio=sr_ratios[3])
for i in range(depths[3])])
self.norm4 = norm_layer(embed_dims[3])
# classification head
# self.head = nn.Linear(embed_dims[3], num_classes) if num_classes > 0 else nn.Identity()
self.apply(self._init_weights)
###
self.up1 = nn.Upsample(scale_factor=4, mode='bilinear', align_corners=True)
self.up2 = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
self.up3 = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
self.cbam = CBAM(128)
self.cbam1 = CBAM(512)
self.conv1 = BasicConv2(512, 128, 1)
self.SIM = cross_module(128)
self.BEM = boundary_enhance_module(128)
self.DMFM = Multi_BranchesModule(128, 128)
self.final_1 = nn.Sequential(
BasicConv2(128, 64, 3, padding=1),
nn.Conv2d(64, 1, 1)
)
self.final_2 = nn.Sequential(
BasicConv2(128, 64, 3, padding=1),
nn.Conv2d(64, 1, 1)
)
def _init_weights(self, m):
if isinstance(m, nn.Linear):
trunc_normal_(m.weight, std=.02)
if isinstance(m, nn.Linear) and m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.LayerNorm):
nn.init.constant_(m.bias, 0)
nn.init.constant_(m.weight, 1.0)
elif isinstance(m, nn.Conv2d):
fan_out = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
fan_out //= m.groups
m.weight.data.normal_(0, math.sqrt(2.0 / fan_out))
if m.bias is not None:
m.bias.data.zero_()
def init_weights(self, pretrained=None):
if isinstance(pretrained, str):
logger = 1
#load_checkpoint(self, pretrained, map_location='cpu', strict=False, logger=logger)
def reset_drop_path(self, drop_path_rate):
dpr = [x.item() for x in torch.linspace(0, drop_path_rate, sum(self.depths))]
cur = 0
for i in range(self.depths[0]):
self.block1[i].drop_path.drop_prob = dpr[cur + i]
cur += self.depths[0]
for i in range(self.depths[1]):
self.block2[i].drop_path.drop_prob = dpr[cur + i]
cur += self.depths[1]
for i in range(self.depths[2]):
self.block3[i].drop_path.drop_prob = dpr[cur + i]
cur += self.depths[2]
for i in range(self.depths[3]):
self.block4[i].drop_path.drop_prob = dpr[cur + i]
def freeze_patch_emb(self):
self.patch_embed1.requires_grad = False
@torch.jit.ignore
def no_weight_decay(self):
return {'pos_embed1', 'pos_embed2', 'pos_embed3', 'pos_embed4', 'cls_token'} # has pos_embed may be better
def get_classifier(self):
return self.head
def reset_classifier(self, num_classes, global_pool=''):
self.num_classes = num_classes
self.head = nn.Linear(self.embed_dim, num_classes) if num_classes > 0 else nn.Identity()
def forward(self, x):
B = x.shape[0]
# outs = []
# stage 1
x, H, W = self.patch_embed1(x)
for i, blk in enumerate(self.block1):
x = blk(x, H, W)
x = self.norm1(x)
x = x.reshape(B, H, W, -1).permute(0, 3, 1, 2).contiguous()
level1 = x ###(8,64, 64,64)
# stage 2
x, H, W = self.patch_embed2(level1)
for i, blk in enumerate(self.block2):
x = blk(x, H, W)
x = self.norm2(x)
level2 = x.reshape(B, H, W, -1).permute(0, 3, 1, 2).contiguous() ###(8,128, 32,32)
######boundary extraction
edge0 = self.cbam(level2)
# stage 3, field extraction branch
x, H, W = self.patch_embed3(level2)
for i, blk in enumerate(self.block3):
x = blk(x, H, W)
x = self.norm3(x)
level3 = x.reshape(B, H, W, -1).permute(0, 3, 1, 2).contiguous() ###(8,320, 16,16)
# stage 4
x, H, W = self.patch_embed4(level3)
for i, blk in enumerate(self.block4):
x = blk(x, H, W)
x = self.norm4(x)
level4 = x.reshape(B, H, W, -1).permute(0, 3, 1, 2).contiguous() ###(8,512, 8, 8)
high_level = self.cbam1(level4)
high_level = self.conv1(high_level)
high_level = self.up1(high_level)
##SIM
edge_semantics, high_semantics = self.SIM(edge0, high_level)
##BEM
edge_semantics = self.up2(edge_semantics)
high_semantics = self.up3(high_semantics)
mask = self.BEM(edge_semantics, high_semantics)
##multi-scale information fusion
out1 = self.DMFM(mask)
##outputs
mask = self.final_1(out1)
mask = F.interpolate(mask, scale_factor=4, mode='bilinear')
boundary = self.final_2(edge_semantics)
boundary = F.interpolate(boundary, scale_factor=4, mode='bilinear')
return [mask, boundary]
@register_model
class pvt_v2_b2(PyramidVisionTransformerImpr):
def __init__(self, **kwargs):
super(pvt_v2_b2, self).__init__(
patch_size=4, embed_dims=[64, 128, 320, 512], num_heads=[1, 2, 5, 8], mlp_ratios=[8, 8, 4, 4],
qkv_bias=True, norm_layer=partial(nn.LayerNorm, eps=1e-6), depths=[3, 4, 6, 3], sr_ratios=[8, 4, 2, 1],
drop_rate=0.0, drop_path_rate=0.1)
##
class BFINet(nn.Module):
def __init__(self, path):
super(BFINet, self).__init__()
self.backbone = pvt_v2_b2() # [64, 128, 320, 512]
self.path = path
save_model = torch.load(self.path)
model_dict = self.backbone.state_dict()
state_dict = {k: v for k, v in save_model.items() if k in model_dict.keys()}
model_dict.update(state_dict)
self.backbone.load_state_dict(model_dict)
def forward(self, x):
mask, boundary = self.backbone(x)
return [mask, boundary]