forked from VisualComputingInstitute/triplet-reid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
executable file
·149 lines (118 loc) · 5.67 KB
/
evaluate.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
#!/usr/bin/env python3
from argparse import ArgumentParser, FileType
from importlib import import_module
from itertools import count
import os
import h5py
import json
import numpy as np
from sklearn.metrics import average_precision_score
import tensorflow as tf
import common
import loss
parser = ArgumentParser(description='Evaluate a ReID embedding.')
parser.add_argument(
'--excluder', required=True, choices=('market1501', 'diagonal'),
help='Excluder function to mask certain matches. Especially for multi-'
'camera datasets, one often excludes pictures of the query person from'
' the gallery if it is taken from the same camera. The `diagonal`'
' excluder should be used if this is *not* required.')
parser.add_argument(
'--query_dataset', required=True,
help='Path to the query dataset csv file.')
parser.add_argument(
'--query_embeddings', required=True,
help='Path to the h5 file containing the query embeddings.')
parser.add_argument(
'--gallery_dataset', required=True,
help='Path to the gallery dataset csv file.')
parser.add_argument(
'--gallery_embeddings', required=True,
help='Path to the h5 file containing the query embeddings.')
parser.add_argument(
'--metric', required=True, choices=loss.cdist.supported_metrics,
help='Which metric to use for the distance between embeddings.')
parser.add_argument(
'--filename', type=FileType('w'),
help='Optional name of the json file to store the results in.')
parser.add_argument(
'--batch_size', default=256, type=common.positive_int,
help='Batch size used during evaluation, adapt based on your memory usage.')
def main():
# Verify that parameters are set correctly.
args = parser.parse_args()
# Load the query and gallery data from the CSV files.
query_pids, query_fids = common.load_dataset(args.query_dataset, None)
gallery_pids, gallery_fids = common.load_dataset(args.gallery_dataset, None)
# Load the two datasets fully into memory.
with h5py.File(args.query_embeddings, 'r') as f_query:
query_embs = np.array(f_query['emb'])
with h5py.File(args.gallery_embeddings, 'r') as f_gallery:
gallery_embs = np.array(f_gallery['emb'])
# Just a quick sanity check that both have the same embedding dimension!
query_dim = query_embs.shape[1]
gallery_dim = gallery_embs.shape[1]
if query_dim != gallery_dim:
raise ValueError('Shape mismatch between query ({}) and gallery ({}) '
'dimension'.format(query_dim, gallery_dim))
# Setup the dataset specific matching function
excluder = import_module('excluders.' + args.excluder).Excluder(gallery_fids)
# We go through the queries in batches, but we always need the whole gallery
batch_pids, batch_fids, batch_embs = tf.data.Dataset.from_tensor_slices(
(query_pids, query_fids, query_embs)
).batch(args.batch_size).make_one_shot_iterator().get_next()
batch_distances = loss.cdist(batch_embs, gallery_embs, metric=args.metric)
# Loop over the query embeddings and compute their APs and the CMC curve.
aps = []
cmc = np.zeros(len(gallery_pids), dtype=np.int32)
with tf.Session() as sess:
for start_idx in count(step=args.batch_size):
try:
# Compute distance to all gallery embeddings
distances, pids, fids = sess.run([
batch_distances, batch_pids, batch_fids])
print('\rEvaluating batch {}-{}/{}'.format(
start_idx, start_idx + len(fids), len(query_fids)),
flush=True, end='')
except tf.errors.OutOfRangeError:
print() # Done!
break
# Convert the array of objects back to array of strings
pids, fids = np.array(pids, '|U'), np.array(fids, '|U')
# Compute the pid matches
pid_matches = gallery_pids[None] == pids[:,None]
# Get a mask indicating True for those gallery entries that should
# be ignored for whatever reason (same camera, junk, ...) and
# exclude those in a way that doesn't affect CMC and mAP.
mask = excluder(fids)
distances[mask] = np.inf
pid_matches[mask] = False
# Keep track of statistics. Invert distances to scores using any
# arbitrary inversion, as long as it's monotonic and well-behaved,
# it won't change anything.
scores = 1 / (1 + distances)
for i in range(len(distances)):
ap = average_precision_score(pid_matches[i], scores[i])
if np.isnan(ap):
print()
print("WARNING: encountered an AP of NaN!")
print("This usually means a person only appears once.")
print("In this case, it's because of {}.".format(fids[i]))
print("I'm excluding this person from eval and carrying on.")
print()
continue
aps.append(ap)
# Find the first true match and increment the cmc data from there on.
k = np.where(pid_matches[i, np.argsort(distances[i])])[0][0]
cmc[k:] += 1
# Compute the actual cmc and mAP values
cmc = cmc / len(query_pids)
mean_ap = np.mean(aps)
# Save important data
if args.filename is not None:
json.dump({'mAP': mean_ap, 'CMC': list(cmc), 'aps': list(aps)}, args.filename)
# Print out a short summary.
print('mAP: {:.2%} | top-1: {:.2%} top-2: {:.2%} | top-5: {:.2%} | top-10: {:.2%}'.format(
mean_ap, cmc[0], cmc[1], cmc[4], cmc[9]))
if __name__ == '__main__':
main()