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

相关文章

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

MySQL中的表连接原理分析

《MySQL中的表连接原理分析》:本文主要介绍MySQL中的表连接原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、环境3、表连接原理【1】驱动表和被驱动表【2】内连接【3】外连接【4编程】嵌套循环连接【5】join buffer4、总结1、背景

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

python中Hash使用场景分析

《python中Hash使用场景分析》Python的hash()函数用于获取对象哈希值,常用于字典和集合,不可变类型可哈希,可变类型不可,常见算法包括除法、乘法、平方取中和随机数哈希,各有优缺点,需根... 目录python中的 Hash除法哈希算法乘法哈希算法平方取中法随机数哈希算法小结在Python中,

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

Java Stream的distinct去重原理分析

《JavaStream的distinct去重原理分析》Javastream中的distinct方法用于去除流中的重复元素,它返回一个包含过滤后唯一元素的新流,该方法会根据元素的hashcode和eq... 目录一、distinct 的基础用法与核心特性二、distinct 的底层实现原理1. 顺序流中的去重

Go语言代码格式化的技巧分享

《Go语言代码格式化的技巧分享》在Go语言的开发过程中,代码格式化是一个看似细微却至关重要的环节,良好的代码格式化不仅能提升代码的可读性,还能促进团队协作,减少因代码风格差异引发的问题,Go在代码格式... 目录一、Go 语言代码格式化的重要性二、Go 语言代码格式化工具:gofmt 与 go fmt(一)

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.