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

相关文章

Redis实现高效内存管理的示例代码

《Redis实现高效内存管理的示例代码》Redis内存管理是其核心功能之一,为了高效地利用内存,Redis采用了多种技术和策略,如优化的数据结构、内存分配策略、内存回收、数据压缩等,下面就来详细的介绍... 目录1. 内存分配策略jemalloc 的使用2. 数据压缩和编码ziplist示例代码3. 优化的

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

使用Spring Cache本地缓存示例代码

《使用SpringCache本地缓存示例代码》缓存是提高应用程序性能的重要手段,通过将频繁访问的数据存储在内存中,可以减少数据库访问次数,从而加速数据读取,:本文主要介绍使用SpringCac... 目录一、Spring Cache简介核心特点:二、基础配置1. 添加依赖2. 启用缓存3. 缓存配置方案方案

MySQL的配置文件详解及实例代码

《MySQL的配置文件详解及实例代码》MySQL的配置文件是服务器运行的重要组成部分,用于设置服务器操作的各种参数,下面:本文主要介绍MySQL配置文件的相关资料,文中通过代码介绍的非常详细,需要... 目录前言一、配置文件结构1.[mysqld]2.[client]3.[mysql]4.[mysqldum

Python多线程实现大文件快速下载的代码实现

《Python多线程实现大文件快速下载的代码实现》在互联网时代,文件下载是日常操作之一,尤其是大文件,然而,网络条件不稳定或带宽有限时,下载速度会变得很慢,本文将介绍如何使用Python实现多线程下载... 目录引言一、多线程下载原理二、python实现多线程下载代码说明:三、实战案例四、注意事项五、总结引

IDEA与MyEclipse代码量统计方式

《IDEA与MyEclipse代码量统计方式》文章介绍在项目中不安装第三方工具统计代码行数的方法,分别说明MyEclipse通过正则搜索(排除空行和注释)及IDEA使用Statistic插件或调整搜索... 目录项目场景MyEclipse代码量统计IDEA代码量统计总结项目场景在项目中,有时候我们需要统计

MySQL设置密码复杂度策略的完整步骤(附代码示例)

《MySQL设置密码复杂度策略的完整步骤(附代码示例)》MySQL密码策略还可能包括密码复杂度的检查,如是否要求密码包含大写字母、小写字母、数字和特殊字符等,:本文主要介绍MySQL设置密码复杂度... 目录前言1. 使用 validate_password 插件1.1 启用 validate_passwo

MySQL实现多源复制的示例代码

《MySQL实现多源复制的示例代码》MySQL的多源复制允许一个从服务器从多个主服务器复制数据,这在需要将多个数据源汇聚到一个数据库实例时非常有用,下面就来详细的介绍一下,感兴趣的可以了解一下... 目录一、多源复制原理二、多源复制配置步骤2.1 主服务器配置Master1配置Master2配置2.2 从服

Go语言使用net/http构建一个RESTful API的示例代码

《Go语言使用net/http构建一个RESTfulAPI的示例代码》Go的标准库net/http提供了构建Web服务所需的强大功能,虽然众多第三方框架(如Gin、Echo)已经封装了很多功能,但... 目录引言一、什么是 RESTful API?二、实战目标:用户信息管理 API三、代码实现1. 用户数据