-
Notifications
You must be signed in to change notification settings - Fork 57
/
model.py
173 lines (153 loc) · 6.38 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
from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.utils.data
from torch.autograd import Variable
import numpy as np
import torch.nn.functional as F
import sys
sys.path.append("./expansion_penalty/")
import expansion_penalty_module as expansion
sys.path.append("./MDS/")
import MDS_module
class STN3d(nn.Module):
def __init__(self, num_points = 2500):
super(STN3d, self).__init__()
self.num_points = num_points
self.conv1 = torch.nn.Conv1d(3, 64, 1)
self.conv2 = torch.nn.Conv1d(64, 128, 1)
self.conv3 = torch.nn.Conv1d(128, 1024, 1)
self.fc1 = nn.Linear(1024, 512)
self.fc2 = nn.Linear(512, 256)
self.fc3 = nn.Linear(256, 9)
self.relu = nn.ReLU()
def forward(self, x):
batchsize = x.size()[0]
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = F.relu(self.conv3(x))
x,_ = torch.max(x, 2)
x = x.view(-1, 1024)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
iden = Variable(torch.from_numpy(np.array([1,0,0,0,1,0,0,0,1]).astype(np.float32))).view(1,9).repeat(batchsize,1)
if x.is_cuda:
iden = iden.cuda()
x = x + iden
x = x.view(-1, 3, 3)
return x
class PointNetfeat(nn.Module):
def __init__(self, num_points = 8192, global_feat = True):
super(PointNetfeat, self).__init__()
self.stn = STN3d(num_points = num_points)
self.conv1 = torch.nn.Conv1d(3, 64, 1)
self.conv2 = torch.nn.Conv1d(64, 128, 1)
self.conv3 = torch.nn.Conv1d(128, 1024, 1)
self.bn1 = torch.nn.BatchNorm1d(64)
self.bn2 = torch.nn.BatchNorm1d(128)
self.bn3 = torch.nn.BatchNorm1d(1024)
self.num_points = num_points
self.global_feat = global_feat
def forward(self, x):
batchsize = x.size()[0]
x = F.relu(self.bn1(self.conv1(x)))
x = F.relu(self.bn2(self.conv2(x)))
x = self.bn3(self.conv3(x))
x,_ = torch.max(x, 2)
x = x.view(-1, 1024)
return x
class PointGenCon(nn.Module):
def __init__(self, bottleneck_size = 8192):
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, self.bottleneck_size//2, 1)
self.conv3 = torch.nn.Conv1d(self.bottleneck_size//2, self.bottleneck_size//4, 1)
self.conv4 = torch.nn.Conv1d(self.bottleneck_size//4, 3, 1)
self.th = nn.Tanh()
self.bn1 = torch.nn.BatchNorm1d(self.bottleneck_size)
self.bn2 = torch.nn.BatchNorm1d(self.bottleneck_size//2)
self.bn3 = torch.nn.BatchNorm1d(self.bottleneck_size//4)
def forward(self, x):
batchsize = x.size()[0]
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 PointNetRes(nn.Module):
def __init__(self):
super(PointNetRes, self).__init__()
self.conv1 = torch.nn.Conv1d(4, 64, 1)
self.conv2 = torch.nn.Conv1d(64, 128, 1)
self.conv3 = torch.nn.Conv1d(128, 1024, 1)
self.conv4 = torch.nn.Conv1d(1088, 512, 1)
self.conv5 = torch.nn.Conv1d(512, 256, 1)
self.conv6 = torch.nn.Conv1d(256, 128, 1)
self.conv7 = torch.nn.Conv1d(128, 3, 1)
self.bn1 = torch.nn.BatchNorm1d(64)
self.bn2 = torch.nn.BatchNorm1d(128)
self.bn3 = torch.nn.BatchNorm1d(1024)
self.bn4 = torch.nn.BatchNorm1d(512)
self.bn5 = torch.nn.BatchNorm1d(256)
self.bn6 = torch.nn.BatchNorm1d(128)
self.bn7 = torch.nn.BatchNorm1d(3)
self.th = nn.Tanh()
def forward(self, x):
batchsize = x.size()[0]
npoints = x.size()[2]
x = F.relu(self.bn1(self.conv1(x)))
pointfeat = x
x = F.relu(self.bn2(self.conv2(x)))
x = self.bn3(self.conv3(x))
x,_ = torch.max(x, 2)
x = x.view(-1, 1024)
x = x.view(-1, 1024, 1).repeat(1, 1, npoints)
x = torch.cat([x, pointfeat], 1)
x = F.relu(self.bn4(self.conv4(x)))
x = F.relu(self.bn5(self.conv5(x)))
x = F.relu(self.bn6(self.conv6(x)))
x = self.th(self.conv7(x))
return x
class MSN(nn.Module):
def __init__(self, num_points = 8192, bottleneck_size = 1024, n_primitives = 16):
super(MSN, self).__init__()
self.num_points = num_points
self.bottleneck_size = bottleneck_size
self.n_primitives = n_primitives
self.encoder = nn.Sequential(
PointNetfeat(num_points, global_feat=True),
nn.Linear(1024, self.bottleneck_size),
nn.BatchNorm1d(self.bottleneck_size),
nn.ReLU()
)
self.decoder = nn.ModuleList([PointGenCon(bottleneck_size = 2 +self.bottleneck_size) for i in range(0,self.n_primitives)])
self.res = PointNetRes()
self.expansion = expansion.expansionPenaltyModule()
def forward(self, x):
partial = x
x = self.encoder(x)
outs = []
for i in range(0,self.n_primitives):
rand_grid = Variable(torch.cuda.FloatTensor(x.size(0),2,self.num_points//self.n_primitives))
rand_grid.data.uniform_(0,1)
y = x.unsqueeze(2).expand(x.size(0),x.size(1), rand_grid.size(2)).contiguous()
y = torch.cat( (rand_grid, y), 1).contiguous()
outs.append(self.decoder[i](y))
outs = torch.cat(outs,2).contiguous()
out1 = outs.transpose(1, 2).contiguous()
dist, _, mean_mst_dis = self.expansion(out1, self.num_points//self.n_primitives, 1.5)
loss_mst = torch.mean(dist)
id0 = torch.zeros(outs.shape[0], 1, outs.shape[2]).cuda().contiguous()
outs = torch.cat( (outs, id0), 1)
id1 = torch.ones(partial.shape[0], 1, partial.shape[2]).cuda().contiguous()
partial = torch.cat( (partial, id1), 1)
xx = torch.cat( (outs, partial), 2)
resampled_idx = MDS_module.minimum_density_sample(xx[:, 0:3, :].transpose(1, 2).contiguous(), out1.shape[1], mean_mst_dis)
xx = MDS_module.gather_operation(xx, resampled_idx)
delta = self.res(xx)
xx = xx[:, 0:3, :]
out2 = (xx + delta).transpose(2,1).contiguous()
return out1, out2, loss_mst