-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
163 lines (125 loc) · 6.33 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
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
from torch import nn
import capsnn
class BasicNet(nn.Module):
def __init__(self):
super(BasicNet, self).__init__()
model = [nn.Conv2d(in_channels=1,out_channels=16,kernel_size=1,padding=0,stride=1),
nn.BatchNorm2d(16),
nn.ReLU(inplace=True)]
# Downsampling
in_features = 16
out_features = in_features*2
for i in range(3):
model += [ nn.Conv2d(in_channels=in_features,out_channels=out_features,kernel_size=3,stride=1,padding=1),
nn.BatchNorm2d(out_features),
nn.ReLU(inplace=True)]
if i != 2:
model += [ nn.Conv2d(in_channels=out_features,out_channels=out_features,kernel_size=3,stride=2,padding=1),
nn.BatchNorm2d(out_features),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_features),
nn.ReLU(inplace=True)]
else:
model += [nn.Conv2d(in_channels=out_features,out_channels=out_features,kernel_size=3,stride=1,padding=1),
nn.BatchNorm2d(out_features),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_features),
nn.ReLU(inplace=True)]
in_features = out_features
out_features = in_features*2
# Upsampling
out_features = in_features//2
for i in range(3):
model += [ nn.ConvTranspose2d(in_channels=in_features,out_channels=in_features,kernel_size=3,stride=1,padding=1),
nn.BatchNorm2d(in_features),
nn.ReLU(inplace=True) ]
if i != 2:
model += [nn.ConvTranspose2d(in_channels=in_features,out_channels=out_features,kernel_size=3,stride=2,padding=1,output_padding=1),
nn.BatchNorm2d(out_features),
nn.ReLU(inplace=True)]
else:
model += [nn.ConvTranspose2d(in_channels=in_features,out_channels=out_features,kernel_size=3,stride=1,padding=1),
nn.BatchNorm2d(out_features),
nn.ReLU(inplace=True)]
in_features = out_features
out_features = in_features//2
# # Output layer
model += [nn.Conv2d(in_channels=16,out_channels=1,kernel_size=1,padding=0,stride=1)]
self.model = nn.Sequential(*model)
def forward(self, x):
return self.model(x)
class CapsBasicNet_PART1(nn.Module):
def __init__(self):
super(CapsBasicNet_PART1, self).__init__()
model = [nn.Conv2d(in_channels=1,out_channels=16,kernel_size=1,padding=0,stride=1),
nn.BatchNorm2d(16),
nn.ReLU(inplace=True)]
# Downsampling
in_features = 16
out_features = in_features*2
for i in range(3):
model += [ nn.Conv2d(in_channels=in_features,out_channels=out_features,kernel_size=3,stride=1,padding=1),
nn.BatchNorm2d(out_features),
nn.ReLU(inplace=True)]
if i != 2:
model += [ nn.Conv2d(in_channels=out_features,out_channels=out_features,kernel_size=3,stride=2,padding=1),
nn.BatchNorm2d(out_features),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_features),
nn.ReLU(inplace=True)]
else:
model += [nn.Conv2d(in_channels=out_features,out_channels=out_features,kernel_size=3,stride=1,padding=1),
nn.BatchNorm2d(out_features),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_features),
nn.ReLU(inplace=True)]
in_features = out_features
out_features = in_features*2
# Capsule transform
model += [capsnn.Conv2CapsuleConv2D(in_channels=128,out_channels=128,dim_caps=8,kernel_size=1,stride=1,padding=0),
capsnn.caps_Conv2d(in_channels=16,out_channels=16,in_capsdim=8,out_capsdim=8,kernel_size=3,padding=1,stride=1,routing_nums=3)]
self.model = nn.Sequential(*model)
def forward(self, x):
return self.model(x)
class CapsBasicNet_PART2(nn.Module):
def __init__(self):
super(CapsBasicNet_PART2,self).__init__()
in_features = 128
out_features = in_features // 2
model = [capsnn.CapsuleConv2D2Conv(in_channels=in_features,out_channels=in_features,kernel_size=1,padding=0,stride=1),
nn.BatchNorm2d(in_features),
nn.ReLU()]
# Upsampling
for i in range(3):
model += [nn.ConvTranspose2d(in_channels=in_features, out_channels=in_features, kernel_size=3, stride=1,
padding=1),
nn.BatchNorm2d(in_features),
nn.ReLU(inplace=True)]
if i != 2:
model += [
nn.ConvTranspose2d(in_channels=in_features, out_channels=out_features, kernel_size=3, stride=2,
padding=1, output_padding=1),
nn.BatchNorm2d(out_features),
nn.ReLU(inplace=True)]
else:
model += [
nn.ConvTranspose2d(in_channels=in_features, out_channels=out_features, kernel_size=3, stride=1,
padding=1),
nn.BatchNorm2d(out_features),
nn.ReLU(inplace=True)]
in_features = out_features
out_features = in_features // 2
# Output layer
model += [nn.Conv2d(in_channels=16, out_channels=1, kernel_size=1, padding=0, stride=1)]
self.model = nn.Sequential(*model)
def forward(self, x):
return self.model(x)
class CapsBasicNet(nn.Module):
def __init__(self):
super(CapsBasicNet,self).__init__()
self.part1 = CapsBasicNet_PART1()
self.part2 = CapsBasicNet_PART2()
def forward(self, x):
out_caps = self.part1(x)
out = self.part2(out_caps)
return out,out_caps