Faster R-CNN代码之 anchors 分析
2016-09-23 20:43
489 查看
anchors作为产生proposal的rpn中的一个重点内容,在Faster R-CNN中被重点介绍,下面我们来学习一下anchors产生部分代码。我主要将其中的部分重点代码展示出来。代码引用自Shaoqing Ren的Matlab下Faster R-CNN。
首先在Faster R-CNN迭代rpn和Fast R-CNN部分训练的前面,有一个产生anchors 的函数,我们称其产生的为base anchor,函数如下:
我在实验过程中设置断点,截取自己生成的anchor数值作为例子,如下:
可以看出,生成的9个anchor,前三排基本除去一些随机抖动以外不同scale但是ratio相同,均为[-2, -1, 2, 1],中间三排为[-1, -1, 1, 1],最后三排为[-1, -2, 1, 2]。
根据文章,这里即文章所说的9中anchor,即base anchor。
在rpn训练的过程中,针对每一张样本图像的大小与网络,得到所有anchor。
首先在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] % -------------------------------------------------------- %% inputs ip = inputParser; ip.addRequired('cache_name', @isstr); % the size of the base anchor ip.addParamValue('base_size', 16, @isscalar); % ratio list of anchors ip.addParamValue('ratios', [0.5, 1, 2], @ismatrix); % scale list of anchors ip.addParamValue('scales', 2.^[3:5], @ismatrix); ip.addParamValue('ignore_cache', false, @islogical); ip.parse(cache_name, varargin{:}); opts = ip.Results; %% if ~opts.ignore_cache anchor_cache_dir = fullfile(pwd, 'output', 'rpn_cachedir', cache_name); mkdir_if_missing(anchor_cache_dir); anchor_cache_file = fullfile(anchor_cache_dir, 'anchors'); end try ld = load(anchor_cache_file); anchors = ld.anchors; catch base_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_cache save(anchor_cache_file, 'anchors'); end end end % 具体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 fcn if ~exist('feature_map_size', 'var') feature_map_size = []; end func = @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); end end function [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_size output_size = cell2mat([conf.output_height_map.values({img_size(1)}), conf.output_width_map.values({img_size(2)})]); else %有特征图时候,直接赋值给output_size im_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)产生一系列anchors anchors = 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 分析
- 4000 【转】Faster RCNN代码理解(Python)
- 【Caffe】Python版本Faster R-CNN+ZF代码运行
- Faster RCNN代码理解(Python)
- tensorflow笔记:多层CNN代码分析
- Faster rcnn代码理解
- C++卷积神经网络实例:tiny_cnn代码详解(9)——partial_connected_layer层结构类分析(下)
- tensorflow笔记:多层CNN代码分析
- tensorflow的一些代码分析(二) tensorflow多层CNN代码分析
- Faster RCNN 源码解析(3.2) -- Anchor 生成(generate_anchors.py)
- Faster R-CNN中的anchors和YOLO中的grid的理解
- Faster RCNN代码理解(Python)
- 【深度学习:目标检测】 faster rcnn RPN之anchor(generate_anchors)源码解析
- 在Ubuntu系统、CPU模式下运行Faster R-cnn代码
- Faster rcnn代码理解(3)
- C++卷积神经网络实例:tiny_cnn代码详解(10)——layer_base和layer类结构分析
- Faster rcnn代码理解(4)
- Faster RCNN 源码解读(1) -- 文件结构分析
- 代码阅读:Faster RCNN
- Faster Rcnn RPN代码解析