提交 7badd5bf 编写于 作者: weixin_46524038's avatar weixin_46524038 提交者: cuicheng01

add metrics and postprocess

上级 a9cb4179
...@@ -346,6 +346,58 @@ class PersonAttribute(object): ...@@ -346,6 +346,58 @@ class PersonAttribute(object):
return batch_res return batch_res
class FaceAttribute(object):
def __init__(self, threshold=0.65, convert_cn=False):
self.threshold = threshold
self.convert_cn = convert_cn
def __call__(self, x, file_names=None):
attribute_list = [
["CheekWhiskers", "刚长出的双颊胡须"], ["ArchedEyebrows", "柳叶眉"],
["Attractive", "吸引人的"], ["BagsUnderEyes", "眼袋"], ["Bald", "秃头"],
["Bangs", "刘海"], ["BigLips", "大嘴唇"], ["BigNose", "大鼻子"],
["BlackHair", "黑发"], ["BlondHair", "金发"], ["Blurry", "模糊的"],
["BrownHair", "棕发"], ["BushyEyebrows", "浓眉"], ["Chubby", "圆胖的"],
["DoubleChin", "双下巴"], ["Eyeglasses", "带眼镜"], ["Goatee", "山羊胡子"],
["GrayHair", "灰发或白发"], ["HeavyMakeup", "浓妆"],
["HighCheekbones", "高颧骨"], ["Male", "男性"],
["MouthSlightlyOpen", "微微张开嘴巴"], ["Mustache", "胡子"],
["NarrowEyes", "细长的眼睛"], ["NoBeard", "无胡子"],
["OvalFace", "椭圆形的脸"], ["PaleSkin", "苍白的皮肤"],
["PointyNose", "尖鼻子"], ["RecedingHairline", "发际线后移"],
["RosyCheeks", "红润的双颊"], ["Sideburns", "连鬓胡子"], ["Smiling", "微笑"],
["StraightHair", "直发"], ["WavyHair", "卷发"],
["WearingEarrings", "戴着耳环"], ["WearingHat", "戴着帽子"],
["WearingLipstick", "涂了唇膏"], ["WearingNecklace", "戴着项链"],
["WearingNecktie", "戴着领带"], ["Young", "年轻人"]
]
gender_list = [["Male", "男性"], ["Female", "女性"]]
age_list = [["Young", "年轻人"], ["Old", "老年人"]]
batch_res = []
if self.convert_cn:
index = 1
else:
index = 0
for idx, res in enumerate(x):
res = res.tolist()
label_res = []
threshold_list = [self.threshold] * len(res)
pred_res = (np.array(res) > np.array(threshold_list)
).astype(np.int8).tolist()
for i, value in enumerate(pred_res):
if i == 20:
label_res.append(gender_list[0][index]
if value == 1 else gender_list[1][index])
elif i == 39:
label_res.append(age_list[0][index]
if value == 1 else age_list[1][index])
else:
if value == 1:
label_res.append(attribute_list[i][index])
batch_res.append({"attributes": label_res, "output": pred_res})
return batch_res
class VehicleAttribute(object): class VehicleAttribute(object):
def __init__(self, color_threshold=0.5, type_threshold=0.5): def __init__(self, color_threshold=0.5, type_threshold=0.5):
self.color_threshold = color_threshold self.color_threshold = color_threshold
......
...@@ -136,7 +136,8 @@ def main(config): ...@@ -136,7 +136,8 @@ def main(config):
if "PersonAttribute" in config[ if "PersonAttribute" in config[
"PostProcess"] or "VehicleAttribute" in config[ "PostProcess"] or "VehicleAttribute" in config[
"PostProcess"] or "TableAttribute" in config[ "PostProcess"] or "TableAttribute" in config[
"PostProcess"]: "PostProcess"] or "FaceAttribute" in config[
"PostProcess"]:
filename = batch_names[number] filename = batch_names[number]
print("{}:\t {}".format(filename, result_dict)) print("{}:\t {}".format(filename, result_dict))
else: else:
......
...@@ -84,6 +84,7 @@ class PersonAttribute(object): ...@@ -84,6 +84,7 @@ class PersonAttribute(object):
if isinstance(x, dict): if isinstance(x, dict):
x = x['logits'] x = x['logits']
assert isinstance(x, paddle.Tensor) assert isinstance(x, paddle.Tensor)
if file_names is not None: if file_names is not None:
assert x.shape[0] == len(file_names) assert x.shape[0] == len(file_names)
x = F.sigmoid(x).numpy() x = F.sigmoid(x).numpy()
...@@ -98,6 +99,7 @@ class PersonAttribute(object): ...@@ -98,6 +99,7 @@ class PersonAttribute(object):
'Skirt&Dress' 'Skirt&Dress'
] ]
batch_res = [] batch_res = []
for idx, res in enumerate(x): for idx, res in enumerate(x):
res = res.tolist() res = res.tolist()
label_res = [] label_res = []
...@@ -171,6 +173,66 @@ class PersonAttribute(object): ...@@ -171,6 +173,66 @@ class PersonAttribute(object):
return batch_res return batch_res
class FaceAttribute(object):
def __init__(self, threshold=0.65, convert_cn=False):
self.threshold = threshold
self.convert_cn = convert_cn
def __call__(self, x, file_names=None):
if isinstance(x, dict):
x = x['logits']
assert isinstance(x, paddle.Tensor)
if file_names is not None:
assert x.shape[0] == len(file_names)
x = F.sigmoid(x).numpy()
attribute_list = [
["CheekWhiskers", "刚长出的双颊胡须"], ["ArchedEyebrows", "柳叶眉"],
["Attractive", "吸引人的"], ["BagsUnderEyes", "眼袋"], ["Bald", "秃头"],
["Bangs", "刘海"], ["BigLips", "大嘴唇"], ["BigNose", "大鼻子"],
["BlackHair", "黑发"], ["BlondHair", "金发"], ["Blurry", "模糊的"],
["BrownHair", "棕发"], ["BushyEyebrows", "浓眉"], ["Chubby", "圆胖的"],
["DoubleChin", "双下巴"], ["Eyeglasses", "带眼镜"], ["Goatee", "山羊胡子"],
["GrayHair", "灰发或白发"], ["HeavyMakeup", "浓妆"],
["HighCheekbones", "高颧骨"], ["Male", "男性"],
["MouthSlightlyOpen", "微微张开嘴巴"], ["Mustache", "胡子"],
["NarrowEyes", "细长的眼睛"], ["NoBeard", "无胡子"],
["OvalFace", "椭圆形的脸"], ["PaleSkin", "苍白的皮肤"],
["PointyNose", "尖鼻子"], ["RecedingHairline", "发际线后移"],
["RosyCheeks", "红润的双颊"], ["Sideburns", "连鬓胡子"], ["Smiling", "微笑"],
["StraightHair", "直发"], ["WavyHair", "卷发"],
["WearingEarrings", "戴着耳环"], ["WearingHat", "戴着帽子"],
["WearingLipstick", "涂了唇膏"], ["WearingNecklace", "戴着项链"],
["WearingNecktie", "戴着领带"], ["Young", "年轻人"]
]
gender_list = [["Male", "男性"], ["Female", "女性"]]
age_list = [["Young", "年轻人"], ["Old", "老年人"]]
batch_res = []
if self.convert_cn:
index = 1
else:
index = 0
for idx, res in enumerate(x):
res = res.tolist()
label_res = []
threshold_list = [self.threshold] * len(res)
pred_res = (np.array(res) > np.array(threshold_list)
).astype(np.int8).tolist()
for i, value in enumerate(pred_res):
if i == 20:
label_res.append(gender_list[0][index]
if value == 1 else gender_list[1][index])
elif i == 39:
label_res.append(age_list[0][index]
if value == 1 else age_list[1][index])
else:
if value == 1:
label_res.append(attribute_list[i][index])
batch_res.append({"attributes": label_res, "output": pred_res})
return batch_res
class TableAttribute(object): class TableAttribute(object):
def __init__( def __init__(
self, self,
......
...@@ -21,7 +21,7 @@ from .metrics import DistillationTopkAcc ...@@ -21,7 +21,7 @@ from .metrics import DistillationTopkAcc
from .metrics import GoogLeNetTopkAcc from .metrics import GoogLeNetTopkAcc
from .metrics import HammingDistance, AccuracyScore from .metrics import HammingDistance, AccuracyScore
from .metrics import ATTRMetric from .metrics import ATTRMetric
from .metrics import TprAtFpr from .metrics import TprAtFpr, MultilabelMeanAccuracy
class CombinedMetrics(AvgMetrics): class CombinedMetrics(AvgMetrics):
......
...@@ -218,6 +218,55 @@ class TprAtFpr(nn.Layer): ...@@ -218,6 +218,55 @@ class TprAtFpr(nn.Layer):
return result return result
class MultilabelMeanAccuracy(nn.Layer):
def __init__(self, class_num=40):
super().__init__()
self.gt_all_score_list = []
self.gt_label_score_list = []
self.max_acc = 0.
self.class_num = class_num
def forward(self, x, label):
if isinstance(x, dict):
x = x["logits"]
x = F.sigmoid(x)
label = label[:, 0, :]
for i in range(len(x)):
self.gt_all_score_list.append(x[i].numpy())
self.gt_label_score_list.append(label[i].numpy())
return {}
def reset(self):
self.gt_all_score_list = []
self.gt_label_score_list = []
self.max_acc = 0.
@property
def avg(self):
return self.max_acc
@property
def avg_info(self):
max_acc = 0.
result = ""
gt_all_score_list = np.array(self.gt_all_score_list)
gt_label_score_list = np.array(self.gt_label_score_list)
for i in range(10):
threshold = 0.4 + i * 0.05
pred_label = (gt_all_score_list > threshold).astype(int)
TP = np.sum(
(gt_label_score_list == 1) * (pred_label == 1)).astype(float)
TN = np.sum(
(gt_label_score_list == 0) * (pred_label == 0)).astype(float)
acc = (TP + TN) / len(gt_all_score_list)
if max_acc <= acc:
max_acc = acc
result = "threshold: {}, mean_acc: {}".format(
threshold, max_acc / self.class_num)
self.max_acc = max_acc / self.class_num
return result
class Recallk(nn.Layer): class Recallk(nn.Layer):
def __init__(self, topk=(1, 5), descending=True): def __init__(self, topk=(1, 5), descending=True):
super().__init__() super().__init__()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册