-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgraph.py
213 lines (204 loc) · 11.8 KB
/
graph.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
import logging, numpy as np
# Thanks to YAN Sijie for the released code on Github (https://github.com/yysijie/st-gcn)
class Graph():
def __init__(self, dataset, max_hop=3, dilation=1):
self.dataset = dataset.split('-')[0]
self.max_hop = max_hop
self.dilation = dilation
# get edges
self.num_node, self.edge, self.connect_joint, self.parts = self._get_edge()
# get adjacency matrix
self.A = self._get_adjacency()
def __str__(self):
return self.A
def _get_edge(self):
if self.dataset == 'kinetics':
num_node = 18
neighbor_link = [(4, 3), (3, 2), (7, 6), (6, 5), (13, 12), (12, 11),
(10, 9), (9, 8), (11, 5), (8, 2), (5, 1), (2, 1),
(0, 1), (15, 0), (14, 0), (17, 15), (16, 14), (8, 11)]
connect_joint = np.array([1,1,1,2,3,1,5,6,2,8,9,5,11,12,0,0,14,15])
parts = [
np.array([5, 6, 7]), # left_arm
np.array([2, 3, 4]), # right_arm
np.array([11, 12, 13]), # left_leg
np.array([8, 9, 10]), # right_leg
np.array([0, 1, 14, 15, 16, 17]) # torso
]
elif self.dataset == 'ntu':
num_node = 25
neighbor_1base = [(1, 2), (2, 21), (3, 21), (4, 3), (5, 21),
(6, 5), (7, 6), (8, 7), (9, 21), (10, 9),
(11, 10), (12, 11), (13, 1), (14, 13), (15, 14),
(16, 15), (17, 1), (18, 17), (19, 18), (20, 19),
(22, 23), (23, 8), (24, 25), (25, 12)]
neighbor_link = [(i - 1, j - 1) for (i, j) in neighbor_1base]
connect_joint = np.array([2,2,21,3,21,5,6,7,21,9,10,11,1,13,14,15,1,17,18,19,2,23,8,25,12]) - 1
parts = [
np.array([5, 6, 7, 8, 22, 23]) - 1, # left_arm
np.array([9, 10, 11, 12, 24, 25]) - 1, # right_arm
np.array([13, 14, 15, 16]) - 1, # left_leg
np.array([17, 18, 19, 20]) - 1, # right_leg
np.array([1, 2, 3, 4, 21]) - 1 # torso
]
elif self.dataset == 'ntu_ax':
num_node = 118
neighbor_1base = [(1, 2), (3, 2), (4, 3), (5, 4), (6, 2), (7, 6),
(8, 7), (9, 2), (10, 9), (11, 10), (12, 11), (13, 9),
(14, 13), (15, 14), (16, 1), (17, 1), (18, 16), (19, 17),
(20, 15), (21, 20), (22, 15), (23, 12), (24, 23), (25, 12),
(26, 8), (27, 26), (28, 27), (29, 28), (30, 29), (31, 26), # Left hand coordinates
(32, 31), (33, 32), (34, 33), (35, 26), (36, 35), (37, 36),
(38, 37), (39, 26), (40, 39), (41, 40), (42, 41), (43, 26),
(44, 43), (45, 44), (46, 45), (47, 5), (48, 47), (49, 48), # Right hand coordinates
(50, 49), (51, 50), (52, 47), (53, 52), (54, 53), (55, 54),
(56, 47), (57, 56), (58, 57), (59, 58), (60, 47), (61, 60),
(62, 61), (63, 62), (64, 47), (65, 64), (66, 65), (67, 66),
(68, 69), (69, 70), (70, 71), (71, 72), (73, 74), (74, 75), # Face coordinates
(75, 76), (76, 77), (78, 1), (79, 78), (80, 79), (81, 80),
(82, 83), (83, 84), (84, 85), (85, 86), (87, 16), (88, 87),
(89, 88), (90, 89), (91, 90), (92, 91), (92, 87), (93, 17),
(94, 93), (95, 94), (96, 95), (97, 96), (98, 97), (98, 93),
(99, 100), (100, 101), (101, 102), (102, 103), (103, 104),
(104, 105), (105, 106), (106, 107), (107, 108), (108, 109),
(109, 110), (110, 99), (111, 112), (112, 113), (113, 114),
(114, 115), (115, 116), (116, 117), (117, 118), (118, 111)]
neighbor_link = [(i - 1, j - 1) for (i, j) in neighbor_1base]
connect_joint = np.array([2,2,21,3,21,5,6,7,21,9,10,11,1,13,14,15,1,17,18,19,2,23,8,25,12]) - 1
parts = [
np.array([5,6,7]), # left_arm
np.array([2,3,4]), # right_arm
np.array([12,13,14,19,20,21]), # left_leg
np.array([9,10,11,22,23,24]), # right_leg
np.array([1,8]), # torso
np.array([0,15,16,17,18]), # head
np.arange(21) + 25, # left fingers
np.arange(21) + 46, # right fingers
np.arange(51) + 67 # face
]
elif self.dataset == 'ntu_hx':
num_node = 67
neighbor_1base = [(1, 2), (3, 2), (4, 3), (5, 4), (6, 2), (7, 6),
(8, 7), (9, 2), (10, 9), (11, 10), (12, 11), (13, 9),
(14, 13), (15, 14), (16, 1), (17, 1), (18, 16), (19, 17),
(20, 15), (21, 20), (22, 15), (23, 12), (24, 23), (25, 12),
(26, 8), (27, 26), (28, 27), (29, 28), (30, 29), (31, 26), # Left hand coordinates
(32, 31), (33, 32), (34, 33), (35, 26), (36, 35), (37, 36),
(38, 37), (39, 26), (40, 39), (41, 40), (42, 41), (43, 26),
(44, 43), (45, 44), (46, 45), (47, 5), (48, 47), (49, 48), # Right hand coordinates
(50, 49), (51, 50), (52, 47), (53, 52), (54, 53), (55, 54),
(56, 47), (57, 56), (58, 57), (59, 58), (60, 47), (61, 60),
(62, 61), (63, 62), (64, 47), (65, 64), (66, 65), (67, 66)]
neighbor_link = [(i - 1, j - 1) for (i, j) in neighbor_1base]
connect_joint = np.array([2, 9, 2, 3, 4, 2, 6, 7, 9, 9, 10, 11, 9, 13, 14, 1, 1, 16, 17, 15, 20, 15, 12, 23, 12, 8, 26, 27, 28, 29, 26, 31, 32, 33, 26, 35, 36, 37,
26, 39, 40, 41, 26, 43, 44, 45, 5, 47, 48, 49, 50, 47, 52, 53, 54, 47, 56, 57, 58, 47, 60, 61, 62, 47, 64, 65, 66]) - 1
parts = [
np.array([5,6,7]), # left_arm
np.array([2,3,4]), # right_arm
np.array([12,13,14,19,20,21]), # left_leg
np.array([9,10,11,22,23,24]), # right_leg
np.array([1,8]), # torso
np.array([0,15,16,17,18]), # head
np.arange(21) + 25, # left fingers
np.arange(21) + 46, # right fingers
]
elif self.dataset == 'sysu':
num_node = 20
neighbor_1base = [(1, 2), (2, 3), (3, 4), (3, 5), (5, 6),
(6, 7), (7, 8), (3, 9), (9, 10), (10, 11),
(11, 12), (1, 13), (13, 14), (14, 15), (15, 16),
(1, 17), (17, 18), (18, 19), (19, 20)]
neighbor_link = [(i - 1, j - 1) for (i, j) in neighbor_1base]
connect_joint = np.array([2,2,2,3,3,5,6,7,3,9,10,11,1,13,14,15,1,17,18,19]) - 1
parts = [
np.array([5, 6, 7, 8]) - 1, # left_arm
np.array([9, 10, 11, 12]) - 1, # right_arm
np.array([13, 14, 15, 16]) - 1, # left_leg
np.array([17, 18, 19, 20]) - 1, # right_leg
np.array([1, 2, 3, 4]) - 1 # torso
]
elif self.dataset == 'ucla':
num_node = 20
neighbor_1base = [(1, 2), (2, 3), (3, 4), (3, 5), (5, 6),
(6, 7), (7, 8), (3, 9), (9, 10), (10, 11),
(11, 12), (1, 13), (13, 14), (14, 15), (15, 16),
(1, 17), (17, 18), (18, 19), (19, 20)]
neighbor_link = [(i - 1, j - 1) for (i, j) in neighbor_1base]
connect_joint = np.array([2,2,2,3,3,5,6,7,3,9,10,11,1,13,14,15,1,17,18,19]) - 1
parts = [
np.array([5, 6, 7, 8]) - 1, # left_arm
np.array([9, 10, 11, 12]) - 1, # right_arm
np.array([13, 14, 15, 16]) - 1, # left_leg
np.array([17, 18, 19, 20]) - 1, # right_leg
np.array([1, 2, 3, 4]) - 1 # torso
]
elif self.dataset == 'cmu':
num_node = 26
neighbor_1base = [(1, 2), (2, 3), (3, 4), (5, 6), (6, 7),
(7, 8), (1, 9), (5, 9), (9, 10), (10, 11),
(11, 12), (12, 13), (13, 14), (12, 15), (15, 16),
(16, 17), (17, 18), (18, 19), (17, 20), (12, 21),
(21, 22), (22, 23), (23, 24), (24, 25), (23, 26)]
neighbor_link = [(i - 1, j - 1) for (i, j) in neighbor_1base]
connect_joint = np.array([9,1,2,3,9,5,6,7,10,10,10,11,12,13,12,15,16,17,18,17,12,21,22,23,24,23]) - 1
parts = [
np.array([15, 16, 17, 18, 19, 20]) - 1, # left_arm
np.array([21, 22, 23, 24, 25, 26]) - 1, # right_arm
np.array([1, 2, 3, 4]) - 1, # left_leg
np.array([5, 6, 7, 8]) - 1, # right_leg
np.array([9, 10, 11, 12, 13, 14]) - 1 # torso
]
elif self.dataset == 'h36m':
num_node = 20
neighbor_1base = [(1, 2), (2, 3), (3, 4), (5, 6), (6, 7),
(7, 8), (1, 9), (5, 9), (9, 10), (10, 11),
(11, 12), (10, 13), (13, 14), (14, 15), (15, 16),
(10, 17), (17, 18), (18, 19), (19, 20)]
neighbor_link = [(i - 1, j - 1) for (i, j) in neighbor_1base]
connect_joint = np.array([9,1,2,3,9,5,6,7,9,9,10,11,10,13,14,15,10,17,18,19]) - 1
parts = [
np.array([13, 14, 15, 16]) - 1, # left_arm
np.array([17, 18, 19, 20]) - 1, # right_arm
np.array([1, 2, 3, 4]) - 1, # left_leg
np.array([5, 6, 7, 8]) - 1, # right_leg
np.array([9, 10, 11, 12]) - 1 # torso
]
else:
num_node, neighbor_link, connect_joint, parts = 0, [], [], []
logging.info('')
logging.error('Error: Do NOT exist this dataset: {}!'.format(self.dataset))
raise ValueError()
self_link = [(i, i) for i in range(num_node)]
edge = self_link + neighbor_link
return num_node, edge, connect_joint, parts
def _get_hop_distance(self):
A = np.zeros((self.num_node, self.num_node))
for i, j in self.edge:
A[j, i] = 1
A[i, j] = 1
hop_dis = np.zeros((self.num_node, self.num_node)) + np.inf
transfer_mat = [np.linalg.matrix_power(A, d) for d in range(self.max_hop + 1)]
arrive_mat = (np.stack(transfer_mat) > 0)
for d in range(self.max_hop, -1, -1):
hop_dis[arrive_mat[d]] = d
return hop_dis
def _get_adjacency(self):
hop_dis = self._get_hop_distance()
valid_hop = range(0, self.max_hop + 1, self.dilation)
adjacency = np.zeros((self.num_node, self.num_node))
for hop in valid_hop:
adjacency[hop_dis == hop] = 1
normalize_adjacency = self._normalize_digraph(adjacency)
A = np.zeros((len(valid_hop), self.num_node, self.num_node))
for i, hop in enumerate(valid_hop):
A[i][hop_dis == hop] = normalize_adjacency[hop_dis == hop]
return A
def _normalize_digraph(self, A):
Dl = np.sum(A, 0)
num_node = A.shape[0]
Dn = np.zeros((num_node, num_node))
for i in range(num_node):
if Dl[i] > 0:
Dn[i, i] = Dl[i]**(-1)
AD = np.dot(A, Dn)
return AD