windows下實現c++版faster-rcnn
本文主要參考: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中libcaffe的相應位置加入roi-pooling層對應的cpp,hpp,cu檔案重新編譯caffe)。因roi-pooling層已經在微軟版本的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(生成各模組),並對整個caffe工程進行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()
{
//init
Caffe::set_mode(Caffe::CPU);
loadnet();
return true;
}
bool Faster_rcnn::loadnet()
{
//load net
rpn_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()
{
//forward
const 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 anchors
feature_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
相關推薦
[目標檢測]windows下實現c++版faster-rcnn
本版本根據windows下matlab版改寫而來。工程可到http://download.csdn.net/detail/oyangzi12/9692597 下載。程式預設使用GPU模式,如果沒有GPU只需在程式中將caffe設定為cpu模式。使用方法:1、配置opencv,
windows下實現c++版faster-rcnn
本文主要參考:http://blog.csdn.net/oYangZi12/article/details/53290426?locationNum=5&fps=1 1.下載微軟提供的caffe(https://github.com/Microsoft/c
windows下的純c++版 Faster R-CNN
效果如下圖: 同時版本經過大幅度效能優化,在GeForce GTX TITAN X顯示卡上,對於VGG16模型的速度是16.6fps左右。 *****************************分割線*************************
一步步實現windows版ijkplayer系列文章之四——windows下編譯ijkplyer版ffmpeg
windows下編譯ijkplyer版ffmpeg ijkplayer版本ffmpeg原始碼說明 ijkplayer使用了ffmpeg 3.4 release(接下來將此版本ffmpeg叫做ijkplayer-ffmpeg),ijkplayer fork版本最後一個官方的commit詳細資訊如下: 提交:
Windows下執行C語言版Word2Vec訓練詞向量
在Word2vec模型中,演算法可以通過無監督的方法為每個詞計算出一個d維的向量,即將每個詞對映為d維的空間中的一個點,d維空間中點之間的距離(即每個詞對應的d維向量的距離)可反映詞之間的相似性。 dav/word2vec是一個經典的利用多執行緒訓練詞向量的程
C++版Faster R-CNN(caffe自定義RPN層實現) 個人見解 問題分析記錄
本文是在實現https://blog.csdn.net/zxj942405301/article/details/72775463這篇部落格裡面的程式碼時遇到的一些問題,然後理解分析記錄在此。前面的程式碼部分按照步驟進行應該不會有錯,有一個提示的地方是在caffe.proto
關於windows下的c++的rand函數詳解
c++ rand rand不是真正的隨機函數,是偽隨機函數srand設置隨機函數種子srand設置一個參數後,每調用一次rand產生一個隨機數srand(1000001)rand – 21589rand – 29335rand – 14469 srand參數相同,多次調用rand依次返回的
windows下實現linux的遠程訪問
你們 用戶名 成功 href get 新建 介紹 lan 優勢 在網絡性能、安全性、可管理性上,Linux有著其他系統無法比擬的強大優勢,而服務器對這些方面要求特別高,因此Linux常常被用來做服務器使用。而當我們需要維護linux服務器的時候,就需要遠程訪問linux
caffe版faster-RCNN環境搭建
mit argument .com tail uil check otto pip __init__ faster-rcnn提出論文: 《Faster R-CNN: Towards Real-Time Object Detection with Region Propo
windows下用C++獲取本機IP地址
ali 返回 轉換成 data info AC ctrl nag != BSTR CamUtilsCtrl::GET_TERM_IP(void){ AFX_MANAGE_STATE(AfxGetStaticModuleState()); CString strResult
redis幾種模式的部署(Windows下實現)
原文地址:https ://www.cnblogs.com/yu421/p/8081544.html <參考> http://www.cnblogs.com/ruiati/p/6374152.html 1.自行下載redis客戶端.redis官方不支援Windows系統,所以官網
Elasticsearch6在windows下使用瀏覽器版head外掛
1,Windows下安裝ES,啟動ES 第一步,安裝JDK ,配置好 JAVA_HOME 即可。(學過Java的都會安裝jdk這裡不再過多闡述) 第二步 ,下載Elasticsearch,你可以去Es官網下載最新版本,我用的是6.24 進入官網後,點選右上角的
使用pytorch版faster-rcnn訓練自己資料集
使用pytorch版faster-rcnn訓練自己資料集 引言 faster-rcnn pytorch程式碼下載 訓練自己資料集 接下來工作 參考文獻 引言 最近在復現目標檢測程式碼(師兄強烈推薦F
LiteIDE 在 Windows 下實現go語言智慧提示程式碼補全
本文以 Windows 8、10 64 位為環境,go1.11.1.windows-amd64 和 liteidex27.2.1.windows-qt5 為例。 成功搭建開發環境後,發現 LiteIDE 沒有程式碼智慧提示,開發程式碼就特別麻煩~ 在 Github 下載 gocod
Windows下實現mysql定時備份
1、寫MySQL備份bat處理 @echo off set "yMd=%date:~,4%%date:~5,2%%date:~8,2%" set "hms=%time:~,2%%time:~3,2%%time:~6,2%" "C:/Program Files (x86)/MySQL/MySQL
tf版faster rcnn執行
資料集製作 資料集格式: 與pascal voc格式一樣, 分為Annotations和JPEGImages xml檔案有這些內容就可以了 <annotation> <folder>VOC2007</folder>
redis sentinel部署(Windows下實現)
一、準備條件 1、作業系統:win7 2、redis版本:redis-2.8.19 二、下載redis並解壓 1、下載 由於redis官方並不支援windows作業系統,所以官網上是下不到的,需要到gitlab上下載,下載地址如下: https://github.co
windows下實現mysql主從複製
MySQL的主從複製是通過binlog日誌來實現的,主從複製中的“主”指的是MySQL主伺服器上的資料庫,“從”指的是MySQL從伺服器上的資料庫,且這種複製是基於資料庫級別的,為此從伺服器中的資料庫名稱必須和主伺服器中的資料庫名稱保持一致,那麼,要想實現主從複製,我們至少要有兩個MySQL伺服器(
如何在windows下使用c++11標準 (Dev C++切換 C++ 11 環境)
安裝之後需要簡單設定一下。 設定方法:Tools - Compiler Options - Settings - Code Generation Lauguage standaerd(-std)選項下拉選單中選擇 GNU C++11 Dev-Cpp 5.4.2
Eclipse在Windows下編寫C語言的HelloWorld程式
Eclipse只是一個開發環境,不具有編譯功能,為了執行C程式,需要安裝和配置GCC或G++編譯器。 本部落格已給了Eclipse的安裝和配置,此處不再贅述。只給出GCC的安裝和配置。 【一】GCC編