-
Notifications
You must be signed in to change notification settings - Fork 71
/
ResNet3D.py
93 lines (81 loc) · 2.52 KB
/
ResNet3D.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
import torch.nn.functional as F
class Sideway(nn.Module):
def __init__(self, features):
super(Sideway, self).__init__()
self.bn = nn.BatchNorm3d(num_features = features)
self.conv = nn.Conv3d( in_channels = features,
out_channels = features,
kernel_size = 3,
stride = 1,
padding = 1)
def forward(self, out):
out = F.relu(self.bn(out))
out = F.relu(self.bn(self.conv(out)))
out = self.conv(out)
return out
class ResNet(nn.Module):
def __init__(self):
super(ResNet, self).__init__()
self.conv1_0 = nn.Conv3d(in_channels = 1,
out_channels = 32,
kernel_size = 3,
stride = 1,
padding = 1)
self.conv1_1 = nn.Conv3d( in_channels = 32,
out_channels = 32,
kernel_size = 3,
stride = 1,
padding = 1)
self.bn1_0 = nn.BatchNorm3d(num_features = 32)
self.bn1_1 = nn.BatchNorm3d(num_features = 32)
self.conv2_0 = nn.Conv3d(in_channels = 32,
out_channels = 64,
kernel_size = 3,
stride = 2,
padding = 1)
self.conv2_1 = nn.Conv3d(in_channels = 64,
out_channels = 64,
kernel_size = 3,
stride = 2,
padding = 1)
self.sideway1_0 = Sideway(features = 64)
self.sideway1_1 = Sideway(features = 64)
self.sideway1_2 = Sideway(features = 64)
self.sideway1_3 = Sideway(features = 64)
self.bn2_0 = nn.BatchNorm3d(num_features = 64)
self.bn2_1 = nn.BatchNorm3d(num_features = 64)
self.conv3 = nn.Conv3d( in_channels =64,
out_channels = 128,
kernel_size = 3,
stride = 2,
padding =1)
self.sideway2_0 = Sideway(features = 128)
self.sideway2_1 = Sideway(features = 128)
self.pool = nn.MaxPool3d(kernel_size = 7,
stride = 1)
self.fc1 = nn.Linear(in_features = 128,
out_features = 128)
self.fc2 = nn.Linear(in_features = 128,
out_features = 3)
self.softmax = nn.Softmax()
def forward(self, out):
out = F.relu(self.bn1_0(self.conv1_0(out)))
out = F.relu(self.bn1_1(self.conv1_1(out)))
out = self.conv2_0(out)
out_s = self.sideway1_0(out)
out_s = self.sideway1_1(out+out_s)
out = F.relu(self.bn2_0(out+out_s))
out = self.conv2_1(out)
out_s = self.sideway1_2(out)
out_s = self.sideway1_3(out+out_s)
out = F.relu(self.bn2_1(out+out_s))
out = self.conv3(out)
out_s = self.sideway2_0(out)
out_s = self.sideway2_1(out+out_s)
out_ = self.pool(out+out_s)
out = out_.view(out_.size(0), 128)
out = F.relu(self.fc1(out))
out = self.softmax(self.fc2(out))
return out