├── Caffe_Net.cpp ├── imgs ├── 000456.jpg ├── 000542.jpg ├── 001150.jpg ├── 001763.jpg ├── 004545.jpg ├── result_000456.jpg ├── result_000542.jpg ├── result_001150.jpg ├── result_001763.jpg └── result_004545.jpg ├── testImage.txt ├── CMakeLists.txt ├── config.h ├── register.h ├── test_faster_rcnn.cpp ├── Caffe_Net.h └── README.md /Caffe_Net.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanglaplace/Faster_rcnn_Cplusplus_vs2013/HEAD/Caffe_Net.cpp -------------------------------------------------------------------------------- /imgs/000456.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanglaplace/Faster_rcnn_Cplusplus_vs2013/HEAD/imgs/000456.jpg -------------------------------------------------------------------------------- /imgs/000542.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanglaplace/Faster_rcnn_Cplusplus_vs2013/HEAD/imgs/000542.jpg -------------------------------------------------------------------------------- /imgs/001150.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanglaplace/Faster_rcnn_Cplusplus_vs2013/HEAD/imgs/001150.jpg -------------------------------------------------------------------------------- /imgs/001763.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanglaplace/Faster_rcnn_Cplusplus_vs2013/HEAD/imgs/001763.jpg -------------------------------------------------------------------------------- /imgs/004545.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanglaplace/Faster_rcnn_Cplusplus_vs2013/HEAD/imgs/004545.jpg -------------------------------------------------------------------------------- /imgs/result_000456.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanglaplace/Faster_rcnn_Cplusplus_vs2013/HEAD/imgs/result_000456.jpg -------------------------------------------------------------------------------- /imgs/result_000542.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanglaplace/Faster_rcnn_Cplusplus_vs2013/HEAD/imgs/result_000542.jpg -------------------------------------------------------------------------------- /imgs/result_001150.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanglaplace/Faster_rcnn_Cplusplus_vs2013/HEAD/imgs/result_001150.jpg -------------------------------------------------------------------------------- /imgs/result_001763.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanglaplace/Faster_rcnn_Cplusplus_vs2013/HEAD/imgs/result_001763.jpg -------------------------------------------------------------------------------- /imgs/result_004545.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanglaplace/Faster_rcnn_Cplusplus_vs2013/HEAD/imgs/result_004545.jpg -------------------------------------------------------------------------------- /testImage.txt: -------------------------------------------------------------------------------- 1 | ./imgs/000456.jpg 2 | ./imgs/000542.jpg 3 | ./imgs/001150.jpg 4 | ./imgs/001763.jpg 5 | ./imgs/004545.jpg 6 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.9) 2 | project(test_faster_rcnn) 3 | 4 | #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 | set(CMAKE_CXX_STANDARD 11) 6 | 7 | find_package(OpenCV) 8 | 9 | find_package(Caffe) 10 | #message(FATAL_ERROR ${Caffe_INCLUDE_DIRS}) 11 | include_directories(${Caffe_INCLUDE_DIRS}) 12 | 13 | set(SOURCE_FILES test_faster_rcnn.cpp Caffe_Net.cpp Caffe_Net.h config.h) 14 | add_executable(test_faster_rcnn ${SOURCE_FILES}) 15 | 16 | target_link_libraries(test_faster_rcnn ${OpenCV_LIBS} ) 17 | target_link_libraries(test_faster_rcnn ${Caffe_LIBRARIES}) 18 | -------------------------------------------------------------------------------- /config.h: -------------------------------------------------------------------------------- 1 | #ifndef _CONFIG_H_ 2 | #define _CONFIG_H_ 3 | 4 | 5 | #include 6 | #include 7 | 8 | typedef struct config{ 9 | int per_nms_topN; 10 | int after_nms_topN; 11 | int target_size; 12 | float nms_overlap_thres_proposal; 13 | float num_overlap_thres_fcnn; 14 | int feat_stride; 15 | int max_size; 16 | int anchors[9][4]; 17 | int test_min_box_size; 18 | double test_fcnn_thres_score; 19 | config(){ 20 | per_nms_topN = 6000; 21 | after_nms_topN = 300; 22 | target_size = 600; 23 | num_overlap_thres_fcnn = 0.3; 24 | max_size = 1000; 25 | feat_stride = 16; 26 | test_min_box_size = 16; 27 | test_fcnn_thres_score = 0.6; 28 | nms_overlap_thres_proposal = 0.70; 29 | int temp[9][4] = { 30 | { -83, -39, 100, 56 }, 31 | { -175, -87, 192, 104 }, 32 | { -359, -183, 376, 200 }, 33 | { -55, -55, 72, 72 }, 34 | { -119, -119, 136, 136 }, 35 | { -247, -247, 264, 264 }, 36 | { -35, -79, 52, 96 }, 37 | { -79, -167, 96, 184 }, 38 | { -167, -343, 184, 360 } 39 | }; 40 | memcpy(anchors, temp, 9 * 4 * sizeof(int)); 41 | } 42 | 43 | }config; 44 | 45 | typedef struct aboxes{ 46 | float x1; 47 | float y1; 48 | float x2; 49 | float y2; 50 | float score; 51 | 52 | }aboxes; 53 | 54 | 55 | 56 | 57 | #endif -------------------------------------------------------------------------------- /register.h: -------------------------------------------------------------------------------- 1 | #ifndef _REGISTER_H_ 2 | #define _REGISTER_H_ 3 | #include "caffe/common.hpp" 4 | #include "caffe/layers/input_layer.hpp" 5 | #include "caffe/layers/inner_product_layer.hpp" 6 | #include "caffe/layers/dropout_layer.hpp" 7 | #include "caffe/layers/conv_layer.hpp" 8 | #include "caffe/layers/relu_layer.hpp" 9 | #include "caffe/layers/reshape_layer.hpp" 10 | #include "caffe/layers/pooling_layer.hpp" 11 | #include "caffe/layers/lrn_layer.hpp" 12 | #include "caffe/layers/softmax_layer.hpp" 13 | #include "caffe/layers/roi_pooling_layer.hpp" 14 | 15 | namespace caffe 16 | { 17 | extern INSTANTIATE_CLASS(InputLayer); 18 | extern INSTANTIATE_CLASS(InnerProductLayer); 19 | extern INSTANTIATE_CLASS(DropoutLayer); 20 | extern INSTANTIATE_CLASS(ConvolutionLayer); 21 | extern INSTANTIATE_CLASS(ROIPoolingLayer); 22 | REGISTER_LAYER_CLASS(Convolution); 23 | extern INSTANTIATE_CLASS(ReLULayer); 24 | REGISTER_LAYER_CLASS(ReLU); 25 | extern INSTANTIATE_CLASS(PoolingLayer); 26 | REGISTER_LAYER_CLASS(Pooling); 27 | extern INSTANTIATE_CLASS(LRNLayer); 28 | REGISTER_LAYER_CLASS(LRN); 29 | extern INSTANTIATE_CLASS(SoftmaxLayer); 30 | REGISTER_LAYER_CLASS(Softmax); 31 | extern INSTANTIATE_CLASS(ReshapeLayer); 32 | // REGISTER_LAYER_CLASS(Reshape); 33 | } 34 | #endif -------------------------------------------------------------------------------- /test_faster_rcnn.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Caffe_Net.h" 4 | using namespace std; 5 | 6 | //matlab model and net prototxt file . 7 | const string rpn_prototxt_file = "./models/proposal_test.prototxt"; 8 | const string rpn_model_file = "./models/proposal_final"; 9 | const string fcnn_prototxt_file = "./models/detection_test.prototxt"; 10 | const string fcnn_model_file = "./models/detection_final"; 11 | 12 | //mean_image.bmp is convert from the model.mat in matlab model directory 13 | //you can save mean_image by command imwrite(uint8(proposal_detection_model.image_means),"mean_image.bmp") 14 | const string mean_image_file = "./models/mean_image.bmp"; 15 | 16 | 17 | 18 | int main(){ 19 | 20 | Caffe_Net net_(rpn_prototxt_file, rpn_model_file, fcnn_prototxt_file, fcnn_model_file, mean_image_file); 21 | ifstream fin("testImage.txt",std::ios_base::in); 22 | string lines; 23 | while(getline(fin,lines)){ 24 | cout< 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "config.h" 9 | #include "register.h" 10 | 11 | using namespace cv; 12 | using caffe::Blob; 13 | using caffe::Caffe; 14 | using caffe::Net; 15 | using caffe::Layer; 16 | using caffe::Solver; 17 | using caffe::shared_ptr; 18 | using caffe::string; 19 | using caffe::Timer; 20 | using caffe::vector; 21 | using caffe::ostringstream; 22 | 23 | 24 | class Caffe_Net{ 25 | private: 26 | Mat src_image, mean_image, net_input_image; 27 | Size feature_map_size_, net_input_image_size_; 28 | shared_ptr >rpn_net_, fcnn_net_; 29 | double im_scale_; 30 | 31 | public: 32 | config conf; 33 | 34 | public: 35 | Caffe_Net(const string rpn_prototxt, const string rpn_model, const string fcnn_prototxt, const string fcnn_model, const string mean_image_file); 36 | Mat fasterrcnn(Mat src); 37 | void process(Mat cv_image); 38 | double prep_im_for_blod_size(int height, int width, int target_size, int max_size); 39 | vector forward(); 40 | Mat proposal_local_anchor(); 41 | Mat proposal_local_anchor1(); 42 | Mat bbox_tranform_inv(Mat anchors, Mat boxs_delta, string type); 43 | Mat get_rpn_score(Blob*result, int w, int h); 44 | void filter_boxes(Mat& pre_box, Mat& score, vector& box); 45 | void nms(vectorinput_boxes, double overlap, vector&vPick, int&nPick); 46 | void boxes_filter(vector&box_, int nPick, vectorbox, vectorvPick); 47 | std::map > fcnn_detect(vector&box); 48 | }; 49 | 50 | 51 | 52 | #endif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Faster-Rcnn-cplusplus 2 | Faster-rcnn cplusplus in windows with visual studio 2013 the project is according to the http://blog.csdn.net/oYangZi12/article/details/53290426?locationNum=5&fps=1 3 | 4 |  you can test with python model according https://github.com/zhanglaplace/faster-rcnn-cplusplus2 5 | 6 | # Platform 7 | You need a VC compiler to build these project, Visual Studio 2013 Community should be fine. You can download from https://www.visualstudio.com/downloads/. 8 | 9 | # Caffe 10 | i use the Microsoft version , ![https://github.com/Microsoft/caffe](https://github.com/Microsoft/caffe) . if you want to use the faster-rcnn you need to add some others file to libcaffe project , in fact ,i add all of the head file in /include/caffe/ 11 | and all of the source file and cu file in /src/caffe/ to libcaffe project and rebuild it;Specifically,See: http://www.cnblogs.com/LaplaceAkuir/p/6445189.html 12 | 13 | # Result 14 | it's a little different from the matlab version .and it cost much time. 15 | 16 | In My compute(GTX760 GPU) a picture of size(375\*500\*3)cost 246ms . 17 | ![image](https://github.com/zhanglaplace/Faster_rcnn_Cplusplus_vs2013/blob/master/imgs/result_004545.jpg) 18 | ![image](https://github.com/zhanglaplace/Faster_rcnn_Cplusplus_vs2013/blob/master/imgs/result_001150.jpg) 19 | ![image](https://github.com/zhanglaplace/Faster_rcnn_Cplusplus_vs2013/blob/master/imgs/result_000456.jpg) 20 | ![image](https://github.com/zhanglaplace/Faster_rcnn_Cplusplus_vs2013/blob/master/imgs/result_000542.jpg) 21 | ![image](https://github.com/zhanglaplace/Faster_rcnn_Cplusplus_vs2013/blob/master/imgs/result_001763.jpg) 22 | 23 | # Mean_images 24 | mean_images is converte from the model.mat in matlab . you can save the mean_image by command imwrite(uint8(proposal_detection_model.image_means),'mean_image.bmp') 25 | or load the model.mat and save it. 26 | 27 | # Faster-RCNN Model 28 | We choose the matlab model file and the prototxt file. in matlab ,faster-rcnn divided into two parts, 29 | rpn_net and faster-rcnn_net;thus there are two models and two prototxt files in matlab;these project is 30 | just a test demo,if you want to train your own model , prehaps you need do it on matlab-faster-rcnn. 31 | and put the model in these project,you can download the model:![http://pan.baidu.com/s/1dF88JvV 32 | ](http://pan.baidu.com/s/1dF88JvV) 33 | 34 | # Caffe-3rdparty 35 | i have sorted out the thrid party depenendent library files in faster_3rdparty,![http://pan.baidu.com/s/1qYttnsS](http://pan.baidu.com/s/1qYttnsS) 36 | password:*d0ud*,it is a release version , you can put it in the caffe-master folder. and we should create a new 37 | porject to test faster-rcnn in caffe-master/windows/folder.As you can see, you can compile the project 38 | directly. What you need to do is only change some necessary paths. All the code has been tested and the test 39 | results are also included. 40 | 41 | # Something else 42 | if you want to test the project using your own model ,please modify the classNum to your class number and each class' name in caffe_net.cpp. 43 | finally,wish it can help you .if it's helpful to you ,please give me a star thanks~ 44 | 45 | 46 | 47 | --------------------------------------------------------------------------------