-
Notifications
You must be signed in to change notification settings - Fork 20
/
add_gcn.py
151 lines (120 loc) · 4.67 KB
/
add_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
import torch
import torch.nn as nn
class DynamicGraphConvolution(nn.Module):
def __init__(self, in_features, out_features, num_nodes):
super(DynamicGraphConvolution, self).__init__()
self.static_adj = nn.Sequential(
nn.Conv1d(num_nodes, num_nodes, 1, bias=False),
nn.LeakyReLU(0.2))
self.static_weight = nn.Sequential(
nn.Conv1d(in_features, out_features, 1),
nn.LeakyReLU(0.2))
self.gap = nn.AdaptiveAvgPool1d(1)
self.conv_global = nn.Conv1d(in_features, in_features, 1)
self.bn_global = nn.BatchNorm1d(in_features)
self.relu = nn.LeakyReLU(0.2)
self.conv_create_co_mat = nn.Conv1d(in_features*2, num_nodes, 1)
self.dynamic_weight = nn.Conv1d(in_features, out_features, 1)
def forward_static_gcn(self, x):
x = self.static_adj(x.transpose(1, 2))
x = self.static_weight(x.transpose(1, 2))
return x
def forward_construct_dynamic_graph(self, x):
### Model global representations ###
x_glb = self.gap(x)
x_glb = self.conv_global(x_glb)
x_glb = self.bn_global(x_glb)
x_glb = self.relu(x_glb)
x_glb = x_glb.expand(x_glb.size(0), x_glb.size(1), x.size(2))
### Construct the dynamic correlation matrix ###
x = torch.cat((x_glb, x), dim=1)
dynamic_adj = self.conv_create_co_mat(x)
dynamic_adj = torch.sigmoid(dynamic_adj)
return dynamic_adj
def forward_dynamic_gcn(self, x, dynamic_adj):
x = torch.matmul(x, dynamic_adj)
x = self.relu(x)
x = self.dynamic_weight(x)
x = self.relu(x)
return x
def forward(self, x):
""" D-GCN module
Shape:
- Input: (B, C_in, N) # C_in: 1024, N: num_classes
- Output: (B, C_out, N) # C_out: 1024, N: num_classes
"""
out_static = self.forward_static_gcn(x)
x = x + out_static # residual
dynamic_adj = self.forward_construct_dynamic_graph(x)
x = self.forward_dynamic_gcn(x, dynamic_adj)
return x
class ADD_GCN(nn.Module):
def __init__(self, model, num_classes):
super(ADD_GCN, self).__init__()
self.features = nn.Sequential(
model.conv1,
model.bn1,
model.relu,
model.maxpool,
model.layer1,
model.layer2,
model.layer3,
model.layer4,
)
self.num_classes = num_classes
self.fc = nn.Conv2d(model.fc.in_features, num_classes, (1,1), bias=False)
self.conv_transform = nn.Conv2d(2048, 1024, (1,1))
self.relu = nn.LeakyReLU(0.2)
self.gcn = DynamicGraphConvolution(1024, 1024, num_classes)
self.mask_mat = nn.Parameter(torch.eye(self.num_classes).float())
self.last_linear = nn.Conv1d(1024, self.num_classes, 1)
# image normalization
self.image_normalization_mean = [0.485, 0.456, 0.406]
self.image_normalization_std = [0.229, 0.224, 0.225]
def forward_feature(self, x):
x = self.features(x)
return x
def forward_classification_sm(self, x):
""" Get another confident scores {s_m}.
Shape:
- Input: (B, C_in, H, W) # C_in: 2048
- Output: (B, C_out) # C_out: num_classes
"""
x = self.fc(x)
x = x.view(x.size(0), x.size(1), -1)
x = x.topk(1, dim=-1)[0].mean(dim=-1)
return x
def forward_sam(self, x):
""" SAM module
Shape:
- Input: (B, C_in, H, W) # C_in: 2048
- Output: (B, C_out, N) # C_out: 1024, N: num_classes
"""
mask = self.fc(x)
mask = mask.view(mask.size(0), mask.size(1), -1)
mask = torch.sigmoid(mask)
mask = mask.transpose(1, 2)
x = self.conv_transform(x)
x = x.view(x.size(0), x.size(1), -1)
x = torch.matmul(x, mask)
return x
def forward_dgcn(self, x):
x = self.gcn(x)
return x
def forward(self, x):
x = self.forward_feature(x)
out1 = self.forward_classification_sm(x)
v = self.forward_sam(x) # B*1024*num_classes
z = self.forward_dgcn(v)
z = v + z
out2 = self.last_linear(z) # B*1*num_classes
mask_mat = self.mask_mat.detach()
out2 = (out2 * mask_mat).sum(-1)
return out1, out2
def get_config_optim(self, lr, lrp):
small_lr_layers = list(map(id, self.features.parameters()))
large_lr_layers = filter(lambda p:id(p) not in small_lr_layers, self.parameters())
return [
{'params': self.features.parameters(), 'lr': lr * lrp},
{'params': large_lr_layers, 'lr': lr},
]