提交 914e6967 编写于 作者: D dongshuilong

fix reid recall metric bugs

上级 f40fd3e6
...@@ -442,10 +442,12 @@ class Trainer(object): ...@@ -442,10 +442,12 @@ class Trainer(object):
keep_mask = paddle.logical_or(query_id_mask, image_id_mask) keep_mask = paddle.logical_or(query_id_mask, image_id_mask)
similarity_matrix = similarity_matrix * keep_mask.astype( similarity_matrix = similarity_matrix * keep_mask.astype(
"float32") "float32")
else:
keep_mask = None
metric_tmp = self.eval_metric_func(similarity_matrix, metric_tmp = self.eval_metric_func(similarity_matrix,
image_id_blocks[block_idx], image_id_blocks[block_idx],
gallery_img_id) gallery_img_id, keep_mask)
for key in metric_tmp: for key in metric_tmp:
if key not in metric_dict: if key not in metric_dict:
......
...@@ -16,6 +16,7 @@ import numpy as np ...@@ -16,6 +16,7 @@ import numpy as np
import paddle import paddle
import paddle.nn as nn import paddle.nn as nn
class TopkAcc(nn.Layer): class TopkAcc(nn.Layer):
def __init__(self, topk=(1, 5)): def __init__(self, topk=(1, 5)):
super().__init__() super().__init__()
...@@ -34,54 +35,72 @@ class TopkAcc(nn.Layer): ...@@ -34,54 +35,72 @@ class TopkAcc(nn.Layer):
x, label, k=k) x, label, k=k)
return metric_dict return metric_dict
class mAP(nn.Layer): class mAP(nn.Layer):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
def forward(self, similarities_matrix, query_img_id, gallery_img_id): def forward(self, similarities_matrix, query_img_id, gallery_img_id,
*args):
metric_dict = dict() metric_dict = dict()
choosen_indices = paddle.argsort(similarities_matrix, axis=1, descending=True) choosen_indices = paddle.argsort(
gallery_labels_transpose = paddle.transpose(gallery_img_id, [1,0]) similarities_matrix, axis=1, descending=True)
gallery_labels_transpose = paddle.broadcast_to(gallery_labels_transpose, shape=[choosen_indices.shape[0], gallery_labels_transpose.shape[1]]) gallery_labels_transpose = paddle.transpose(gallery_img_id, [1, 0])
choosen_label = paddle.index_sample(gallery_labels_transpose, choosen_indices) gallery_labels_transpose = paddle.broadcast_to(
gallery_labels_transpose,
shape=[
choosen_indices.shape[0], gallery_labels_transpose.shape[1]
])
choosen_label = paddle.index_sample(gallery_labels_transpose,
choosen_indices)
equal_flag = paddle.equal(choosen_label, query_img_id) equal_flag = paddle.equal(choosen_label, query_img_id)
equal_flag = paddle.cast(equal_flag, 'float32') equal_flag = paddle.cast(equal_flag, 'float32')
acc_sum = paddle.cumsum(equal_flag, axis=1) acc_sum = paddle.cumsum(equal_flag, axis=1)
div = paddle.arange(acc_sum.shape[1]).astype("float32") + 1 div = paddle.arange(acc_sum.shape[1]).astype("float32") + 1
precision = paddle.divide(acc_sum, div) precision = paddle.divide(acc_sum, div)
#calc map #calc map
precision_mask = paddle.multiply(equal_flag, precision) precision_mask = paddle.multiply(equal_flag, precision)
ap = paddle.sum(precision_mask, axis=1) / paddle.sum(equal_flag, axis=1) ap = paddle.sum(precision_mask, axis=1) / paddle.sum(equal_flag,
axis=1)
metric_dict["mAP"] = paddle.mean(ap).numpy()[0] metric_dict["mAP"] = paddle.mean(ap).numpy()[0]
return metric_dict return metric_dict
class mINP(nn.Layer): class mINP(nn.Layer):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
def forward(self, similarities_matrix, query_img_id, gallery_img_id): def forward(self, similarities_matrix, query_img_id, gallery_img_id,
*args):
metric_dict = dict() metric_dict = dict()
choosen_indices = paddle.argsort(similarities_matrix, axis=1, descending=True) choosen_indices = paddle.argsort(
gallery_labels_transpose = paddle.transpose(gallery_img_id, [1,0]) similarities_matrix, axis=1, descending=True)
gallery_labels_transpose = paddle.broadcast_to(gallery_labels_transpose, shape=[choosen_indices.shape[0], gallery_labels_transpose.shape[1]]) gallery_labels_transpose = paddle.transpose(gallery_img_id, [1, 0])
choosen_label = paddle.index_sample(gallery_labels_transpose, choosen_indices) gallery_labels_transpose = paddle.broadcast_to(
gallery_labels_transpose,
shape=[
choosen_indices.shape[0], gallery_labels_transpose.shape[1]
])
choosen_label = paddle.index_sample(gallery_labels_transpose,
choosen_indices)
tmp = paddle.equal(choosen_label, query_img_id) tmp = paddle.equal(choosen_label, query_img_id)
tmp = paddle.cast(tmp, 'float64') tmp = paddle.cast(tmp, 'float64')
#do accumulative sum #do accumulative sum
div = paddle.arange(tmp.shape[1]).astype("float64") + 2 div = paddle.arange(tmp.shape[1]).astype("float64") + 2
minus = paddle.divide(tmp, div) minus = paddle.divide(tmp, div)
auxilary = paddle.subtract(tmp, minus) auxilary = paddle.subtract(tmp, minus)
hard_index = paddle.argmax(auxilary, axis=1).astype("float64") hard_index = paddle.argmax(auxilary, axis=1).astype("float64")
all_INP = paddle.divide(paddle.sum(tmp, axis=1), hard_index) all_INP = paddle.divide(paddle.sum(tmp, axis=1), hard_index)
mINP = paddle.mean(all_INP) mINP = paddle.mean(all_INP)
metric_dict["mINP"] = mINP.numpy()[0] metric_dict["mINP"] = mINP.numpy()[0]
return metric_dict return metric_dict
class Recallk(nn.Layer): class Recallk(nn.Layer):
def __init__(self, topk=(1, 5)): def __init__(self, topk=(1, 5)):
super().__init__() super().__init__()
...@@ -90,25 +109,43 @@ class Recallk(nn.Layer): ...@@ -90,25 +109,43 @@ class Recallk(nn.Layer):
topk = [topk] topk = [topk]
self.topk = topk self.topk = topk
def forward(self, similarities_matrix, query_img_id, gallery_img_id): def forward(self, similarities_matrix, query_img_id, gallery_img_id,
keep_mask):
metric_dict = dict() metric_dict = dict()
#get cmc #get cmc
choosen_indices = paddle.argsort(similarities_matrix, axis=1, descending=True) choosen_indices = paddle.argsort(
gallery_labels_transpose = paddle.transpose(gallery_img_id, [1,0]) similarities_matrix, axis=1, descending=True)
gallery_labels_transpose = paddle.broadcast_to(gallery_labels_transpose, shape=[choosen_indices.shape[0], gallery_labels_transpose.shape[1]]) gallery_labels_transpose = paddle.transpose(gallery_img_id, [1, 0])
choosen_label = paddle.index_sample(gallery_labels_transpose, choosen_indices) gallery_labels_transpose = paddle.broadcast_to(
gallery_labels_transpose,
shape=[
choosen_indices.shape[0], gallery_labels_transpose.shape[1]
])
choosen_label = paddle.index_sample(gallery_labels_transpose,
choosen_indices)
equal_flag = paddle.equal(choosen_label, query_img_id) equal_flag = paddle.equal(choosen_label, query_img_id)
if keep_mask is not None:
keep_mask = paddle.index_sample(
keep_mask.astype('float32'), choosen_indices)
equal_flag = paddle.logical_and(equal_flag,
keep_mask.astype('bool'))
equal_flag = paddle.cast(equal_flag, 'float32') equal_flag = paddle.cast(equal_flag, 'float32')
real_query_num = paddle.sum(equal_flag, axis=1)
real_query_num = paddle.sum(
paddle.greater_than(real_query_num, paddle.to_tensor(0.)).astype(
"float32"))
acc_sum = paddle.cumsum(equal_flag, axis=1) acc_sum = paddle.cumsum(equal_flag, axis=1)
mask = paddle.greater_than(acc_sum, paddle.to_tensor(0.)).astype("float32") mask = paddle.greater_than(acc_sum,
all_cmc = paddle.mean(mask, axis=0).numpy() paddle.to_tensor(0.)).astype("float32")
all_cmc = (paddle.sum(mask, axis=0) / real_query_num).numpy()
for k in self.topk: for k in self.topk:
metric_dict["recall{}".format(k)] = all_cmc[k - 1] metric_dict["recall{}".format(k)] = all_cmc[k - 1]
return metric_dict return metric_dict
class DistillationTopkAcc(TopkAcc): class DistillationTopkAcc(TopkAcc):
def __init__(self, model_key, feature_key=None, topk=(1, 5)): def __init__(self, model_key, feature_key=None, topk=(1, 5)):
super().__init__(topk=topk) super().__init__(topk=topk)
...@@ -132,4 +169,3 @@ class GoogLeNetTopkAcc(TopkAcc): ...@@ -132,4 +169,3 @@ class GoogLeNetTopkAcc(TopkAcc):
def forward(self, x, label): def forward(self, x, label):
return super().forward(x[0], label) return super().forward(x[0], label)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册