-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.py
executable file
·274 lines (250 loc) · 9.73 KB
/
test.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
import argparse
import json
import os
import random
import time as dt
from pydoc import locate
import numpy as np
import sklearn.metrics
import torch.utils.data
import datasets
import utils
parser = argparse.ArgumentParser()
parser.add_argument(
"--load", action="append", type=lambda kv: kv.split("="), dest="load"
)
parser.add_argument("--opt_file", type=str)
parser.add_argument("--list_path", type=str, default="lists/3d_test")
parser.add_argument("--load_scores", nargs="+", default=None)
parser.add_argument(
"--list_name",
nargs="+",
default=[
"O1_test_occluded_dynamic_1_nobj1",
"O1_test_occluded_dynamic_1_nobj2",
"O1_test_occluded_dynamic_1_nobj3",
"O1_test_occluded_dynamic_2_nobj1",
"O1_test_occluded_dynamic_2_nobj2",
"O1_test_occluded_dynamic_2_nobj3",
"O1_test_occluded_static_nobj1",
"O1_test_occluded_static_nobj2",
"O1_test_occluded_static_nobj3",
"O1_test_visible_dynamic_1_nobj1",
"O1_test_visible_dynamic_1_nobj2",
"O1_test_visible_dynamic_1_nobj3",
"O1_test_visible_dynamic_2_nobj1",
"O1_test_visible_dynamic_2_nobj2",
"O1_test_visible_dynamic_2_nobj3",
"O1_test_visible_static_nobj1",
"O1_test_visible_static_nobj2",
"O1_test_visible_static_nobj3",
"O2_test_occluded_dynamic_1_nobj1",
"O2_test_occluded_dynamic_1_nobj2",
"O2_test_occluded_dynamic_1_nobj3",
"O2_test_occluded_dynamic_2_nobj1",
"O2_test_occluded_dynamic_2_nobj2",
"O2_test_occluded_dynamic_2_nobj3",
"O2_test_occluded_static_nobj1",
"O2_test_occluded_static_nobj2",
"O2_test_occluded_static_nobj3",
"O2_test_visible_dynamic_1_nobj1",
"O2_test_visible_dynamic_1_nobj2",
"O2_test_visible_dynamic_1_nobj3",
"O2_test_visible_dynamic_2_nobj1",
"O2_test_visible_dynamic_2_nobj2",
"O2_test_visible_dynamic_2_nobj3",
"O2_test_visible_static_nobj1",
"O2_test_visible_static_nobj2",
"O2_test_visible_static_nobj3",
"O3_test_occluded_dynamic_1_nobj1",
"O3_test_occluded_dynamic_1_nobj2",
"O3_test_occluded_dynamic_1_nobj3",
"O3_test_occluded_dynamic_2_nobj1",
"O3_test_occluded_dynamic_2_nobj2",
"O3_test_occluded_dynamic_2_nobj3",
"O3_test_occluded_static_nobj1",
"O3_test_occluded_static_nobj2",
"O3_test_occluded_static_nobj3",
"O3_test_visible_dynamic_1_nobj1",
"O3_test_visible_dynamic_1_nobj2",
"O3_test_visible_dynamic_1_nobj3",
"O3_test_visible_dynamic_2_nobj1",
"O3_test_visible_dynamic_2_nobj2",
"O3_test_visible_dynamic_2_nobj3",
"O3_test_visible_static_nobj1",
"O3_test_visible_static_nobj2",
"O3_test_visible_static_nobj3",
],
)
parser.add_argument("--name", default="test")
parser.add_argument("--checkpoint", type=str, default="checkpoints/tests")
parser.add_argument("--viz", action="store_true")
parser.add_argument("--verbose", action="store_true")
parser.add_argument("--image_save", action="store_true")
parser.add_argument("--image_save_interval", type=int, default=5)
parser.add_argument("--visdom", action="store_true")
parser.add_argument("--visdom_interval", type=int, default=1)
parser.add_argument("--manualSeed", type=int, default=1)
parser.add_argument("--eval", action="store_true")
parser.add_argument("--gpu", action="store_true")
parser.add_argument("--num_workers", type=int, default=20)
parser.add_argument("--count", type=int, default=25)
parser.add_argument("--mask_object", nargs="+", default=["object", "occluder"])
opt_test = parser.parse_args()
opt_test.name += "_" + time.strftime("%y%m%d_%H%M%S")
opt_test.checkpoint = os.path.join(opt_test.checkpoint, opt_test.name)
print(opt_test)
random.seed(opt_test.manualSeed)
torch.manual_seed(opt_test.manualSeed)
if opt_test.gpu:
torch.cuda.manual_seed_all(opt_test.manualSeed)
if opt_test.load:
with open(opt_test.opt_file, "r") as f:
data = f.readlines()
opt = json.loads(data[0])
opt["p_red"] = 0
opt["mask_object"] = opt_test.mask_object
print(opt)
opt = utils.to_namespace(opt)
opt.bsz = opt.m
opt.count = opt_test.count
model = locate("models.%s" % opt.model)(opt, test=True)
model.load(opt_test.load)
if opt_test.gpu:
model.gpu()
if opt_test.eval:
model.eval()
else:
print("WARNING: no call to eval()")
viz = utils.Viz(opt_test)
viz_output = utils.Viz(opt_test)
else:
assert opt_test.load_scores
def process_batch(batch, j, t0):
"""Compute score for every frames in a video (which is also a batch).
batch = [input, target]: all frames in the video
j: index of the video
t0: time when the test started
"""
nbatch = vars(opt)["nbatch_test"]
frame_scores = np.zeros((opt.m, 4))
d3, d4 = batch[0].size(3), batch[0].size(4)
for i in range(opt.bsz):
for c in range(4):
data = [batch[0][i][c], batch[1][i][c]]
frame_scores[i][c] = model.score(data)
if opt_test.image_save or opt_test.visdom:
to_plot = []
nviz = 1
to_plot.append(utils.stack(data[0].unsqueeze(0), nviz, opt.input_len))
to_plot.append(utils.stack(data[1].unsqueeze(0), nviz, opt.target_len))
to_plot.append(utils.stack(model.output(), nviz, opt.target_len))
img = np.concatenate(to_plot, 2)
viz_output(img, {}, i, j, nbatch, "output")
if opt_test.verbose:
batch_time = (dt.time() - t0) / (j + 1)
eta = nbatch * batch_time
out = " test: batch %.5d/%.5d |" % (j, nbatch - 1)
mean = frame_scores.mean(0)
out += " Mean: %.2e - %.2e - %.2e - %.2e |" % (
mean[0],
mean[1],
mean[2],
mean[3],
)
out += " batch time: %.2fs | test eta: %.2dH%.2dm%.2ds" % (
batch_time,
eta / (60 * 60),
(eta / 60) % 60,
eta % 60,
)
print(out, end="\r")
if opt_test.image_save or opt_test.visdom:
for c in range(4):
to_plot = []
nviz = opt.m
to_plot.append(utils.stack(batch[0].select(1, c), nviz, opt.input_len))
to_plot.append(utils.stack(batch[1].select(1, c), nviz, opt.target_len))
img = np.concatenate(to_plot, 2)
viz(img, {"c": frame_scores}, i, j, nbatch, str(c))
return frame_scores
def _acc(scores, labels, k=2):
r = np.random.random(scores.shape)
# lexsort allows to randomly choose between ties
idx = np.lexsort((r, scores), axis=scores.ndim - 1)
m = 1
if idx.ndim > 1:
for i in range(k):
m -= idx[:, i].choose(labels.T).mean() / k
else:
m -= labels[idx[:k]].mean()
return m
def absolute_acc(scores, labels):
"""Computes accuracy for absolute classification task."""
k = scores.shape[0] * 2
return _acc(scores.flatten(), labels.flatten(), k)
def auc(scores, labels):
"""Computes Area Under the Roc Curve."""
return sklearn.metrics.roc_auc_score(labels.flatten(), scores.flatten())
def relative_acc(scores, labels):
"""Compute accuracy for relative classification task."""
mean_pos = (scores * labels).mean(1)
mean_imp = (scores * np.logical_not(labels)).mean(1)
return (np.mean(mean_pos > mean_imp) + np.mean(mean_pos >= mean_imp)) / 2
def test(list_name):
"""Performs the test for a given set of videos.
list_name: path to the subfolder (in opt_test.list_path) containing
the videos to test
"""
scores_mean, scores_min = [], []
if opt_test.load_scores:
for p in opt_test.load_scores:
scores_mean.append(np.load(p + "/" + list_name + "_scores_mean.npy"))
scores_min.append(np.load(p + "/" + list_name + "_scores_min.npy"))
scores_mean = np.array(scores_mean).mean(0)
scores_min = np.array(scores_min).mean(0)
else:
opt.list = os.path.join(opt_test.list_path, list_name)
testLoader = torch.utils.data.DataLoader(
datasets.IntPhys(opt, "test"),
opt.bsz,
num_workers=opt_test.num_workers,
shuffle=False,
)
t0 = dt.time()
for j, batch in enumerate(testLoader, 0):
frame_scores = process_batch(batch, j, t0)
scores_mean.append(frame_scores.mean(0))
scores_min.append(frame_scores.min(0))
scores_mean = np.array(scores_mean)
scores_min = np.array(scores_min)
np.save(os.path.join(opt_test.checkpoint, list_name + "_scores_min"), scores_min)
np.save(os.path.join(opt_test.checkpoint, list_name + "_scores_mean"), scores_mean)
labels = np.ones(scores_mean.shape, dtype=bool)
labels[:, -2:] = False
res = {}
dscores = {"scores_mean": scores_mean, "scores_min": scores_min}
dacc = {"absolute_acc": absolute_acc, "auc": auc, "relative_acc": relative_acc}
for keysc, sc in dscores.items():
for keyacc, acc in dacc.items():
res["%s(%s)" % (keyacc, keysc)] = acc(sc, labels)
return res
if not os.path.isdir(opt_test.checkpoint):
os.mkdir(opt_test.checkpoint)
if opt_test.load:
with open(os.path.join(opt_test.checkpoint, "opt.txt"), "w") as f:
json.dump(vars(opt), f)
with open(os.path.join(opt_test.checkpoint, "opt_test.txt"), "w") as f:
json.dump(vars(opt_test), f)
results = {}
for list_name in opt_test.list_name:
res = test(list_name)
print(list_name)
print(res)
results[list_name] = res
with open(os.path.join(opt_test.checkpoint, "results.txt"), "w") as f:
json.dump(results, f)
print(
"python paper/format_table.py --results checkpoints/tests/%s/results.txt --score 'relative_acc(scores_mean)'"
% opt_test.name
)