本文是优化小技巧-softmax_哔哩哔哩_bilibili的学习笔记!本文主要是针对2个类别的softmax,用来加速推理。
本文softmax 主要是针对2个类别,场景:Blazeface、Retinaface 做检测时候,2个类别来定义正负样本。
比如:
pred = [neg, pos] # 网络预测两个值
条件 = p > threshold
所以:
条件 = sigmoid(pos - neg) > threshold
源代码:
n, p = softmax(neg, pos)
if p > threhold:
do something
新代码:
if pos - neg > desigmoid_threshold:
do something
验证:
def softmax(neg, pos):
n, p = math.exp(neg), math.exp(pos)
return n / (n + p), p / (n + p)
neg = -0.8
pos = 1.7
n, p = softmax(neg, pos)
p # 0.92414...
sigmoid(pos - neg) # # 0.92414...
C++ 中应用:
static float dethreshold = desigmoid(0.5f);
for (int i = 0; i < length; ++i){
face = scores_buffer[i * 2]; # pos
back = scores_buffer[i * 2 + 1]; # neg
float object_deconfidence = (face - back);
if(object_deconfidence > dethreshold){
// do something
}
}
输出概率值时候,直接 sigmoid(pos - neg) 即可!