-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
135 lines (106 loc) · 4.18 KB
/
model.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
import torch.nn as nn
import torch
from torch.nn.parameter import Parameter
import math
import numpy as np
class GraphConvolution(nn.Module):
"""
adapted from : https://github.com/tkipf/gcn/blob/92600c39797c2bfb61a508e52b88fb554df30177/gcn/layers.py#L132
"""
def __init__(self, in_features, out_features, bias=True, node_n=34):
super(GraphConvolution, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = Parameter(torch.FloatTensor(in_features, out_features))
self.att = Parameter(torch.FloatTensor(node_n, node_n))
if bias:
self.bias = Parameter(torch.FloatTensor(out_features))
else:
self.register_parameter('bias', None)
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.weight.size(1))
self.weight.data.uniform_(-stdv, stdv)
self.att.data.uniform_(-stdv, stdv)
if self.bias is not None:
self.bias.data.uniform_(-stdv, stdv)
def forward(self, input):
support = torch.matmul(input, self.weight)
output = torch.matmul(self.att, support)
if self.bias is not None:
return output + self.bias
else:
return output
def __repr__(self):
return self.__class__.__name__ + ' (' \
+ str(self.in_features) + ' -> ' \
+ str(self.out_features) + ')'
class GC_Block(nn.Module):
def __init__(self, in_features, p_dropout, bias=True, node_n=34):
"""
Define a residual block of GCN
"""
super(GC_Block, self).__init__()
self.in_features = in_features
self.out_features = in_features
self.gc1 = GraphConvolution(in_features, in_features, node_n=node_n, bias=bias)
self.bn1 = nn.BatchNorm1d(node_n * in_features)
self.gc2 = GraphConvolution(in_features, in_features, node_n=node_n, bias=bias)
self.bn2 = nn.BatchNorm1d(node_n * in_features)
self.do = nn.Dropout(p_dropout)
self.act_f = nn.Tanh()
def forward(self, x):
y = self.gc1(x)
b, n, f = y.shape
y = self.bn1(y.view(b, -1)).view(b, n, f)
y = self.act_f(y)
y = self.do(y)
y = self.gc2(y)
b, n, f = y.shape
y = self.bn2(y.view(b, -1)).view(b, n, f)
y = self.act_f(y)
y = self.do(y)
return y + x
def __repr__(self):
return self.__class__.__name__ + ' (' \
+ str(self.in_features) + ' -> ' \
+ str(self.out_features) + ')'
class GCN(nn.Module):
def __init__(self, input_feature, hidden_feature, p_dropout, num_stage=1, keypoints=17, dims=2):
"""
:param input_feature: num of input feature
:param hidden_feature: num of hidden feature
:param p_dropout: drop out prob.
:param num_stage: number of residual blocks
:param node_n: number of nodes in graph
"""
super(GCN, self).__init__()
self.input_feature = input_feature
self.num_stage = num_stage
self.keypoints = keypoints
self.dims = dims
node_n = keypoints * dims
self.gc1 = GraphConvolution(input_feature, hidden_feature, node_n=node_n)
self.bn1 = nn.BatchNorm1d(node_n * hidden_feature)
self.gcbs = []
for i in range(num_stage):
self.gcbs.append(GC_Block(hidden_feature, p_dropout=p_dropout, node_n=node_n))
self.gcbs = nn.ModuleList(self.gcbs)
self.gc7 = GraphConvolution(hidden_feature, input_feature, node_n=node_n)
self.do = nn.Dropout(p_dropout)
self.act_f = nn.Tanh()
def forward(self, x):
x = torch.reshape(x, (x.shape[0], self.input_feature, self.keypoints * self.dims))
x = x.permute(0, 2, 1)
y = self.gc1(x)
b, n, f = y.shape
y = self.bn1(y.view(b, -1)).view(b, n, f)
y = self.act_f(y)
y = self.do(y)
for i in range(self.num_stage):
y = self.gcbs[i](y)
y = self.gc7(y)
y = y + x
y = y.permute(0, 2, 1)
y = torch.reshape(y, (y.shape[0], self.input_feature, self.keypoints, self.dims))
return y