Safemotion Lib
Loading...
Searching...
No Matches
graph.py
Go to the documentation of this file.
1import numpy as np
2
3class Graph():
4 """ The Graph to model the skeletons extracted by the openpose
5
6 Args:
7 strategy (string): must be one of the follow candidates
8 - uniform: Uniform Labeling
9 - distance: Distance Partitioning
10 - spatial: Spatial Configuration
11 For more information, please refer to the section 'Partition Strategies'
12 in our paper (https://arxiv.org/abs/1801.07455).
13
14 layout (string): must be one of the follow candidates
15 - openpose: Is consists of 18 joints. For more information, please
16 refer to https://github.com/CMU-Perceptual-Computing-Lab/openpose#output
17 - ntu-rgb+d: Is consists of 25 joints. For more information, please
18 refer to https://github.com/shahroudy/NTURGB-D
19
20 max_hop (int): the maximal distance between two connected nodes
21 dilation (int): controls the spacing between the kernel points
22
23 """
24
25 def __init__(self,
26 layout='openpose',
27 strategy='uniform',
28 max_hop=1,
29 dilation=1):
30 self.max_hop = max_hop
31 self.dilation = dilation
32
33 self.get_edge(layout)
35 self.num_node, self.edge, max_hop=max_hop)
36 self.get_adjacency(strategy)
37
38 def __str__(self):
39 return self.A
40
41 def get_edge(self, layout):
42 if layout == 'openpose':
43 self.num_node = 18
44 self_link = [(i, i) for i in range(self.num_node)]
45 neighbor_link = [(4, 3), (3, 2), (7, 6), (6, 5), (13, 12), (12,
46 11),
47 (10, 9), (9, 8), (11, 5), (8, 2), (5, 1), (2, 1),
48 (0, 1), (15, 0), (14, 0), (17, 15), (16, 14)]
49 self.edge = self_link + neighbor_link
50 self.center = 1
51 elif layout == 'ntu-rgb+d':
52 self.num_node = 25
53 self_link = [(i, i) for i in range(self.num_node)]
54 neighbor_1base = [(1, 2), (2, 21), (3, 21), (4, 3), (5, 21),
55 (6, 5), (7, 6), (8, 7), (9, 21), (10, 9),
56 (11, 10), (12, 11), (13, 1), (14, 13), (15, 14),
57 (16, 15), (17, 1), (18, 17), (19, 18), (20, 19),
58 (22, 23), (23, 8), (24, 25), (25, 12)]
59 neighbor_link = [(i - 1, j - 1) for (i, j) in neighbor_1base]
60 self.edge = self_link + neighbor_link
61 self.center = 21 - 1
62 elif layout == 'ntu_edge':
63 self.num_node = 24
64 self_link = [(i, i) for i in range(self.num_node)]
65 neighbor_1base = [(1, 2), (3, 2), (4, 3), (5, 2), (6, 5), (7, 6),
66 (8, 7), (9, 2), (10, 9), (11, 10), (12, 11),
67 (13, 1), (14, 13), (15, 14), (16, 15), (17, 1),
68 (18, 17), (19, 18), (20, 19), (21, 22), (22, 8),
69 (23, 24), (24, 12)]
70 neighbor_link = [(i - 1, j - 1) for (i, j) in neighbor_1base]
71 self.edge = self_link + neighbor_link
72 self.center = 2
73 elif layout=='coco':
74 self.num_node = 17
75 self_link = [(i, i) for i in range(self.num_node)]
76 neighbor_link = [(15, 13), (13, 11), (16, 14), (14, 12), (11, 5),
77 (12, 6), (9, 7), (7, 5), (10, 8), (8, 6), (5, 0),
78 (6, 0), (1, 0), (3, 1), (2, 0), (4, 2)]
79 self.edge = self_link + neighbor_link
80 self.center = 0
81 else:
82 raise ValueError("Do Not Exist This Layout.")
83
84 def get_adjacency(self, strategy):
85 valid_hop = range(0, self.max_hop + 1, self.dilation)
86 adjacency = np.zeros((self.num_node, self.num_node))
87 for hop in valid_hop:
88 adjacency[self.hop_dis == hop] = 1
89 normalize_adjacency = normalize_digraph(adjacency)
90
91 if strategy == 'uniform':
92 A = np.zeros((1, self.num_node, self.num_node))
93 A[0] = normalize_adjacency
94 self.A = A
95 elif strategy == 'distance':
96 A = np.zeros((len(valid_hop), self.num_node, self.num_node))
97 for i, hop in enumerate(valid_hop):
98 A[i][self.hop_dis == hop] = normalize_adjacency[self.hop_dis ==
99 hop]
100 self.A = A
101 elif strategy == 'spatial':
102 A = []
103 for hop in valid_hop:
104 a_root = np.zeros((self.num_node, self.num_node))
105 a_close = np.zeros((self.num_node, self.num_node))
106 a_further = np.zeros((self.num_node, self.num_node))
107 for i in range(self.num_node):
108 for j in range(self.num_node):
109 if self.hop_dis[j, i] == hop:
110 if self.hop_dis[j, self.center] == self.hop_dis[
111 i, self.center]:
112 a_root[j, i] = normalize_adjacency[j, i]
113 elif self.hop_dis[j, self.
114 center] > self.hop_dis[i, self.
115 center]:
116 a_close[j, i] = normalize_adjacency[j, i]
117 else:
118 a_further[j, i] = normalize_adjacency[j, i]
119 if hop == 0:
120 A.append(a_root)
121 else:
122 A.append(a_root + a_close)
123 A.append(a_further)
124 A = np.stack(A)
125 self.A = A
126 else:
127 raise ValueError("Do Not Exist This Strategy")
128
129
130def get_hop_distance(num_node, edge, max_hop=1):
131 A = np.zeros((num_node, num_node))
132 for i, j in edge:
133 A[j, i] = 1
134 A[i, j] = 1
135
136 # compute hop steps
137 hop_dis = np.zeros((num_node, num_node)) + np.inf
138 transfer_mat = [np.linalg.matrix_power(A, d) for d in range(max_hop + 1)]
139 arrive_mat = (np.stack(transfer_mat) > 0)
140 for d in range(max_hop, -1, -1):
141 hop_dis[arrive_mat[d]] = d
142 return hop_dis
143
144
146 Dl = np.sum(A, 0)
147 num_node = A.shape[0]
148 Dn = np.zeros((num_node, num_node))
149 for i in range(num_node):
150 if Dl[i] > 0:
151 Dn[i, i] = Dl[i]**(-1)
152 AD = np.dot(A, Dn)
153 return AD
154
155
157 Dl = np.sum(A, 0)
158 num_node = A.shape[0]
159 Dn = np.zeros((num_node, num_node))
160 for i in range(num_node):
161 if Dl[i] > 0:
162 Dn[i, i] = Dl[i]**(-0.5)
163 DAD = np.dot(np.dot(Dn, A), Dn)
164 return DAD
get_adjacency(self, strategy)
Definition graph.py:84
__str__(self)
Definition graph.py:38
get_edge(self, layout)
Definition graph.py:41
__init__(self, layout='openpose', strategy='uniform', max_hop=1, dilation=1)
Definition graph.py:29
normalize_undigraph(A)
Definition graph.py:156
get_hop_distance(num_node, edge, max_hop=1)
Definition graph.py:130
normalize_digraph(A)
Definition graph.py:145