Using vs2013 to call caffe's c++ interface directly under Windows

Keywords: OpenCV Windows network

1 Overview
Because of the most urgent need for flame detection, at first, we thought about using traditional machine learning algorithm to do it. Later, we found that the current flame detection is based on color. In actual scenes, there are often serious false alarms due to the external effects of street lights and the sun. Later, we decided to use in-depth learning for flame detection images to classify whether there is a fire or not. Disaster, training data set is tens of thousands of data crawled on the Internet by Zhang Xu, mainly based on the model of caffe framework training. If you want to apply it in engineering, you must think of using vs2013 to call directly the c++ interface of caffe to classify.
After checking on the internet, we can directly use vs2013 to call the caffe interface to classify the resources. The first link is as follows:
http://blog.csdn.net/sinat_30071459/article/details/50974695
http://blog.csdn.net/qq_14845119/article/details/52541622
Previously, Wang Xun had successfully invoked the interface of Caffe with the vs2013 project under Windows, mainly using the third Library of automatically downloaded ugetPackages and the Lib caffe.lib compiled and generated by Caffe itself and the dependent include header file. The following can be described in detail how to build and configure the project.
Build a console project of vs2013 and choose release x64. Because the header files and lib files of the third library and dll are shown in Figure 1 in NugetPackages, you can copy this folder directly into your new project, and then copy the Lib caffe. lib files generated during Caffe compilation and the corresponding include s into a new document. The folder caffelib is shown in Figure 2.
      
Figure 1
                           
Figure 2
                       
The include folder is copied directly from the caffe-master, and the Lib folder is saved from the ____________ Copied lib caffe.lib in caffe-master Build x64 Release
Note: The project you build should be set up under release (lib caffe.lib must be used in the same mode because caffe.sln regeneration is generated under release)
Note: In previous projects, caffe static libraries of release were used to debug programs. Later, it was found that caffe can compile debug version under Windows. In fact, the configuration under debug is almost the same as that under release, that is, the paths of loading library files are different from those of opencv in link input, and the others are the same.
2 Under Release:
1 Add:

D:\[Project]\classificationT\classificationT\caffelib\include
D:\[Project]\classificationT\classificationT\caffelib\include\caffe
D:\[Project]\classificationT\classificationT\caffelib\include\caffe\proto
D:\[Project]\classificationT\classificationT\NugetPackages\boost.1.59.0.0\lib\native\include 
D:\[Project]\classificationT\classificationT\NugetPackages\gflags.2.1.2.1\build\native\include
D:\[Project]\classificationT\classificationT\NugetPackages\glog.0.3.3.0\build\native\include
D:\[Project]\classificationT\classificationT\NugetPackages\protobuf-v120.2.6.1\build\native\include  
D:\[Project]\classificationT\classificationT\NugetPackages\OpenBLAS.0.2.14.1\lib\native\include  
D:\[Project]\classificationT\classificationT\NugetPackages\OpenCV.2.4.10\build\native\include
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include

2 Add:

D:\[Project]\classificationT\classificationT\caffelib\lib  
D:\[Project]\classificationT\classificationT\NugetPackages\boost_date_time-vc120.1.59.0.0\lib\native\address-model-64\lib  
D:\[Project]\classificationT\classificationT\NugetPackages\boost_filesystem-vc120.1.59.0.0\lib\native\address-model-64\lib  
D:\[Project]\classificationT\classificationT\NugetPackages\boost_system-vc120.1.59.0.0\lib\native\address-model-64\lib  
D:\[Project]\classificationT\classificationT\NugetPackages\boost_thread-vc120.1.59.0.0\lib\native\address-model-64\lib  
D:\[Project]\classificationT\classificationT\NugetPackages\boost_chrono-vc120.1.59.0.0\lib\native\address-model-64\lib  
D:\[Project]\classificationT\classificationT\NugetPackages\glog.0.3.3.0\build\native\lib\x64\v120\Release\dynamic  
D:\[Project]\classificationT\classificationT\NugetPackages\protobuf-v120.2.6.1\build\native\lib\x64\v120\Release  
D:\[Project]\classificationT\classificationT\NugetPackages\OpenBLAS.0.2.14.1\lib\native\lib\x64  
D:\[Project]\classificationT\classificationT\NugetPackages\hdf5-v120-complete.1.8.15.2\lib\native\lib\x64  
D:\[Project]\classificationT\classificationT\NugetPackages\OpenCV.2.4.10\build\native\lib\x64\v120\Release  
D:[Project]\classificationT\classificationT\NugetPackages\gflags.2.1.2.1\build\native\x64\v120\dynamic\Lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\lib\x64

3 Add in additional dependencies:

libcaffe.lib
libopenblas.dll.a
libprotobuf.lib
opencv_highgui2410.lib
opencv_core2410.lib
opencv_imgproc2410.lib
libglog.lib
gflags.lib
hdf5.lib
hdf5_hl.lib
cublas.lib
cuda.lib
curand.lib
cudart.lib

3 Under Debug:
1 Add:

D:\[Project]\classificationT\classificationT\caffelib\include
D:\[Project]\classificationT\classificationT\caffelib\include\caffe
D:\[Project]\classificationT\classificationT\caffelib\include\caffe\proto  
D:\[Project]\classificationT\classificationT\NugetPackages\boost.1.59.0.0\lib\native\include  
D:\[Project]\classificationT\classificationT\NugetPackages\gflags.2.1.2.1\build\native\include
D:\[Project]\classificationT\classificationT\NugetPackages\OpenBLAS.0.2.14.1\lib\native\include  
D:\[Project]\classificationT\classificationT\NugetPackages\OpenCV.2.4.10\build\native\include
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include

2 Add:

D:\[Project]\classificationT\classificationT\caffelib\lib  
D:\[Project]\classificationT\classificationT\NugetPackages\boost_date_time-vc120.1.59.0.0\lib\native\address-model-64\lib  
D:\[Project]\classificationT\classificationT\NugetPackages\boost_filesystem-vc120.1.59.0.0\lib\native\address-model-64\lib  
D:\[Project]\classificationT\classificationT\NugetPackages\boost_system-vc120.1.59.0.0\lib\native\address-model-64\lib  
D:\[Project]\classificationT\classificationT\NugetPackages\boost_thread-vc120.1.59.0.0\lib\native\address-model-64\lib  
D:\[Project]\classificationT\classificationT\NugetPackages\boost_chrono-vc120.1.59.0.0\lib\native\address-model-64\lib  
D:\[Project]\classificationT\classificationT\NugetPackages\glog.0.3.3.0\build\native\lib\x64\v120\ Debug\dynamic  
D:\[Project]\classificationT\classificationT\NugetPackages\protobuf-v120.2.6.1\build\native\lib\x64\v120\Debug  
D:\[Project]\classificationT\classificationT\NugetPackages\OpenBLAS.0.2.14.1\lib\native\lib\x64  
D:\[Project]\classificationT\classificationT\NugetPackages\hdf5-v120-complete.1.8.15.2\lib\native\lib\x64  
D:\[Project]\classificationT\classificationT\NugetPackages\OpenCV.2.4.10\build\native\lib\x64\v120\ Debug 
D:\[Project]\classificationT\classificationT\NugetPackages\gflags.2.1.2.1\build\native\x64\v120\dynamic\Lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\lib\x64

3 Add in additional dependencies:

libcaffe.lib
libopenblas.dll.a
libprotobuf.lib
opencv_highgui2410d.lib
opencv_core2410d.lib
opencv_imgproc2410d.lib
libglog.lib
gflags.lib
hdf5.lib
hdf5_hl.lib
cublas.lib
cuda.lib
curand.lib
cudart.lib

4 Add in the precompiler:
C/C++-> Preprocessor-> Preprocessor Definition, add here:

  _CRT_SECURE_NO_WARNINGS

5 classification.cpp as demo
The code in the new project can refer to caffe's official file, which has classification.cpp as demo. Here I put the Classifier class definition in a separate file. My code will be stored in the folder.
             
The relationships between the parameters in the Classifier class constructor are as follows:

  • string model_file -- deploy.prototxt (network structure file name)
  • string trained_file -- network.caffemodel (training weight file name)
  • string mean_file -- mean.binaryprot (mean file name)
  • stringlabel_file -- labels.txt

6 head.h header file
In my own project, I also define a head.h as follows, and then include the header file in the. cpp file where the main function is located. If the header file does not contain the header file, there will be "Check failed: registry. count (t ype) === 1 (0 vs. 1) Unknown layer t ype", jump to the CHECK of net.cpp (Caffe:: root_solver()| | root_net_)< "root_net_ needs to be needed" Et for all non-root solvers"
The content of head.h is as follows:
 

 #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/pooling_layer.hpp"
  #include "caffe/layers/lrn_layer.hpp"
  #include "caffe/layers/softmax_layer.hpp"
  namespace caffe
  {
  extern INSTANTIATE_CLASS(InputLayer);
  extern INSTANTIATE_CLASS(InnerProductLayer);
  extern INSTANTIATE_CLASS(DropoutLayer);
  extern INSTANTIATE_CLASS(ConvolutionLayer);
  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);
  }

Note: Corresponding adjustments should be made according to their own specific paths.
Modify classification.cpp
According to the above steps, the new project should be able to run the program. Later, when I transplanted the project to the previous project of abnormal behavior analysis, some incompatible variables and references appeared. The main errors were some variables defined at the bottom of caffe and some conflicts between the references of boost library and MFC and boost library of c++. The modifications were as follows:
7 dll
The required DLLs are in the caffe-master Build x64 Release or Debug file after successful caffe compilation.

Posted by Cogen2 on Wed, 17 Apr 2019 10:06:33 -0700