-
Notifications
You must be signed in to change notification settings - Fork 0
/
models_bid_pointconv.py
288 lines (227 loc) · 10.7 KB
/
models_bid_pointconv.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
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
"""
Modified based on Bi-pointflownet: Bidirectional learning for point cloud based scene flow estimation (ECCV'2022).
In our work, we apply our self-supervised learning method to BiFlow model.
Reference:
BiFlow: https://github.com/cwc1260/BiFlow/blob/new1/models_bid_pointconv.py
"""
import torch.nn as nn
import torch
import numpy as np
import torch.nn.functional as F
from pointconv_util import PointConv, PointConvD, PointWarping, UpsampleFlow, CrossLayerLight as CrossLayer
from pointconv_util import SceneFlowEstimatorResidual
from pointconv_util import index_points_gather as index_points, index_points_group, Conv1d, square_distance
import time
scale = 1.0
class PointConvBidirection(nn.Module):
def __init__(self):
super(PointConvBidirection, self).__init__()
flow_nei = 32
feat_nei = 16
self.scale = scale
#l0: 8192
self.level0 = Conv1d(3, 32)
self.level0_1 = Conv1d(32, 32)
# self.level0_1_t1 = Conv1d(32, 32)
# self.level0_1_t2 = Conv1d(32, 32)
self.cross0 = CrossLayer(flow_nei, 32 + 32 , [32, 32], [32, 32])
self.flow0 = SceneFlowEstimatorResidual(32 + 64, 32)
self.level0_2 = Conv1d(32, 64)
#l1: 2048
self.level1 = PointConvD(2048, feat_nei, 64 + 3, 64)
self.cross1 = CrossLayer(flow_nei, 64 + 32, [64, 64], [64, 64])
self.flow1 = SceneFlowEstimatorResidual(64 + 64, 64)
self.level1_0 = Conv1d(64, 64)
# self.level1_0_t1 = Conv1d(64, 64)
# self.level1_0_t2 = Conv1d(64, 64)
self.level1_1 = Conv1d(64, 128)
#l2: 512
self.level2 = PointConvD(512, feat_nei, 128 + 3, 128)
self.cross2 = CrossLayer(flow_nei, 128 + 64, [128, 128], [128, 128])
self.flow2 = SceneFlowEstimatorResidual(128 + 64, 128)
self.level2_0 = Conv1d(128, 128)
# self.level2_0_t1 = Conv1d(128, 128)
# self.level2_0_t2 = Conv1d(128, 128)
self.level2_1 = Conv1d(128, 256)
#l3: 256
self.level3 = PointConvD(256, feat_nei, 256 + 3, 256)
self.cross3 = CrossLayer(flow_nei, 256 + 64, [256, 256], [256, 256])
self.flow3 = SceneFlowEstimatorResidual(256, 256)
self.level3_0 = Conv1d(256, 256)
# self.level3_0_t1 = Conv1d(256, 256)
# self.level3_0_t2 = Conv1d(256, 256)
self.level3_1 = Conv1d(256, 512)
#l4: 64
self.level4 = PointConvD(64, feat_nei, 512 + 3, 256)
#deconv
# self.deconv4_3_t1 = Conv1d(256, 64)
# self.deconv4_3_t2 = Conv1d(256, 64)
self.deconv4_3 = Conv1d(256, 64)
self.deconv3_2 = Conv1d(256, 64)
self.deconv2_1 = Conv1d(128, 32)
self.deconv1_0 = Conv1d(64, 32)
#warping
self.warping = PointWarping()
#upsample
self.upsample = UpsampleFlow()
def forward(self, xyz1, xyz2, color1, color2):
#xyz1, xyz2: B, N, 3
#color1, color2: B, N, 3
#l0
pc1_l0 = xyz1.permute(0, 2, 1)
pc2_l0 = xyz2.permute(0, 2, 1)
color1 = color1.permute(0, 2, 1) # B 3 N
color2 = color2.permute(0, 2, 1) # B 3 N
feat1_l0 = self.level0(color1)
# feat1_l0 = self.level0_1_t1(feat1_l0)
feat1_l0 = self.level0_1(feat1_l0)
feat1_l0_1 = self.level0_2(feat1_l0)
feat2_l0 = self.level0(color2)
# feat2_l0 = self.level0_1_t2(feat2_l0)
feat2_l0 = self.level0_1(feat2_l0)
feat2_l0_1 = self.level0_2(feat2_l0)
#l1
pc1_l1, feat1_l1, fps_pc1_l1 = self.level1(pc1_l0, feat1_l0_1)
# feat1_l1 = self.level1_0_t1(feat1_l1)
feat1_l1 = self.level1_0(feat1_l1)
feat1_l1_2 = self.level1_1(feat1_l1)
pc2_l1, feat2_l1, fps_pc2_l1 = self.level1(pc2_l0, feat2_l0_1)
# feat2_l1 = self.level1_0_t2(feat2_l1)
feat2_l1 = self.level1_0(feat2_l1)
feat2_l1_2 = self.level1_1(feat2_l1)
#l2
pc1_l2, feat1_l2, fps_pc1_l2 = self.level2(pc1_l1, feat1_l1_2)
# feat1_l2 = self.level2_0_t1(feat1_l2)
feat1_l2 = self.level2_0(feat1_l2)
feat1_l2_3 = self.level2_1(feat1_l2)
pc2_l2, feat2_l2, fps_pc2_l2 = self.level2(pc2_l1, feat2_l1_2)
# feat2_l2 = self.level2_0_t2(feat2_l2)
feat2_l2 = self.level2_0(feat2_l2)
feat2_l2_3 = self.level2_1(feat2_l2)
#l3
pc1_l3, feat1_l3, fps_pc1_l3 = self.level3(pc1_l2, feat1_l2_3)
# feat1_l3 = self.level3_0_t1(feat1_l3)
feat1_l3 = self.level3_0(feat1_l3)
feat1_l3_4 = self.level3_1(feat1_l3)
pc2_l3, feat2_l3, fps_pc2_l3 = self.level3(pc2_l2, feat2_l2_3)
# feat2_l3 = self.level3_0_t2(feat2_l3)
feat2_l3 = self.level3_0(feat2_l3)
feat2_l3_4 = self.level3_1(feat2_l3)
#l4
pc1_l4, feat1_l4, _ = self.level4(pc1_l3, feat1_l3_4)
feat1_l4_3 = self.upsample(pc1_l3, pc1_l4, feat1_l4)
# feat1_l4_3 = self.deconv4_3_t1(feat1_l4_3)
feat1_l4_3 = self.deconv4_3(feat1_l4_3)
pc2_l4, feat2_l4, _ = self.level4(pc2_l3, feat2_l3_4)
feat2_l4_3 = self.upsample(pc2_l3, pc2_l4, feat2_l4)
# feat2_l4_3 = self.deconv4_3_t2(feat2_l4_3)
feat2_l4_3 = self.deconv4_3(feat2_l4_3)
#l3
c_feat1_l3 = torch.cat([feat1_l3, feat1_l4_3], dim = 1)
c_feat2_l3 = torch.cat([feat2_l3, feat2_l4_3], dim = 1)
feat1_new_l3, feat2_new_l3, cross3 = self.cross3(pc1_l3, pc2_l3, c_feat1_l3, c_feat2_l3)
feat3, flow3 = self.flow3(pc1_l3, feat1_l3, cross3)
feat1_l3_2 = self.upsample(pc1_l2, pc1_l3, feat1_new_l3)
feat1_l3_2 = self.deconv3_2(feat1_l3_2)
feat2_l3_2 = self.upsample(pc2_l2, pc2_l3, feat2_new_l3)
feat2_l3_2 = self.deconv3_2(feat2_l3_2)
c_feat1_l2 = torch.cat([feat1_l2, feat1_l3_2], dim = 1)
c_feat2_l2 = torch.cat([feat2_l2, feat2_l3_2], dim = 1)
#l2
up_flow2 = self.upsample(pc1_l2, pc1_l3, self.scale * flow3)
pc2_l2_warp = self.warping(pc1_l2, pc2_l2, up_flow2)
feat1_new_l2, feat2_new_l2, cross2 = self.cross2(pc1_l2, pc2_l2_warp, c_feat1_l2, c_feat2_l2)
feat3_up = self.upsample(pc1_l2, pc1_l3, feat3)
new_feat1_l2 = torch.cat([feat1_l2, feat3_up], dim = 1)
feat2, flow2 = self.flow2(pc1_l2, new_feat1_l2, cross2, up_flow2)
feat1_l2_1 = self.upsample(pc1_l1, pc1_l2, feat1_new_l2)
feat1_l2_1 = self.deconv2_1(feat1_l2_1)
feat2_l2_1 = self.upsample(pc2_l1, pc2_l2, feat2_new_l2)
feat2_l2_1 = self.deconv2_1(feat2_l2_1)
c_feat1_l1 = torch.cat([feat1_l1, feat1_l2_1], dim = 1)
c_feat2_l1 = torch.cat([feat2_l1, feat2_l2_1], dim = 1)
#l1
up_flow1 = self.upsample(pc1_l1, pc1_l2, self.scale * flow2)
pc2_l1_warp = self.warping(pc1_l1, pc2_l1, up_flow1)
feat1_new_l1, feat2_new_l1, cross1 = self.cross1(pc1_l1, pc2_l1_warp, c_feat1_l1, c_feat2_l1)
feat2_up = self.upsample(pc1_l1, pc1_l2, feat2)
new_feat1_l1 = torch.cat([feat1_l1, feat2_up], dim = 1)
feat1, flow1 = self.flow1(pc1_l1, new_feat1_l1, cross1, up_flow1)
feat1_l1_0 = self.upsample(pc1_l0, pc1_l1, feat1_new_l1)
feat1_l1_0 = self.deconv1_0(feat1_l1_0)
feat2_l1_0 = self.upsample(pc2_l0, pc2_l1, feat2_new_l1)
feat2_l1_0 = self.deconv1_0(feat2_l1_0)
c_feat1_l0 = torch.cat([feat1_l0, feat1_l1_0], dim = 1)
c_feat2_l0 = torch.cat([feat2_l0, feat2_l1_0], dim = 1)
#l0
up_flow0 = self.upsample(pc1_l0, pc1_l1, self.scale * flow1)
pc2_l0_warp = self.warping(pc1_l0, pc2_l0, up_flow0)
_, _, cross0 = self.cross0(pc1_l0, pc2_l0_warp, c_feat1_l0, c_feat2_l0)
feat1_up = self.upsample(pc1_l0, pc1_l1, feat1)
new_feat1_l0 = torch.cat([feat1_l0, feat1_up], dim = 1)
_, flow0 = self.flow0(pc1_l0, new_feat1_l0, cross0, up_flow0)
flows = [flow0, flow1, flow2, flow3]
pc1 = [pc1_l0, pc1_l1, pc1_l2, pc1_l3]
pc2 = [pc2_l0, pc2_l1, pc2_l2, pc2_l3]
fps_pc1_idxs = [fps_pc1_l1, fps_pc1_l2, fps_pc1_l3]
fps_pc2_idxs = [fps_pc2_l1, fps_pc2_l2, fps_pc2_l3]
return flows, fps_pc1_idxs, fps_pc2_idxs, pc1, pc2
def multiScaleLoss(pred_flows, gt_flow, fps_idxs, alpha = [0.02, 0.04, 0.08, 0.16]):
#num of scale
num_scale = len(pred_flows)
offset = len(fps_idxs) - num_scale + 1
#generate GT list and mask1s
gt_flows = [gt_flow]
for i in range(1, len(fps_idxs) + 1):
fps_idx = fps_idxs[i - 1]
sub_gt_flow = index_points(gt_flows[-1], fps_idx) / scale
gt_flows.append(sub_gt_flow)
total_loss = torch.zeros(1).cuda()
for i in range(num_scale):
diff_flow = pred_flows[i].permute(0, 2, 1) - gt_flows[i + offset]
total_loss += alpha[i] * torch.norm(diff_flow, dim = 2).sum(dim = 1).mean()
return total_loss
def multiScaleLoss_mask(pred_flows, gt_flow, fps_idxs, mask1, alpha = [0.02, 0.04, 0.08, 0.16]):
#num of scale
num_scale = len(pred_flows)
offset = len(fps_idxs) - num_scale + 1
#generate GT list and mask1s
gt_flows = [gt_flow]
gt_masks = [mask1]
for i in range(1, len(fps_idxs) + 1):
fps_idx = fps_idxs[i - 1]
sub_gt_flow = index_points(gt_flows[-1], fps_idx) / scale
gt_flows.append(sub_gt_flow)
sub_gt_mask = index_points(gt_masks[-1].unsqueeze(-1), fps_idx) / scale
gt_masks.append(sub_gt_mask.squeeze(-1))
total_loss = torch.zeros(1).cuda()
for i in range(num_scale):
diff_flow = pred_flows[i].permute(0, 2, 1) - gt_flows[i + offset]
truncated_mask = (torch.norm(diff_flow, dim=2) < 5.0).float()
# total_loss += alpha[i] * torch.norm(diff_flow, dim = 2).sum(dim = 1).mean()
# total_loss += alpha[i] * torch.sum(torch.norm(diff_flow, dim=2) * gt_masks[i + offset], 1).mean()
total_loss += alpha[i] * torch.sum(torch.norm(diff_flow, dim=2) * truncated_mask * gt_masks[i + offset], 1).mean()
return total_loss
from thop import profile, clever_format
if __name__ == '__main__':
import os
import torch
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
input = torch.randn((1,8192,3)).float().cuda()
model = PointConvBidirection().cuda()
# print(model)
output = model(input,input,input,input)
macs, params = profile(model, inputs=(input,input,input,input))
macs, params = clever_format([macs, params], "%.3f")
print(macs, params)
total = sum([param.nelement() for param in model.parameters()])
print("Number of parameter: %.2fM" % (total/1e6))
dump_input = torch.randn((1,8192,3)).float().cuda()
traced_model = torch.jit.trace(model, (dump_input, dump_input, dump_input, dump_input))
timer = 0
timer = 0
for i in range(100):
t = time.time()
_ = traced_model(input,input,input,input)
timer += time.time() - t
print(timer / 100.0)