-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathdecoder.py
83 lines (69 loc) · 2.7 KB
/
decoder.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from .common import ActivatedBatchNorm, SeparableConv2d
from .ibn import ImprovedIBNaDecoderBlock
from .scse import SELayer, SCSEBlock
from .oc import BaseOC
class DecoderUnetSCSE(nn.Module):
def __init__(self, in_channels, middle_channels, out_channels):
super().__init__()
self.block = nn.Sequential(
nn.Conv2d(in_channels, middle_channels, kernel_size=3, padding=1),
ActivatedBatchNorm(middle_channels),
SCSEBlock(middle_channels),
nn.ConvTranspose2d(middle_channels, out_channels, kernel_size=4, stride=2, padding=1)
)
def forward(self, *args):
x = torch.cat(args, 1)
return self.block(x)
class DecoderUnetSEIBN(nn.Module):
def __init__(self, in_channels, middle_channels, out_channels):
super().__init__()
self.block = nn.Sequential(
SELayer(in_channels),
ImprovedIBNaDecoderBlock(in_channels, out_channels)
)
def forward(self, *args):
x = torch.cat(args, 1)
return self.block(x)
class DecoderUnetOC(nn.Module):
def __init__(self, in_channels, middle_channels, out_channels):
super().__init__()
self.block = nn.Sequential(
nn.Conv2d(in_channels, middle_channels, kernel_size=3, padding=1),
ActivatedBatchNorm(middle_channels),
BaseOC(in_channels=middle_channels,
out_channels=middle_channels,
dropout=0.2),
nn.ConvTranspose2d(middle_channels, out_channels, kernel_size=4, stride=2, padding=1),
)
def forward(self, *args):
x = torch.cat(args, 1)
return self.block(x)
class DecoderSPP(nn.Module):
def __init__(self):
super().__init__()
self.conv = nn.Conv2d(256, 48, 1, bias=False)
self.bn = nn.BatchNorm2d(48)
self.relu = nn.ReLU(inplace=True)
self.sep1 = SeparableConv2d(304, 256, relu_first=False)
self.sep2 = SeparableConv2d(256, 256, relu_first=False)
def forward(self, x, low_level_feat):
x = F.interpolate(x, size=low_level_feat.shape[2:], mode='bilinear', align_corners=True)
low_level_feat = self.conv(low_level_feat)
low_level_feat = self.bn(low_level_feat)
low_level_feat = self.relu(low_level_feat)
x = torch.cat((x, low_level_feat), dim=1)
x = self.sep1(x)
x = self.sep2(x)
return x
def create_decoder(dec_type):
if dec_type == 'unet_scse':
return DecoderUnetSCSE
elif dec_type == 'unet_seibn':
return DecoderUnetSEIBN
elif dec_type == 'unet_oc':
return DecoderUnetOC
else:
raise NotImplementedError