Faster R-CNN代码之 anchors 分析

2024-08-21 16:08
文章标签 分析 代码 cnn faster anchors

本文主要是介绍Faster R-CNN代码之 anchors 分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

anchors作为产生proposal的rpn中的一个重点内容,在Faster R-CNN中被重点介绍,下面我们来学习一下anchors产生部分代码。我主要将其中的部分重点代码展示出来。代码引用自Shaoqing Ren的Matlab下Faster R-CNN。

首先在Faster R-CNN迭代rpn和Fast R-CNN部分训练的前面,有一个产生anchors 的函数,我们称其产生的为base anchor,函数如下:

function anchors = proposal_generate_anchors(cache_name, varargin)
% anchors = proposal_generate_anchors(cache_name, varargin)
% --------------------------------------------------------
% Faster R-CNN
% Copyright (c) 2015, Shaoqing Ren
% Licensed under The MIT License [see LICENSE for details]
% --------------------------------------------------------%% inputsip = inputParser;ip.addRequired('cache_name',                        @isstr);% the size of the base anchor ip.addParamValue('base_size',       16,             @isscalar);% ratio list of anchorsip.addParamValue('ratios',          [0.5, 1, 2],    @ismatrix);% scale list of anchorsip.addParamValue('scales',          2.^[3:5],       @ismatrix);    ip.addParamValue('ignore_cache',    false,          @islogical);ip.parse(cache_name, varargin{:});opts = ip.Results;%%if ~opts.ignore_cacheanchor_cache_dir            = fullfile(pwd, 'output', 'rpn_cachedir', cache_name); mkdir_if_missing(anchor_cache_dir);anchor_cache_file           = fullfile(anchor_cache_dir, 'anchors');endtryld                      = load(anchor_cache_file);anchors                 = ld.anchors;catchbase_anchor             = [1, 1, opts.base_size, opts.base_size];% 围绕[base_anchor]随机ratios抖动ratio_anchors           = ratio_jitter(base_anchor, opts.ratios);% 围绕[base_anchor]随机scales抖动anchors                 = cellfun(@(x) scale_jitter(x, opts.scales), num2cell(ratio_anchors, 2), 'UniformOutput', false);anchors                 = cat(1, anchors{:});if ~opts.ignore_cachesave(anchor_cache_file, 'anchors');endendend
% 具体ratio_jitter,scale_jitter函数请关注原代码

我在实验过程中设置断点,截取自己生成的anchor数值作为例子,如下:

anchor:9*4
[   -83     -39     100    56    ]
[   -175    -87     192    104   ]
[   -359    -183    376    200   ]
[   -55     -55     72     72    ]
[   -119    -119    136    136   ]
[   -247    -247    264    264   ]
[   -35     -79     52     96    ]
[   -79     -167    96     184   ]
[   -167    -343    184    360   ]

可以看出,生成的9个anchor,前三排基本除去一些随机抖动以外不同scale但是ratio相同,均为[-2, -1, 2, 1],中间三排为[-1, -1, 1, 1],最后三排为[-1, -2, 1, 2]。
根据文章,这里即文章所说的9中anchor,即base anchor。

在rpn训练的过程中,针对每一张样本图像的大小与网络,得到所有anchor。

function [anchors, im_scales] = proposal_locate_anchors(conf, im_size, target_scale, feature_map_size)
% [anchors, im_scales] = proposal_locate_anchors(conf, im_size, target_scale, feature_map_size)
% --------------------------------------------------------
% Faster R-CNN
% Copyright (c) 2015, Shaoqing Ren
% Licensed under The MIT License [see LICENSE for details]
% --------------------------------------------------------   
% generate anchors for each scale% only for fcnif ~exist('feature_map_size', 'var')feature_map_size = [];endfunc = @proposal_locate_anchors_single_scale;if exist('target_scale', 'var')[anchors, im_scales] = func(im_size, conf, target_scale, feature_map_size);else[anchors, im_scales] = arrayfun(@(x) func(im_size, conf, x, feature_map_size), ...conf.scales, 'UniformOutput', false);endendfunction [anchors, im_scale] = proposal_locate_anchors_single_scale(im_size, conf, target_scale, feature_map_size)if isempty(feature_map_size)im_scale = prep_im_for_blob_size(im_size, target_scale, conf.max_size);img_size = round(im_size * im_scale);% 没有特征图时候,基于前面计算出的output高和宽,计算output_sizeoutput_size = cell2mat([conf.output_height_map.values({img_size(1)}), conf.output_width_map.values({img_size(2)})]);else%有特征图时候,直接赋值给output_sizeim_scale = prep_im_for_blob_size(im_size, target_scale, conf.max_size);output_size = feature_map_size;end% 针对output的高和宽,产生shift_x,shift_y。% shift_x大小为1*output列数shift_x = [0:(output_size(2)-1)] * conf.feat_stride;% shift_y大小为1*output行数shift_y = [0:(output_size(1)-1)] * conf.feat_stride;[shift_x, shift_y] = meshgrid(shift_x, shift_y);% concat anchors as [channel, height, width], where channel is the fastest dimension.% 这里意思就是对应output每一个像素处,根据conf.anchors(即前面提到的生成的base anchors)产生一系列anchorsanchors = reshape(bsxfun(@plus, permute(conf.anchors, [1, 3, 2]), ...permute([shift_x(:), shift_y(:), shift_x(:), shift_y(:)], [3, 1, 2])), [], 4);%   equals to  
%     anchors = arrayfun(@(x, y) single(bsxfun(@plus, conf.anchors, [x, y, x, y])), shift_x, shift_y, 'UniformOutput', false);
%     anchors = reshape(anchors, [], 1);
%     anchors = cat(1, anchors{:});end

这篇关于Faster R-CNN代码之 anchors 分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1093633

相关文章

JAVA项目swing转javafx语法规则以及示例代码

《JAVA项目swing转javafx语法规则以及示例代码》:本文主要介绍JAVA项目swing转javafx语法规则以及示例代码的相关资料,文中详细讲解了主类继承、窗口创建、布局管理、控件替换、... 目录最常用的“一行换一行”速查表(直接全局替换)实际转换示例(JFramejs → JavaFX)迁移建

Spring Boot Interceptor的原理、配置、顺序控制及与Filter的关键区别对比分析

《SpringBootInterceptor的原理、配置、顺序控制及与Filter的关键区别对比分析》本文主要介绍了SpringBoot中的拦截器(Interceptor)及其与过滤器(Filt... 目录前言一、核心功能二、拦截器的实现2.1 定义自定义拦截器2.2 注册拦截器三、多拦截器的执行顺序四、过

Go异常处理、泛型和文件操作实例代码

《Go异常处理、泛型和文件操作实例代码》Go语言的异常处理机制与传统的面向对象语言(如Java、C#)所使用的try-catch结构有所不同,它采用了自己独特的设计理念和方法,:本文主要介绍Go异... 目录一:异常处理常见的异常处理向上抛中断程序恢复程序二:泛型泛型函数泛型结构体泛型切片泛型 map三:文

MyBatis中的两种参数传递类型详解(示例代码)

《MyBatis中的两种参数传递类型详解(示例代码)》文章介绍了MyBatis中传递多个参数的两种方式,使用Map和使用@Param注解或封装POJO,Map方式适用于动态、不固定的参数,但可读性和安... 目录✅ android方式一:使用Map<String, Object>✅ 方式二:使用@Param

SpringBoot实现图形验证码的示例代码

《SpringBoot实现图形验证码的示例代码》验证码的实现方式有很多,可以由前端实现,也可以由后端进行实现,也有很多的插件和工具包可以使用,在这里,我们使用Hutool提供的小工具实现,本文介绍Sp... 目录项目创建前端代码实现约定前后端交互接口需求分析接口定义Hutool工具实现服务器端代码引入依赖获

利用Python在万圣节实现比心弹窗告白代码

《利用Python在万圣节实现比心弹窗告白代码》:本文主要介绍关于利用Python在万圣节实现比心弹窗告白代码的相关资料,每个弹窗会显示一条温馨提示,程序通过参数方程绘制爱心形状,并使用多线程技术... 目录前言效果预览要点1. 爱心曲线方程2. 显示温馨弹窗函数(详细拆解)2.1 函数定义和延迟机制2.2

C++ scoped_ptr 和 unique_ptr对比分析

《C++scoped_ptr和unique_ptr对比分析》本文介绍了C++中的`scoped_ptr`和`unique_ptr`,详细比较了它们的特性、使用场景以及现代C++推荐的使用`uni... 目录1. scoped_ptr基本特性主要特点2. unique_ptr基本用法3. 主要区别对比4. u

Springmvc常用的注解代码示例

《Springmvc常用的注解代码示例》本文介绍了SpringMVC中常用的控制器和请求映射注解,包括@Controller、@RequestMapping等,以及请求参数绑定注解,如@Request... 目录一、控制器与请求映射注解二、请求参数绑定注解三、其他常用注解(扩展)四、注解使用注意事项一、控制

Nginx内置变量应用场景分析

《Nginx内置变量应用场景分析》Nginx内置变量速查表,涵盖请求URI、客户端信息、服务器信息、文件路径、响应与性能等类别,这篇文章给大家介绍Nginx内置变量应用场景分析,感兴趣的朋友跟随小编一... 目录1. Nginx 内置变量速查表2. 核心变量详解与应用场景3. 实际应用举例4. 注意事项Ng

Java多种文件复制方式以及效率对比分析

《Java多种文件复制方式以及效率对比分析》本文总结了Java复制文件的多种方式,包括传统的字节流、字符流、NIO系列、第三方包中的FileUtils等,并提供了不同方式的效率比较,同时,还介绍了遍历... 目录1 背景2 概述3 遍历3.1listFiles()3.2list()3.3org.codeha