当前位置:网站首页>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]]) """
```
边栏推荐
- 2022杭电多校第三场 L题 Two Permutations
- 软件测试面试题:黑盒测试、白盒测试以及单元测试、集成测试、系统测试、验收测试的区别与联系?
- 刘润直播预告 | 顶级高手,如何创造财富
- Metasploit-域名上线隐藏IP
- MAUI Blazor 权限经验分享 (定位,使用相机)
- 《WEB安全渗透测试》(28)Burp Collaborator-dnslog外带技术
- Getting started with 3D modeling for games, what modeling software can I choose?
- 【论文笔记】—低照度图像增强—Unsupervised—EnlightenGAN—2019-TIP
- 克服项目管理中恐惧心理
- 【LeetCode】滑动窗口题解汇总
猜你喜欢

Essential knowledge for entry-level 3D game modelers

matlab中rcosdesign函数升余弦滚降成型滤波器

gorm joint table query - actual combat

【LeetCode】滑动窗口题解汇总

《MySQL入门很轻松》第2章:MySQL管理工具介绍

TinyMCE disable escape

Mysql_14 存储引擎

Cloud native - Kubernetes 】 【 scheduling constraints
![[Cloud Native--Kubernetes] Pod Controller](/img/e1/1a8cc82223f9a9be79ebbf1211e9a4.png)
[Cloud Native--Kubernetes] Pod Controller

论文解读( AF-GCL)《Augmentation-Free Graph Contrastive Learning with Performance Guarantee》
随机推荐
D - I Hate Non-integer Number (count of selected number dp
What is next-generation modeling (with learning materials)
软件测试面试题:关于自动化测试工具?
2022牛客多校第三场 J题 Journey
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?
MAUI Blazor 权限经验分享 (定位,使用相机)
找不到DiscoveryClient类型的Bean
标识符、关键字、常量 和变量(C语言)
性能测试如何准备测试数据
Software testing interview questions: What are the strategies for system testing?
软件测试面试题:请你分别画出 OSI 的七层网络结构图和 TCP/IP 的四层结构图?
【LeetCode】图解 904. 水果成篮
僵尸进程和孤儿进程
canvas 高斯模糊效果
电子行业MES管理系统的主要功能与用途
Software Testing Interview Questions: What do you think about software process improvement? Is there something that needs improvement in the enterprise you have worked for? What do you expect the idea
[idea] idea configures sql formatting
Metasploit-域名上线隐藏IP
oracle创建用户以后的权限问题
gorm joint table query - actual combat