RPN之生成anchors

article/2025/11/5 14:49:01

看不懂anchor生成方法,就看不懂参数。

faster_rcnn的anchors生成

在这里我就不看faster_rcnn的anchors生成源代码了,我们用numpy自己去实现,在128*128大小的图片中生成anchors.

开始上代码

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches# %matplotlib inline

feature_map的大小是16,在faster_rcnn中feature_map是一张图片经过卷积后生成的。如这里输入128*128的图片卷积后16*16大小,也就等于在128*128的图片里平均找16*16个像素点,每个像素点都生成3*3个锚框,scales和ratios决定框的大小。

size_Y = 16
size_X = 16
rpn_stride = 8
scales = [8, 16, 32]
ratios = [0.5, 1, 2]

 

scales, ratios = np.meshgrid(scales, ratios)
scales, ratios = scales.flatten(), ratios.flatten()
scalesY = scales * np.sqrt(ratios) 
scalesX = scales / np.sqrt(ratios)shiftX = np.arange(0, size_X) * rpn_stride
shiftY = np.arange(0, size_Y) * rpn_stride
shiftX, shiftY = np.meshgrid(shiftX, shiftY)
centerX, anchorX = np.meshgrid(shiftX, scalesX)
centerY, anchorY = np.meshgrid(shiftY, scalesY)
anchor_center = np.stack([centerY, centerX], axis=2).reshape(-1, 2)
anchor_size = np.stack([anchorY, anchorX], axis=2).reshape(-1, 2)
boxes = np.concatenate([anchor_center -  0.5*anchor_size, anchor_center +  0.5*anchor_size], axis=1)

建一个空白图片在上面显示anchors

plt.figure(figsize=(15, 15))
img = np.ones((150,150, 3))
plt.imshow(img)
Axs = plt.gca()
for i in range(anchors.shape[0]):box = anchors[i]rec = patches.Rectangle((box[0], box[1]), box[2]-box[0], box[3]-box[1], edgecolor='r', facecolor='none')Axs.add_patch(rec)

这个输出是经过修改scales=(1,2,4)后的结果,每个点3*3个框,修改后显示得更明显,scales影响着锚框的平均大小。

可以看到锚框最大的正方形也就是4*4的。当我们把scales调到(8, 16, 32)最大的正方形锚框是32*32,最小的是8*8。

这样当出现占满50%图片的物体时根本没办法框住,在mask_rcnn中对anchors的生成就有改进。

mask_rcnn的anchors生成

我们先看一下生成anchors的一点代码,参数输入:

t = generate_pyramid_anchors((32, 64, 128, 256, 512),[0.5, 1, 2],[ [32,32],[16,16],[8,8],[4,4],[2,2]],# [[256, 256], [128, 128], [64, 64], [32, 32], [16, 16]],[4, 8, 16, 32, 64],1)

这里面还有一部分跳转,和前面faster_rcnn的生成方法一样的。

我们先看一下feature_shapes这个参数,是根据输入图片大小决定的,每个feature_map是固定的,因为这是网络结构卷积后每一层输出的feature_map大小,输入图片128*128得到[ [32,32],[16,16],[8,8],[4,4],[2,2]],输入1024*1024得到[[256, 256], [128, 128], [64, 64], [32, 32], [16, 16]],也就是分出5个feature_map的大小,对逐个feature_map进行anchors的生成。

可以看到对参数进行了5次循环。

def generate_pyramid_anchors(scales, ratios, feature_shapes, feature_strides,anchor_stride):anchors = []for i in range(len(scales)): # (32, 64, 128, 256, 512)anchors.append(generate_anchors(scales[i], ratios, feature_shapes[i],feature_strides[i], anchor_stride))return np.concatenate(anchors, axis=0)

我们来看一下每次代入的参数和生成的anchors数量。

这里还是以128*128来进行。

整个运行生成4092个anchors。

把参数分解出来。

第一次循环参数:

生成anchors是(3072, 4) , 这里每个点只生成3个框,这个生成的最大正方形anchors是32*32。

scales = [32]
ratios = [0.25, 0.5, 1]
size_Y = size_X = 32
rpn_stride = 4

 这里用了前面的代码,复制就可以运行(加上上面的参数)

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches%matplotlib inlinescales, ratios = np.meshgrid(scales, ratios)
scales, ratios = scales.flatten(), ratios.flatten()
scalesY = scales * np.sqrt(ratios) 
scalesX = scales / np.sqrt(ratios)shiftX = np.arange(0, size_X) * rpn_stride
shiftY = np.arange(0, size_Y) * rpn_stride
shiftX, shiftY = np.meshgrid(shiftX, shiftY)
centerX, anchorX = np.meshgrid(shiftX, scalesX)
centerY, anchorY = np.meshgrid(shiftY, scalesY)
anchor_center = np.stack([centerY, centerX], axis=2).reshape(-1, 2)
anchor_size = np.stack([anchorY, anchorX], axis=2).reshape(-1, 2)
boxes = np.concatenate([anchor_center -  0.5*anchor_size, anchor_center +  0.5*anchor_size], axis=1)plt.figure(figsize=(15, 15))
img = np.ones((150,150, 3))
plt.imshow(img)
Axs = plt.gca()
for i in range(anchors.shape[0]):box = anchors[i]rec = patches.Rectangle((box[0], box[1]), box[2]-box[0], box[3]-box[1], edgecolor='r', facecolor='none')print((box[0], box[1]), box[2]-box[0], box[3]-box[1])Axs.add_patch(rec)

改了scalse生成的,可以看到每个点只生成3个anchors

第二次循环参数:

生成anchors是(768, 4), 这个生成的最大正方形anchors是64*64

scales = [64]
ratios = [0.25, 0.5, 1]
size_Y = size_X = 16
rpn_stride = 8

第三次循环参数:

生成anchors是(192, 4), 这个生成的最大正方形anchors是128*128

scales = [128]
ratios = [0.25, 0.5, 1]
size_Y = size_X = 8
rpn_stride = 16

第四次循环参数:

生成anchors是(48, 4), 这个生成的最大正方形anchors是256*256

scales = [256]
ratios = [0.25, 0.5, 1]
size_Y = size_X = 4
rpn_stride = 32

第五次循环参数:

生成anchors是(12, 4), 这个生成的最大正方形anchors是512*512

scales = [512]
ratios = [0.25, 0.5, 1]
size_Y = size_X = 2
rpn_stride = 64

我们可以看到3072+768+192+48+12=4092,前面faster_rcnn中生成anchors个数是2304个,这里明显多了,但我们发现第四次循环和第五次循环,生成的anchors大小都到256、512,可是图片大小也就128,我暂时的见解是后面这些框没必要生成,太大了。

 

rpn生成anchors粗劣看了生成结果:

当ratios =  [0.5, 1, 2]固定

scales = [128, 256, 512]

生成最小的框128*0.66=84

生成最大的框512/0.66=775

 

那么就可以得出:

图片大小128*128时 scales = [16, 32, 64]  合适

图片大小1024*1024时 scales = [128, 256, 512]  合适

图片大小512*512时 scales = [64, 128, 256] 合适

 

 

 

 

 

 

 

 

 

 

 

 


http://chatgpt.dhexx.cn/article/yLyw4wB8.shtml

相关文章

Qml anchors锚点布局

关于qml的锚点布局的一些分享,并不能算是教学之类的。 锚点布局会自动设置宽度高度,并且优先级比 width height更高 import QtQuick 2.13 import QtQuick.Window 2.12 import QtQml 2.13Window {width: 640height: 480visible: truetitle: qsTr("a…

yolo_anchors.txt超参数值设置

keras-yolov3在训练自定义图片集之前,设置合理的yolo_anchors.txt值,有利于模型训练的收敛,一般都带有默认的参数如下: 在实际项目中,yolo_anchors的值可以根据kmeans.py计算获取,通过聚类得到最佳anchors数…

QML之anchors锚布局

QML的布局方式一般采用两种,一种就是直接设置,X与Y坐标的值。一种是采用相对位置布局,anchors锚布局。使用锚布局的能够使界面更紧凑,更有整体化。我个人相对比较喜欢使用锚布局,选取一个最合适的参考点,其…

YOLOV5源代码学习之check_anchors()函数

该函数主要在train.py中调用 为了方便直观的阅读代码,对代码中的变量值进行了输出 def check_anchors(dataset, model, thr4.0, imgsz640):# Check anchor fit to data, recompute if necessaryprefix colorstr(autoanchor: )print(f\n{prefix}Analyzing anchors.…

QML入门教程(3): anchors的用法

QML的布局方法是anchors,锚。 例如界面上四个按钮,可以用绝对坐标 (x, y) 设置他们的位置, 代码如下: import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.5Window {visible: truewidth: 320height: 480title: qsTr("Hello World")co…

目标检测2——借Detectron2源码理解Anchors与RPN

文章目录 1.Regionn Proposal Network背景2.Regionn Proposal Network的结构3.Anchors4.Regionn Proposal Network的训练参考资料 欢迎访问个人网络日志🌹🌹知行空间🌹🌹 1.Regionn Proposal Network背景 RPN,Region …

QML基础:锚anchors

正文 除了传统的Grid、Row和Column外,Qt Quick还提供了一种使用锚点的方法来布局。可以将每个元素视为具有7条不可见的“锚定线”的集合:left、horizontalCenter、right、top、verticalCenter、baseline和bottom。 baseline(上图未展示)对应于文本所在的虚线。对于没有文…

细说目标检测中的Anchors

本文转载自AI公园。 作者:Raghul Asokan 编译:ronghuaiyang 导读 给大家再次解释一下Anchors在物体检测中的作用。 今天,我将讨论在物体检测器中引入的一个优雅的概念 —— Anchors,它是如何帮助检测图像中的物体,以及…

anchors布局

anchors布局 import QtQuick 2.2 import QtQuick.Window 2.2 Window {visible: truewidth: 640height: 480title: qsTr("Hello World")Rectangle {width: 300;height: 200;color: "blue";Rectangle {id: rectl;anchors.left: parent.left;anchors.leftMar…

PDU Session Anchors

在同一时刻,UE可以建立多条到同一个DN或不同DN的PDU会话;建立到同一个DN的多个PDU会话时,通过不同的UPF;每条PDU会话对应的SMF可以不同。 为了支持到DN的可选择路由功能或支持SSC mode 3模式,SMF可以控制PDU session的…

kmeans++聚类生成anchors

kmeans聚类生成anchors 说明 使用yolo系列通常需要通过kmeans聚类算法生成anchors, 但kmeans算法本身具有一定的局限性,聚类结果容易受初始值选取影响。 因此通过改进原kmeans_for_anchors.py实现 kmeans聚类生成anchors。具体实现如下: i…

anchors如何获得_Yolov3通过k-means聚类得到自己数据的anchors

本文代码参考: https://github.com/lars76/kmeans-anchor-boxes Yolov3中默认的9个anchors是作者通过对voc数据聚类得到的。 anchors 10,14, 23,27, 37,58, 81,82, 135,169, 344,319 不过,当我们训练自己的数据时,如果也采用默认的anchors,可…

Unity 锚点 Anchors

锚点介绍 锚点是Rect Transform组件中的属性,用于描述当前物体相对于父物体的对齐方式。 选中一个UI元素,就会显示其父物体的矩形框以及相对于父物体的锚点。 锚点只能位于父物体的矩形框之内。 锚点表现为四个相对出现的小三角形, 它们时而…

QML anchors 锚布局

锚布局 锚布局有7种锚线 anchors.leftanchors.rightanchors.topanchors.bottomanchors.horizontalCenteranchors.verticalCenteranchors.baseline 5种锚边距 anchors.leftMarginanchors.rightMarginanchors.topMarginanchors.bottomMarginanchors.margins 3种锚偏移 anchor…

anchors生成

关于修改anchor anchor与图片的输入分辨率有关系。 You should use this repository to get anchors: https://github.com/AlexeyAB/darknet By using this command for Yolo v3 (or v2): ./darknet detector calc_anchors data/hand.data -num_of_clusters 9 -width 720 -he…

使用k-means聚类anchors

在之前讲yolo理论基础知识时有提到过,从yolov2开始使用的anchors都是通过聚类得到的。如果想了解更多yolo相关的知识可以看看我在bilibili上录得视频:https://www.bilibili.com/video/BV1yi4y1g7ro 今天补下之前没有细讲的聚类anchors相关知识&#xff…

细说物体检测中的Anchors

点击上方“AI公园”,关注公众号,选择加“星标“或“置顶” 作者:Raghul Asokan 编译:ronghuaiyang 导读 给大家再次解释一下Anchors在物体检测中的作用。 今天,我将讨论在物体检测器中引入的一个优雅的概念 —— Ancho…

带图讲解,深度学习YOLO里面的anchors的进阶理解

如果有了解过yolo网络,那肯定也听说过anchors,当然anchors这个概念布置在YOLO里面才有,在其他的目标检测中也存在anchors这个概念。对于anchors计算的一些公式这篇文章就不进行讲解了,这篇文章主要是讲在训练网络模型过程中anchor…

Linux终端的网易云音乐——musicbox

网易云音乐是听歌的不错的选择,如果能够在命令行听歌就更cool了。特来推荐musicbox。 网易云音乐的musicbox是网易云音乐命令行版本,这款命令行的客户端使用 Python 构建,以 mpg123 作为播放后端。提供了很多使用的功能,如&#x…

MusicStore-2

1.按照MusicStore-1步骤创建mvc项目,并初始化数据库 2.修改HomeController using Chapter8.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc;namespace Chapter8.Controllers {public class…