日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

windows下实现c++版faster-rcnn

發(fā)布時(shí)間:2024/9/21 c/c++ 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 windows下实现c++版faster-rcnn 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本文主要參考:http://blog.csdn.net/oYangZi12/article/details/53290426?locationNum=5&fps=1?
1.下載微軟提供的caffe(https://github.com/Microsoft/caffe)并編譯,Pre-Build Steps?
Copy .\windows\CommonSettings.props.example to .\windows\CommonSettings.props 選擇cpu或者gpu模式。?
2.在caffe-master中l(wèi)ibcaffe的相應(yīng)位置加入roi-pooling層對(duì)應(yīng)的cpp,hpp,cu文件重新編譯caffe)。因roi-pooling層已經(jīng)在微軟版本的caffe中存在,只需要找到并添加到libcaffe模塊下。caffe-master\include\caffe\layers\roi_pooling_layer.hpp,caffe-master\include\caffe\layers\smooth_l1_loss_layer.hpp添加至caffe-master\include\caffe\layers路徑下,將caffe-master\src\caffe\layers\roi_pooling_layer.cpp,caffe-master\src\caffe\layers\smooth_l1_loss_layer添加至caffe-master\src\caffe\layers路徑下。?
3.重新build(生成各模塊),并對(duì)整個(gè)caffe工程進(jìn)行build.build成功則roi_pooling_layer層添加成功。?
4.caffe-master工程下添加Faster_rcnn模塊,這里直接將Classification模塊中classification.cpp替換掉,Classification重命名成Detect,添加Faster_rcnn.h,Faster_rcnn.cpp和Detect.cpp,添加路徑為..\caffe-master\examples\cpp_classification\?
5.Faster_rcnn.h代碼如下:

#pragma once #include <gflags\gflags.h> #include <glog\logging.h> #include <cstring> #include <map> #include <string> #include <vector> #include "boost\algorithm\string.hpp" #include "caffe\caffe.hpp" #include "caffe\util\signal_handler.h" #include <opencv2\opencv.hpp> using namespace cv; using caffe::Blob; using caffe::Caffe; using caffe::Net; using caffe::Layer; using caffe::Solver; using caffe::shared_ptr; using caffe::string; using caffe::Timer; using caffe::vector; using std::ostringstream; struct config{int maxsize;int target_size;int feat_stride;int anchor[9][4];int test_min_box_size;int per_nms_topN;int after_nms_topN;float overlap;config(){maxsize = 1000;target_size = 600;feat_stride = 16;int tmp[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}};memcpy(anchor, tmp, 9 * 4 * sizeof(int));test_min_box_size = 16;per_nms_topN = 6000;after_nms_topN = 300;overlap = 0.7;} }; struct abox {float x1;float y1;float x2;float y2;float score; }; class Faster_rcnn { public:Faster_rcnn(Mat);~Faster_rcnn();bool init();Mat gettarget(Mat); public:config conf;private:Mat im, m_src;Size input_geometry_;shared_ptr<Net<float> > rpn_net, faster_rcnn_net;double im_scale;Size feature_map_size; private:bool loadnet();bool imgtoblob();vector<abox> forward();bool rpn_converttoboxs();void prep_im_size();Mat proposal_local_anchor();Mat bbox_tranform_inv(Mat, Mat, string);Mat get_rpn_score(Blob<float>*, int w, int h);void m_sort(Mat&, Mat&);void boxes_filter(vector<abox>&, int, vector<abox>, vector<int>);void filter_boxs(Mat&, Mat&, vector<abox>&);void nms(vector<abox>, double overlap, vector<int>& vPick, int &nPick);void testdetection(vector<abox>&);};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95

Faster_rcnn.cpp代碼如下:

#include "Faster_rcnn.h" #include <opencv2\opencv.hpp>#include <algorithm> #include "caffe\common.hpp" #include "caffe\layers\input_layer.hpp" #include "caffe\layers\inner_product_layer.hpp" #include "caffe\layers\dropout_layer.hpp" #include "caffe\layers\conv_layer.hpp" #include "caffe\layers\relu_layer.hpp" #include "caffe\layers\reshape_layer.hpp" #include "caffe\layers\pooling_layer.hpp" #include "caffe\layers\lrn_layer.hpp" #include "caffe\layers\softmax_layer.hpp" #include "caffe\layers\roi_pooling_layer.hpp" // //namespace caffe //{ // extern INSTANTIATE_CLASS(InputLayer); // extern INSTANTIATE_CLASS(InnerProductLayer); // extern INSTANTIATE_CLASS(DropoutLayer); // extern INSTANTIATE_CLASS(ConvolutionLayer); // extern INSTANTIATE_CLASS(ROIPoolingLayer); // REGISTER_LAYER_CLASS(Convolution); // extern INSTANTIATE_CLASS(ReLULayer); // REGISTER_LAYER_CLASS(ReLU); // extern INSTANTIATE_CLASS(PoolingLayer); // REGISTER_LAYER_CLASS(Pooling); // extern INSTANTIATE_CLASS(LRNLayer); // REGISTER_LAYER_CLASS(LRN); // extern INSTANTIATE_CLASS(SoftmaxLayer); // REGISTER_LAYER_CLASS(Softmax); // extern INSTANTIATE_CLASS(ReshapeLayer); // //}cv::Scalar colortable[20] = { cv::Scalar(0, 0, 0), cv::Scalar(0, 0, 125), cv::Scalar(0, 125, 125), cv::Scalar(125, 125, 125), cv::Scalar(125, 0, 0), cv::Scalar(125, 125, 0), cv::Scalar(0, 125, 0), cv::Scalar(125, 0, 125), cv::Scalar(0, 0, 255), cv::Scalar(0, 255, 255), cv::Scalar(255, 255, 255), cv::Scalar(255, 0, 0), cv::Scalar(255, 255, 0), cv::Scalar(0, 255, 0), cv::Scalar(255, 0, 255), cv::Scalar(0, 255, 100), cv::Scalar(0, 0, 100), cv::Scalar(255, 0, 100), cv::Scalar(255, 255, 100), cv::Scalar(100, 100, 100) }; string classname[20] = { "aeroplane", "bike", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motobike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" }; Faster_rcnn::Faster_rcnn(Mat src) {m_src = src; } Faster_rcnn::~Faster_rcnn() {//if (boxs != NULL) delete boxs; } bool Faster_rcnn::init() {//initCaffe::set_mode(Caffe::CPU);loadnet();return true; } bool Faster_rcnn::loadnet() {//load netrpn_net.reset(new Net<float>("F:\\fast rcnn\\fasterrcnn_vs2013_beta\\fasterrcnn_vs2013\\faster_rcnn_VOC0712_ZF\\proposal_test.prototxt", caffe::TEST));rpn_net->CopyTrainedLayersFrom("F:\\fast rcnn\\fasterrcnn_vs2013_beta\\fasterrcnn_vs2013\\faster_rcnn_VOC0712_ZF\\proposal_final");faster_rcnn_net.reset(new Net<float>("F:\\fast rcnn\\fasterrcnn_vs2013_beta\\fasterrcnn_vs2013\\faster_rcnn_VOC0712_ZF\\detection_test.prototxt", caffe::TEST));faster_rcnn_net->CopyTrainedLayersFrom("F:\\fast rcnn\\fasterrcnn_vs2013_beta\\fasterrcnn_vs2013\\faster_rcnn_VOC0712_ZF\\detection_final");/*rpn_net.reset(new Net<float>("faster_rcnn_VOC0712_ZF\\proposal_test.prototxt", caffe::TEST));rpn_net->CopyTrainedLayersFrom("faster_rcnn_VOC0712_ZF\\proposal_final");faster_rcnn_net.reset(new Net<float>("faster_rcnn_VOC0712_ZF\\detection_test.prototxt", caffe::TEST));faster_rcnn_net->CopyTrainedLayersFrom("faster_rcnn_VOC0712_ZF\\detection_final");*/return true; } bool Faster_rcnn::imgtoblob() {Mat sample_float;m_src.convertTo(sample_float, CV_32FC3);cv::Scalar channel_mean = cv::mean(sample_float);Mat mean = cv::Mat(m_src.rows, m_src.cols, sample_float.type(), channel_mean);Mat sample_normalized;subtract(sample_float, mean, sample_normalized);prep_im_size();resize(sample_normalized, sample_normalized, input_geometry_);Blob<float>* input_layer = rpn_net->input_blobs()[0];input_layer->Reshape(1, sample_normalized.channels(), sample_normalized.rows, sample_normalized.cols);rpn_net->Reshape();float* input_data = input_layer->mutable_cpu_data();vector<cv::Mat> input_channels;for (int i = 0; i < input_layer->channels(); ++i){cv::Mat channel(sample_normalized.rows, sample_normalized.cols, CV_32FC1, input_data);input_channels.push_back(channel);input_data += sample_normalized.rows * sample_normalized.cols;}cv::split(sample_normalized, input_channels);CHECK(reinterpret_cast<float*>(input_channels.at(0).data) == rpn_net->input_blobs()[0]->cpu_data()) << "Input channels are not wrapping the input layer of the network.";return true; } bool aboxcomp(abox& b1, abox& b2) {return b1.score > b2.score; } vector<abox> Faster_rcnn::forward() {//forwardconst vector<Blob<float>*>& result = rpn_net->Forward();Blob<float>* resule0 = result[0];Blob<float>* resule1 = result[1];Mat boxs_delta(resule0->num()*resule0->channels()*resule0->width()*resule0->height() / 4, 4, CV_32FC1);float* p = resule0->mutable_cpu_data();int num = 0;for (int i = 0; i < resule0->num()*resule0->channels()*resule0->width()*resule0->height() / 4; i++){for (int j = 0; j < 4; j++){boxs_delta.at<float>(i, j) = resule0->data_at(0, num%resule0->channels(),(num - num / resule0->channels() / resule0->height() * resule0->channels() * resule0->height()) / resule0->height(),num / resule0->channels() / resule0->height());num++;//int order = j + i * 4;//boxs_delta.at<float>(i, j) = resule0->data_at(0, (order % (resule0->height()*resule0->channels())) % resule0->channels(), (order % (resule0->height()*resule0->channels())) / resule0->channels(), order / (resule0->height()*resule0->channels()));}}//create anchorsfeature_map_size = Size(resule0->width(), resule0->height());//prep_im_size();Mat anchors = proposal_local_anchor();Mat pre_box = bbox_tranform_inv(anchors, boxs_delta, "rpn");//Mat score(resule0->width(), resule0->height(), CV_32FC1);Mat score = get_rpn_score(resule1, resule0->width(), resule0->height());vector<abox> aboxes;filter_boxs(pre_box, score, aboxes);std::sort(aboxes.begin(), aboxes.end(), aboxcomp);//m_sort(pre_box,score);vector<int> vPick(aboxes.size());int nPick;/有cuda版,待加入,此處為cpu版///nms(aboxes, conf.overlap, vPick, nPick);vector<abox> aboxes_;boxes_filter(aboxes_, nPick, aboxes, vPick);return aboxes_; } void Faster_rcnn::nms(vector<abox> input_boxes, double overlap, vector<int> &vPick, int &nPick) {int nSample = min(int(input_boxes.size()), conf.per_nms_topN);vector<double> vArea(nSample);for (int i = 0; i < nSample; ++i){vArea[i] = double(input_boxes.at(i).x2 - input_boxes.at(i).x1 + 1)* (input_boxes.at(i).y2 - input_boxes.at(i).y1 + 1);}std::multimap<double, int> scores;for (int i = 0; i < nSample; ++i)scores.insert(std::pair<double, int>(input_boxes.at(i).score, i));nPick = 0;do{int last = scores.rbegin()->second;vPick[nPick] = last;nPick += 1;for (std::multimap<double, int>::iterator it = scores.begin(); it != scores.end();){int it_idx = it->second;double xx1 = max(input_boxes.at(last).x1, input_boxes.at(it_idx).x1);double yy1 = max(input_boxes.at(last).y1, input_boxes.at(it_idx).y1);double xx2 = min(input_boxes.at(last).x2, input_boxes.at(it_idx).x2);double yy2 = min(input_boxes.at(last).y2, input_boxes.at(it_idx).y2);double w = max(double(0.0), xx2 - xx1 + 1), h = max(double(0.0), yy2 - yy1 + 1);double ov = w*h / (vArea[last] + vArea[it_idx] - w*h);if (ov > overlap){it = scores.erase(it);}else{it++;}}} while (scores.size() != 0); } void Faster_rcnn::boxes_filter(vector<abox>& aboxes, int nPick, vector<abox> row, vector<int> vPick) {int n = min(nPick, conf.after_nms_topN);for (int i = 0; i < n; i++){aboxes.push_back(row[vPick[i]]);} } void Faster_rcnn::filter_boxs(Mat& pre_box, Mat& score, vector<abox>& aboxes) {aboxes.clear();for (int i = 0; i < pre_box.rows; i++){int widths = pre_box.at<float>(i, 2) - pre_box.at<float>(i, 0) + 1;int heights = pre_box.at<float>(i, 3) - pre_box.at<float>(i, 1) + 1;if (widths < conf.test_min_box_size || heights < conf.test_min_box_size){pre_box.at<float>(i, 0) = 0;pre_box.at<float>(i, 1) = 0;pre_box.at<float>(i, 2) = 0;pre_box.at<float>(i, 3) = 0;score.at<float>(i, 0) = 0;}abox tmp;tmp.x1 = pre_box.at<float>(i, 0);tmp.y1 = pre_box.at<float>(i, 1);tmp.x2 = pre_box.at<float>(i, 2);tmp.y2 = pre_box.at<float>(i, 3);tmp.score = score.at<float>(i, 0);aboxes.push_back(tmp);} } void Faster_rcnn::m_sort(Mat& pre_box, Mat& score) {for (int i = 0; i < pre_box.rows - 1; i++){for (int j = i + 1; j < pre_box.rows; j++){if (score.at<float>(i, 0) < score.at<float>(j, 0)){float tmp = score.at<float>(j, 0);score.at<float>(j, 0) = score.at<float>(i, 0);score.at<float>(i, 0) = tmp;float tmp0 = pre_box.at<float>(j, 0);float tmp1 = pre_box.at<float>(j, 1);float tmp2 = pre_box.at<float>(j, 2);float tmp3 = pre_box.at<float>(j, 3);pre_box.at<float>(j, 0) = pre_box.at<float>(i, 0);pre_box.at<float>(j, 1) = pre_box.at<float>(i, 1);pre_box.at<float>(j, 2) = pre_box.at<float>(i, 2);pre_box.at<float>(j, 3) = pre_box.at<float>(i, 3);pre_box.at<float>(i, 0) = tmp0;pre_box.at<float>(i, 1) = tmp1;pre_box.at<float>(i, 2) = tmp2;pre_box.at<float>(i, 3) = tmp3;}}} } Mat Faster_rcnn::get_rpn_score(Blob<float>* resule1, int w, int h) {//Blob<float> tmp;int channel = resule1->width()*resule1->height() / (w * h);Mat score(resule1->width()*resule1->height(), 1, CV_32FC1);//tmp.Reshape(1, resule1->width()*resule1->height() / (w * h),h,w);//float* p = tmp.mutable_cpu_data;int num = 0;for (int i = 0; i < w; i++){for (int j = 0; j < h; j++){for (int k = 0; k < channel; k++){score.at<float>(num, 0) = resule1->data_at(0, 1, h*k + j, i);num++;}}}return score; } Mat Faster_rcnn::bbox_tranform_inv(Mat anchors, Mat boxs_delta, string type) {if (type == "rpn"){Mat pre_box(anchors.rows, anchors.cols, CV_32FC1);for (int i = 0; i < anchors.rows; i++){double pred_ctr_x, pred_ctr_y, src_ctr_x, src_ctr_y;double dst_ctr_x, dst_ctr_y, dst_scl_x, dst_scl_y;double src_w, src_h, pred_w, pred_h;src_w = anchors.at<float>(i, 2) - anchors.at<float>(i, 0) + 1;src_h = anchors.at<float>(i, 3) - anchors.at<float>(i, 1) + 1;src_ctr_x = anchors.at<float>(i, 0) + 0.5 * (src_w - 1);src_ctr_y = anchors.at<float>(i, 1) + 0.5 * (src_h - 1);dst_ctr_x = boxs_delta.at<float>(i, 0);dst_ctr_y = boxs_delta.at<float>(i, 1);dst_scl_x = boxs_delta.at<float>(i, 2);dst_scl_y = boxs_delta.at<float>(i, 3);pred_ctr_x = dst_ctr_x*src_w + src_ctr_x;pred_ctr_y = dst_ctr_y*src_h + src_ctr_y;pred_w = exp(dst_scl_x) * src_w;pred_h = exp(dst_scl_y) * src_h;pre_box.at<float>(i, 0) = ((pred_ctr_x - 0.5*(pred_w - 1)) - 1) * (float)(m_src.cols) / (im.cols - 1) + 1;pre_box.at<float>(i, 1) = ((pred_ctr_y - 0.5*(pred_h - 1)) - 1) * (float)(m_src.rows) / (im.rows - 1) + 1;pre_box.at<float>(i, 2) = ((pred_ctr_x + 0.5*(pred_w - 1)) - 1) * (float)(m_src.cols) / (im.cols - 1) + 1;pre_box.at<float>(i, 3) = ((pred_ctr_y + 0.5*(pred_h - 1)) - 1) * (float)(m_src.rows) / (im.rows - 1) + 1;if (pre_box.at<float>(i, 0) < 0) pre_box.at<float>(i, 0) = 0;if (pre_box.at<float>(i, 0) > (m_src.cols - 1)) pre_box.at<float>(i, 0) = m_src.cols - 1;if (pre_box.at<float>(i, 2) < 0) pre_box.at<float>(i, 2) = 0;if (pre_box.at<float>(i, 2) > (m_src.cols - 1)) pre_box.at<float>(i, 2) = m_src.cols - 1;if (pre_box.at<float>(i, 1) < 0) pre_box.at<float>(i, 1) = 0;if (pre_box.at<float>(i, 1) > (m_src.rows - 1)) pre_box.at<float>(i, 1) = m_src.rows - 1;if (pre_box.at<float>(i, 3) < 0) pre_box.at<float>(i, 3) = 0;if (pre_box.at<float>(i, 3) > (m_src.rows - 1)) pre_box.at<float>(i, 3) = m_src.rows - 1;}return pre_box;}if (type == "rcnn"){Mat pre_box(boxs_delta.rows, boxs_delta.cols, CV_32FC1);for (int i = 0; i < boxs_delta.rows; i++){for (int j = 1; j < boxs_delta.cols / 4; j++){double pred_ctr_x, pred_ctr_y, src_ctr_x, src_ctr_y;double dst_ctr_x, dst_ctr_y, dst_scl_x, dst_scl_y;double src_w, src_h, pred_w, pred_h;src_w = anchors.at<float>(i, 2) - anchors.at<float>(i, 0) + 1;src_h = anchors.at<float>(i, 3) - anchors.at<float>(i, 1) + 1;src_ctr_x = anchors.at<float>(i, 0) + 0.5 * (src_w - 1);src_ctr_y = anchors.at<float>(i, 1) + 0.5 * (src_h - 1);dst_ctr_x = boxs_delta.at<float>(i, 4 * j + 0);dst_ctr_y = boxs_delta.at<float>(i, 4 * j + 1);dst_scl_x = boxs_delta.at<float>(i, 4 * j + 2);dst_scl_y = boxs_delta.at<float>(i, 4 * j + 3);pred_ctr_x = dst_ctr_x*src_w + src_ctr_x;pred_ctr_y = dst_ctr_y*src_h + src_ctr_y;pred_w = exp(dst_scl_x) * src_w;pred_h = exp(dst_scl_y) * src_h;pre_box.at<float>(i, 4 * (j - 1) + 0) = ((pred_ctr_x - 0.5*(pred_w - 1)) - 1);pre_box.at<float>(i, 4 * (j - 1) + 1) = ((pred_ctr_y - 0.5*(pred_h - 1)) - 1);pre_box.at<float>(i, 4 * (j - 1) + 2) = ((pred_ctr_x + 0.5*(pred_w - 1)) - 1);pre_box.at<float>(i, 4 * (j - 1) + 3) = ((pred_ctr_y + 0.5*(pred_h - 1)) - 1);if (pre_box.at<float>(i, 4 * (j - 1) + 0) < 0) pre_box.at<float>(i, 4 * (j - 1) + 0) = 0;if (pre_box.at<float>(i, 4 * (j - 1) + 0) > (m_src.cols - 1)) pre_box.at<float>(i, 4 * (j - 1) + 0) = m_src.cols - 1;if (pre_box.at<float>(i, 4 * (j - 1) + 2) < 0) pre_box.at<float>(i, 4 * (j - 1) + 2) = 0;if (pre_box.at<float>(i, 4 * (j - 1) + 2) > (m_src.cols - 1)) pre_box.at<float>(i, 4 * (j - 1) + 2) = m_src.cols - 1;if (pre_box.at<float>(i, 4 * (j - 1) + 1) < 0) pre_box.at<float>(i, 4 * (j - 1) + 1) = 0;if (pre_box.at<float>(i, 4 * (j - 1) + 1) > (m_src.rows - 1)) pre_box.at<float>(i, 4 * (j - 1) + 1) = m_src.rows - 1;if (pre_box.at<float>(i, 4 * (j - 1) + 3) < 0) pre_box.at<float>(i, 4 * (j - 1) + 3) = 0;if (pre_box.at<float>(i, 4 * (j - 1) + 3) > (m_src.rows - 1)) pre_box.at<float>(i, 4 * (j - 1) + 3) = m_src.rows - 1;}}return pre_box;}} Mat Faster_rcnn::proposal_local_anchor() {Blob<float> shift;Mat shitf_x(feature_map_size.height, feature_map_size.width, CV_32SC1);Mat shitf_y(feature_map_size.height, feature_map_size.width, CV_32SC1);for (size_t i = 0; i < feature_map_size.width; i++){for (size_t j = 0; j < feature_map_size.height; j++){shitf_x.at<int>(j, i) = i * conf.feat_stride;shitf_y.at<int>(j, i) = j * conf.feat_stride;}}shift.Reshape(9, feature_map_size.width*feature_map_size.height, 4, 1);float *p = shift.mutable_cpu_diff(), *a = shift.mutable_cpu_data();for (int i = 0; i < feature_map_size.width*feature_map_size.height; i++){for (int j = 0; j < 9; j++){{size_t num = i * 4 + j * 4 * feature_map_size.width*feature_map_size.height;p[num + 0] = -shitf_x.at<int>(i % shitf_x.rows, i / shitf_x.rows);p[num + 2] = -shitf_x.at<int>(i % shitf_x.rows, i / shitf_x.rows);p[num + 1] = -shitf_y.at<int>(i % shitf_y.rows, i / shitf_y.rows);p[num + 3] = -shitf_y.at<int>(i % shitf_y.rows, i / shitf_y.rows);a[num + 0] = conf.anchor[j][0];a[num + 1] = conf.anchor[j][1];a[num + 2] = conf.anchor[j][2];a[num + 3] = conf.anchor[j][3];}}}shift.Update();Mat anchors(9 * feature_map_size.width*feature_map_size.height, 4, CV_32FC1);size_t num = 0;for (int i = 0; i < anchors.cols; i++){for (int j = 0; j < anchors.rows; j++){anchors.at<float>(j, i) = shift.data_at(num%shift.num(),(num - num / (shift.num() * shift.channels())*shift.num() * shift.channels()) / shift.num(),num / (shift.num() * shift.channels()), 0);num++;}}/*for (int i = 0; i < 4; i++){for (int k = 0; k < feature_map_size.width*feature_map_size.height; k++){for (int j = 0; j < 9; j++){anchors.at<float>(num%anchors.rows, num / anchors.rows) = shift.data_at(j, k, i, 0);num++;}}}*/return anchors; } void Faster_rcnn::prep_im_size() {int im_size_min = min(m_src.cols, m_src.rows);int im_size_max = max(m_src.cols, m_src.rows);im_scale = double(conf.target_size) / im_size_min;if (round(im_scale*im_size_max) > conf.maxsize){im_scale = double(conf.maxsize) / im_size_max;}input_geometry_ = Size(round(m_src.cols * im_scale), round(m_src.rows * im_scale));resize(m_src, im, input_geometry_); } bool Faster_rcnn::rpn_converttoboxs() {return true; } Mat Faster_rcnn::gettarget(Mat src) {m_src = src;//img=>blobimgtoblob();vector<abox> abox = forward();testdetection(abox);return m_src; } void Faster_rcnn::testdetection(vector<abox>& aboxes) {float scales = im_scale;Mat anchors(aboxes.size(), 4, CV_32FC1);//int a= faster_rcnn_net->blob_by_name("data")->count();//vector<string> a = rpn_net->blob_names();// int b = rpn_net->blob_by_name("conv5")->count();faster_rcnn_net->blob_by_name("data")->CopyFrom(*rpn_net->blob_by_name("conv5").get(), false, true);Blob<float>* input_layer = faster_rcnn_net->input_blobs()[1];int sub_blob_num = aboxes.size();input_layer->Reshape(sub_blob_num, 5, 1, 1);float* input_data = input_layer->mutable_cpu_data();int num = 0;for (auto ite = aboxes.begin(); ite != aboxes.end(); ite++){anchors.at<float>(num, 0) = ite->x1;anchors.at<float>(num, 1) = ite->y1;anchors.at<float>(num, 2) = ite->x2;anchors.at<float>(num, 3) = ite->y2;ite->score = 0;ite->x1 = (ite->x1 - 1) * scales + 1 - 1;ite->y1 = (ite->y1 - 1) * scales + 1 - 1;ite->x2 = (ite->x2 - 1) * scales + 1 - 1;ite->y2 = (ite->y2 - 1) * scales + 1 - 1;input_data[num * 5 + 0] = ite->score;input_data[num * 5 + 1] = ite->x1;input_data[num * 5 + 2] = ite->y1;input_data[num * 5 + 3] = ite->x2;input_data[num * 5 + 4] = ite->y2;num++;}const vector<Blob<float>*>& result = faster_rcnn_net->Forward();Blob<float>* scores = result[1];Blob<float>* box_deltas = result[0];box_deltas->data_at(0, 3, 0, 0);Mat boxs_delta(box_deltas->num(), box_deltas->channels(), CV_32FC1);for (int i = 0; i < box_deltas->num(); i++){for (int j = 0; j < box_deltas->channels(); j++){boxs_delta.at<float>(i, j) = box_deltas->data_at(i, j, 0, 0);}}Mat score(scores->num(), scores->channels() - 1, CV_32FC1);for (int i = 0; i < scores->num(); i++){for (int j = 0 + 1; j < scores->channels(); j++){score.at<float>(i, j - 1) = scores->data_at(i, j, 0, 0);}}Mat pred_boxes = bbox_tranform_inv(anchors, boxs_delta, "rcnn");std::map<int, vector<Rect>> classer;20為類別數(shù)///for (int i = 0; i < 20; i++){vector<Rect> r;vector<abox> aboxes;for (int j = 0; j < pred_boxes.rows; j++){abox tmp;tmp.x1 = pred_boxes.at<float>(j, i * 4);tmp.y1 = pred_boxes.at<float>(j, i * 4 + 1);tmp.x2 = pred_boxes.at<float>(j, i * 4 + 2);tmp.y2 = pred_boxes.at<float>(j, i * 4 + 3);tmp.score = score.at<float>(j, i);aboxes.push_back(tmp);}vector<int> vPick(aboxes.size());int nPick;nms(aboxes, 0.3, vPick, nPick);for (int i = 0; i < nPick; i++){if (aboxes[vPick[i]].score > 0.6){Rect rt;rt.x = aboxes[vPick[i]].x1;rt.y = aboxes[vPick[i]].y1;rt.width = aboxes[vPick[i]].x2 - aboxes[vPick[i]].x1 + 1;rt.height = aboxes[vPick[i]].y2 - aboxes[vPick[i]].y1 + 1;r.push_back(rt);}}classer.insert(std::pair<int, vector<Rect>>(i, r));}for (int i = 0; i < 20; i++){if (!classer[i].empty()){//printf("%d\t%d\n", i, classer[i].size());for (auto ite = classer[i].begin(); ite != classer[i].end(); ite++){rectangle(m_src, *ite, colortable[i]);putText(m_src, classname[i], Point(ite->x, ite->y), CV_FONT_HERSHEY_SIMPLEX, 0.5, colortable[i], 2, 2);}}} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542

Detect.cpp代碼如下:

#include "Faster_rcnn.h" //#include <opencv2\opencv.hpp>int main() {Mat im = imread("D:\\fast-rcnn-master\\data\\demo\\001551.jpg");Faster_rcnn detect(im);detect.init();while (1){Mat img = detect.gettarget(im);imshow("img", img);cvWaitKey(20);}return 0; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

6.將matlab版faster-rcnn的ZF網(wǎng)絡(luò)模型文件放到工程faster_rcnn_VOC0712_ZF文件夾下。https://github.com/ShaoqingRen/faster_rcnn?最后幾行給出了下載地址,下載地址為:https://pan.baidu.com/s/1hsFKmeK,下載完成放在自己的路徑下,并在Faster_rcnn.cpp文件bool Faster_rcnn::loadnet()函數(shù)中正確添加所需的模型文件路徑。?
7. build(生成)Detect模塊,如果是Debug模式,則可以在….\caffe-master\Build\x64\Debug\路徑下找到Detect.exe,?
cmd進(jìn)入到….\caffe-master\Build\x64\Debug\路徑,執(zhí)行?
..\x64\Debug>Detect.exe?
8.出來(lái)檢測(cè)結(jié)果:?
因圖片大小問(wèn)題不能上傳成功。

總結(jié)

以上是生活随笔為你收集整理的windows下实现c++版faster-rcnn的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。