forked from PaddlePaddle/PaddleVideo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathava_utils.py
394 lines (337 loc) · 13.5 KB
/
ava_utils.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import csv
import heapq
import logging
import time
from collections import defaultdict
from .ava_evaluation import object_detection_evaluation as det_eval
from .ava_evaluation import standard_fields
from .recall import eval_recalls
import shutil
import pickle
import time
import os
import os.path as osp
from paddlevideo.utils import get_logger, get_dist_info
import paddle.distributed as dist
import sys
import numpy as np
from pathlib import Path
from datetime import datetime
import paddle
def det2csv(info, dataset_len, results, custom_classes):
csv_results = []
for idx in range(dataset_len):
video_id = info[idx]['video_id']
timestamp = info[idx]['timestamp']
result = results[idx]
for label, _ in enumerate(result):
for bbox in result[label]:
if type(bbox) == paddle.Tensor:
bbox = bbox.numpy()
bbox_ = tuple(bbox.tolist())
if custom_classes is not None:
actual_label = custom_classes[label + 1]
else:
actual_label = label + 1
csv_results.append((
video_id,
timestamp,
) + bbox_[:4] + (actual_label, ) + bbox_[4:])
return csv_results
# results is organized by class
def results2csv(info, dataset_len, results, out_file, custom_classes=None):
if isinstance(results[0], list):
csv_results = det2csv(info, dataset_len, results, custom_classes)
# save space for float
def tostr(item):
if isinstance(item, float):
return f'{item:.3f}'
return str(item)
with open(out_file, 'w') as f:
for csv_result in csv_results:
f.write(','.join(map(lambda x: tostr(x), csv_result)))
f.write('\n')
def print_time(message, start):
print('==> %g seconds to %s' % (time.time() - start, message))
def make_image_key(video_id, timestamp):
"""Returns a unique identifier for a video id & timestamp."""
return f'{video_id},{int(timestamp):04d}'
def read_csv(csv_file, class_whitelist=None, capacity=0):
"""Loads boxes and class labels from a CSV file in the AVA format.
CSV file format described at https://research.google.com/ava/download.html.
Args:
csv_file: A file object.
class_whitelist: If provided, boxes corresponding to (integer) class
labels not in this set are skipped.
capacity: Maximum number of labeled boxes allowed for each example.
Default is 0 where there is no limit.
Returns:
boxes: A dictionary mapping each unique image key (string) to a list of
boxes, given as coordinates [y1, x1, y2, x2].
labels: A dictionary mapping each unique image key (string) to a list
of integer class lables, matching the corresponding box in `boxes`.
scores: A dictionary mapping each unique image key (string) to a list
of score values lables, matching the corresponding label in `labels`.
If scores are not provided in the csv, then they will default to 1.0.
"""
start = time.time()
entries = defaultdict(list)
boxes = defaultdict(list)
labels = defaultdict(list)
scores = defaultdict(list)
reader = csv.reader(csv_file)
for row in reader:
assert len(row) in [7, 8], 'Wrong number of columns: ' + row
image_key = make_image_key(row[0], row[1])
x1, y1, x2, y2 = [float(n) for n in row[2:6]]
action_id = int(row[6])
if class_whitelist and action_id not in class_whitelist:
continue
score = 1.0
if len(row) == 8:
score = float(row[7])
if capacity < 1 or len(entries[image_key]) < capacity:
heapq.heappush(entries[image_key],
(score, action_id, y1, x1, y2, x2))
elif score > entries[image_key][0][0]:
heapq.heapreplace(entries[image_key],
(score, action_id, y1, x1, y2, x2))
for image_key in entries:
# Evaluation API assumes boxes with descending scores
entry = sorted(entries[image_key], key=lambda tup: -tup[0])
for item in entry:
score, action_id, y1, x1, y2, x2 = item
boxes[image_key].append([y1, x1, y2, x2])
labels[image_key].append(action_id)
scores[image_key].append(score)
print_time('read file ' + csv_file.name, start)
return boxes, labels, scores
def read_exclusions(exclusions_file):
"""Reads a CSV file of excluded timestamps.
Args:
exclusions_file: A file object containing a csv of video-id,timestamp.
Returns:
A set of strings containing excluded image keys, e.g.
"aaaaaaaaaaa,0904",
or an empty set if exclusions file is None.
"""
excluded = set()
if exclusions_file:
reader = csv.reader(exclusions_file)
for row in reader:
assert len(row) == 2, 'Expected only 2 columns, got: ' + row
excluded.add(make_image_key(row[0], row[1]))
return excluded
def read_labelmap(labelmap_file):
"""Reads a labelmap without the dependency on protocol buffers.
Args:
labelmap_file: A file object containing a label map protocol buffer.
Returns:
labelmap: The label map in the form used by the
object_detection_evaluation
module - a list of {"id": integer, "name": classname } dicts.
class_ids: A set containing all of the valid class id integers.
"""
labelmap = []
class_ids = set()
name = ''
class_id = ''
for line in labelmap_file:
if line.startswith(' name:'):
name = line.split('"')[1]
elif line.startswith(' id:') or line.startswith(' label_id:'):
class_id = int(line.strip().split(' ')[-1])
labelmap.append({'id': class_id, 'name': name})
class_ids.add(class_id)
return labelmap, class_ids
# Seems there is at most 100 detections for each image
def ava_eval(result_file,
result_type,
label_file,
ann_file,
exclude_file,
max_dets=(100, ),
verbose=True,
custom_classes=None):
assert result_type in ['mAP']
start = time.time()
categories, class_whitelist = read_labelmap(open(label_file))
if custom_classes is not None:
custom_classes = custom_classes[1:]
assert set(custom_classes).issubset(set(class_whitelist))
class_whitelist = custom_classes
categories = [cat for cat in categories if cat['id'] in custom_classes]
# loading gt, do not need gt score
gt_boxes, gt_labels, _ = read_csv(open(ann_file), class_whitelist, 0)
if verbose:
print_time('Reading detection results', start)
if exclude_file is not None:
excluded_keys = read_exclusions(open(exclude_file))
else:
excluded_keys = list()
start = time.time()
boxes, labels, scores = read_csv(open(result_file), class_whitelist, 0)
if verbose:
print_time('Reading detection results', start)
if result_type == 'proposal':
gts = [
np.array(gt_boxes[image_key], dtype=float) for image_key in gt_boxes
]
proposals = []
for image_key in gt_boxes:
if image_key in boxes:
proposals.append(
np.concatenate(
(np.array(boxes[image_key], dtype=float),
np.array(scores[image_key], dtype=float)[:, None]),
axis=1))
else:
# if no corresponding proposal, add a fake one
proposals.append(np.array([0, 0, 1, 1, 1]))
# Proposals used here are with scores
recalls = eval_recalls(gts, proposals, np.array(max_dets),
np.arange(0.5, 0.96, 0.05))
ar = recalls.mean(axis=1)
ret = {}
for i, num in enumerate(max_dets):
print(f'[email protected]@{num}\t={recalls[i, 0]:.4f}')
print(f'AR@{num}\t={ar[i]:.4f}')
ret[f'[email protected]@{num}'] = recalls[i, 0]
ret[f'AR@{num}'] = ar[i]
return ret
if result_type == 'mAP':
pascal_evaluator = det_eval.PascalDetectionEvaluator(categories)
start = time.time()
for image_key in gt_boxes:
if verbose and image_key in excluded_keys:
logging.info(
'Found excluded timestamp in detections: %s.'
'It will be ignored.', image_key)
continue
pascal_evaluator.add_single_ground_truth_image_info(
image_key, {
standard_fields.InputDataFields.groundtruth_boxes:
np.array(gt_boxes[image_key], dtype=float),
standard_fields.InputDataFields.groundtruth_classes:
np.array(gt_labels[image_key], dtype=int),
standard_fields.InputDataFields.groundtruth_difficult:
np.zeros(len(gt_boxes[image_key]), dtype=bool)
})
if verbose:
print_time('Convert groundtruth', start)
start = time.time()
for image_key in boxes:
if verbose and image_key in excluded_keys:
logging.info(
'Found excluded timestamp in detections: %s.'
'It will be ignored.', image_key)
continue
pascal_evaluator.add_single_detected_image_info(
image_key, {
standard_fields.DetectionResultFields.detection_boxes:
np.array(boxes[image_key], dtype=float),
standard_fields.DetectionResultFields.detection_classes:
np.array(labels[image_key], dtype=int),
standard_fields.DetectionResultFields.detection_scores:
np.array(scores[image_key], dtype=float)
})
if verbose:
print_time('convert detections', start)
start = time.time()
metrics = pascal_evaluator.evaluate()
if verbose:
print_time('run_evaluator', start)
for display_name in metrics:
print(f'{display_name}=\t{metrics[display_name]}')
ret = {
display_name: metrics[display_name]
for display_name in metrics if 'ByCategory' not in display_name
}
return ret
def mkdir_or_exist(dir_name, mode=0o777):
if dir_name == '':
return
dir_name = osp.expanduser(dir_name)
os.makedirs(dir_name, mode=mode, exist_ok=True)
def dump_to_fileobj(obj, file, **kwargs):
kwargs.setdefault('protocol', 2)
pickle.dump(obj, file, **kwargs)
def dump_to_path(obj, filepath, mode='wb'):
with open(filepath, mode) as f:
dump_to_fileobj(obj, f)
def load_from_fileobj(file, **kwargs):
return pickle.load(file, **kwargs)
def load_from_path(filepath, mode='rb'):
with open(filepath, mode) as f:
return load_from_fileobj(f)
def collect_results_cpu(result_part, size):
"""Collect results in cpu mode.
It saves the results on different gpus to 'tmpdir' and collects
them by the rank 0 worker.
"""
tmpdir = osp.join('./', 'collect_results_cpu')
#1. load results of all parts from tmp dir
mkdir_or_exist(tmpdir)
rank, world_size = get_dist_info()
dump_to_path(result_part, osp.join(tmpdir, f'part_{rank}.pkl'))
dist.barrier()
if rank != 0:
return None
#2. collect all parts
while 1:
all_exist = True
for i in range(world_size):
part_file = osp.join(tmpdir, f'part_{i}.pkl')
if not Path(part_file).exists():
all_exist = False
if all_exist:
break
else:
time.sleep(60)
time.sleep(120)
#3. load results of all parts from tmp dir
part_list = []
for i in range(world_size):
part_file = osp.join(tmpdir, f'part_{i}.pkl')
part_list.append(load_from_path(part_file))
#4. sort the results
ordered_results = []
for res in zip(*part_list):
ordered_results.extend(list(res))
ordered_results = ordered_results[:
size] #the dataloader may pad some samples
#5. remove results of all parts from tmp dir, avoid dump_file fail to tmp dir when dir not exists.
for i in range(world_size):
part_file = osp.join(tmpdir, f'part_{i}.pkl')
os.remove(part_file)
return ordered_results
def ava_evaluate_results(info, dataset_len, results, custom_classes, label_file,
file_path, exclude_file):
# need to create a temp result file
time_now = datetime.now().strftime('%Y%m%d_%H%M%S')
temp_file = f'AVA_{time_now}_result.csv'
results2csv(info, dataset_len, results, temp_file)
ret = {}
eval_result = ava_eval(
temp_file,
'mAP',
label_file,
file_path, #ann_file,
exclude_file,
custom_classes=custom_classes)
ret.update(eval_result)
os.remove(temp_file)
return ret