forked from facebookresearch/selavi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_clusters.py
362 lines (315 loc) · 12.5 KB
/
get_clusters.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
import os
import pickle
import torch
import torch.distributed as dist
from torch.utils.data.sampler import (
SubsetRandomSampler,
Sampler
)
from datasets.AVideoDataset import AVideoDataset
from utils import (
init_distributed_mode,
init_signal_handler,
load_model_parameters
)
from model import load_model
class Subset_Sampler(Sampler):
"""
Sample indices.
"""
def __init__(self, indices):
self.indices = indices
def __iter__(self):
return iter(self.indices)
def __len__(self):
return len(self.indices)
def accuracy(output, target, topk=(1,)):
"""Computes the precision@k for the specified values of k."""
with torch.no_grad():
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def get_cluster_assignments_gpu(
args,
dataset,
model,
logger=None,
device='cuda'
):
# clear cache at beginning
torch.cuda.empty_cache()
model.eval()
N = len(dataset)
# this process deals only with a subset of the dataset
local_nmb_data = N // args.world_size
train_indices = torch.arange(
args.rank * local_nmb_data,
(args.rank + 1) * local_nmb_data
).int()
# create subset sampler
sampler = Subset_Sampler(train_indices)
# we need a data loader
dataloader = torch.utils.data.DataLoader(
dataset,
batch_size=args.batch_size,
sampler=sampler,
num_workers=args.workers,
pin_memory=True,
collate_fn=None,
shuffle=False
)
# Ensure processes reach to end of optim clusters
if args.distributed:
dist.barrier()
# use GAP features
if args.headcount > 1:
model.module.return_features = True
aggregtensor = torch.cuda.DoubleTensor if args.headcount == 1 else torch.cuda.FloatTensor
dtype = torch.float64 if args.headcount == 1 else torch.float32
for batch_idx, batch in enumerate(dataloader):
print(f"{batch_idx}/{len(dataloader)}", end='\r', flush=True)
# Get data
video, audio, label, _, _ = batch
# Move to GPU
video = video.cuda(non_blocking=True)
audio = audio.cuda(non_blocking=True)
label = label.cuda(non_blocking=True)
# Forward pass
feat_v, feat_a = model(video, audio)
# gather the features computed by all processes
if args.distributed:
all_feat_v_list = [aggregtensor(feat_v.size()) for src in range(args.world_size)]
all_feat_a_list = [aggregtensor(feat_a.size()) for src in range(args.world_size)]
all_labels_list = [torch.zeros(label.size(0), dtype=torch.long).cuda() for _ in range(args.world_size)]
dist.all_gather(all_feat_v_list, feat_v)
dist.all_gather(all_feat_a_list, feat_a)
dist.all_gather(all_labels_list, label)
else:
all_feat_v_list = [feat_v]
all_feat_a_list = [feat_a]
all_labels_list = [label]
# only main process stores all features
if args.rank == 0:
all_feat_v = torch.cat(all_feat_v_list)
all_feat_a = torch.cat(all_feat_a_list)
all_labels = torch.cat(all_labels_list).cpu()
if batch_idx == 0 and (args.rank == 0):
fr = 0
K = feat_v.size(1)
PS_v = torch.zeros((N, K), dtype=dtype, device=device)
PS_a = torch.zeros((N, K), dtype=dtype, device=device)
labels = torch.zeros(N, dtype=torch.long)
# fill in arrays on main node
if args.rank == 0:
to = fr + all_feat_v.shape[0]
PS_v[fr: to] = all_feat_v
PS_a[fr: to] = all_feat_a
labels[fr: to] = all_labels
fr = to
if args.distributed:
dist.barrier()
# Dump results
if args.rank == 0:
PS_v_heads,PS_a_heads = [], []
for h in range(args.headcount):
head_a = getattr(model.module, f'mlp_a{h}')
head_v = getattr(model.module, f'mlp_v{h}')
PS_v_heads.append(head_v.forward(PS_v))
PS_a_heads.append(head_a.forward(PS_a))
PS = [PS_v_heads, labels, PS_a_heads]
os.makedirs(args.output_dir, exist_ok=True)
save_path = os.path.join(args.output_dir, f'{args.exp_desc}.pkl')
with open(save_path, 'wb') as handle:
pickle.dump(PS, handle, protocol=pickle.HIGHEST_PROTOCOL)
print(f"Finished Dumping!")
# Make other processes wait
if args.distributed:
dist.barrier()
return
def parse_args():
def str2bool(v):
v = v.lower()
if v in ('yes', 'true', 't', '1'):
return True
elif v in ('no', 'false', 'f', '0'):
return False
raise ValueError('Boolean argument needs to be true or false. '
'Instead, it is %s.' % v)
import argparse
parser = argparse.ArgumentParser(description='Video Cluster Fit')
parser.register('type', 'bool', str2bool)
parser.add_argument('--output_dir', default='.', type=str,
help='path where to save')
parser.add_argument('--weights_path', default='', type=str,
help='Path to weights file')
parser.add_argument('--exp_desc', default='vggsound_clusters', type=str,
help='desc of exp')
parser.add_argument('--pretrained', default='False', type='bool',
help="Use pre-trained models from the modelzoo")
parser.add_argument('--dataset', default='vggsound', type=str,
choices=['kinetics', 'vggsound', 'kinetics_sound', 'ave'],
help='name of dataset')
parser.add_argument("--root_dir", type=str, default="/path/to/dataset",
help="root dir of dataset")
parser.add_argument('--mode', default='val', type=str,
help='mode of dataset')
parser.add_argument('--num_data_samples', default=14032, type=int,
help='number of samples in dataset')
# AUDIO UTILS
parser.add_argument("--num_sec_aud", type=int, default=1,
help="number of seconds of audio")
parser.add_argument("--aud_sample_rate", type=int, default=24000,
help="audio sample rate")
parser.add_argument("--aud_spec_type", type=int, default=2,
help="audio spec type")
parser.add_argument('--use_volume_jittering', type='bool', default='False',
help='use volume jittering')
parser.add_argument('--use_audio_temp_jittering', type='bool', default='False',
help='use audio temporal jittering')
parser.add_argument('--z_normalize', type='bool', default='True',
help='z-normalize the audio')
### DATA
parser.add_argument('--batch_size', default=96, type=int)
parser.add_argument('--workers', default=10, type=int,
help='number of data loading workers (default: 16)')
### MODEL
parser.add_argument("--vid_base_arch", default="r2plus1d_18", type=str,
help="video architecture", choices=['r2plus1d_18'])
parser.add_argument("--aud_base_arch", default="resnet9", type=str,
help="audio architecture", choices=['resnet9', 'resnet18'])
parser.add_argument('--use_mlp', type='bool', default='True',
help='use MLP head')
parser.add_argument('--norm_feat', type='bool', default='False',
help='normalize pre-mlp features')
parser.add_argument("--num_clusters", default=256, type=int,
help="final layer dimension in projection head")
parser.add_argument("--headcount", default=1, type=int,
help="number of heads")
# distributed training parameters
parser.add_argument("--dist_url", default="env://", type=str,
help="""url used to set up distributed
training; see https://pytorch.org/docs/stable/distributed.html""")
parser.add_argument("--world_size", default=-1, type=int, help="""
number of processes: it is set automatically and
should not be passed as argument""")
parser.add_argument("--rank", default=0, type=int,
help="""rank of this process:
it is set automatically and should not be passed as argument""")
parser.add_argument("--local_rank", default=0, type=int,
help="this argument is not used and should be ignored")
parser.add_argument("--distributed", default='False', type='bool',
help="in distributed mode")
args = parser.parse_args()
return args
if __name__ == '__main__':
# parse args
args = parse_args()
# Init distributed mode
if args.distributed:
init_distributed_mode(args)
init_signal_handler()
else:
args.rank = 0
args.world_size = 1
# Set up dataset hyper-params
if args.dataset == 'vggsound':
args.num_clusters = 309
if args.mode == 'train':
args.num_data_samples = 170752
else:
args.num_data_samples = 14032
elif args.dataset == 'kinetics':
args.num_clusters = 400
if args.mode == 'train':
args.num_data_samples = 230976
else:
args.num_data_samples = 18968
elif args.dataset == 'kinetics_sound':
args.num_clusters = 32
if args.mode == 'train':
args.num_data_samples = 22408
else:
args.num_data_samples = 22408
elif args.dataset == 'ave':
args.num_clusters = 28
if args.mode == 'train':
args.num_data_samples = 3328
else:
args.num_data_samples = 3328
# Get dataset
dataset = AVideoDataset(
ds_name=args.dataset,
root_dir=args.root_dir,
mode=args.mode,
num_frames=30,
sample_rate=1,
train_crop_size=112,
num_data_samples=args.num_data_samples,
target_fps=30,
decode_audio=True,
num_sec=args.num_sec_aud,
aud_sample_rate=args.aud_sample_rate,
aud_spec_type=args.aud_spec_type,
use_volume_jittering=args.use_volume_jittering,
use_temporal_jittering=args.use_audio_temp_jittering,
z_normalize=args.z_normalize,
center_crop=True,
temp_jitter=False,
)
weight_path_type = type(args.weights_path)
if weight_path_type == str:
weight_path_not_none = args.weights_path != 'None'
else:
weight_path_not_none = args.weights_path is not None
# Load model
args.headcount = args.headcount if weight_path_not_none else 1
model = load_model(
vid_base_arch=args.vid_base_arch,
aud_base_arch=args.aud_base_arch,
pretrained=args.pretrained,
norm_feat=args.norm_feat,
use_mlp=args.use_mlp,
headcount=args.headcount,
num_classes=args.num_clusters,
)
# Load model weights
to_restore = {'epoch': 0}
if not args.pretrained:
if weight_path_not_none:
print("Loading model weights")
if os.path.exists(args.weights_path):
ckpt_dict = torch.load(args.weights_path)
model_weights = ckpt_dict["model"]
epoch = ckpt_dict["epoch"]
print(f"Epoch checkpoint: {epoch}")
load_model_parameters(model, model_weights)
else:
print("Random weights")
# Put model in distributed mode
model = model.cuda()
if args.distributed:
ngpus_per_node = torch.cuda.device_count()
model = torch.nn.parallel.DistributedDataParallel(
model,
device_ids=[args.local_rank],
output_device=args.local_rank,
broadcast_buffers=False
)
else:
model = torch.nn.DataParallel(model)
# Get cluster assignments
with torch.no_grad():
get_cluster_assignments_gpu(args, dataset, model)