51 def save_rank_result(self, query_indices, output, max_rank=5, vis_label=False, label_sort='ascending',
54 fig, axes = plt.subplots(2, max_rank + 1, figsize=(3 * max_rank, 12))
56 fig, axes = plt.subplots(1, max_rank + 1, figsize=(3 * max_rank, 6))
57 for cnt, q_idx
in enumerate(tqdm.tqdm(query_indices)):
60 query_info = self.
dataset[q_idx]
61 query_img = query_info[
'images']
62 cam_id = query_info[
'camids']
66 if 'img_paths' in query_info:
67 query_name = query_info[
'img_paths'].split(
'/')[-1]
69 query_name = f
'{cnt}.jpg'
71 all_imgs.append(query_img)
72 query_img = np.rollaxis(np.asarray(query_img.numpy(), dtype=np.uint8), 0, 3)
74 ax = fig.add_subplot(1, max_rank + 1, 1)
76 ax.set_title(
'{}/{:.2f}/cam{}'.format(query_name, self.
all_ap[q_idx], cam_id))
78 for i
in range(max_rank):
80 ax = fig.add_subplot(2, max_rank + 1, i + 2)
82 ax = fig.add_subplot(1, max_rank + 1, i + 2)
84 gallery_info = self.
dataset[g_idx]
85 gallery_img = gallery_info[
'images']
86 cam_id = gallery_info[
'camids']
87 all_imgs.append(gallery_img)
88 gallery_img = np.rollaxis(np.asarray(gallery_img, dtype=np.uint8), 0, 3)
91 ax.add_patch(plt.Rectangle(xy=(0, 0), width=gallery_img.shape[1] - 1,
92 height=gallery_img.shape[0] - 1, edgecolor=(1, 0, 0),
93 fill=
False, linewidth=5))
96 ax.add_patch(plt.Rectangle(xy=(0, 0), width=gallery_img.shape[1] - 1,
97 height=gallery_img.shape[0] - 1,
98 edgecolor=(0, 0, 1), fill=
False, linewidth=5))
99 ax.imshow(gallery_img)
100 ax.set_title(f
'{self.sim[q_idx, sort_idx[i]]:.3f}/{label}/cam{cam_id}')
121 label_indice = np.where(cmc == 1)[0]
122 if label_sort ==
"ascending": label_indice = label_indice[::-1]
123 label_indice = label_indice[:max_rank]
124 for i
in range(max_rank):
125 if i >= len(label_indice):
break
128 gallery_info = self.
dataset[g_idx]
129 gallery_img = gallery_info[
'images']
130 cam_id = gallery_info[
'camids']
131 gallery_img = np.rollaxis(np.asarray(gallery_img, dtype=np.uint8), 0, 3)
132 ax = fig.add_subplot(2, max_rank + 1, max_rank + 3 + i)
133 ax.add_patch(plt.Rectangle(xy=(0, 0), width=gallery_img.shape[1] - 1,
134 height=gallery_img.shape[0] - 1,
136 fill=
False, linewidth=5))
137 ax.imshow(gallery_img)
138 ax.set_title(f
'{self.sim[q_idx, sort_idx[j]]:.3f}/cam{cam_id}')
142 filepath = os.path.join(output,
"{}.jpg".format(cnt))
143 fig.savefig(filepath)
147 def vis_rank_list(self, output, vis_label, num_vis=100, rank_sort="ascending", label_sort="ascending", max_rank=5,
149 r"""Visualize rank list of query instance
151 output (str): a directory to save rank list result.
152 vis_label (bool): if visualize label of query
154 rank_sort (str): save visualization results by which order,
155 if rank_sort is ascending, AP from low to high, vice versa.
157 max_rank (int): maximum number of rank result to visualize
160 assert rank_sort
in [
'ascending',
'descending'],
"{} not match [ascending, descending]".format(rank_sort)
162 query_indices = np.argsort(self.
all_ap)
163 if rank_sort ==
'descending': query_indices = query_indices[::-1]
165 query_indices = query_indices[:num_vis]
166 self.
save_rank_result(query_indices, output, max_rank, vis_label, label_sort, actmap)
169 PathManager.mkdirs(output)
171 for i, q
in enumerate(self.
q_pids):
173 ind_pos = np.where(cmc == 1)[0]
174 q_dist = self.
dist[i]
175 pos.extend(q_dist[sort_idx[ind_pos]])
177 ind_neg = np.where(cmc == 0)[0]
178 neg.extend(q_dist[sort_idx[ind_neg]])
180 scores = np.hstack((pos, neg))
181 labels = np.hstack((np.zeros(len(pos)), np.ones(len(neg))))
183 fpr, tpr, thresholds = metrics.roc_curve(labels, scores)
186 filepath = os.path.join(output,
"roc.jpg")
187 plt.savefig(filepath)
189 filepath = os.path.join(output,
"pos_neg_dist.jpg")
190 plt.savefig(filepath)
191 return fpr, tpr, pos, neg
197 plt.semilogx(np.arange(0, 1, 0.01), np.arange(0, 1, 0.01),
'r', linestyle=
'--', label=
'Random guess')
198 plt.semilogx(fpr, tpr, color=(random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1)),
199 label=
'ROC curve with {}'.format(name))
200 plt.title(
'Receiver Operating Characteristic')
201 plt.xlabel(
'False Positive Rate')
202 plt.ylabel(
'True Positive Rate')
203 plt.legend(loc=
'best')
210 pos_color = (random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1))
211 n, bins, _ = plt.hist(pos, bins=80, alpha=0.7, density=
True,
213 label=
'positive with {}'.format(name))
216 y = norm.pdf(bins, mu, sigma)
217 plt.plot(bins, y, color=pos_color)
219 neg_color = (random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1))
220 n, bins, _ = plt.hist(neg, bins=80, alpha=0.5, density=
True,
222 label=
'negative with {}'.format(name))
225 y = norm.pdf(bins, mu, sigma)
226 plt.plot(bins, y, color=neg_color)
228 plt.xticks(np.arange(0, 1.5, 0.1))
229 plt.title(
'positive and negative pairs distribution')
230 plt.legend(loc=
'best')