大家好,我是灿视。
今天给大家带来两道纯工程的题,是一位博士在面试face++时,被问到的。
看文章之前,别忘了关注我们,在我们这里,有你所需要的干货哦!
1. 目标检测中的IOU
假设,我们有两个框, r e c 1 rec1 rec1与 r e c 2 rec2 rec2,我们要计算其 I O U IOU IOU。其中 I O U IOU IOU的计算公式为,其交叉面积 I n t e r s e c t i o n Intersection Intersection除以其并集 U n i o n Union Union。
I
O
U
IOU
IOU的数学公式为:
I
o
U
=
S
r
e
c
1
∩
S
r
e
c
2
S
r
e
c
1
+
S
r
e
c
2
−
S
r
e
c
1
⋂
S
r
e
c
2
I o U=\frac{S_{rec1} \cap S_{rec2}}{S_{rec1} + S_{rec2} - S_{rec1} \bigcap S_{rec2}}
IoU=Srec1+Srec2−Srec1⋂Srec2Srec1∩Srec2
上代码:
def compute_iou(rec1, rec2):
"""
computing IoU
param rec1: (y0, x0, y1, x1) , which reflects (top, left, bottom, right)
param rec2: (y0, x0, y1, x1) , which reflects (top, left, bottom, right)
return : scale value of IoU
"""
S_rec1 =(rec1[2] -rec1[0]) *(rec1[3] -rec1[1])
S_rec2 =(rec2[2] -rec2[0]) *(rec2[3] -rec2[1])
#computing the sum area
sum_area =S_rec1 +S_rec2
#find the each edge of interest rectangle
left_line =max(rec1[1], rec2[1])
right_line =min(rec1[3], rec2[3])
top_line =max(rec1[0], rec2[0])
bottom_line =min(rec1[2], rec2[2])
#judge if there is an intersect
if left_line >=right_line or top_line >=bottom_line:
return 0
else:
intersect =(right_line -left_line) +(bottom_line -top_line)
return intersect /(sum_area -intersect)
这里我们主要讨论下这个 i f if if判断,我们以横轴 x x x方向为例,其中对 y y y纵轴方向是一样的,我们来判断两个框重合与否。其中 x 0 x_{0} x0为 r e c 1 rec1 rec1左上角的 x x x坐标, x 1 x_{1} x1是 r e c 1 rec1 rec1右下角的 x x x坐标。 A 0 A_{0} A0为 r e c 2 rec2 rec2的左上角 x x x坐标, A 1 A_{1} A1是 r e c 2 rec2 rec2的右下角 x x x坐标。
2. 语义分割中的IOU
先回顾下一些基础知识:
常常将预测出来的结果分为四个部分:true positive , f a l s e false false p o s i t i v e positive positive, t r u e true true n e g a t i v e negative negative, f a l s e false false n e g a t i v e negative negative,其中 n e g a t i v e negative negative就是指非物体标签的部分(可以直接理解为背景),positive$就是指有标签的部分。下图显示了四个部分的区别:
p r e d i c t i o n prediction prediction图被分成四个部分,其中大块的白色斜线标记的是 t r u e true true n e g a t i v e negative negative(TN,预测中真实的背景部分),红色线部分标记是 f a l s e false false n e g a t i v e negative negative( F N FN FN,预测中被预测为背景,但实际上并不是背景的部分),蓝色的斜线是 f a l s e false false p o s i t i v e positive positive( F P FP FP,预测中分割为某标签的部分,但是实际上并不是该标签所属的部分),中间荧光黄色块就是 t r u e true true p o s i t i v e positive positive( T P TP TP,预测的某标签部分,符合真值)。
同样的,
I
O
U
IOU
IOU计算公式:
I
O
U
=
target
⋀
prediction
t
a
r
g
e
t
⋃
p
r
e
d
i
c
t
i
o
n
IOU = \frac{\text { target } \bigwedge \text { prediction }}{target \bigcup prediction}
IOU=target⋃prediction target ⋀ prediction
def compute_ious(pred, label, classes):
'''computes iou for one ground truth mask and predicted mask'''
ious = [] # 记录每一类的iou
for c in classes:
label_c = (label == c) # label_c为true/false矩阵
pred_c = (pred == c)
intersection = np.logical_and(pred_c, label_c).sum()
union = np.logical_or(pred_c, label_c).sum()
if union == 0:
ious.append(float('nan'))
else
ious.append(intersection / union)
return np.nanmean(ious) #返回当前图片里所有类的mean iou
其中,对于 l a b e l label label与 p r e d pred pred有多种形式。
如识别目标为4类,那么 l a b e l label label的形式可以是一张图片对应一份 m a s k [ 0 , 1 , 2 , 3 , 4 ] mask[0,1,2,3,4] mask[0,1,2,3,4],其中 0 0 0 为背景,我们省略,则 c l a s s class class可以为 [ 1 , 2 , 3 , 4 ] [1,2,3,4] [1,2,3,4]。也可以是对应四份二进制 m a s k mask mask [ 0 , 1 ] [0,1] [0,1], 这四层 m a s k mask mask的取值为 0 / 1 0/1 0/1。 c l a s s class class为 [ 1 ] [1] [1]了。
总结
对于目标检测,写 I O U IOU IOU那就是必考题,但是我们也要回顾下图像分割的 I O U IOU IOU怎么计算的。
其它干货
- 算法岗,不会写简历?我把它拆开,手把手教你写!
- (算法从业人员必备!)Ubuntu办公环境搭建!
- “我能分清奥特曼们了,你能分清我的口红吗?”
- 入门算法,看这个呀!(资料可下载)
- 放弃大厂算法Offer,去银行做开发,现在…
- 超6k字长文,带你纵横谈薪市场(建议工程师收藏!)
引用
- https://blog.csdn.net/weixin_42135399/article/details/101025941
- https://blog.csdn.net/lingzhou33/article/details/87901365
- https://blog.csdn.net/lingzhou33/article/details/87901365
大家好,我是灿视。目前是位算法工程师 + 创业者 + 奶爸的时间管理者!
我曾在19,20年联合了各大厂面试官,连续推出两版《百面计算机视觉》,受到了广泛好评,帮助了数百位同学们斩获了BAT等大小厂算法Offer。现在,我们继续出发,持续更新最强算法面经。
我曾经花了4个月,跨专业从双非上岸华五软工硕士,也从不会编程到进入到百度与腾讯实习。
欢迎加我私信,点赞朋友圈,参加朋友圈抽奖活动。如果你想加入<百面计算机视觉交流群>,也可以私我。