-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextract_layer_features.py
executable file
·265 lines (222 loc) · 11.4 KB
/
extract_layer_features.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
import json
import logging.config
import os
import time
from argparse import ArgumentParser
from collections import OrderedDict
from math import ceil
from pathlib import Path
import numpy as np
import torch
from addict import Dict
from torch.utils.data import DataLoader
from torch_geometric.data import Batch
from tqdm import tqdm
from IO.datasets import SoccerNetFeaturesTesting
from IO.datasets import SoccerNetGCNTesting
from models import ActionSpotter
from models import GraphActionSpotter
from models import Multistream
from utils import parse_model_conf, parse_logs, get_git_revision_hash
class Hook():
def __init__(self, module, backward=False):
if backward:
self.hook = module.register_backward_hook(self.hook_fn)
else:
self.hook = module.register_forward_hook(self.hook_fn)
def hook_fn(self, module, input, output):
self.input = input
self.output = output
def close(self):
self.hook.remove()
def compute_layer_output(model, layer, features):
hook = Hook(layer)
if isinstance(features, Batch):
features = features.to_data_list()
num_features = len(features)
batch_size = model.frames_per_window
layer_outputs = []
for b in range(ceil(num_features / batch_size)):
start_frame = batch_size * b
end_frame = min(num_features, start_frame + batch_size)
f = Batch.from_data_list(features[start_frame:end_frame])
model(f.cuda())
layer_outputs.append(hook.output.cpu().detach().numpy())
layer_outputs = np.concatenate(layer_outputs)
else:
features = [f.squeeze(0) for f in features]
num_features = len(features[0])
layer_outputs = []
for b in range(num_features):
start_frame = b
end_frame = min(num_features, start_frame + 1)
f = [f[start_frame:end_frame].cuda() for f in features]
model(f[0]) if len(features) == 1 else model(f)
layer_outputs.append(hook.output.cpu().detach().numpy())
layer_outputs = np.concatenate(layer_outputs)
return layer_outputs
def main(model_args, extraction_args, main_args):
logging.info("Parameters:")
logging.info(model_args)
logging.info(extraction_args)
has_graphs = any([s.name.startswith('graph') for s in model_args.streams])
if len(model_args.streams) == 1:
if has_graphs:
model = GraphActionSpotter(model_args.pool_layer.name,
model_args.graph.backbone.name,
model_args.window_size_sec,
model_args.frame_rate,
model_args.num_classes,
model_args.pool_layer.vocab_size,
model_args.graph.backbone.feature_multiplier,
model_args.graph.calibration_flags.use_confidence,
model_args.graph.feature_vector_size,
extraction_args.weights).cuda()
else:
model = ActionSpotter(model_args.pool_layer.name,
model_args.streams[0].features.dim,
model_args.window_size_sec,
model_args.frame_rate,
model_args.num_classes,
model_args.pool_layer.vocab_size,
extraction_args.weights).cuda()
else:
assert not has_graphs, "Graphs for multiple streams processing not yet implemented"
model = Multistream([(s.name, s.features.dim) for s in model_args.streams],
model_args.pool_layer.name,
model_args.window_size_sec,
model_args.frame_rate,
model_args.num_classes,
model_args.pool_layer.vocab_size,
extraction_args.weights).cuda()
logging.info(model)
assert hasattr(model, extraction_args.layer), f"Model has not layer named {extraction_args.layer}"
for split in extraction_args.splits:
if has_graphs:
dataset = SoccerNetGCNTesting(extraction_args.dataset_path,
[split],
model_args.streams[0],
model_args.window_size_sec,
model_args.frame_rate,
model_args.graph.distance_players,
model_args.graph.invert_second_half_positions,
model_args.graph.filter_positions,
model_args.graph.min_detection_score,
model_args.graph.use_velocity,
model_args.graph.use_player_prediction,
model_args.graph.calibration_flags,
main_args.tiny,
main_args.cache,
main_args.overwrite_cache)
test_collate = SoccerNetGCNTesting.collate
else:
dataset = SoccerNetFeaturesTesting(extraction_args.dataset_path,
[split],
model_args.streams,
model_args.window_size_sec,
model_args.frame_rate,
main_args.tiny)
test_collate = None
loader = DataLoader(dataset,
batch_size=1,
shuffle=False,
num_workers=extraction_args.max_num_workers,
pin_memory=True,
collate_fn=test_collate)
model.eval()
extraction_layer = getattr(model, extraction_args.layer)
with tqdm(enumerate(loader), total=len(loader)) as t:
for i, (match_id, feat_half1, feat_half2) in t:
# Batch size of 1
match_id = match_id[0]
for j, feat_half in enumerate([feat_half1, feat_half2]):
features_filename = extraction_args.features_filename.format(extraction_layer.out_features, j+1)
extraction_fpath = extraction_args.dataset_path.joinpath(match_id, features_filename)
try:
if not main_args.overwrite_features and extraction_fpath.exists():
continue
features = feat_half.to_data_list()
outputs = compute_layer_output(model, extraction_layer, feat_half)
np.save(extraction_fpath, outputs)
except Exception as e:
logging.info(f'An exception occurred: {e}')
def parse_args():
parser = ArgumentParser(description='Multi-modal action spotting implementation')
parser.add_argument('layer', help='Layer name from the model to extract features from (default: None)',
default=None, type=str)
parser.add_argument('conf', help='JSON model configuration filepath',
type=lambda p: Path(p))
parser.add_argument('-d', '--dataset_path', required=False,
help='Path for SoccerNet dataset (default: data/soccernet)',
default="data/soccernet", type=lambda p: Path(p))
parser.add_argument('--splits', nargs='+', dest='splits',
help='list of splits for training (default: [train])',
default=["train", "valid", "test"])
parser.add_argument('--max_num_workers', required=False,
help='number of worker to load data (default: 1)',
default=1, type=int)
parser.add_argument('--weights_dir', required=False,
help='Path for weights saving directory (default: weights)',
default="weights", type=lambda p: Path(p))
parser.add_argument('--weights', required=False,
help='Weights to load (default: None)',
default=None, type=str)
parser.add_argument('--GPU', required=False,
help='ID of the GPU to use (default: -1)',
default=-1, type=int)
parser.add_argument('--log_config', required=False,
help='Logging configuration file (default: config/log_config.yml)',
default="config/log_config.yml", type=lambda p: Path(p))
parser.add_argument('--tiny', required=False,
help='Consider smaller amount of games (default: None)',
type=int, default=None)
parser.add_argument('--no_cache', required=False,
help="Don't use cached data (default: False)",
action='store_true')
parser.add_argument('--overwrite_features', required=False,
help='Overwrite features cached data (default: False)',
action='store_true')
args = parser.parse_args()
conf = parse_model_conf(args.conf, args.weights_dir)
if args.weights is None:
weights = conf.model.weights_dir.joinpath("model.pth.tar")
else:
weights = args.weights
extraction = Dict({'dataset_path': args.dataset_path,
'layer': args.layer,
'features_filename': f'{conf.model.identifier} layer:{args.layer} vector_size:' + '{} half:{}.npy',
'splits': args.splits,
'max_num_workers': args.max_num_workers,
'weights': weights})
main_args = Dict({'tiny': args.tiny,
'overwrite_features': args.overwrite_features,
'cache': not args.no_cache,
})
logs = parse_logs(args, conf.model)
return Dict({'model': conf.model,
'optimization': conf.optimization,
'extraction': extraction,
'logs': logs,
'GPU': args.GPU,
'main': main_args,
'program_args': args})
if __name__ == '__main__':
args = parse_args()
args.logs.log_fpath.parent.mkdir(parents=True, exist_ok=True)
logging.config.dictConfig(args.logs.log_config)
logging.info("Input arguments:")
logging.info(args.program_args)
with args.program_args.conf.open() as json_file:
conf = json.load(json_file, object_pairs_hook=OrderedDict)
logging.info("Configuration:")
logging.info(conf)
logging.info(f"Git commit hash: {get_git_revision_hash()}")
torch.manual_seed(args.optimization.random_seed)
np.random.seed(args.optimization.random_seed)
if args.GPU >= 0:
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = str(args.GPU)
start = time.time()
logging.info('Starting layer output extraction function')
main(args.model, args.extraction, args.main)
logging.info(f'Total Execution Time is {time.time() - start} seconds')