106def eval_market1501(distmat, q_feats, g_feats, q_pids, g_pids, q_camids, g_camids, max_rank, use_distmat):
107 """Evaluation with market1501 metric
108 Key: for each query identity, its gallery images from the same camera view are discarded.
109 """
110 num_q, num_g = distmat.shape
111 dim = q_feats.shape[1]
112
113
114 q_feats = q_feats.detach().numpy()
115 g_feats = g_feats.detach().numpy()
116
117
118 index = faiss.IndexFlatL2(dim)
119 index.add(g_feats)
120
121 print(f'\nnum_q = {num_q}')
122 print(f'num_g = {num_g}')
123 print(f'dim = {dim}')
124 print(f'index = {index}')
125 print(f'max_rank = {max_rank}')
126 print(f'q_camids = {q_camids}')
127 print(f'g_camids = {g_camids}')
128
129
130 if num_g < max_rank:
131 max_rank = num_g
132 print('Note: number of gallery samples is quite small, got {}'.format(num_g))
133
134 if use_distmat:
135 indices = np.argsort(distmat, axis=1)
136 else:
137 _, indices = index.search(q_feats, k=num_g)
138
139 matches = (g_pids[indices] == q_pids[:, np.newaxis]).astype(np.int32)
140
141 print(f'matches = {matches}')
142
143
144 all_cmc = []
145 all_AP = []
146 all_INP = []
147 num_valid_q = 0.
148
149 for q_idx in range(num_q):
150
151 q_pid = q_pids[q_idx]
152 q_camid = q_camids[q_idx]
153
154
155 order = indices[q_idx]
156 remove = (g_pids[order] == q_pid) & (g_camids[order] == q_camid)
157 keep = np.invert(remove)
158 print(f'order = {order}')
159 print(f'remove = {remove}')
160 print(f'keep = {keep}')
161 print(f'matches = {matches}')
162 print(f'matches.shape = {matches.shape}')
163
164
165 np.place(keep, keep == False, True)
166
167
168
169 raw_cmc = matches[q_idx][keep]
170 print(f'raw_cmc = {raw_cmc}')
171 if not np.any(raw_cmc):
172
173 continue
174
175 cmc = raw_cmc.cumsum()
176
177 pos_idx = np.where(raw_cmc == 1)
178 max_pos_idx = np.max(pos_idx)
179 inp = cmc[max_pos_idx] / (max_pos_idx + 1.0)
180 all_INP.append(inp)
181
182 cmc[cmc > 1] = 1
183
184 all_cmc.append(cmc[:max_rank])
185 num_valid_q += 1.
186
187
188
189 num_rel = raw_cmc.sum()
190 tmp_cmc = raw_cmc.cumsum()
191 tmp_cmc = [x / (i + 1.) for i, x in enumerate(tmp_cmc)]
192 tmp_cmc = np.asarray(tmp_cmc) * raw_cmc
193 AP = tmp_cmc.sum() / num_rel
194 all_AP.append(AP)
195
196 print(f'num_valid_q = {num_valid_q}')
197 print(f'all_INP = {all_INP}')
198 print(f'all_AP = {all_AP}')
199
200 assert num_valid_q > 0, 'Error: all query identities do not appear in gallery'
201
202 all_cmc = np.asarray(all_cmc).astype(np.float32)
203 all_cmc = all_cmc.sum(0) / num_valid_q
204
205 print(f'all_cmc = {all_cmc}')
206
207 return all_cmc, all_AP, all_INP
208
209