当前位置:网站首页>The principle of NMS and its code realization
The principle of NMS and its code realization
2022-08-05 00:23:00 【Le0v1n】
1. 为什么要用NMS
YOLOv3在预测阶段, Each target will generate at least 3个proposals, But a target generally only shows oneproposal, 因此需要对proposals进行去重,
这里去重的方法是NMS. 而NMSThe filter is based on IoU.
2. NMS的步骤
先对所有proposalsDo confidence(confidence)的排序, Sort by descending order of confidence(从大到小排序).
will be of maximum confidenceproposal ( p r o p o s a l m a x {\rm proposal_{max}} proposalmax)取出来, 与剩下的proposals( p r o p o s a l s r e s t {\rm proposals_{rest}} proposalsrest)进行IoU的计算, The purpose here is to filter the latterproposals, 意思是说:
如果 p r o p o s a l s r e s t {\rm proposals_{rest}} proposalsrest中的某一个proposal与 p r o p o s a l m a x {\rm proposal_{max}} proposalmaxcalculated betweenIoUvalue is less than the set threshold( t h r e s h {\rm thresh} thresh), 那么就认为这个proposal是可用的(retainable)
一旦有IoU > t h r e s h {\rm thresh} thresh的proposal, We think soproposal和 p r o p o s a l m a x {\rm proposal_{max}} proposalmaxThe same target is predicted(object), So the box is redundant(Because its confidence is not p r o p o s a l m a x {\rm proposal_{max}} proposalmaxof high confidence), 需要去除.
the most confidentproposalwill definitely keep it, We are picking the restproposals
p r o p o s a l m a x {\rm proposal_{max}} proposalmax与所有 p r o p o s a l s r e s t {\rm proposals_{rest}} proposalsrest的IoUAfter calculating and filtering, The confidence pointer points to the next one(The second highest confidenceproposal)
重复2, 3 -> 递归
NMSThe core main thing is: IoU > 阈值 的proposals需要去除, < 阈值的proposals则保留, The purpose is not to affect the forecast otherobject的proposals受到影响.
3. NMS的代码实现
import torch
def iou(proposal, proposals, isMin=False):
"""计算proposals的IoU 在计算IoU时, Need to find the intersection and union of the two. Suppose the coordinates of the two boxes are respectively: (x_11, y_11, x_12, y_12)和(x_21, y2_1, x_22, y_22) The coordinates of the intersection box: (max(x_11, x_21), max(y_11, y_21), min(x_12, x_22), min(y_12, y_22)) Args: proposal (_type_): 置信度最高的proposal -> [4] proposals (_type_): 剩余的proposals -> [N, 4] isMin (bool, optional): IoU的计算模式, 有两种: 1. (True) 交集 / 最小面积 2. (False -> Default) 交集 / 并集 Return: IoU (float): 返回proposal与proposals的IoU """
# Calculates the area of the current box: proposal = [x, y, w, h]
box_area = (proposal[2] - proposal[0]) * (proposal[3] - proposal[1])
# 计算proposalsThe area of all boxes in proposals = [N, [x, y, w, h]]
boxes_area = (proposals[:, 2] - proposals[:, 0]) * (proposals[:, 3] - proposals[:, 1])
# 计算交集proposal和proposals的计算
xx_1 = torch.maximum(proposal[0], proposals[:, 0]) # The upper left corner of the intersectionx坐标
yy_1 = torch.maximum(proposal[1], proposals[:, 1]) # The upper left corner of the intersectiony坐标
xx_2 = torch.minimum(proposal[2], proposals[:, 2]) # Bottom right corner of the intersectionx坐标
yy_2 = torch.minimum(proposal[3], proposals[:, 3]) # Bottom right corner of the intersectiony坐标
# 特殊情况: The two boxes are not next to each other -> 没有交集
w, h = torch.maximum(torch.Tensor([0]), xx_2 - xx_1), torch.maximum(torch.Tensor([0]), yy_2 - yy_1)
# Get the area of the intersecting boxes
intersection_area = w * h
if isMin: # If one box is inside another box
return intersection_area / torch.min(box_area, boxes_area)
else: # 两个框相交 -> 交集 / 并集
return intersection_area / (box_area + boxes_area - intersection_area)
def nms(proposals, thresh=0.3, isMin=False):
"""Non-maximum suppression is used to remove redundancyproposals Args: proposals (torch.tensor): 网络推理得到的proposals -> [conf, x, y, w, h] thresh (float, optional): NMSFilter threshold. Defaults to 0.3. isMin (bool, optional): IoU的计算方式, Defaults to intersection/并集. Defaults to False. """
# 根据proposalsThe confidences are sorted in descending order
sorted_proposals = proposals[proposals[:, 0].argsort(descending=True)]
# 定义一个ls, Used to save what needs to be preservedproposals
keep_boxes = []
while len(sorted_proposals) > 0:
# 取出置信度最高的proposal并存放到ls中
_box = sorted_proposals[0]
keep_boxes.append(_box)
if len(sorted_proposals) > 1:
# Take out the restproposals
_boxes = sorted_proposals[1:]
# 置信度最高的proposal与其他proposals进行IoU的计算
""" 需要注意的是, NMSreserved during screeningIoU小于thresh的. 为什么? 两个proposal的IoU越小, 说明两个proposalThe framed objects are more different, 别忘了, NMS是为了去重, So need to keep less thanIoU的proposals torch.where(条件): 返回符合条件的索引 """
sorted_proposals = _boxes[torch.where(iou(_box, _boxes, isMin) < thresh)]
# 当剩下最后一个时候, 就不进行IoU计算了(Calculate with yourselfIoU没有意义)
else:
break
# 将lsConvert to high dimensionaltensor
return torch.stack(keep_boxes)
if __name__ == "__main__":
proposal = torch.tensor(data=[0, 0, 4, 4])
proposals = torch.tensor(data=[[4, 4, 5, 5], # 没有交集
[1, 1, 5, 5]]) # 有交集
print(iou(proposal, proposals)) # tensor([0.0000, 0.3913])
boxes = torch.tensor(data=[
[0.5, 1, 1, 10, 10],
[0.9, 1, 1, 11, 11], # Very similar to the one above
[0.4, 8, 8, 12, 12] # Not similar to the above two
])
print(nms(boxes, thresh=0.1))
"""仅保留了2个 tensor([[ 0.9000, 1.0000, 1.0000, 11.0000, 11.0000], [ 0.4000, 8.0000, 8.0000, 12.0000, 12.0000]]) """
print(nms(boxes, thresh=0.3))
"""All are reserved tensor([[ 0.9000, 1.0000, 1.0000, 11.0000, 11.0000], [ 0.5000, 1.0000, 1.0000, 10.0000, 10.0000], [ 0.4000, 8.0000, 8.0000, 12.0000, 12.0000]]) """
```
边栏推荐
- [230] Execute command error after connecting to Redis MISCONF Redis is configured to save RDB snapshots
- D - I Hate Non-integer Number (count of selected number dp
- 软件测试面试题:测试用例通常包括那些内容?
- 典型相关分析CCA计算过程
- ansible学习笔记分享-含剧本示例
- 简单的顺序结构程序(C语言)
- 《WEB安全渗透测试》(28)Burp Collaborator-dnslog外带技术
- [230]连接Redis后执行命令错误 MISCONF Redis is configured to save RDB snapshots
- 【unity编译器扩展之模型动画拷贝】
- 软件测试面试题:您如何看待软件过程改进?在您曾经工作过的企业中,是否有一些需要改进的东西呢?您期望的理想的测试人员的工作环境是怎样的?
猜你喜欢
【Valentine's Day special effects】--Canvas realizes full screen love
TinyMCE禁用转义
2 用D435i运行VINS-fusion
gorm joint table query - actual combat
【LeetCode】Summary of Two Pointer Problems
英特尔WiFi 7产品将于2024年亮相 最高速度可达5.8Gbps
Getting started with 3D modeling for games, what modeling software can I choose?
"Relish Podcast" #397 The factory manager is here: How to use technology to empower the law?
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
刘润直播预告 | 顶级高手,如何创造财富
随机推荐
关于我仔细检查审核过关于工作人员页面,返回一个所属行业问题
Software testing interview questions: the difference and connection between black box testing, white box testing, and unit testing, integration testing, system testing, and acceptance testing?
Three tips for you to successfully get started with 3D modeling
Cloud native - Kubernetes 】 【 scheduling constraints
电赛必备技能___定时ADC+DMA+串口通信
软件测试面试题:您以往所从事的软件测试工作中,是否使用了一些工具来进行软件缺陷(Bug)的管理?如果有,请结合该工具描述软件缺陷(Bug)跟踪管理的流程?
E - Many Operations (bitwise consideration + dp thought to record the result after the operation
Zombie and orphan processes
E - Many Operations (按位考虑 + dp思想记录操作后的结果
"Relish Podcast" #397 The factory manager is here: How to use technology to empower the law?
软件测试面试题:测试用例通常包括那些内容?
D - I Hate Non-integer Number (选数的计数dp
2022牛客多校训练第二场 H题 Take the Elevator
怎样进行在不改变主线程执行的时候,进行日志的记录
Mysql_14 存储引擎
倒计时1天!8月2日—4日与你聊聊开源与就业那些事!
leetcode: 266. All Palindromic Permutations
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
what?测试/开发程序员要被淘汰了?年龄40被砍到了32?一瞬间,有点缓不过神来......
QSunSync Qiniu cloud file synchronization tool, batch upload