当前位置:   article > 正文

f1 score 代码_F1-Score的栗子-求取办法-以及参考代码

字符级f1评分代码

F1-Score 求取办法

来做一道小学题:

小明和小红在同一所学校上课,他们家li学校的距离相同。小明每天用速度6千米每小时的步行速度上学。小红每天用4千米每小时的速度上学。

问:从家到学校这段长度相等路程上,小明和小红的平均速度是多少?

错误的解法:

(6+4)/2=5 千米每小时

错误原因:

路程相等,但是因为小明和小红用的时间不一样,所以不能直接求平均值。速度,应该是算出总路程,然后算法总时间,用总路程/总时间。

正确的求法:

先求出小明走1千米的耗时:1/6 小时,再求出小红走1千米的耗时:1/4小时。

那么他们走2千米的耗时为:(1/6+1/4)小时

所以他们的平均速度为:2千米/(1/6+1/4)小时= 2/(10/24)=48/10=4.8千米每小时。

我们把公式抽象出来:

速度是描述人行走的速度,是一种量度。

凡是要在量度上,求平均值,都可以用这种方式来求取。

准确率P是一种量度,评价模型所给出的所有结果的正确程度。

召回率R是一种量度,评价模型发现正确目标的能力。

准确率和召回率都是一种量度,他们求取平均值,也可以用这种方法

我们通常叫它F1-Score:

检测问题的F1-Score 代码:

其实,检测问题主要是用IoU来求出 R和P,F1-score就是套用公式即可。

#用IoU计算gt与pre的匹配性

if len(gtPols)>0 and len(detPols)>0:

#Calculate IoU and precision matrixs

outputShape=[len(gtPols),len(detPols)]

iouMat = np.empty(outputShape)

# 匹配标记,0未匹配,1已匹配

gtRectMat = np.zeros(len(gtPols),np.int8)

detRectMat = np.zeros(len(detPols),np.int8)

# 二重循环计算IoU矩阵

for gtNum in range(len(gtPols)):

for detNum in range(len(detPols)):

pG = gtPols[gtNum]

pD = detPols[detNum]

iouMat[gtNum,detNum] = get_intersection_over_union(pD,pG)

for gtNum in range(len(gtPols)):

for detNum in range(len(detPols)):

# 若标签和预测框均为匹配,且均不忽略,则判断IoU

if gtRectMat[gtNum] == 0 and detRectMat[detNum] == 0:

# 若IoU大于某个阈值,则匹配成功

if iouMat[gtNum,detNum]>IOU_CONSTRAINT:

# 更新匹配标记

gtRectMat[gtNum] = 1

detRectMat[detNum] = 1

# 匹配数量+1

detMatched += 1

# 增加匹配对

pairs.append({'gt':gtNum,'det':detNum})

# 记录成功匹配的预测框index

detMatchedNums.append(detNum)

# 计算有效框的数量)

numGtCare = len(gtPols)

numDetCare = len(detPols)

# 将该图片的计数记录到全局总数中

matchedSum += detMatched

numGlobalCareGt += numGtCare

numGlobalCareDet += numDetCare

# 计算全部图片的结果

# 计算全部图片的召回率

methodRecall = 0 if numGlobalCareGt == 0 else float(matchedSum)/numGlobalCareGt

# 计算全部图片的准确率

methodPrecision = 0 if numGlobalCareDet == 0 else float(matchedSum)/numGlobalCareDet

# 计算全部图片的hmean(即F-Score)

methodHmean = 0 if methodRecall + methodPrecision==0 else 2* methodRecall * methodPrecision / (methodRecall + methodPrecision)

methodMetrics = {'precision':methodPrecision, 'recall':methodRecall,'hmean': methodHmean}

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号