-
Notifications
You must be signed in to change notification settings - Fork 4
/
lcovnet.py
189 lines (140 loc) · 5.5 KB
/
lcovnet.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
# -*- coding: utf-8 -*-
from __future__ import print_function, division
import torch
import torch.nn as nn
import numpy as np
from torch.utils.checkpoint import checkpoint
class UnetBlock_Encode(nn.Module):
def __init__(self, in_channels, out_channel):
super(UnetBlock_Encode, self).__init__()
self.in_chns = in_channels
self.out_chns = out_channel
self.conv1 = nn.Sequential(
nn.Conv3d(self.in_chns, self.out_chns, kernel_size=(1, 1, 3),
padding=(0, 0, 1)),
nn.BatchNorm3d(self.out_chns),
nn.ReLU(inplace=True)
)
self.conv2_1 = nn.Sequential(
nn.Conv3d(self.out_chns, self.out_chns, kernel_size=(3, 3, 1),
padding=(1, 1, 0), groups=1),
nn.BatchNorm3d(self.out_chns),
nn.ReLU(inplace=True),
nn.Dropout(p=0.2)
)
self.conv2_2 = nn.Sequential(
nn.AvgPool3d(kernel_size=4, stride=2, padding=1),
nn.Conv3d(self.out_chns, self.out_chns, kernel_size=1,
padding=0),
nn.BatchNorm3d(self.out_chns),
nn.Upsample(scale_factor=2, mode='trilinear', align_corners=False)
)
def forward(self, x):
x = self.conv1(x)
x1 = self.conv2_1(x)
x2 = self.conv2_2(x)
x2 = torch.sigmoid(x2)
x = x1 + x2 * x
return x
class UnetBlock_Encode_4(nn.Module):
def __init__(self, in_channels, out_channel):
super(UnetBlock_Encode_4, self).__init__()
self.in_chns = in_channels
self.out_chns = out_channel
self.conv1 = nn.Sequential(
nn.Conv3d(self.in_chns, self.out_chns, kernel_size=(1, 1, 3),
padding=(0, 0, 1)),
nn.BatchNorm3d(self.out_chns),
nn.ReLU(inplace=True)
)
self.conv2_1 = nn.Sequential(
nn.Conv3d(self.out_chns, self.out_chns, kernel_size=(3, 3, 1),
padding=(1, 1, 0), groups=self.out_chns),
nn.BatchNorm3d(self.out_chns),
nn.ReLU(inplace=True),
nn.Dropout(p=0.2)
)
self.conv2_2 = nn.Sequential(
nn.Conv3d(self.out_chns, self.out_chns, kernel_size=1,
padding=0),
nn.BatchNorm3d(self.out_chns)
)
def forward(self, x):
x = self.conv1(x)
x1 = self.conv2_1(x)
x2 = self.conv2_2(x)
x2 = torch.sigmoid(x2)
x = x1 + x2 * x
return x
class UnetBlock_Down(nn.Module):
def __init__(self, in_channels, out_channel):
super(UnetBlock_Down, self).__init__()
self.avg_pool = nn.AvgPool3d(kernel_size=2)
def forward(self, x):
x = self.avg_pool(x)
return x
class UnetBlock_Up(nn.Module):
def __init__(self, in_channels, out_channel):
super(UnetBlock_Up, self).__init__()
self.conv = self.conv1 = nn.Sequential(
nn.Conv3d(in_channels, out_channel, kernel_size=1,
padding=0, groups=1),
nn.BatchNorm3d(out_channel),
nn.ReLU(inplace=True),
nn.Dropout(p=0.2)
)
self.up = nn.Upsample(scale_factor=2, mode='trilinear', align_corners = False)
def forward(self, x):
x = self.conv(x)
x = self.up(x)
return x
class UNet_Seg(nn.Module):
def __init__(self, C_in=1, n_classes=1):
super(UNet_Seg, self).__init__()
self.in_chns = C_in
self.n_class = n_classes
inchn = 32
self.ft_chns = [inchn, inchn*2, inchn*4, inchn*8]
self.resolution_level = len(self.ft_chns)
self.block1 = UnetBlock_Encode(self.in_chns, self.ft_chns[0])
self.block2 = UnetBlock_Encode(self.ft_chns[0], self.ft_chns[1])
self.block3 = UnetBlock_Encode(self.ft_chns[1], self.ft_chns[2])
self.block4 = UnetBlock_Encode_4(self.ft_chns[2], self.ft_chns[3])
self.block5 = UnetBlock_Encode(2*self.ft_chns[2], self.ft_chns[2])
self.block6 = UnetBlock_Encode(2*self.ft_chns[1], self.ft_chns[1])
self.block7 = UnetBlock_Encode(2*self.ft_chns[0], self.ft_chns[0])
self.down1 = UnetBlock_Down(self.ft_chns[0], self.ft_chns[0])
self.down2 = UnetBlock_Down(self.ft_chns[1], self.ft_chns[1])
self.down3 = UnetBlock_Down(self.ft_chns[2], self.ft_chns[2])
self.up1 = UnetBlock_Up(self.ft_chns[3], self.ft_chns[2])
self.up2 = UnetBlock_Up(self.ft_chns[2], self.ft_chns[1])
self.up3 = UnetBlock_Up(self.ft_chns[1], self.ft_chns[0])
self.conv = nn.Conv3d(self.ft_chns[0], self.n_class, kernel_size=3, padding=1)
def forward(self, x):
f1 = self.block1(x)
d1 = self.down1(f1)
f2 = self.block2(d1)
d2 = self.down2(f2)
f3 = self.block3(d2)
d3 = self.down3(f3)
f4 = self.block4(d3)
f4up = self.up1(f4)
f3cat = torch.cat((f3, f4up), dim=1)
f5 = self.block5(f3cat)
f5up = self.up2(f5)
f2cat = torch.cat((f2, f5up), dim=1)
f6 = self.block6(f2cat)
f6up = self.up3(f6)
f1cat = torch.cat((f1, f6up), dim=1)
f7 = self.block7(f1cat)
output = self.conv(f7)
return output
class LCOVNet(nn.Module):
def __init__(self, input_channels, n_classes):
super(LCOVNet, self).__init__()
self.seg_network = UNet_Seg(input_channels, n_classes)
def seg(self, x):
output = self.seg_network(x)
def forward(self, x):
output = self.seg_network(x)
return output