-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
135 lines (113 loc) · 4.08 KB
/
model.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
# full assembly of the sub-parts to form the complete net
import torch
import torch.nn as nn
import torch.nn.functional as F
class SCSEBlock(nn.Module):
def __init__(self, channel, reduction=16):
super().__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.channel_excitation = nn.Sequential(nn.Linear(channel, int(channel // reduction)),
nn.ReLU(inplace=True),
nn.Linear(int(channel // reduction), channel))
self.spatial_se = nn.Conv2d(channel, 1, kernel_size=1,
stride=1, padding=0, bias=False)
def forward(self, x):
bahs, chs, _, _ = x.size()
# Returns a new tensor with the same data as the self tensor but of a different size.
chn_se = self.avg_pool(x).view(bahs, chs)
chn_se = torch.sigmoid(self.channel_excitation(chn_se).view(bahs, chs, 1, 1))
chn_se = torch.mul(x, chn_se)
spa_se = torch.sigmoid(self.spatial_se(x))
spa_se = torch.mul(x, spa_se)
return torch.add(chn_se, 1, spa_se)
class double_conv(nn.Module):
'''(conv => BN => ReLU) * 2'''
def __init__(self, in_ch, out_ch):
super(double_conv, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_ch, out_ch, 3, padding=1),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True),
nn.Conv2d(out_ch, out_ch, 3, padding=1),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True)
)
def forward(self, x):
x = self.conv(x)
return x
class inconv(nn.Module):
def __init__(self, in_ch, out_ch):
super(inconv, self).__init__()
self.conv = double_conv(in_ch, out_ch)
def forward(self, x):
x = self.conv(x)
return x
class down(nn.Module):
def __init__(self, in_ch, out_ch):
super(down, self).__init__()
self.mpconv = nn.Sequential(
nn.MaxPool2d(2),
double_conv(in_ch, out_ch)
)
def forward(self, x):
x = self.mpconv(x)
return x
class up(nn.Module):
def __init__(self, in_ch, out_ch, bilinear=True):
super(up, self).__init__()
if bilinear:
self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
else:
self.up = nn.ConvTranspose2d(in_ch // 2, in_ch // 2, 2, stride=2)
self.conv = double_conv(in_ch, out_ch)
def forward(self, x1, x2):
x1 = self.up(x1)
x2 = F.upsample(x2, (x1.size(2), x1.size(3)), mode='bilinear')
x = torch.cat([x2, x1], dim=1)
x = self.conv(x)
return x
class outconv(nn.Module):
def __init__(self, in_ch, out_ch):
super(outconv, self).__init__()
self.conv = nn.Conv2d(in_ch, out_ch, 1)
def forward(self, x):
x = self.conv(x)
return x
class UNet(nn.Module):
def __init__(self, n_classes):
super(UNet, self).__init__()
self.inc = inconv(15, 64)
self.down1 = down(64, 128)
self.down2 = down(128, 256)
self.down3 = down(256, 512)
self.down4 = down(512, 512)
self.up1 = up(1024, 256)
self.up2 = up(512, 128)
self.up3 = up(256, 64)
self.up4 = up(128, 64)
self.outc = outconv(64, n_classes)
def forward(self, x):
#print ('x_size',x.size())
x1 = self.inc(x)
#print(x1.size())
x2 = self.down1(x1)
#print(x2.size())
x3 = self.down2(x2)
#print(x3.size())
x4 = self.down3(x3)
#print(x4.size())
x5 = self.down4(x4)
#print(x5.size(), x4.size())
#x4 = F.upsample(x4, (x5.size(2)*2, x5.size(3)*2), mode='bilinear')
x6 = self.up1(x5, x4)
#print(x6.size())
x7 = self.up2(x6, x3)
#print(x7.size())
x8 = self.up3(x7, x2)
#print(x8.size())
x9 = self.up4(x8, x1)
#print(x9.size())
x10 = self.outc(x9)
#print(x10.size())
x10 = F.upsample(x10, (x.size(2), x.size(3) ), mode='bilinear')
return x10