-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvnet.py
93 lines (73 loc) · 2.52 KB
/
vnet.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
import torch
import torch.nn as nn
class RepeatConv(nn.Module):
"""
Repeat Conv + PReLU n times
"""
def __init__(self, n_channels, n_conv):
super(RepeatConv, self).__init__()
conv_list = []
for _ in range(n_conv):
conv_list.append(nn.Conv3d(n_channels, n_channels, kernel_size=5, padding=2))
conv_list.append(nn.PReLU())
self.conv = nn.Sequential(
*conv_list
)
def forward(self, x):
return self.conv(x)
class Down(nn.Module):
def __init__(self, in_channels, out_channels, n_conv):
super(Down, self).__init__()
self.downconv = nn.Sequential(
nn.Conv3d(in_channels, out_channels, kernel_size=2, stride=2),
nn.PReLU()
)
self.conv = RepeatConv(out_channels, n_conv)
def forward(self, x):
out = self.downconv(x)
return out + self.conv(out)
class Up(nn.Module):
def __init__(self, in_channels, out_channels, n_conv):
super(Up, self).__init__()
self.upconv = nn.Sequential(
nn.ConvTranspose3d(in_channels, int(out_channels / 2), kernel_size=2, stride=2),
nn.PReLU()
)
self.conv = RepeatConv(out_channels, n_conv)
def forward(self, x, down):
x = self.upconv(x)
cat = torch.cat([x, down], dim=1)
return cat + self.conv(cat)
class VNet(nn.Module):
"""
Main model
"""
def __init__(self, in_channels, num_class):
super(VNet, self).__init__()
self.down1 = nn.Sequential(
nn.Conv3d(1, 16, kernel_size=5, padding=2),
nn.PReLU()
)
self.down2 = Down(16, 32, 2)
self.down3 = Down(32, 64, 3)
self.down4 = Down(64, 128, 3)
self.down5 = Down(128, 256, 3)
self.up1 = Up(256, 256, 3)
self.up2 = Up(256, 128, 3)
self.up3 = Up(128, 64, 2)
self.up4 = Up(64, 32, 1)
self.up5 = nn.Sequential(
nn.Conv3d(32, num_class, kernel_size=1),
nn.PReLU()
)
def forward(self, x):
down1 = self.down1(x) + torch.cat(16*[x], dim=1)
down2 = self.down2(down1)
down3 = self.down3(down2)
down4 = self.down4(down3)
center = self.down5(down4)
up1 = self.up1(center, down4)
up2 = self.up2(up1, down3)
up3 = self.up3(up2, down2)
up4 = self.up4(up3, down1)
return self.up5(up4)