-
Notifications
You must be signed in to change notification settings - Fork 174
/
gaitssb.py
280 lines (226 loc) · 10 KB
/
gaitssb.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import torch
import numpy as np
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from ..base_model import BaseModel
from ..modules import PackSequenceWrapper, HorizontalPoolingPyramid, SetBlockWrapper, ParallelBN1d, SeparateFCs
from utils import np2var, list2var, get_valid_args, ddp_all_gather
from data.transform import get_transform
from einops import rearrange
# Modified from https://github.com/PatrickHua/SimSiam/blob/main/models/simsiam.py
class GaitSSB_Pretrain(BaseModel):
def __init__(self, cfgs, training=True):
super(GaitSSB_Pretrain, self).__init__(cfgs, training=training)
def build_network(self, model_cfg):
self.p = model_cfg['parts_num']
self.Backbone = self.get_backbone(model_cfg['backbone_cfg'])
self.Backbone = SetBlockWrapper(self.Backbone)
self.TP = PackSequenceWrapper(torch.max)
self.HPP = HorizontalPoolingPyramid([16, 8, 4, 2, 1])
out_channels = model_cfg['backbone_cfg']['channels'][-1]
hidden_dim = out_channels
self.projector = nn.Sequential(SeparateFCs(self.p, out_channels, hidden_dim),
ParallelBN1d(self.p, hidden_dim),
nn.ReLU(inplace=True),
SeparateFCs(self.p, hidden_dim, out_channels),
ParallelBN1d(self.p, out_channels))
self.predictor = nn.Sequential(SeparateFCs(self.p, out_channels, hidden_dim),
ParallelBN1d(self.p, hidden_dim),
nn.ReLU(inplace=True),
SeparateFCs(self.p, hidden_dim, out_channels))
def inputs_pretreament(self, inputs):
if self.training:
seqs_batch, labs_batch, typs_batch, vies_batch, seqL_batch = inputs
trf_cfgs = self.engine_cfg['transform']
seq_trfs = get_transform(trf_cfgs)
requires_grad = True if self.training else False
batch_size = int(len(seqs_batch[0]) / 2)
img_q = [np2var(np.asarray([trf(fra) for fra in seq[:batch_size]]), requires_grad=requires_grad).float() for trf, seq in zip(seq_trfs, seqs_batch)]
img_k = [np2var(np.asarray([trf(fra) for fra in seq[batch_size:]]), requires_grad=requires_grad).float() for trf, seq in zip(seq_trfs, seqs_batch)]
seqs = [img_q, img_k]
typs = typs_batch
vies = vies_batch
if self.training:
labs = list2var(labs_batch).long()
else:
labs = None
if seqL_batch is not None:
seqL_batch = np2var(seqL_batch).int()
seqL = seqL_batch
ipts = seqs
del seqs
return ipts, labs, typs, vies, (seqL, seqL)
else:
return super().inputs_pretreament(inputs)
def encoder(self, inputs):
sils, seqL = inputs
assert sils.size(-1) in [44, 88]
outs = self.Backbone(sils) # [n, c, s, h, w]
outs = self.TP(outs, seqL, options={"dim": 2})[0] # [n, c, h, w]
feat = self.HPP(outs) # [n, c, p], Horizontal Pooling, HP
return feat
def forward(self, inputs):
'''
Input:
sils_q: a batch of query images, [n, s, h, w]
sils_k: a batch of key images, [n, s, h, w]
Output:
logits, targets
'''
if self.training:
(sils_q, sils_k), labs, typs, vies, (seqL_q, seqL_k) = inputs
sils_q, sils_k = sils_q[0].unsqueeze(1), sils_k[0].unsqueeze(1)
q_input = (sils_q, seqL_q)
q_feat = self.encoder(q_input) # [n, c, p]
z1 = self.projector(q_feat)
p1 = self.predictor(z1)
k_input = (sils_k, seqL_k)
k_feat = self.encoder(k_input) # [n, c, p]
z2 = self.projector(k_feat)
p2 = self.predictor(z2)
logits1, labels1 = self.D(p1, z2)
logits2, labels2 = self.D(p2, z1)
retval = {
'training_feat': {'softmax1': {'logits': logits1, 'labels': labels1},
'softmax2': {'logits': logits2, 'labels': labels2}
},
'visual_summary': {'image/encoder_q': rearrange(sils_q, 'n c s h w -> (n s) c h w'),
'image/encoder_k': rearrange(sils_k, 'n c s h w -> (n s) c h w'),
},
'inference_feat': None
}
return retval
else:
sils, labs, typs, vies, seqL = inputs
sils = sils[0].unsqueeze(1)
feat = self.encoder((sils, seqL)) # [n, c, p]
feat = self.projector(feat) # [n, c, p]
feat = self.predictor(feat) # [n, c, p]
retval = {
'training_feat': None,
'visual_summary': None,
'inference_feat': {'embeddings': F.normalize(feat, dim=1)}
}
return retval
def D(self, p, z): # negative cosine similarity
"""
p: [n, c, p]
z: [n, c, p]
"""
z = z.detach() # stop gradient
n = p.size(0)
p = F.normalize(p, dim=1) # l2-normalize, [n, c, p]
z = F.normalize(z, dim=1) # l2-normalize, [n, c, p]
z = ddp_all_gather(z, dim=0, requires_grad=False) # [m, c, p], m = n * the number of GPUs
logits = torch.einsum('ncp, mcp->nmp', [p, z]) # [n, m, p]
rank = torch.distributed.get_rank()
labels = torch.arange(rank*n, (rank+1)*n, dtype=torch.long).cuda()
return logits, labels
import torch.optim as optim
import numpy as np
from utils import get_valid_args, list2var
class no_grad(torch.no_grad):
def __init__(self, enable=True):
super(no_grad, self).__init__()
self.enable = enable
def __enter__(self):
if self.enable:
super().__enter__()
else:
pass
def __exit__(self, *args):
if self.enable:
super().__exit__(*args)
else:
pass
class GaitSSB_Finetune(BaseModel):
def __init__(self, cfgs, training=True):
super(GaitSSB_Finetune, self).__init__(cfgs, training=training)
def build_network(self, model_cfg):
self.p = model_cfg['parts_num']
self.Backbone = self.get_backbone(model_cfg['backbone_cfg'])
self.Backbone = SetBlockWrapper(self.Backbone)
self.TP = PackSequenceWrapper(torch.max)
self.HPP = HorizontalPoolingPyramid([16, 8, 4, 2, 1])
out_channels = model_cfg['backbone_cfg']['channels'][-1]
hidden_dim = out_channels
self.projector = nn.Sequential(SeparateFCs(self.p, out_channels, hidden_dim),
ParallelBN1d(self.p, hidden_dim),
nn.ReLU(inplace=True),
SeparateFCs(self.p, hidden_dim, out_channels),
ParallelBN1d(self.p, out_channels))
self.backbone_lr = model_cfg['backbone_lr']
self.projector_lr = model_cfg['projector_lr']
self.head0 = SeparateFCs(self.p, out_channels, out_channels, norm=True)
def get_optimizer(self, optimizer_cfg):
optimizer = getattr(optim, optimizer_cfg['solver'])
valid_arg = get_valid_args(optimizer, optimizer_cfg, ['solver'])
ft_param_list = []
self.fix_layer = []
for i, ft_lr in enumerate(self.backbone_lr):
if ft_lr != 0:
ft_param_list.append({
'params': getattr(self.Backbone.forward_block, 'layer%d'%(i+1)).parameters(),
'lr': ft_lr,
})
else:
self.fix_layer.append('layer%d'%(i+1))
ft_param_list.append({
'params': self.projector.parameters(),
'lr': self.projector_lr,
})
ft_param_list.append({
'params': self.head0.parameters(),
'lr': valid_arg['lr']
})
optimizer = optimizer(ft_param_list, **valid_arg)
return optimizer
def encoder(self, inputs):
sils, seqL = inputs
n = sils.size(0)
sils = rearrange(sils, 'n c s h w -> (n s) c h w')
if not self.training:
self.fix_layer = ['layer1', 'layer2', 'layer3', 'layer4']
with no_grad():
outs = self.Backbone.forward_block.conv1(sils)
outs = self.Backbone.forward_block.bn1(outs)
outs = self.Backbone.forward_block.relu(outs)
with no_grad('layer1' in self.fix_layer):
outs = self.Backbone.forward_block.layer1(outs)
with no_grad('layer2' in self.fix_layer):
outs = self.Backbone.forward_block.layer2(outs)
with no_grad('layer3' in self.fix_layer):
outs = self.Backbone.forward_block.layer3(outs)
with no_grad('layer4' in self.fix_layer):
outs = self.Backbone.forward_block.layer4(outs)
outs = rearrange(outs, '(n s) c h w -> n c s h w', n=n)
outs = self.TP(outs, seqL, options={"dim": 2})[0] # [n, c, h, w]
feat = self.HPP(outs) # [n, c, p], Horizontal Pooling, HP
return feat
def forward(self, inputs):
if self.training:
self.maintain_non_zero_learning_rate()
sils, labs, typs, vies, seqL = inputs
sils = sils[0].unsqueeze(1)
feat = self.encoder([sils, seqL]) # [n, c, p]
feat = self.projector(feat) # [n, c, p]
feat = F.normalize(feat, dim=1)
embed = self.head0(feat) # [n, c, p]
retval = {
'training_feat': {
'triplet': {'embeddings': embed, 'labels': labs}
},
'visual_summary': {
'image/sils': rearrange(sils, 'n c s h w -> (n s) c h w')
},
'inference_feat': {
'embeddings': embed
}
}
return retval
def maintain_non_zero_learning_rate(self):
if self.iteration % 1000 == 0:
for param_group in self.optimizer.param_groups:
if param_group['lr'] < 1e-4:
param_group['lr'] = 1e-4