-
Notifications
You must be signed in to change notification settings - Fork 10
/
stgcn.py
191 lines (164 loc) · 6.02 KB
/
stgcn.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
"""Ref: https://colab.research.google.com/github/machine-perception-robotics-group/MPRGDeepLearningLectureNotebook/blob/master/15_gcn/03_action_recognition_ST_GCN.ipynb#scrollTo=Vk-AMCVb5jqM
"""
import numpy as np
import torch
import torch.nn as nn
class SpatialGraphConvLayer(nn.Module):
def __init__(self, in_channels: int, out_channels: int, Ks: int):
"""Implementation of Spacial Graph Convolution Layer.
Args:
in_channels (int): _description_
out_channels (int): _description_
Ks (int): _description_
"""
super().__init__()
self.Ks = Ks
self.conv = nn.Conv2d(in_channels=in_channels,
out_channels=out_channels * Ks,
kernel_size=1)
def forward(self, x: torch.Tensor, A: torch.Tensor) -> torch.Tensor:
"""
Args:
x (torch.Tensor): shape=(N, CH, FRAMES, VERTEX)
A (torch.Tensor): shape=(Ks, VERTEX, VERTEX)
Returns:
torch.Tensor: the same shape as input ``x``.
"""
x = self.conv(x)
n, kc, t, v = x.size()
x = x.view(n, self.Ks, kc // self.Ks, t, v)
# Apply GraphConv and sum up features.
x = torch.einsum('nkctv,kvw->nctw', (x, A))
return x.contiguous()
class TemporalConvLayer(nn.Module):
def __init__(
self,
in_channels: int,
Kt: int,
stride: int = 1,
dropout: float = 0.5,
) -> None:
"""Implementation of temporal convolution layer.
Args:
in_channels (int): _description_
Kt (int): kernel size for temporal domain.
stride (int): stride for temporal domain.
dropout (float): _description_
"""
super().__init__()
self.block = nn.Sequential(
nn.BatchNorm2d(in_channels),
nn.ReLU(),
nn.Dropout(dropout),
nn.Conv2d(in_channels,
in_channels,
(Kt, 1),
(stride, 1),
((Kt - 1) // 2, 0)),
nn.BatchNorm2d(in_channels),
nn.ReLU(),
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
Args:
x (torch.Tensor): shape=(BATCH, CH, FRAMES, VERTEX)
Returns:
torch.Tensor: the same shape as input
"""
x = self.block(x)
return x
class STConvBlock(nn.Module):
"""Implementation of Spatial-temporal convolutional block with
learnable edge.
"""
def __init__(
self,
in_channels: int,
out_channels: int,
Ks: int = None,
Kt: int = None,
num_vertex: int = None,
stride: int = 1,
dropout=0.5,
):
super().__init__()
# 空間グラフの畳み込み
self.sgc = SpatialGraphConvLayer(
in_channels=in_channels,
out_channels=out_channels,
Ks=Ks,
)
# Learnable weight matrix M エッジに重みを与えます. どのエッジが重要かを学習します.
self.M = nn.Parameter(torch.ones((Ks, num_vertex, num_vertex)))
self.tgc = TemporalConvLayer(out_channels, Kt, stride)
def forward(self, x, A):
x = self.sgc(x, A * self.M)
x = self.tgc(x)
return x
# -----------------------------------------------------------------------------
class STGCN4Seg(nn.Module):
"""Implementation of ST-GCN for segmentation task.
"""
def __init__(
self,
in_channels: int = None,
num_classes: int = None,
Ks: int = None,
Kt: int = None,
A: np.ndarray = None,
):
super().__init__()
A = torch.tensor(A, dtype=torch.float32, requires_grad=False)
self.register_buffer('A', A)
A_size = A.size()
num_vertex = A.size(1)
# Batch Normalization
self.bn = nn.BatchNorm1d(in_channels * A_size[1])
# STConvBlocks
self.stgc1 = STConvBlock(
in_channels,
32,
Ks=Ks,
Kt=Kt,
num_vertex=num_vertex)
self.stgc2 = STConvBlock(32, 32, Ks=Ks, Kt=Kt, num_vertex=num_vertex)
self.stgc3 = STConvBlock(32, 32, Ks=Ks, Kt=Kt, num_vertex=num_vertex)
self.stgc4 = STConvBlock(32, 64, Ks=Ks, Kt=Kt, num_vertex=num_vertex)
self.stgc5 = STConvBlock(64, 64, Ks=Ks, Kt=Kt, num_vertex=num_vertex)
self.stgc6 = STConvBlock(64, 64, Ks=Ks, Kt=Kt, num_vertex=num_vertex)
self.stgc7 = STConvBlock(64, 64, Ks=Ks, Kt=Kt, num_vertex=num_vertex)
self.stgc8 = STConvBlock(64, 64, Ks=Ks, Kt=Kt, num_vertex=num_vertex)
self.stgc9 = STConvBlock(64, 64, Ks=Ks, Kt=Kt, num_vertex=num_vertex)
self.stgc10 = STConvBlock(64, 64, Ks=Ks, Kt=Kt, num_vertex=num_vertex)
self.stgc11 = STConvBlock(64, 64, Ks=Ks, Kt=Kt, num_vertex=num_vertex)
self.stgc12 = STConvBlock(64, 64, Ks=Ks, Kt=Kt, num_vertex=num_vertex)
# Prediction
self.fc = nn.Conv2d(64, num_classes, kernel_size=(1, num_vertex))
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
Args:
x (torch.Tensor): shape=(BATCH, IN_CH, FRAMES, VERTEX)
Returns:
torch.Tensor: the same shape as the input ``x``.
"""
# Batch Normalization
N, C, T, V = x.size() # batch, channel, frame, node
x = x.permute(0, 3, 1, 2).contiguous().view(N, V * C, T)
x = self.bn(x)
x = x.view(N, V, C, T).permute(0, 2, 3, 1).contiguous()
# STGC_blocks
x = self.stgc1(x, self.A)
x = self.stgc2(x, self.A)
x = self.stgc3(x, self.A)
x = self.stgc4(x, self.A)
x = self.stgc5(x, self.A)
x = self.stgc6(x, self.A)
x = self.stgc7(x, self.A)
x = self.stgc8(x, self.A)
x = self.stgc9(x, self.A)
x = self.stgc10(x, self.A)
x = self.stgc11(x, self.A)
x = self.stgc12(x, self.A)
# Prediction
x = self.fc(x)
return x