RPN网络讲解

article/2025/10/11 13:56:23

讲完了anchor机制,接下来我们讲RPN(region proposal network)区域候选网络,它的作用是为了得到候选框。
先看以下内容:
faster rcnn结构及代码讲解

原理解释

Feature Map进入RPN后,先经过一次33的卷积,同样,特征图大小依然是6040,数量512,这样做的目的应该是进一步集中特征信息,接着看到两个全卷积,即kernel_size=1*1,p=0,stride=1;
RPN
如上图中标识:

① rpn_cls:60*40*512-d ⊕ 1*1*512*18 ==> 60*40*9*2
逐像素对其9个Anchor box进行二分类

② rpn_bbox:60*40*512-d ⊕ 1*1*512*36==>60*40*9*4
逐像素得到其9个Anchor box四个坐标信息(其实是偏移量,后面介绍)

如下图所示:
在这里插入图片描述

代码解析

这段代码基本上就是第一张图,代码注意看注释:

def _region_proposal(self, net_conv, is_training, initializer):rpn = slim.conv2d(net_conv, cfg.RPN_CHANNELS, [3, 3], trainable=is_training, weights_initializer=initializer,scope="rpn_conv/3x3")#cfg.RPN_CHANNELS = 512, 3*3卷积处理,cfg.RPN_CHANNELS = 512,output shape=(1, 60, 40, 512)self._act_summaries.append(rpn)#可视化保存rpn_cls_score = slim.conv2d(rpn, self._num_anchors*2, [1, 1], trainable=is_training,weights_initializer=initializer,padding='VALID', activation_fn=None, scope='rpn_cls_score')#1*1卷积处理,分类,是否为前景,self._num_anchors=9,ouput shape=(1, 60, 40, 18)rpn_cls_score_reshape = _reshape_layer(self, rpn_cls_score, 2, 'rpn_cls_score_reshape')#后面需要进行softmax和argmax# change it so that the score has 2 as its channel size#shape=(1, 540, 40, 2),540=9*60#该函数后面有详细讲解rpn_cls_prob_reshape = _softmax_layer(self, rpn_cls_score_reshape, "rpn_cls_prob_reshape")#(1, 540, 40, 2)#该函数后面有详细讲解rpn_cls_pred = tf.argmax(tf.reshape(rpn_cls_score_reshape, [-1, 2]), axis=1, name="rpn_cls_pred")#tf.reshape=>shape=(21600, 2)  output shape=(21600,)rpn_cls_prob = self._reshape_layer(rpn_cls_prob_reshape, self._num_anchors * 2, "rpn_cls_prob")#shape=(1, 60, 40, 18)#该函数后面有详细讲解rpn_bbox_pred = slim.conv2d(rpn, self._num_anchors * 4, [1, 1], trainable=is_training,weights_initializer=initializer,padding='VALID', activation_fn=None, scope='rpn_bbox_pred')# shape=(1, 60, 40, 36), 注意这里预测的不是 left, bottom, right, top而是anchor与真实框之间的误差值。if is_training:rois, roi_scores = self._proposal_layer(rpn_cls_prob, rpn_bbox_pred, "rois")#输出roi shape=(1*60*40*9,5)以及roi_scores shape=(1*60*40*9,1)#该函数后面有详细讲解rpn_labels = self._anchor_target_layer(rpn_cls_score, "anchor")#rpn_cls_score 在这个函数,只提取了它的H,W# Try to have a deterministic order for the computing graph, for reproducibility#这个函数主要生成标签#该函数后面有详细讲解with tf.control_dependencies([rpn_labels]):rois, _ = self._proposal_target_layer(rois, roi_scores, "rpn_rois")#该函数后面有详细讲解else:if cfg.TEST.MODE == 'nms':rois, _ = self._proposal_layer(rpn_cls_prob, rpn_bbox_pred, "rois")elif cfg.TEST.MODE == 'top':rois, _ = self._proposal_top_layer(rpn_cls_prob, rpn_bbox_pred, "rois")else:raise NotImplementedErrorself._predictions["rpn_cls_score"] = rpn_cls_scoreself._predictions["rpn_cls_score_reshape"] = rpn_cls_score_reshapeself._predictions["rpn_cls_prob"] = rpn_cls_probself._predictions["rpn_cls_pred"] = rpn_cls_predself._predictions["rpn_bbox_pred"] = rpn_bbox_predself._predictions["rois"] = roisreturn rois

以上代码得到了6个后续主要所需值:
1.rpn_cls_score:分类分数,shape=(1, 60, 40, 2*9)
2.rpn_cls_score_reshape:分类分数,shape=(1, 540, 40, 2)
3.rpn_cls_prob:分类分概率,shape=(1, 60, 40, 18)
4.rpn_cls_pred:分类预测,shape=(21600, 2)
5.rpn_bbox_pred:shape=(1, 60, 40, 36),输出框,输出形式为[ Δ x \Delta x Δx, Δ y \Delta y Δy, Δ w \Delta w Δw, Δ h \Delta h Δh],为误差值。误差值的计算方式:
  anchor box: 中心点位置坐标 x a x_a xa, y a y_a ya和宽高 w a w_a wa, h a h_a ha
  ground truth(真实框):标定的框也对应一个中心点位置坐标 x ∗ x^* x, y ∗ y^* y和宽高 w ∗ w^* w, h ∗ h^* h
  所以,偏移量:
   Δ x = ( x ∗ − x a ) / h a \Delta x=(x^*-x_a)/h_a Δx=(xxa)/ha Δ y = ( y ∗ − y a ) / h a \Delta y=(y^*-y_a)/h_a Δy=(yya)/ha
   Δ w = l o g ( w ∗ / w a ) \Delta w=log(w^*/w_a) Δw=log(w/wa) Δ h = l o g ( h ∗ / h a ) \Delta h=log(h^*/h_a) Δh=log(h/ha)
6.rois

深入代码解析

  同时,这段代码有5个函数需要深入了解以下:
1._reshape_layer:改变输入层的shape
2._softmax_layer:进行softmax
3._proposal_layer:提取ROI
4._anchor_target_layer:打标签
5._proposal_target_layer:打标签
  前两篇博客,将在本篇博客进行讲解,后三个点链接

1._reshape_layer

  def _reshape_layer(self, bottom, num_dim, name):input_shape = tf.shape(bottom)with tf.variable_scope(name) as scope:# change the channel to the caffe format#之所以怎么做,是想把第3维度和第1维放在一起好做做个处理to_caffe = tf.transpose(bottom, [0, 3, 1, 2])# then force it to have channel 2#注意看tf.concat里面,num_dim为输入值,-1自己算reshaped = tf.reshape(to_caffe,tf.concat(axis=0, values=[[1, num_dim, -1], [input_shape[2]]]))# then swap the channel backto_tf = tf.transpose(reshaped, [0, 2, 3, 1])return to_tf#以下我们举个栗子:shape的前后变化(1, 60, 40, num_dim*9)=>(1, 60*9, 40, num_dim)

2._softmax_layer

  def _softmax_layer(self, bottom, name):if name.startswith('rpn_cls_prob_reshape'):#rpn_cls_prob_reshape层进入if语句,因为需要变形,再softmaxinput_shape = tf.shape(bottom)bottom_reshaped = tf.reshape(bottom, [-1, input_shape[-1]])#(1, 540, 40, 2)=>(1*540*40, 2)reshaped_score = tf.nn.softmax(bottom_reshaped, name=name)return tf.reshape(reshaped_score, input_shape)return tf.nn.softmax(bottom, name=name)

其实就是简单的softmax语句其中有一个if语句

3._proposal_layer
主要进行了box的修正(通过我们预测的误差值),nms的筛选和超出原图的裁剪。详细见:
https://blog.csdn.net/m0_37663944/article/details/103728902

4._anchor_target_layer
主要根据真实框与anchor的iou值进行了筛选,打标签(分类0,1,然后得出回归预测的误差值等)。这一部分得出的值是用来训练的,训练RPN(也就是得到roi的网络)
https://blog.csdn.net/m0_37663944/article/details/103735094

5._proposal_target_layer
https://blog.csdn.net/m0_37663944/article/details/103737373
这个函数主要根据_proposal_layer函数得到的roi与真实框得到iou,进行筛选,打标签(分类0,1,然后得出回归预测的误差值),和上面的_anchor_target_layer有以下不同:
1.它是用来训练最终的分类回归的,不在本篇博客的图中。

参考文献:
https://www.cnblogs.com/wangyong/p/8513563.html
https://blog.csdn.net/qq_41576083/article/details/82966489


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

相关文章

RPN架构及其PyTorch实现

RPN(Region Proposal Network) 引言RPN架构RPNAnchor 生成Proposals 的选择loss 的计算FPN for RPNRPN网络对于正负样本的选择 FPN for Fast R-CNN RPN架构及其Pytorch实现基于RPN产生~20k个候选框~20k个候选框(1):RPN~20k个候选框(2)&#x…

RPN 模型简要介绍

概述 本文介绍了 Faster R-CNN 目标检测模型中提出的图像候选框提取模型—— RPN 模型,这是一种区域生成网络,结构简单嵌入方便,极大加快了目标检测模型的运行速度,使目标检测模型真正融合到同一个神经网络内,对之后影…

RPN网络代码解读

1. 说在前面的话 在目标检测领域Faster RCNN可以说是无人不知无人不晓,它里面有一个网络结构RPN(Region Proposal Network)用于在特征图上产生候选预测区域。但是呢,这个网络结构具体是怎么工作的呢?网上有很多种解释,但是都是云里雾里的,还是直接撸代码来得直接,这里…

Siam-RPN

Siam-RPN 1. 研究动机: 在Siam-FC中,作者提出的算法首次将孪生网络引入了目标跟踪的领域,实现了端到端的训练,它是一个兼容了速度和精度的算法,在3个尺度变换和5个尺度变换的条件下,跟踪的速率分别达到了…

RPN的深度理解(实现层面)

参考http://www.telesens.co/2018/03/11/object-detection-and-classification-using-r-cnns/ 这是我看过的讲faster rcnn最好的一篇博客了,花了一下午时间看得差不多了,不过他也有些小细节没讲,我这里总结补充一下 另外还有一文读懂Faster R…

Faster RCNN 中的RPN解析

Faster RCNN 中的RPN解析 文章目录 Faster RCNN 中的RPN解析Anchor分类bounding box regressionproposal 参考 RCNN和Fast RCNN中使用Selective Search方法找出所有的候选框,SS方法非常耗时。Faster RCNN中提出RPN(region proposal network)替代SS方法提取候选框&am…

网络模型—RPN

仅作记录,自用 简单来说:输入一张图片,经过骨干网络的特征提取,得到一个m*n的特征图,映射到原图上,将原图划分为m*n个区域(每个区域很小),原图的每个区域的中心点由这个…

RPN网络

RPN 思路: 1、先通过conv层pooling层relu层,可以是vgg,得到feature maps。 2、在feature maps上提取对应的图。在第一步基础上,先通过rpn生成region proposals。通过softmax判断anchors(9个框)&#xff0…

RPN

RPN&#xff08;区域生成网络&#xff09; </h1><div class"clear"></div><div class"postBody">转&#xff1a;懒人元&#xff08;侵删&#xff09; RPN全称是Region Proposal Network&#xff0c;Region Proposal的中文意思是“…

RPN详解

RPN 思路&#xff1a; 1、先通过conv层pooling层relu层&#xff0c;可以是vgg&#xff0c;得到feature maps。 2、在feature maps上提取对应的图。在第一步基础上&#xff0c;先通过rpn生成region proposals。通过softmax判断anchors&#xff08;9个框&#xff09;&#xff0…

RPN 解析

RPN全称是Region Proposal Network,Region Proposal的中文意思是“区域选取”,也就是“提取候选框”的意思,所以RPN就是用来提取候选框的网络; 1. RPN的意义 RPN第一次出现在世人眼中是在Faster RCNN这个结构中,专门用来提取候选框,在RCNN和Fast RCNN等物体检测架构中,…

RPN(区域生成网络)

RPN全称是Region Proposal Network&#xff0c;Region Proposal的中文意思是“区域选取”&#xff0c;也就是“提取候选框”的意思&#xff0c;所以RPN就是用来提取候选框的网络。 1. RPN的意义 RPN第一次出现在世人眼中是在Faster RCNN这个结构中&#xff0c;专门用来提取候选…

RPN简介

RPN全称是Region Proposal Network&#xff0c;Region Proposal的中文意思是“区域选取”&#xff0c;也就是“提取候选框”的意思&#xff0c;所以RPN就是用来提取候选框的网络 文章目录 前言 一、RPN是什么&#xff1f; 二、计算RPN的输入特征图 三、生成Anchors 总结 前言 …

深度学习之 RPN(RegionProposal Network)- 区域候选网络

anchor boxes基本概念与作用: feature map 上的一个点可以映射回输入图片上的一个点&#xff0c;以特征图上这个点为中心&#xff0c;预先人为设定 k 个 boxes&#xff0c;这些 boxes 就称为在这个点上生成的 k 个 anchor boxes&#xff08;所有anchor boxes的中心点坐标是一样…

基于51单片机蓝牙小车

文章目录 接线电机控制模块接线&#xff08;需要额外电源&#xff09;单片机接线 实物图代码相关目录树.c文件main.cSerial.c&#xff08;不会参考我写的串口文章&#xff09;CarTurn.c&#xff08;车的相关操作&#xff09;delay.c .h文件reg51.h&#xff08;keil软件自带&…

51单片机 | 红外遥控实验

文章目录 一、红外遥控介绍1.红外线简介2.红外遥控的原理2.1 红外发射装置2.2 红外接收设备二、硬件设计三、软件设计1.红外初始化函数2.红外解码函数3.主函数四、实验现象这一节来介绍一种无线通信技术–红外遥控通信。我们开发板标配了一个一体化红外接收头和红外遥控器,我们…

基于51单片机手机电话拨号盘模拟proteus仿真

功能简介&#xff1a; 1.采用4X4矩阵键盘实现号码输入&#xff0c;并在LCD液晶上显示 2.按下enter键&#xff0c;显示calling…&#xff0c;并且蜂鸣器模拟电话拨号发声 3.按下*键&#xff0c;退格&#xff0c;按下#键&#xff0c;清楚电话号码 4.拨号过程中&#xff0c;长按#取…

51单片机 | 串口通信实验

文章目录 一、通信的基本概念1.串行通信与并行通信2. 异步通信与同步通信3.单工、半双工与全双工通信4.通信速率二、51单片机串口介绍1.串口通信介绍2.串口相关寄存器3.串口工作方式3.1 方式03.2 方式13.3 方式2和方式34.串口的使用方法三、硬件设计四、软件设计五、实验现象这…

keil c51+protues 实现简单密码锁

原理图 源码&#xff1a; 矩阵键盘key.c #include<reg51.h>sbit key1P1^0; sbit key2P1^1; sbit key3P1^2; sbit key4P1^3; sbit key5P1^4; sbit key6P1^5; sbit key7P1^6; sbit key8P1^7; sbit key9P3^0;void delay(unsigned int m){unsigned int n,k0;for(k0;k<…

51单片机智能小车蓝牙

材料&#xff1a; &#xff08;1&#xff09;51单片机开发板 &#xff08;2&#xff09;l298n电机驱动模块1个 &#xff08;3&#xff09;四个电机 &#xff08;4&#xff09;JDY-31蓝牙模块 一、组装 (1)L298N电机驱动模块与开发板接线如下图&#xff1a; (2)蓝牙接线&a…