-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
303 lines (235 loc) · 10.6 KB
/
model.py
File metadata and controls
303 lines (235 loc) · 10.6 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"""bcaps_model.py"""
from collections import OrderedDict
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.init as init
from torch.autograd import Variable
import sys, os
# function definitions
def reparametrize(mu, logvar):
std = logvar.div(2).exp()
eps = Variable(std.data.new(std.size()).normal_())
return mu + std*eps
def kaiming_init(m):
if isinstance(m, (nn.Linear, nn.Conv2d)):
init.kaiming_normal(m.weight)
if m.bias is not None:
m.bias.data.fill_(0)
elif isinstance(m, (nn.BatchNorm1d, nn.BatchNorm2d)):
m.weight.data.fill_(1)
if m.bias is not None:
m.bias.data.fill_(0)
def normal_init(m, mean, std):
if isinstance(m, (nn.Linear, nn.Conv2d)):
m.weight.data.normal_(mean, std)
if m.bias.data is not None:
m.bias.data.zero_()
elif isinstance(m, (nn.BatchNorm2d, nn.BatchNorm1d)):
m.weight.data.fill_(1)
if m.bias.data is not None:
m.bias.data.zero_()
# aux modules
class View(nn.Module):
def __init__(self, size):
super(View, self).__init__()
self.size = size
def forward(self, tensor):
return tensor.view(self.size)
# encoding layers
class ConvLayer(nn.Module):
def __init__(self):
super(ConvLayer, self).__init__()
self.conv1 = torch.nn.Conv1d(3, 64, 1) ###
self.conv2 = torch.nn.Conv1d(64, 128, 1) ###
self.bn1 = nn.BatchNorm1d(64) ##
self.bn2 = nn.BatchNorm1d(128) ##
def forward(self, x):
x = F.relu(self.bn1(self.conv1(x))) ###
x = F.relu(self.bn2(self.conv2(x))) ###
return x
class PrimaryPointCapsLayer(nn.Module):
def __init__(self, prim_vec_size=8, num_points=2048):
super(PrimaryPointCapsLayer, self).__init__()
self.capsules = nn.ModuleList([
torch.nn.Sequential(OrderedDict([
('conv3', torch.nn.Conv1d(128, 1024, 1)),
('bn3', nn.BatchNorm1d(1024)),
('mp1', torch.nn.MaxPool1d(num_points)),
]))
for _ in range(prim_vec_size)])
def forward(self, x):
u = [capsule(x) for capsule in self.capsules]
u = torch.stack(u, dim=2)
return self.squash(u.squeeze())
def squash(self, input_tensor):
squared_norm = (input_tensor ** 2).sum(-1, keepdim=True)
output_tensor = squared_norm * input_tensor / \
((1. + squared_norm) * torch.sqrt(squared_norm))
if(output_tensor.dim() == 2):
output_tensor = torch.unsqueeze(output_tensor, 0)
return output_tensor
class LatentCapsLayer(nn.Module):
def __init__(self, latent_caps_size=16, prim_caps_size=1024, prim_vec_size=16, latent_vec_size=64):
super(LatentCapsLayer, self).__init__()
self.prim_vec_size = prim_vec_size
self.prim_caps_size = prim_caps_size
self.latent_caps_size = latent_caps_size
self.W = nn.Parameter(0.01*torch.randn(latent_caps_size, prim_caps_size, latent_vec_size, prim_vec_size))
def forward(self, x): # https://pechyonkin.me/capsules-3/
u_hat = torch.squeeze(torch.matmul(self.W, x[:, None, :, :, None]), dim=-1)
u_hat_detached = u_hat.detach()
b_ij = Variable(torch.zeros(x.size(0), self.latent_caps_size, self.prim_caps_size)).cuda()
num_iterations = 3
for iteration in range(num_iterations):
c_ij = F.softmax(b_ij, 1)
if iteration == num_iterations - 1:
v_j = self.squash(torch.sum(c_ij[:, :, :, None] * u_hat, dim=-2, keepdim=True))
else:
v_j = self.squash(torch.sum(c_ij[:, :, :, None] * u_hat_detached, dim=-2, keepdim=True))
b_ij = b_ij + torch.sum(v_j * u_hat_detached, dim=-1)
return v_j.squeeze(-2) # removes given dimension if size is 1
def squash(self, input_tensor):
squared_norm = (input_tensor ** 2).sum(-1, keepdim=True)
output_tensor = squared_norm * input_tensor / \
((1. + squared_norm) * torch.sqrt(squared_norm))
return output_tensor
# decoding layers
class PointGenCon(nn.Module):
def __init__(self, bottleneck_size=2500):
self.bottleneck_size = bottleneck_size
super(PointGenCon, self).__init__()
self.conv1 = torch.nn.Conv1d(self.bottleneck_size, self.bottleneck_size, 1)
self.conv2 = torch.nn.Conv1d(self.bottleneck_size, int(self.bottleneck_size/2), 1)
self.conv3 = torch.nn.Conv1d(int(self.bottleneck_size/2), int(self.bottleneck_size/4), 1)
self.conv4 = torch.nn.Conv1d(int(self.bottleneck_size/4), 3, 1)
self.th = torch.nn.Tanh()
self.bn1 = torch.nn.BatchNorm1d(self.bottleneck_size)
self.bn2 = torch.nn.BatchNorm1d(int(self.bottleneck_size/2))
self.bn3 = torch.nn.BatchNorm1d(int(self.bottleneck_size/4))
def forward(self, x):
x = F.relu(self.bn1(self.conv1(x)))
x = F.relu(self.bn2(self.conv2(x)))
x = F.relu(self.bn3(self.conv3(x)))
x = self.th(self.conv4(x))
return x
class CapsDecoder(nn.Module):
def __init__(self, latent_caps_size, latent_vec_size, num_points):
super(CapsDecoder, self).__init__()
self.latent_caps_size = latent_caps_size
self.bottleneck_size=latent_vec_size
self.num_points = num_points
self.nb_primitives=int(num_points/latent_caps_size)
self.decoder = nn.ModuleList(
[PointGenCon(bottleneck_size=self.bottleneck_size+2) for i in range(0, self.nb_primitives)])
def forward(self, x):
outs = []
for i in range(0, self.nb_primitives):
rand_grid = Variable(torch.cuda.FloatTensor(x.size(0), 2, self.latent_caps_size))
rand_grid.data.uniform_(0, 1)
#print(x.size())
#print(rand_grid.size())
y = torch.cat((rand_grid, x.transpose(2, 1)), 1).contiguous()
outs.append(self.decoder[i](y))
return torch.cat(outs, 2).contiguous()
# entirety
class PointCapsNet(nn.Module):
''' original Point Capsnet by Zhao et. al.'''
def __init__(self, prim_caps_size, prim_vec_size, latent_caps_size, latent_vec_size, num_points):
super(PointCapsNet, self).__init__()
self.conv_layer = ConvLayer()
self.primary_point_caps_layer = PrimaryPointCapsLayer(prim_vec_size, num_points)
self.latent_caps_layer = LatentCapsLayer(latent_caps_size, prim_caps_size, prim_vec_size, latent_vec_size)
self.caps_decoder = CapsDecoder(latent_caps_size,latent_vec_size, num_points)
def forward(self, data):
x1 = self.conv_layer(data)
x2 = self.primary_point_caps_layer(x1)
latent_capsules = self.latent_caps_layer(x2)
reconstructions = self.caps_decoder(latent_capsules)
return latent_capsules, reconstructions
class CapsSegNet(nn.Module):
def __init__(self, latent_caps_size,latent_vec_size , num_classes, num_cats=16):
''' num_classes: total part classes in all categories, num_cats: number of category'''
super(CapsSegNet, self).__init__()
self.num_classes=num_classes
self.latent_caps_size=latent_caps_size
self.seg_convs= nn.Conv1d(latent_vec_size+num_cats, num_classes, 1)
def forward(self, data):
batchsize= data.size(0)
output = self.seg_convs(data)
output = output.transpose(2,1).contiguous()
output = F.log_softmax(output.view(-1,self.num_classes), dim=-1)
output = output.view(batchsize, self.latent_caps_size, self.num_classes)
return output
class CapsuleBVAE(nn.Module):
"""Model proposed in original beta-VAE paper(Higgins et al, ICLR, 2017)."""
def __init__(self, z_dim=10, nc=1):
super(CapsuleBVAE, self).__init__()
self.z_dim = z_dim
self.nc = nc #number of channels
self.encoder = nn.Sequential(
nn.Conv2d(nc, 32, 4, 2, 1), # B, 32, 32, 32
nn.ReLU(True),
nn.Conv2d(32, 32, 4, 2, 1), # B, 32, 16, 16
nn.ReLU(True),
nn.Conv2d(32, 64, 4, 2, 1), # B, 64, 8, 8
nn.ReLU(True),
nn.Conv2d(64, 64, 4, 2, 1), # B, 64, 4, 4
nn.ReLU(True),
nn.Conv2d(64, 256, 4, 1), # B, 256, 1, 1
nn.ReLU(True),
View((-1, 256*1*1)), # B, 256
nn.Linear(256, z_dim*2), # B, z_dim*2
)
self.decoder = nn.Sequential(
nn.Linear(z_dim, 256), # B, 256
View((-1, 256, 1, 1)), # B, 256, 1, 1
nn.ReLU(True),
nn.ConvTranspose2d(256, 64, 4), # B, 64, 4, 4
nn.ReLU(True),
nn.ConvTranspose2d(64, 64, 4, 2, 1), # B, 64, 8, 8
nn.ReLU(True),
nn.ConvTranspose2d(64, 32, 4, 2, 1), # B, 32, 16, 16
nn.ReLU(True),
nn.ConvTranspose2d(32, 32, 4, 2, 1), # B, 32, 32, 32
nn.ReLU(True),
nn.ConvTranspose2d(32, nc, 4, 2, 1), # B, nc, 64, 64
)
self.weight_init()
def weight_init(self):
for block in self._modules:
for m in self._modules[block]:
kaiming_init(m)
def forward(self, x):
distributions = self._encode(x)
mu = distributions[:, :self.z_dim]
logvar = distributions[:, self.z_dim:]
z = reparametrize(mu, logvar)
x_recon = self._decode(z) # x_reconstructed
return x_recon, mu, logvar
def _encode(self, x):
return self.encoder(x)
def _decode(self, z):
return self.decoder(z)
if __name__ == '__main__':
from chamfer_distance import ChamferDistance
CD = ChamferDistance()
USE_CUDA = True
batch_size = 16 # ORIGINAL WAS 8
prim_caps_size = 1024
prim_vec_size = 16
latent_caps_size = 32 # number of latent capsules
latent_vec_size = 16 # scale of (number of neurons in) latent capsules
num_points = 2048
point_caps_ae = PointCapsNet(prim_caps_size,prim_vec_size,latent_caps_size,latent_vec_size,num_points)
point_caps_ae=torch.nn.DataParallel(point_caps_ae).cuda()
rand_data = torch.rand(batch_size,num_points, 3)
rand_data = Variable(rand_data)
rand_data = rand_data.transpose(2, 1)
rand_data = rand_data.cuda()
_, reconstruction = point_caps_ae(rand_data) # what forward() function returns, e.g. x_recon, mu, logvar
rand_data_ = rand_data.transpose(2, 1).contiguous()
reconstruction_ = reconstruction.transpose(2, 1).contiguous()
dist1, dist2 = CD(rand_data_, reconstruction_)
loss = (torch.mean(dist1)) + (torch.mean(dist2))
print("[DONE] loss: ",loss.item())