Commit e4e374e2 authored by Wangyida's avatar Wangyida

add FindCaffe cmake file for necessary dependency

parent 4483d00c
set(the_description "CNN for 3D object recognition and pose estimation including a completed Sphere View on 3D objects") set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR})
ocv_define_module(cnn_3dobj opencv_core opencv_imgproc opencv_viz opencv_highgui caffe protobuf glog OPTIONAL WRAP python)
target_link_libraries(opencv_cnn_3dobj caffe protobuf glog) find_package(Caffe)
if(Caffe_FOUND)
message(STATUS "Caffe: YES")
set(HAVE_CAFFE 1)
else()
message(STATUS "Caffe: NO")
endif()
if(HAVE_CAFFE)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cnn_3dobj_config.hpp.in
${CMAKE_CURRENT_SOURCE_DIR}/include/opencv2/cnn_3dobj_config.hpp @ONLY)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
if(${Caffe_FOUND})
include_directories(${Caffe_INCLUDE_DIR})
endif()
set(the_description "CNN for 3D object recognition and pose estimation including a completed Sphere View on 3D objects")
ocv_define_module(cnn_3dobj opencv_core opencv_imgproc opencv_viz opencv_highgui OPTIONAL WRAP python)
if(${Caffe_FOUND})
target_link_libraries(opencv_cnn_3dobj ${Caffe_LIBS})
endif()
endif()
# Caffe package for CNN Triplet training
unset(Caffe_FOUND)
find_path(Caffe_INCLUDE_DIR NAMES caffe/caffe.hpp caffe/common.hpp caffe/net.hpp caffe/proto/caffe.pb.h caffe/util/io.hpp caffe/vision_layers.hpp
HINTS
/usr/local/include)
find_library(Caffe_LIBS NAMES caffe
HINTS
/usr/local/lib)
if(Caffe_LIBS AND Caffe_INCLUDE_DIR)
set(Caffe_FOUND 1)
endif()
#ifndef __OPENCV_CNN_3DOBJ_CONFIG_HPP__
#define __OPENCV_CNN_3DOBJ_CONFIG_HPP__
// HAVE CAFFE
#cmakedefine HAVE_CAFFE
#endif
...@@ -58,12 +58,16 @@ the use of this software, even if advised of the possibility of such damage. ...@@ -58,12 +58,16 @@ the use of this software, even if advised of the possibility of such damage.
#include <tr1/memory> #include <tr1/memory>
#include <dirent.h> #include <dirent.h>
#define CPU_ONLY #define CPU_ONLY
#include "caffe/blob.hpp"
#include "caffe/common.hpp" #ifdef HAVE_CAFFE
#include "caffe/net.hpp" #include <caffe/blob.hpp>
#include "caffe/proto/caffe.pb.h" #include <caffe/common.hpp>
#include "caffe/util/io.hpp" #include <caffe/net.hpp>
#include "caffe/vision_layers.hpp" #include <caffe/proto/caffe.pb.h>
#include <caffe/util/io.hpp>
#include <caffe/vision_layers.hpp>
#endif
#include "opencv2/viz/vizcore.hpp" #include "opencv2/viz/vizcore.hpp"
#include "opencv2/highgui.hpp" #include "opencv2/highgui.hpp"
#include "opencv2/highgui/highgui_c.h" #include "opencv2/highgui/highgui_c.h"
...@@ -132,10 +136,10 @@ class CV_EXPORTS_W IcoSphere ...@@ -132,10 +136,10 @@ class CV_EXPORTS_W IcoSphere
}; };
class CV_EXPORTS_W Classification class CV_EXPORTS_W Feature
{ {
private: private:
caffe::shared_ptr<caffe::Net<float> > net_; caffe::Net<float>* net_;
cv::Size input_geometry_; cv::Size input_geometry_;
int num_channels_; int num_channels_;
cv::Mat mean_; cv::Mat mean_;
...@@ -150,7 +154,7 @@ class CV_EXPORTS_W Classification ...@@ -150,7 +154,7 @@ class CV_EXPORTS_W Classification
/** @brief Convert the input image to the input image format of the network. /** @brief Convert the input image to the input image format of the network.
*/ */
public: public:
Classification(); Feature();
void list_dir(const char *path,std::vector<string>& files,bool r); void list_dir(const char *path,std::vector<string>& files,bool r);
/** @brief Get the file name from a root dictionary. /** @brief Get the file name from a root dictionary.
*/ */
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
* *
*/ */
#define HAVE_CAFFE
#include <opencv2/cnn_3dobj.hpp> #include <opencv2/cnn_3dobj.hpp>
#include <iomanip> #include <iomanip>
using namespace cv; using namespace cv;
...@@ -50,7 +51,7 @@ int main(int argc, char** argv) ...@@ -50,7 +51,7 @@ int main(int argc, char** argv)
"{device | CPU | device}" "{device | CPU | device}"
"{dev_id | 0 | dev_id}"; "{dev_id | 0 | dev_id}";
cv::CommandLineParser parser(argc, argv, keys); cv::CommandLineParser parser(argc, argv, keys);
parser.about("Demo for Sphere View data generation"); parser.about("Demo for object data classification and pose estimation");
if (parser.has("help")) if (parser.has("help"))
{ {
parser.printMessage(); parser.printMessage();
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
* *
*/ */
#define HAVE_CAFFE
#include <opencv2/cnn_3dobj.hpp> #include <opencv2/cnn_3dobj.hpp>
#include <opencv2/viz/vizcore.hpp> #include <opencv2/viz/vizcore.hpp>
#include <iostream> #include <iostream>
......
...@@ -6,8 +6,8 @@ namespace cv ...@@ -6,8 +6,8 @@ namespace cv
{ {
namespace cnn_3dobj namespace cnn_3dobj
{ {
Classification::Classification(){}; Feature::Feature(){};
void Classification::list_dir(const char *path,vector<string>& files,bool r) void Feature::list_dir(const char *path,vector<string>& files,bool r)
{ {
DIR *pDir; DIR *pDir;
struct dirent *ent; struct dirent *ent;
...@@ -25,7 +25,7 @@ namespace cnn_3dobj ...@@ -25,7 +25,7 @@ namespace cnn_3dobj
if(r) if(r)
{ {
sprintf(childpath, "%s/%s", path, ent->d_name); sprintf(childpath, "%s/%s", path, ent->d_name);
Classification::list_dir(childpath,files,false); Feature::list_dir(childpath,files,false);
} }
} }
else else
...@@ -36,7 +36,7 @@ namespace cnn_3dobj ...@@ -36,7 +36,7 @@ namespace cnn_3dobj
sort(files.begin(),files.end()); sort(files.begin(),files.end());
}; };
void Classification::NetSetter(const string& model_file, const string& trained_file, const string& mean_file, const string& cpu_only, int device_id) void Feature::NetSetter(const string& model_file, const string& trained_file, const string& mean_file, const string& cpu_only, int device_id)
{ {
if (strcmp(cpu_only.c_str(), "CPU") == 0) if (strcmp(cpu_only.c_str(), "CPU") == 0)
{ {
...@@ -48,7 +48,7 @@ namespace cnn_3dobj ...@@ -48,7 +48,7 @@ namespace cnn_3dobj
caffe::Caffe::SetDevice(device_id); caffe::Caffe::SetDevice(device_id);
} }
/* Load the network. */ /* Load the network. */
net_.reset(new Net<float>(model_file, TEST)); net_ = new Net<float>(model_file, TEST);
net_->CopyTrainedLayersFrom(trained_file); net_->CopyTrainedLayersFrom(trained_file);
CHECK_EQ(net_->num_inputs(), 1) << "Network should have exactly one input."; CHECK_EQ(net_->num_inputs(), 1) << "Network should have exactly one input.";
CHECK_EQ(net_->num_outputs(), 1) << "Network should have exactly one output."; CHECK_EQ(net_->num_outputs(), 1) << "Network should have exactly one output.";
...@@ -61,14 +61,14 @@ namespace cnn_3dobj ...@@ -61,14 +61,14 @@ namespace cnn_3dobj
SetMean(mean_file); SetMean(mean_file);
}; };
void Classification::GetLabellist(const std::vector<string>& name_gallery) void Feature::GetLabellist(const std::vector<string>& name_gallery)
{ {
for (unsigned int i = 0; i < name_gallery.size(); ++i) for (unsigned int i = 0; i < name_gallery.size(); ++i)
labels_.push_back(name_gallery[i]); labels_.push_back(name_gallery[i]);
}; };
/* Return the indices of the top N values of vector v. */ /* Return the indices of the top N values of vector v. */
std::vector<int> Classification::Argmax(const std::vector<float>& v, int N) std::vector<int> Feature::Argmax(const std::vector<float>& v, int N)
{ {
std::vector<std::pair<float, int> > pairs; std::vector<std::pair<float, int> > pairs;
for (size_t i = 0; i < v.size(); ++i) for (size_t i = 0; i < v.size(); ++i)
...@@ -81,7 +81,7 @@ namespace cnn_3dobj ...@@ -81,7 +81,7 @@ namespace cnn_3dobj
}; };
//Return the top N predictions. //Return the top N predictions.
std::vector<std::pair<string, float> > Classification::Classify(const cv::Mat& reference, const cv::Mat& target, int N) std::vector<std::pair<string, float> > Feature::Classify(const cv::Mat& reference, const cv::Mat& target, int N)
{ {
std::vector<float> output; std::vector<float> output;
for (int i = 0; i < reference.rows; i++) for (int i = 0; i < reference.rows; i++)
...@@ -102,7 +102,7 @@ namespace cnn_3dobj ...@@ -102,7 +102,7 @@ namespace cnn_3dobj
}; };
/* Load the mean file in binaryproto format. */ /* Load the mean file in binaryproto format. */
void Classification::SetMean(const string& mean_file) void Feature::SetMean(const string& mean_file)
{ {
BlobProto blob_proto; BlobProto blob_proto;
ReadProtoFromBinaryFileOrDie(mean_file.c_str(), &blob_proto); ReadProtoFromBinaryFileOrDie(mean_file.c_str(), &blob_proto);
...@@ -130,7 +130,7 @@ namespace cnn_3dobj ...@@ -130,7 +130,7 @@ namespace cnn_3dobj
mean_ = cv::Mat(input_geometry_, mean.type(), channel_mean); mean_ = cv::Mat(input_geometry_, mean.type(), channel_mean);
}; };
void Classification::FeatureExtract(InputArray inputimg, OutputArray feature, bool mean_subtract, std::string featrue_blob) void Feature::FeatureExtract(InputArray inputimg, OutputArray feature, bool mean_subtract, std::string featrue_blob)
{ {
Blob<float>* input_layer = net_->input_blobs()[0]; Blob<float>* input_layer = net_->input_blobs()[0];
input_layer->Reshape(1, num_channels_, input_layer->Reshape(1, num_channels_,
...@@ -183,7 +183,7 @@ namespace cnn_3dobj ...@@ -183,7 +183,7 @@ namespace cnn_3dobj
* don't need to rely on cudaMemcpy2D. The last preprocessing * don't need to rely on cudaMemcpy2D. The last preprocessing
* operation will write the separate channels directly to the input * operation will write the separate channels directly to the input
* layer. */ * layer. */
void Classification::WrapInputLayer(std::vector<cv::Mat>* input_channels) void Feature::WrapInputLayer(std::vector<cv::Mat>* input_channels)
{ {
Blob<float>* input_layer = net_->input_blobs()[0]; Blob<float>* input_layer = net_->input_blobs()[0];
int width = input_layer->width(); int width = input_layer->width();
...@@ -197,7 +197,7 @@ namespace cnn_3dobj ...@@ -197,7 +197,7 @@ namespace cnn_3dobj
} }
}; };
void Classification::Preprocess(const cv::Mat& img, void Feature::Preprocess(const cv::Mat& img,
std::vector<cv::Mat>* input_channels, bool mean_subtract) std::vector<cv::Mat>* input_channels, bool mean_subtract)
{ {
/* Convert the input image to the input image format of the network. */ /* Convert the input image to the input image format of the network. */
......
...@@ -42,6 +42,7 @@ the use of this software, even if advised of the possibility of such damage. ...@@ -42,6 +42,7 @@ the use of this software, even if advised of the possibility of such damage.
#ifndef __OPENCV_CNN_3DOBJ_PRECOMP_HPP__ #ifndef __OPENCV_CNN_3DOBJ_PRECOMP_HPP__
#define __OPENCV_CNN_3DOBJ_PRECOMP_HPP__ #define __OPENCV_CNN_3DOBJ_PRECOMP_HPP__
#include <opencv2/cnn_3dobj_config.hpp>
#include <opencv2/cnn_3dobj.hpp> #include <opencv2/cnn_3dobj.hpp>
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment