当前位置: 首页>編程日記>正文

目标检测:SSD目标检测中PriorBox代码解读

目标检测:SSD目标检测中PriorBox代码解读

这篇博客主要写prior_box_layer 
这一层完成的是给定一系列feature map后如何在上面生成prior box。SSD的做法很有意思,对于输入大小是W×H的feature map,生成的prior box中心就是W×H个,均匀分布在整张图上,像下图中演示的一样。在每个中心上,可以生成多个不同长宽比的prior box,如[1/3, 1/2, 1, 2, 3]。所以在一个feature map上可以生成的prior box总数是W×H×length_of_aspect_ratio,对于比较大的feature map,如VGG的conv4_3,生成的prior box可以达到数千个。当然对于边界上的box,还要做一些处理保证其不超出图片范围,这都是细节了。

这里需要注意的是,虽然prior box的位置是在W×H的格子上,但prior box的大小并不是跟格子一样大,而是人工指定的,原论文中随着feature map从底层到高层,prior box的大小在0.2到0.9之间均匀变化。

一开始看SSD的时候很困扰我的一点就是形状的匹配问题:SSD用卷积层做bbox的拟合,输出的不应该是feature map吗,怎么能正好输出4个坐标呢?这里的做法有点暴力,比如需要输出W×H×length_of_aspect_ratio×4个坐标,就直接用length_of_aspect_ratio×4个channel的卷积层做拟合,这样就得到length_of_aspect_ratio×4个大小为W×H的feature map,然后把feature map拉成一个长度为W×H×length_of_aspect_ratio×4的向量,用SmoothL1之类的loss去拟合,效果还意外地不错…… 

代码解读:

#include <algorithm>
#include <functional>
#include <utility>
#include <vector>#include "caffe/layers/prior_box_layer.hpp"namespace caffe {template <typename Dtype>
void PriorBoxLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,    // 参数解析const vector<Blob<Dtype>*>& top) {const PriorBoxParameter& prior_box_param =this->layer_param_.prior_box_param();CHECK_GT(prior_box_param.min_size_size(), 0) << "must provide min_size."; for (int i = 0; i < prior_box_param.min_size_size(); ++i) {   // min_size_size()=1min_sizes_.push_back(prior_box_param.min_size(i));CHECK_GT(min_sizes_.back(), 0) << "min_size must be positive.";}aspect_ratios_.clear();aspect_ratios_.push_back(1.);   // 加入1,在ProtoTXT只设置了2,3或者2flip_ = prior_box_param.flip();  // 默认truefor (int i = 0; i < prior_box_param.aspect_ratio_size(); ++i) {    // aspect_ratio_size=2float ar = prior_box_param.aspect_ratio(i);bool already_exist = false;for (int j = 0; j < aspect_ratios_.size(); ++j) {  // 这里判断是不是已近把ratio压入栈,保证每个ratios都只有一个1/ratiosif (fabs(ar - aspect_ratios_[j]) < 1e-6) {    // 这里aspect_ratios_只有1一个值already_exist = true;break;   // 跳出for循环}}if (!already_exist) {aspect_ratios_.push_back(ar);if (flip_) {     //  翻转,改变长宽比aspect_ratios_.push_back(1./ar);  // 得到1,2,3,1/2,1/3}}    // 到这里,共有5个ratios,分别为1,2,1/2,3,1/3}num_priors_ = aspect_ratios_.size() * min_sizes_.size();  // min_sizes_.size()=1   5*1if (prior_box_param.max_size_size() > 0) {CHECK_EQ(prior_box_param.min_size_size(), prior_box_param.max_size_size());  // 最大和最小不能相等for (int i = 0; i < prior_box_param.max_size_size(); ++i) {  // max_size_size=1max_sizes_.push_back(prior_box_param.max_size(i));CHECK_GT(max_sizes_[i], min_sizes_[i])<< "max_size must be greater than min_size.";num_priors_ += 1;    // num_priors_ = 6;这里很重要,不然就只有5个,和论文中的6个就不相符了}}clip_ = prior_box_param.clip();           // true 默认falseif (prior_box_param.variance_size() > 1) {   // variance_size = 4// Must and only provide 4 variance.CHECK_EQ(prior_box_param.variance_size(), 4);   // 必须有4个variancefor (int i = 0; i < prior_box_param.variance_size(); ++i) {   // variance:0.1 0.1 0.2 0.2CHECK_GT(prior_box_param.variance(i), 0);variance_.push_back(prior_box_param.variance(i));}} else if (prior_box_param.variance_size() == 1) {   // 或者只设置一个,设为0.1CHECK_GT(prior_box_param.variance(0), 0);variance_.push_back(prior_box_param.variance(0));} else {// Set default to 0.1.variance_.push_back(0.1);}if (prior_box_param.has_img_h() || prior_box_param.has_img_w()) {   // 设置图片的长宽CHECK(!prior_box_param.has_img_size())<< "Either img_size or img_h/img_w should be specified; not both.";img_h_ = prior_box_param.img_h();CHECK_GT(img_h_, 0) << "img_h should be larger than 0.";img_w_ = prior_box_param.img_w();CHECK_GT(img_w_, 0) << "img_w should be larger than 0.";} else if (prior_box_param.has_img_size()) {const int img_size = prior_box_param.img_size();CHECK_GT(img_size, 0) << "img_size should be larger than 0.";img_h_ = img_size;img_w_ = img_size;} else {img_h_ = 0;img_w_ = 0;}if (prior_box_param.has_step_h() || prior_box_param.has_step_w()) {  // step,tesp_h,step_w参数设置CHECK(!prior_box_param.has_step())<< "Either step or step_h/step_w should be specified; not both.";step_h_ = prior_box_param.step_h();CHECK_GT(step_h_, 0.) << "step_h should be larger than 0.";step_w_ = prior_box_param.step_w();CHECK_GT(step_w_, 0.) << "step_w should be larger than 0.";} else if (prior_box_param.has_step()) {const float step = prior_box_param.step();CHECK_GT(step, 0) << "step should be larger than 0.";step_h_ = step;step_w_ = step;} else {step_h_ = 0;step_w_ = 0;}offset_ = prior_box_param.offset();   // 偏移量,默认0.5
}  // layersetup 结束template <typename Dtype>
void PriorBoxLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,const vector<Blob<Dtype>*>& top) {const int layer_width = bottom[0]->width();    // 输入feature map的大小const int layer_height = bottom[0]->height();vector<int> top_shape(3, 1);// Since all images in a batch has same height and width, we only need to// generate one set of priors which can be shared across all images.top_shape[0] = 1;// 2 channels. First channel stores the mean of each prior coordinate.// Second channel stores the variance of each prior coordinate.top_shape[1] = 2;top_shape[2] = layer_width * layer_height * num_priors_ * 4;  // 输出坐标,就是需要这么多个map,类似faster rcnn,注意:这里,如果没有在ptototxt中没有设置max_size,num_priors_的值就要减1CHECK_GT(top_shape[2], 0);top[0]->Reshape(top_shape);// 在mbox_priorbox层中,concat是选的axis: 2,就是说是concat的map数。
}template <typename Dtype>
void PriorBoxLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,const vector<Blob<Dtype>*>& top) {const int layer_width = bottom[0]->width();    // 上一层feature mapconst int layer_height = bottom[0]->height();int img_width, img_height;if (img_h_ == 0 || img_w_ == 0) {img_width = bottom[1]->width();             // data layer出来的结果,原始图img_height = bottom[1]->height();} else {img_width = img_w_;    // 对图进行缩放,可以设置参数img_height = img_h_;}float step_w, step_h;if (step_w_ == 0 || step_h_ == 0) {   // 得到缩放比例,相当于faster的feat_stride,这里处理的稍好些,长宽都有相应参数step_w = static_cast<float>(img_width) / layer_width;  // 这里都用的float,不像faster直接暴力int型step_h = static_cast<float>(img_height) / layer_height;} else {step_w = step_w_;step_h = step_h_;}Dtype* top_data = top[0]->mutable_cpu_data();int dim = layer_height * layer_width * num_priors_ * 4;  // 一般情况下w*h*6*4,conv4_3除外,详细参考笔记上的框架图int idx = 0;for (int h = 0; h < layer_height; ++h) {   // 对于feature map上的每个点逐一映射for (int w = 0; w < layer_width; ++w) {// 这里和Faster RCNN 一样,就是把feature map上的点映射回原图,这里加上0.5也是为了四舍五入,和faster rcnn python代码类似float center_x = (w + offset_) * step_w;   float center_y = (h + offset_) * step_h;float box_width, box_height;for (int s = 0; s < min_sizes_.size(); ++s) {  // min_sizes_.size()=1int min_size_ = min_sizes_[s]; // 这里的min_size从fc7_mbox_priorbox的60到最后的276,就是s_k从0.2到0.92的过程// first prior: aspect_ratio = 1, size = min_sizebox_width = box_height = min_size_;  // xmintop_data[idx++] = (center_x - box_width / 2.) / img_width;    // // ymintop_data[idx++] = (center_y - box_height / 2.) / img_height;// xmaxtop_data[idx++] = (center_x + box_width / 2.) / img_width;// ymaxtop_data[idx++] = (center_y + box_height / 2.) / img_height;if (max_sizes_.size() > 0) {CHECK_EQ(min_sizes_.size(), max_sizes_.size());int max_size_ = max_sizes_[s];// second prior: aspect_ratio = 1, size = sqrt(min_size * max_size)  // 这里就和论文中一致,s_k的选法,每个都不同box_width = box_height = sqrt(min_size_ * max_size_);// xmintop_data[idx++] = (center_x - box_width / 2.) / img_width;// ymintop_data[idx++] = (center_y - box_height / 2.) / img_height;// xmaxtop_data[idx++] = (center_x + box_width / 2.) / img_width;// ymaxtop_data[idx++] = (center_y + box_height / 2.) / img_height;}// rest of priorsfor (int r = 0; r < aspect_ratios_.size(); ++r) {  // 其他几个比例计算float ar = aspect_ratios_[r];if (fabs(ar - 1.) < 1e-6) {continue;}box_width = min_size_ * sqrt(ar);box_height = min_size_ / sqrt(ar);// xmintop_data[idx++] = (center_x - box_width / 2.) / img_width;// ymintop_data[idx++] = (center_y - box_height / 2.) / img_height;// xmaxtop_data[idx++] = (center_x + box_width / 2.) / img_width;// ymaxtop_data[idx++] = (center_y + box_height / 2.) / img_height;}}  // end for min_size=1}  // end for w}  // end for h// 到这里,所有的prior_box选取完成,共6个比例,和论文中相符合,同时在每一层中算一个s_k,就是每一层都会设置一个min_size// clip the prior's coordidate such that it is within [0, 1]if (clip_) {                        // 裁剪到[0,1]for (int d = 0; d < dim; ++d) {top_data[d] = std::min<Dtype>(std::max<Dtype>(top_data[d], 0.), 1.);}}// set the variance.// 解答: https://github.com/weiliu89/caffe/issues/75// 除以variance是对预测box和真实box的误差进行放大,从而增加loss,增大梯度,加快收敛。// 另外,top_data += top[0]->offset(0, 1);已经使指针指向新的地址,所以variance不会覆盖前面的结果。// offse一般都是4个参数的offset(n,c,w,h),设置相应的参数就可以指到下一张图(以四位张量为例)top_data += top[0]->offset(0, 1); // 这里我猜是指向了下一个chanelif (variance_.size() == 1) {caffe_set<Dtype>(dim, Dtype(variance_[0]), top_data);// 用常数variance_[0]对top_data进行初始化} else {int count = 0;for (int h = 0; h < layer_height; ++h) {for (int w = 0; w < layer_width; ++w) {for (int i = 0; i < num_priors_; ++i) {for (int j = 0; j < 4; ++j) {top_data[count] = variance_[j];++count;}}}}}
}INSTANTIATE_CLASS(PriorBoxLayer);
REGISTER_LAYER_CLASS(PriorBox);}  // namespace caffe


https://www.fengoutiyan.com/post/14684.html

相关文章:

  • 遥感图像目标检测代码
  • ssd 检测
  • 目标检测开源
  • 带角度的目标检测
  • 目标检测与处理
  • 目标识别与目标检测的区别
  • web目标检测
  • 目标检测ssd
  • 鏡像模式如何設置在哪,圖片鏡像操作
  • 什么軟件可以把圖片鏡像翻轉,C#圖片處理 解決左右鏡像相反(旋轉圖片)
  • 手機照片鏡像翻轉,C#圖像鏡像
  • 視頻鏡像翻轉軟件,python圖片鏡像翻轉_python中鏡像實現方法
  • 什么軟件可以把圖片鏡像翻轉,利用PS實現圖片的鏡像處理
  • 照片鏡像翻轉app,java實現圖片鏡像翻轉
  • 什么軟件可以把圖片鏡像翻轉,python圖片鏡像翻轉_python圖像處理之鏡像實現方法
  • matlab下載,matlab如何鏡像處理圖片,matlab實現圖像鏡像
  • 圖片鏡像翻轉,MATLAB:鏡像圖片
  • 鏡像翻轉圖片的軟件,圖像處理:實現圖片鏡像(基于python)
  • canvas可畫,JavaScript - canvas - 鏡像圖片
  • 圖片鏡像翻轉,UGUI優化:使用鏡像圖片
  • Codeforces,CodeForces 1253C
  • MySQL下載安裝,Mysql ERROR: 1253 解決方法
  • 勝利大逃亡英雄逃亡方案,HDU - 1253 勝利大逃亡 BFS
  • 大一c語言期末考試試題及答案匯總,電大計算機C語言1253,1253《C語言程序設計》電大期末精彩試題及其問題詳解
  • lu求解線性方程組,P1253 [yLOI2018] 扶蘇的問題 (線段樹)
  • c語言程序設計基礎題庫,1253號C語言程序設計試題,2016年1月試卷號1253C語言程序設計A.pdf
  • 信奧賽一本通官網,【信奧賽一本通】1253:抓住那頭牛(詳細代碼)
  • c語言程序設計1253,1253c語言程序設計a(2010年1月)
  • 勝利大逃亡英雄逃亡方案,BFS——1253 勝利大逃亡
  • 直流電壓測量模塊,IM1253B交直流電能計量模塊(艾銳達光電)
  • c語言程序設計第三版課后答案,【渝粵題庫】國家開放大學2021春1253C語言程序設計答案
  • 18轉換為二進制,1253. 將數字轉換為16進制
  • light-emitting diode,LightOJ-1253 Misere Nim
  • masterroyale魔改版,1253 Dungeon Master
  • codeformer官網中文版,codeforces.1253 B
  • c語言程序設計考研真題及答案,2020C語言程序設計1253,1253計算機科學與技術專業C語言程序設計A科目2020年09月國家開 放大學(中央廣播電視大學)
  • c語言程序設計基礎題庫,1253本科2016c語言程序設計試題,1253電大《C語言程序設計A》試題和答案200901
  • 肇事逃逸車輛無法聯系到車主怎么辦,1253尋找肇事司機