-
Notifications
You must be signed in to change notification settings - Fork 13
/
eval_flow.py
283 lines (244 loc) · 10.6 KB
/
eval_flow.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
import argparse
import mlflow
import numpy as np
import torch
from torch.optim import *
from configs.parser import YAMLParser
from dataloader.h5 import H5Loader
from loss.flow import FWL, RSAT, AEE
from models.model import (
FireNet,
RNNFireNet,
LeakyFireNet,
FireFlowNet,
LeakyFireFlowNet,
E2VID,
EVFlowNet,
RecEVFlowNet,
LeakyRecEVFlowNet,
RNNRecEVFlowNet,
)
from models.model import (
LIFFireNet,
PLIFFireNet,
ALIFFireNet,
XLIFFireNet,
LIFFireFlowNet,
SpikingRecEVFlowNet,
PLIFRecEVFlowNet,
ALIFRecEVFlowNet,
XLIFRecEVFlowNet,
)
from utils.iwe import compute_pol_iwe
from utils.utils import load_model, create_model_dir
from utils.mlflow import log_config, log_results
from utils.visualization import Visualization, vis_activity
def test(args, config_parser):
mlflow.set_tracking_uri(args.path_mlflow)
run = mlflow.get_run(args.runid)
config = config_parser.merge_configs(run.data.params)
# configs
if config["loader"]["batch_size"] > 1:
config["vis"]["enabled"] = False
config["vis"]["store"] = False
config["vis"]["bars"] = False # progress bars not yet compatible batch_size > 1
# asserts
if "AEE" in config["metrics"]["name"]:
assert (
config["data"]["mode"] == "gtflow_dt1" or config["data"]["mode"] == "gtflow_dt4"
), "AEE computation not possible without ground truth mode"
if "AEE" in config["metrics"]["name"]:
assert config["data"]["window"] <= 1, "AEE computation not compatible with window > 1"
assert np.isclose(
(1.0 / config["data"]["window"]) % 1.0, 0.0
), "AEE computation not compatible with windows whose inverse is not a round number"
if config["data"]["mode"] == "frames":
if config["data"]["window"] <= 1.0:
assert np.isclose(
(1.0 / config["data"]["window"]) % 1.0, 0.0
), "Frames mode not compatible with < 1 windows whose inverse is not a round number"
else:
assert np.isclose(
config["data"]["window"] % 1.0, 0.0
), "Frames mode not compatible with > 1 fractional windows"
if not args.debug:
# create directory for inference results
path_results = create_model_dir(args.path_results, args.runid)
# store validation settings
eval_id = log_config(path_results, args.runid, config)
else:
path_results = None
eval_id = -1
# initialize settings
device = config_parser.device
kwargs = config_parser.loader_kwargs
# visualization tool
if config["vis"]["enabled"] or config["vis"]["store"]:
vis = Visualization(config, eval_id=eval_id, path_results=path_results)
# model initialization and settings
model = eval(config["model"]["name"])(config["model"]).to(device)
model = load_model(args.runid, model, device)
model.eval()
# validation metric
criteria = []
if "metrics" in config.keys():
for metric in config["metrics"]["name"]:
criteria.append(eval(metric)(config, device, flow_scaling=config["metrics"]["flow_scaling"]))
# data loader
data = H5Loader(config, config["model"]["num_bins"])
dataloader = torch.utils.data.DataLoader(
data,
drop_last=True,
batch_size=config["loader"]["batch_size"],
collate_fn=data.custom_collate,
worker_init_fn=config_parser.worker_init_fn,
**kwargs,
)
# inference loop
idx_AEE = 0
val_results = {}
end_test = False
activity_log = None
with torch.no_grad():
while True:
for inputs in dataloader:
if data.new_seq:
data.new_seq = False
activity_log = None
model.reset_states()
# finish inference loop
if data.seq_num >= len(data.files):
end_test = True
break
# forward pass
x = model(
inputs["event_voxel"].to(device), inputs["event_cnt"].to(device), log=config["vis"]["activity"]
)
# mask flow for visualization
flow_vis = x["flow"][-1].clone()
if model.mask:
flow_vis *= inputs["event_mask"].to(device)
# image of warped events
iwe = compute_pol_iwe(
x["flow"][-1],
inputs["event_list"].to(device),
config["loader"]["resolution"],
inputs["event_list_pol_mask"][:, :, 0:1].to(device),
inputs["event_list_pol_mask"][:, :, 1:2].to(device),
flow_scaling=config["metrics"]["flow_scaling"],
round_idx=True,
)
iwe_window_vis = None
events_window_vis = None
masked_window_flow_vis = None
if "metrics" in config.keys():
# event flow association
for metric in criteria:
metric.event_flow_association(x["flow"], inputs)
# validation
for i, metric in enumerate(config["metrics"]["name"]):
if criteria[i].num_events >= config["data"]["window_eval"]:
# overwrite intermedia flow estimates with the final ones
if config["loss"]["overwrite_intermediate"]:
criteria[i].overwrite_intermediate_flow(x["flow"])
if metric == "AEE" and inputs["dt_gt"] <= 0.0:
continue
if metric == "AEE":
idx_AEE += 1
if idx_AEE != np.round(1.0 / config["data"]["window"]):
continue
# compute metric
val_metric = criteria[i]()
if metric == "AEE":
idx_AEE = 0
# accumulate results
for batch in range(config["loader"]["batch_size"]):
filename = data.files[data.batch_idx[batch] % len(data.files)].split("/")[-1]
if filename not in val_results.keys():
val_results[filename] = {}
for metric in config["metrics"]["name"]:
val_results[filename][metric] = {}
val_results[filename][metric]["metric"] = 0
val_results[filename][metric]["it"] = 0
if metric == "AEE":
val_results[filename][metric]["percent"] = 0
val_results[filename][metric]["it"] += 1
if metric == "AEE":
val_results[filename][metric]["metric"] += val_metric[0][batch].cpu().numpy()
val_results[filename][metric]["percent"] += val_metric[1][batch].cpu().numpy()
else:
val_results[filename][metric]["metric"] += val_metric[batch].cpu().numpy()
# visualize
if (
i == 0
and config["data"]["mode"] == "events"
and (config["vis"]["enabled"] or config["vis"]["store"])
and config["data"]["window"] < config["data"]["window_eval"]
):
events_window_vis = criteria[i].compute_window_events()
iwe_window_vis = criteria[i].compute_window_iwe()
masked_window_flow_vis = criteria[i].compute_masked_window_flow()
# reset criteria
criteria[i].reset()
# visualize
if config["vis"]["bars"]:
for bar in data.open_files_bar:
bar.next()
if config["vis"]["enabled"]:
vis.update(inputs, flow_vis, iwe, events_window_vis, masked_window_flow_vis, iwe_window_vis)
if config["vis"]["store"]:
sequence = data.files[data.batch_idx[0] % len(data.files)].split("/")[-1].split(".")[0]
vis.store(
inputs,
flow_vis,
iwe,
sequence,
events_window_vis,
masked_window_flow_vis,
iwe_window_vis,
ts=data.last_proc_timestamp,
)
# visualize activity
if config["vis"]["activity"]:
activity_log = vis_activity(x["activity"], activity_log)
if end_test:
break
if config["vis"]["bars"]:
for bar in data.open_files_bar:
bar.finish()
# store validation config and results
results = {}
if not args.debug and "metrics" in config.keys():
for metric in config["metrics"]["name"]:
results[metric] = {}
if metric == "AEE":
results[metric + "_percent"] = {}
for key in val_results.keys():
results[metric][key] = str(val_results[key][metric]["metric"] / val_results[key][metric]["it"])
if metric == "AEE":
results[metric + "_percent"][key] = str(
val_results[key][metric]["percent"] / val_results[key][metric]["it"]
)
log_results(args.runid, results, path_results, eval_id)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("runid", help="mlflow run")
parser.add_argument(
"--config",
default="configs/eval_flow.yml",
help="config file, overwrites mlflow settings",
)
parser.add_argument(
"--path_mlflow",
default="",
help="location of the mlflow ui",
)
parser.add_argument("--path_results", default="results_inference/")
parser.add_argument(
"--debug",
action="store_true",
help="don't save stuff",
)
args = parser.parse_args()
# launch testing
test(args, YAMLParser(args.config))