MMDetection-RetinaNet(BBox)(mmdetection bbox_overlaps)
ztj100 2024-11-14 19:24 20 浏览 0 评论
AnchorGenerator
RetinaNet 属于 Anchor-based 算法,在运行 bbox 属性分配前需要得到每个输出特征图位置的 anchor 列表,故在分析 BBox Assigner 前,需要先详细说明下 anchor 生成过程,其对应配置如下所示:
anchor_generator=dict(
type='AnchorGenerator',
# 特征图 anchor 的 base scale, 值越大,所有 anchor 的尺度都会变大
octave_base_scale=4,
# 每个特征图有3个尺度,2**0, 2**(1/3), 2**(2/3)
scales_per_octave=3,
# 每个特征图有3个高宽比例
ratios=[0.5, 1.0, 2.0],
# 特征图对应的 stride,必须特征图 stride 一致,不可以随意更改
strides=[8, 16, 32, 64, 128]),
从上面配置可以看出:RetinaNet 一共 5 个输出特征图,每个特征图上有 3 种尺度和 3 种宽高比,每个位置一共 9 个 anchor,并且通过 octave_base_scale 参数来控制全局 anchor 的 base scales ,如果自定义数据集中普遍都是大物体或者小物体,则可能修改更改 octave_base_scale 参数。
为了方便理解,可以写个简单脚本可视化下指定特征图位置的 anchor 情况。
import numpy as np
from mmcv.visualization import imshow_bboxes
import matplotlib.pyplot as plt
from mmdet.core import build_anchor_generator
if __name__ == '__main__':
anchor_generator_cfg = dict(
type='AnchorGenerator',
octave_base_scale=4,
scales_per_octave=3,
ratios=[0.5, 1.0, 2.0],
strides=[8, 16, 32, 64, 128])
anchor_generator = build_anchor_generator(anchor_generator_cfg)
# 输出原图尺度上 anchor 坐标 xyxy 左上角格式
# base_anchors 长度为5,表示5个输出特征图,不同的特征图尺度相差的只是 strides
# 故我们取 strides=8 的位置 anchor 可视化即可
base_anchors = anchor_generator.base_anchors[0]
h = 100
w = 160
img = np.ones([h, w, 3], np.uint8) * 255
base_anchors[:, 0::2] += w // 2
base_anchors[:, 1::2] += h // 2
colors = ['green', 'red', 'blue']
for i in range(3):
base_anchor = base_anchors[i::3, :].cpu().numpy()
imshow_bboxes(img, base_anchor, show=False, colors=colors[i])
plt.grid()
plt.imshow(img)
plt.show()
结果如下所示:
相同颜色表示在该特征图中基本尺度是相同的,只是宽高比不一样而已。
在对 AnchorGenerator 有基本认识后,下面对其实现源码进行分析:
- 先对单个位置 (0,0) 生成 base anchors
w = base_size
h = base_size
# 计算高宽比例
h_ratios = torch.sqrt(ratios)
w_ratios = 1 / h_ratios
# base_size 乘上宽高比例乘上尺度,就可以得到 n 个 anchor 的原图尺度wh值
ws = (w * w_ratios[:, None] * scales[None, :]).view(-1)
hs = (h * h_ratios[:, None] * scales[None, :]).view(-1)
# 得到 x1y1x2y2 格式的 base_anchor 坐标值
base_anchors = [
x_center - 0.5 * ws, y_center - 0.5 * hs, x_center + 0.5 * ws,
y_center + 0.5 * hs
]
# 堆叠起来即可
base_anchors = torch.stack(base_anchors, dim=-1)
- 利用输入特征图尺寸加上 base anchors,得到每个特征图位置的对于原图的 anchors
feat_h, feat_w = featmap_size
# 遍历特征图上所有位置,并且乘上 stride,从而变成原图坐标
shift_x = torch.arange(0, feat_w, device=device) * stride[0]
shift_y = torch.arange(0, feat_h, device=device) * stride[1]
shift_xx, shift_yy = self._meshgrid(shift_x, shift_y)
shifts = torch.stack([shift_xx, shift_yy, shift_xx, shift_yy], dim=-1)
shifts = shifts.type_as(base_anchors)
# (0,0) 位置的 base_anchor,假设原图上坐标 shifts,即可得到特征图上面每个点映射到原图坐标上的 anchor
all_anchors = base_anchors[None, :, :] + shifts[:, None, :]
all_anchors = all_anchors.view(-1, 4)
return all_anchors
简单来说就是:假设一共 m 个输出特征图
- 遍历 m 个输出特征图,在每个特征图的 (0,0) 或者说原图的 (0,0) 坐标位置生成 base_anchors,注意 base_anchors 不是特征图尺度,而是原图尺度
- 遍历 m 个输出特征图中每个特征图上每个坐标点,将其映射到原图坐标上
- 原图坐标点加上 base_anchors,就可以得到特征图每个位置的对应到原图尺度的 anchor 列表,anchor 列表长度为 m
BBox Assigner
计算得到输出特征图上面每个点对应的原图 anchor 坐标后,就可以和 gt 信息计算每个 anchor 的正负样本属性,对应配置如下:
assigner=dict(
# 最大 IoU 原则分配器
type='MaxIoUAssigner',
# 正样本阈值
pos_iou_thr=0.5,
# 负样本阈值
neg_iou_thr=0.4,
# 正样本阈值下限
min_pos_iou=0,
# 忽略 bboes 的阈值,-1表示不忽略
ignore_iof_thr=-1)
仅从上面的描述可能比较难理解参数含义,通过下面的流程分析就比较容易理解每个参数含义了。MaxIoUAssigner 操作包括 4 个步骤:
- 初始化所有 anchor 为忽略样本
假设所有输出特征的所有 anchor 总数一共 n 个,对应某张图片中 gt bbox 个数为 m,首先初始化长度为 n 的 assigned_gt_inds,全部赋值为 -1,表示当前全部设置为忽略样本。
# 1. assign -1 by default
assigned_gt_inds = overlaps.new_full((num_bboxes, ),
-1,
dtype=torch.long)
- 计算背景样本
将每个 anchor 和所有 gt bbox 计算 iou,找出最大 iou,如果该 iou 小于 neg_iou_thr 或者在背景样本阈值范围内,则该 anchor 对应索引位置的 assigned_gt_inds 设置为 0,表示是负样本(背景样本)。
max_overlaps, argmax_overlaps = overlaps.max(dim=0)
gt_max_overlaps, gt_argmax_overlaps = overlaps.max(dim=1)
# 2. assign negative: below
# the negative inds are set to be 0
if isinstance(self.neg_iou_thr, float):
assigned_gt_inds[(max_overlaps >= 0)
& (max_overlaps < self.neg_iou_thr)] = 0
elif isinstance(self.neg_iou_thr, tuple):
assert len(self.neg_iou_thr) == 2
# 可以设置一个范围
assigned_gt_inds[(max_overlaps >= self.neg_iou_thr[0])
& (max_overlaps < self.neg_iou_thr[1])] = 0
- 计算高质量正样本
将每个 anchor 和所有 gt bbox 计算 iou,找出最大 iou,如果其最大 iou 大于等于 pos_iou_thr,则设置该 anchor 对应所有的 assigned_gt_inds 设置为当前匹配 gt bbox 的编号 +1(后面会减掉 1),表示该 anchor 负责预测该 gt bbox,且是高质量 anchor。之所以要加 1,是为了区分背景样本(背景样本的 assigned_gt_inds 值为 0)
# 3. assign positive: above positive IoU thresholdpos_inds = max_overlaps >= self.pos_iou_thrassigned_gt_inds[pos_inds] = argmax_overlaps[pos_inds] + 1
- 适当增加更多正样本
在第三步计算高质量正样本中可能会出现某些 gt bbox 没有分配给任何一个 anchor (由于 iou 低于 pos_iou_thr),导致该 gt bbox 不被认为是前景物体,此时可以通过 self.match_low_quality=True 配置进行补充正样本。
对于每个 gt bbox 需要找出和其最大 iou 的 anchor 索引,如果其 iou 大于 min_pos_iou,则将该 anchor 对应索引的 assigned_gt_inds 设置为正样本,表示该 anchor 负责预测对应的 gt bbox。通过本步骤,可以最大程度保证每个 gt bbox 都有相应的 anchor 负责预测,但是如果其最大 iou 值还是小于 min_pos_iou,则依然不被认为是前景物体。
if self.match_low_quality:
# Low-quality matching will overwirte the assigned_gt_inds assigned
# in Step 3. Thus, the assigned gt might not be the best one for
# prediction.
# For example, if bbox A has 0.9 and 0.8 iou with GT bbox 1 & 2,
# bbox 1 will be assigned as the best target for bbox A in step 3.
# However, if GT bbox 2's gt_argmax_overlaps = A, bbox A's
# assigned_gt_inds will be overwritten to be bbox B.
# This might be the reason that it is not used in ROI Heads.
for i in range(num_gts):
if gt_max_overlaps[i] >= self.min_pos_iou:
if self.gt_max_assign_all:
#如果有多个相同最高 iou 的 anchor 和该 gt bbox 对应,则一并赋值
max_iou_inds = overlaps[i, :] == gt_max_overlaps[i]
# 同样需要加1
assigned_gt_inds[max_iou_inds] = i + 1
else:
assigned_gt_inds[gt_argmax_overlaps[i]] = i + 1
此时可以可以得到如下总结:
- 如果 anchor 和所有 gt bbox 的最大 iou 值小于 0.4,那么该 anchor 就是背景样本
- 如果 anchor 和所有 gt bbox 的最大 iou 值大于等于 0.5,那么该 anchor 就是正样本
- 如果 gt bbox 和所有 anchor 的最大 iou 值大于等于 0(可以看出每个 gt bbox 都一定有至少一个 anchor 匹配),那么该 gt bbox 所对应的 anchor 也是正样本
- 其余样本全部为忽略样本即 anchor 和所有 gt bbox 的最大 iou 值处于 [0.4,0.5) 区间的 anchor 为忽略样本,不计算 loss
相关推荐
- 如何将数据仓库迁移到阿里云 AnalyticDB for PostgreSQL
-
阿里云AnalyticDBforPostgreSQL(以下简称ADBPG,即原HybridDBforPostgreSQL)为基于PostgreSQL内核的MPP架构的实时数据仓库服务,可以...
- Python数据分析:探索性分析
-
写在前面如果你忘记了前面的文章,可以看看加深印象:Python数据处理...
- C++基础语法梳理:算法丨十大排序算法(二)
-
本期是C++基础语法分享的第十六节,今天给大家来梳理一下十大排序算法后五个!归并排序...
- C 语言的标准库有哪些
-
C语言的标准库并不是一个单一的实体,而是由一系列头文件(headerfiles)组成的集合。每个头文件声明了一组相关的函数、宏、类型和常量。程序员通过在代码中使用#include<...
- [深度学习] ncnn安装和调用基础教程
-
1介绍ncnn是腾讯开发的一个为手机端极致优化的高性能神经网络前向计算框架,无第三方依赖,跨平台,但是通常都需要protobuf和opencv。ncnn目前已在腾讯多款应用中使用,如QQ,Qzon...
- 用rust实现经典的冒泡排序和快速排序
-
1.假设待排序数组如下letmutarr=[5,3,8,4,2,7,1];...
- ncnn+PPYOLOv2首次结合!全网最详细代码解读来了
-
编辑:好困LRS【新智元导读】今天给大家安利一个宝藏仓库miemiedetection,该仓库集合了PPYOLO、PPYOLOv2、PPYOLOE三个算法pytorch实现三合一,其中的PPYOL...
- C++特性使用建议
-
1.引用参数使用引用替代指针且所有不变的引用参数必须加上const。在C语言中,如果函数需要修改变量的值,参数必须为指针,如...
- Qt4/5升级到Qt6吐血经验总结V202308
-
00:直观总结增加了很多轮子,同时原有模块拆分的也更细致,估计为了方便拓展个管理。把一些过度封装的东西移除了(比如同样的功能有多个函数),保证了只有一个函数执行该功能。把一些Qt5中兼容Qt4的方法废...
- 到底什么是C++11新特性,请看下文
-
C++11是一个比较大的更新,引入了很多新特性,以下是对这些特性的详细解释,帮助您快速理解C++11的内容1.自动类型推导(auto和decltype)...
- 掌握C++11这些特性,代码简洁性、安全性和性能轻松跃升!
-
C++11(又称C++0x)是C++编程语言的一次重大更新,引入了许多新特性,显著提升了代码简洁性、安全性和性能。以下是主要特性的分类介绍及示例:一、核心语言特性1.自动类型推导(auto)编译器自...
- 经典算法——凸包算法
-
凸包算法(ConvexHull)一、概念与问题描述凸包是指在平面上给定一组点,找到包含这些点的最小面积或最小周长的凸多边形。这个多边形没有任何内凹部分,即从一个多边形内的任意一点画一条线到多边形边界...
- 一起学习c++11——c++11中的新增的容器
-
c++11新增的容器1:array当时的初衷是希望提供一个在栈上分配的,定长数组,而且可以使用stl中的模板算法。array的用法如下:#include<string>#includ...
- C++ 编程中的一些最佳实践
-
1.遵循代码简洁原则尽量避免冗余代码,通过模块化设计、清晰的命名和良好的结构,让代码更易于阅读和维护...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- idea eval reset (50)
- vue dispatch (70)
- update canceled (42)
- order by asc (53)
- spring gateway (67)
- 简单代码编程 贪吃蛇 (40)
- transforms.resize (33)
- redisson trylock (35)
- 卸载node (35)
- np.reshape (33)
- torch.arange (34)
- node卸载 (33)
- npm 源 (35)
- vue3 deep (35)
- win10 ssh (35)
- exceptionininitializererror (33)
- vue foreach (34)
- idea设置编码为utf8 (35)
- vue 数组添加元素 (34)
- std find (34)
- tablefield注解用途 (35)
- python str转json (34)
- java websocket客户端 (34)
- tensor.view (34)
- java jackson (34)