-
Notifications
You must be signed in to change notification settings - Fork 11
/
model.py
228 lines (172 loc) · 7.23 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""
Copyright (c) 2018, National Institute of Informatics
All rights reserved.
Author: Huy H. Nguyen
-----------------------------------------------------
Script for Capsule-Forensics model
"""
import sys
sys.setrecursionlimit(15000)
import torch
import torch.nn.functional as F
from torch import nn
import torch.backends.cudnn as cudnn
from torch.autograd import Variable
import torchvision.models as models
class StatsNet(nn.Module):
def __init__(self):
super(StatsNet, self).__init__()
def forward(self, x):
x = x.view(x.data.shape[0], x.data.shape[1], x.data.shape[2]*x.data.shape[3])
mean = torch.mean(x, 2)
std = torch.std(x, 2)
return torch.stack((mean, std), dim=1)
class View(nn.Module):
def __init__(self, *shape):
super(View, self).__init__()
self.shape = shape
def forward(self, input):
return input.view(self.shape)
class VggExtractor(nn.Module):
def __init__(self):
super(VggExtractor, self).__init__()
self.vgg_1 = self.Vgg(models.vgg19(pretrained=True), 0, 18)
self.vgg_1.eval()
def Vgg(self, vgg, begin, end):
features = nn.Sequential(*list(vgg.features.children())[begin:(end+1)])
return features
def forward(self, input):
return self.vgg_1(input)
class FeatureExtractor(nn.Module):
def __init__(self):
super(FeatureExtractor, self).__init__()
self.ext_1 = nn.Sequential(
nn.Conv2d(256, 64, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 16, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(16),
nn.ReLU(),
StatsNet(),
nn.Conv1d(2, 8, kernel_size=5, stride=2, padding=2),
nn.BatchNorm1d(8),
nn.Conv1d(8, 1, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(1),
View(-1, 8),
)
self.ext_2 = nn.Sequential(
nn.Conv2d(256, 64, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 16, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(16),
nn.ReLU(),
StatsNet(),
nn.Conv1d(2, 8, kernel_size=5, stride=2, padding=2),
nn.BatchNorm1d(8),
nn.Conv1d(8, 1, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(1),
View(-1, 8),
)
self.ext_3 = nn.Sequential(
nn.Conv2d(256, 64, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 16, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(16),
nn.ReLU(),
StatsNet(),
nn.Conv1d(2, 8, kernel_size=5, stride=2, padding=2),
nn.BatchNorm1d(8),
nn.Conv1d(8, 1, kernel_size=3, stride=1, padding=1),
nn.BatchNorm1d(1),
View(-1, 8),
)
def squash(self, tensor, dim):
squared_norm = (tensor ** 2).sum(dim=dim, keepdim=True)
scale = squared_norm / (1 + squared_norm)
return scale * tensor / (torch.sqrt(squared_norm))
def forward(self, x):
output_1 = self.ext_1(x.detach())
output_2 = self.ext_2(x.detach())
output_3 = self.ext_3(x.detach())
output = torch.stack((output_1, output_2, output_3), dim=-1)
return self.squash(output, dim=-1)
class RoutingLayer(nn.Module):
def __init__(self, gpu_id, num_input_capsules, num_output_capsules, data_in, data_out, num_iterations):
super(RoutingLayer, self).__init__()
self.gpu_id = gpu_id
self.num_iterations = num_iterations
self.route_weights = nn.Parameter(torch.randn(num_output_capsules, num_input_capsules, data_out, data_in))
def squash(self, tensor, dim):
squared_norm = (tensor ** 2).sum(dim=dim, keepdim=True)
scale = squared_norm / (1 + squared_norm)
return scale * tensor / (torch.sqrt(squared_norm))
def forward(self, x, random=True):
# x[b, data, in_caps]
x = x.transpose(2, 1)
# x[b, in_caps, data]
if random:
noise = Variable(0.01*torch.randn(*self.route_weights.size()))
if self.gpu_id >= 0:
noise = noise.cuda(self.gpu_id)
route_weights = self.route_weights + noise
else:
route_weights = self.route_weights
priors = route_weights[:, None, :, :, :] @ x[None, :, :, :, None]
# route_weights [out_caps , 1 , in_caps , data_out , data_in]
# x [ 1 , b , in_caps , data_in , 1 ]
# priors [out_caps , b , in_caps , data_out, 1 ]
priors = priors.transpose(1, 0)
# priors[b, out_caps, in_caps, data_out, 1]
logits = Variable(torch.zeros(*priors.size()))
# logits[b, out_caps, in_caps, data_out, 1]
if self.gpu_id >= 0:
logits = logits.cuda(self.gpu_id)
num_iterations = self.num_iterations
for i in range(num_iterations):
probs = F.softmax(logits, dim=2)
outputs = self.squash((probs * priors).sum(dim=2, keepdim=True), dim=3)
if i != self.num_iterations - 1:
delta_logits = priors * outputs
logits = logits + delta_logits
# outputs[b, out_caps, 1, data_out, 1]
outputs = outputs.squeeze()
if len(outputs.shape) == 3:
outputs = outputs.transpose(2, 1).contiguous()
else:
outputs = outputs.unsqueeze_(dim=0).transpose(2, 1).contiguous()
# outputs[b, data_out, out_caps]
return outputs
class CapsuleNet(nn.Module):
def __init__(self, gpu_id):
super(CapsuleNet, self).__init__()
self.fea_ext = FeatureExtractor()
self.fea_ext.apply(self.weights_init)
self.routing_stats = RoutingLayer(gpu_id=gpu_id, num_input_capsules=3, num_output_capsules=2, data_in=8, data_out=4, num_iterations=2)
def weights_init(self, m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
m.weight.data.normal_(0.0, 0.02)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
def forward(self, x, random):
z = self.fea_ext(x)
z = self.routing_stats(z, random)
# z[b, data, out_caps]
classes = F.softmax(z, dim=-1)
class_ = classes.detach()
class_ = class_.mean(dim=1)
return classes, class_
class CapsuleLoss(nn.Module):
def __init__(self, gpu_id):
super(CapsuleLoss, self).__init__()
self.cross_entropy_loss = nn.CrossEntropyLoss()
if gpu_id >= 0:
self.cross_entropy_loss.cuda(gpu_id)
def forward(self, classes, labels):
loss_t = self.cross_entropy_loss(classes[:,0,:], labels)
for i in range(classes.size(1) - 1):
loss_t = loss_t + self.cross_entropy_loss(classes[:,i+1,:], labels)
return loss_t