forked from zhan-xu/RigNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPairCls_GCN.py
151 lines (128 loc) · 6.36 KB
/
PairCls_GCN.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
#-------------------------------------------------------------------------------
# Name: PairCls_GCN.py
# Purpose: definition of connectivity prediction module.
# RigNet Copyright 2020 University of Massachusetts
# RigNet is made available under General Public License Version 3 (GPLv3), or under a Commercial License.
# Please see the LICENSE README.txt file in the main directory for more information and instruction on using and licensing RigNet.
#-------------------------------------------------------------------------------
import numpy as np
import torch
from models.gcn_basic_modules import MLP, GCU
from torch.nn import Sequential, Dropout, Linear
from torch_scatter import scatter_max
from torch_geometric.nn import PointConv, fps, radius, global_max_pool, knn_interpolate
class SAModule(torch.nn.Module):
def __init__(self, ratio, r, nn):
super(SAModule, self).__init__()
self.ratio = ratio
self.r = r
self.conv = PointConv(nn)
def forward(self, x, pos, batch):
idx = fps(pos, batch, ratio=self.ratio)
row, col = radius(pos, pos[idx], self.r, batch, batch[idx],
max_num_neighbors=64)
edge_index = torch.stack([col, row], dim=0)
x = self.conv(x, (pos, pos[idx]), edge_index)
pos, batch = pos[idx], batch[idx]
return x, pos, batch
class GlobalSAModule(torch.nn.Module):
def __init__(self, nn):
super(GlobalSAModule, self).__init__()
self.nn = nn
def forward(self, x, pos, batch):
x = self.nn(torch.cat([x, pos], dim=1))
x = global_max_pool(x, batch)
pos = pos.new_zeros((x.size(0), 3))
batch = torch.arange(x.size(0), device=batch.device)
return x, pos, batch
class FPModule(torch.nn.Module):
def __init__(self, k, nn):
super(FPModule, self).__init__()
self.k = k
self.nn = nn
def forward(self, x, pos, batch, x_skip, pos_skip, batch_skip):
x = knn_interpolate(x, pos, pos_skip, batch, batch_skip, k=self.k)
if x_skip is not None:
x = torch.cat([x, x_skip], dim=1)
x = self.nn(x)
return x, pos_skip, batch_skip
class ShapeEncoder(torch.nn.Module):
def __init__(self, aggr='max'):
super(ShapeEncoder, self).__init__()
self.gcu_1 = GCU(in_channels=3, out_channels=64, aggr=aggr)
self.gcu_2 = GCU(in_channels=64, out_channels=128, aggr=aggr)
self.gcu_3 = GCU(in_channels=128, out_channels=256, aggr=aggr)
self.mlp_glb = MLP([(64 + 128 + 256), 256, 64])
def forward(self, data):
x_1 = self.gcu_1(data.pos, data.tpl_edge_index, data.geo_edge_index)
x_2 = self.gcu_2(x_1, data.tpl_edge_index, data.geo_edge_index)
x_3 = self.gcu_3(x_2, data.tpl_edge_index, data.geo_edge_index)
x_4 = self.mlp_glb(torch.cat([x_1, x_2, x_3], dim=1))
x_global_shape, _ = scatter_max(x_4, data.batch, dim=0)
return x_global_shape
class JointEncoder(torch.nn.Module):
def __init__(self):
super(JointEncoder, self).__init__()
#self.mlp_1 = MLP([3, 64, 128, 1024])
#self.mlp_2 = MLP([1024, 256, 128])
self.sa1_module_joints = SAModule(0.999, 0.4, MLP([3, 64, 64, 128]))
self.sa2_module_joints = SAModule(0.33, 0.6, MLP([128 + 3, 128, 128, 256]))
self.sa3_module_joints = GlobalSAModule(MLP([256 + 3, 256, 256, 512, 256, 128]))
def forward(self, joints_norepeat, joints_batch):
'''x1 = self.mlp_1(joints_norepeat)
x_glb, _ = scatter_max(x1, joints_batch, dim=0)
x_glb = self.mlp_2(x_glb)
return x_glb'''
sa0_joints = (None, joints_norepeat, joints_batch)
sa1_joints = self.sa1_module_joints(*sa0_joints)
sa2_joints = self.sa2_module_joints(*sa1_joints)
sa3_joints = self.sa3_module_joints(*sa2_joints)
x_glb_joint = sa3_joints[0]
return x_glb_joint
class PairCls(torch.nn.Module):
def __init__(self):
super(PairCls, self).__init__()
self.expand_joint_feature = Sequential(MLP([8, 32, 64, 128, 256]))
self.shape_encoder = ShapeEncoder()
self.joint_encoder = JointEncoder()
input_concat_dim = 448
self.mix_transform = Sequential(MLP([input_concat_dim, 128, 64]), Dropout(0.7), Linear(64, 1))
def forward(self, data):
joints = data.y
joints_norepeat = []
joints_batch = []
joints_sample_1 = []
joints_sample_2 = []
pair_batch = []
label = []
accumulate_start_pair = 0
for i in range(len(torch.unique(data.batch))):
joints_sample = joints[data.batch == i, :]
joints_sample = joints_sample[:data.num_joint[i], :]
joints_norepeat.append(joints_sample)
joints_batch.append(data.batch.new_full((data.num_joint[i],), i))
pair_idx = data.pairs[accumulate_start_pair: accumulate_start_pair + data.num_pair[i]]
accumulate_start_pair += data.num_pair[i]
if np.random.uniform() > 0.5:
joints_sample_1.append(joints_sample[pair_idx[:, 0].long()])
joints_sample_2.append(joints_sample[pair_idx[:, 1].long()])
else:
joints_sample_1.append(joints_sample[pair_idx[:, 1].long()])
joints_sample_2.append(joints_sample[pair_idx[:, 0].long()])
pair_batch.append(data.batch.new_full((data.num_pair[i],), i))
label.append(pair_idx[:, -1])
joints_norepeat = torch.cat(joints_norepeat, dim=0)
joints_batch = torch.cat(joints_batch).long()
pair_batch = torch.cat(pair_batch).long()
joints_sample_1 = torch.cat(joints_sample_1, dim=0)
joints_sample_2 = torch.cat(joints_sample_2, dim=0)
label = torch.cat(label, dim=0).unsqueeze(1)
joints_pair = torch.cat((joints_sample_1, joints_sample_2, data.pairs[:, 2:4]), dim=1)
pair_feature = self.expand_joint_feature(joints_pair)
joint_feature = self.joint_encoder(joints_norepeat, joints_batch)
joint_feature = torch.repeat_interleave(joint_feature, torch.bincount(pair_batch), dim=0)
shape_feature = self.shape_encoder(data)
shape_feature = torch.repeat_interleave(shape_feature, torch.bincount(pair_batch), dim=0)
pair_feature = torch.cat((shape_feature, joint_feature, pair_feature), dim=1)
pre_label = self.mix_transform(pair_feature)
return pre_label, label