From a0d5630117c6a2afebf3bb0b10bc8821e5dc56ef Mon Sep 17 00:00:00 2001 From: Wangyida Date: Sun, 16 Aug 2015 10:57:05 +0800 Subject: [PATCH] add test for feature extraction using CNN --- modules/cnn_3dobj/README.md | 4 +- .../test/test_cnn_3dobj_feature_extract.cpp | 70 +++++++++++++++++++ modules/cnn_3dobj/test/test_main.cpp | 3 + modules/cnn_3dobj/test/test_precomp.hpp | 18 +++++ 4 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 modules/cnn_3dobj/test/test_cnn_3dobj_feature_extract.cpp create mode 100644 modules/cnn_3dobj/test/test_main.cpp create mode 100644 modules/cnn_3dobj/test/test_precomp.hpp diff --git a/modules/cnn_3dobj/README.md b/modules/cnn_3dobj/README.md index ae180ca42..09512c0b1 100644 --- a/modules/cnn_3dobj/README.md +++ b/modules/cnn_3dobj/README.md @@ -21,7 +21,7 @@ $ cmake -D CMAKE_INSTALL_PREFIX=/usr/local .. $ make all -j4 $ sudo make install ``` -###After all these steps, the headers and libs of CAFFE will be set on /usr/local/ path, and when you compiling opencv with opencv_contrib modules as below, the protobuf and caffe will be recognized as already installed while building. Protobuf is +###After all these steps, the headers and libs of CAFFE will be set on /usr/local/ path, and when you compiling opencv with opencv_contrib modules as below, the protobuf and caffe will be recognized as already installed while building. Protobuf is needed. #Compiling OpenCV ``` @@ -57,7 +57,7 @@ $ make ``` $ ./sphereview_test -plymodel=../3Dmodel/ape.ply -label_class=0 ``` -###press 'Q' to start 2D image genaration +###press 'Q' to start 2D image genaration ``` $ ./sphereview_test -plymodel=../3Dmodel/ant.ply -label_class=1 ``` diff --git a/modules/cnn_3dobj/test/test_cnn_3dobj_feature_extract.cpp b/modules/cnn_3dobj/test/test_cnn_3dobj_feature_extract.cpp new file mode 100644 index 000000000..15fa87d6d --- /dev/null +++ b/modules/cnn_3dobj/test/test_cnn_3dobj_feature_extract.cpp @@ -0,0 +1,70 @@ +/* + * Created on: Aug 14, 2015 + * Author: yidawang + */ + +#include "test_precomp.hpp" + +using namespace cv; +using namespace cv::cnn_3dobj; + +class CV_CNN_Feature_Test : public cvtest::BaseTest +{ +public: + CV_CNN_Feature_Test(); +protected: + void run(int); +}; + +CV_CNN_Feature_Test::CV_CNN_Feature_Test() +{ +} + +/** + * This test checks the following: + * Feature extraction by the triplet trained CNN model + */ +void CV_CNN_Feature_Test::run(int) +{ + string caffemodel = ts->get_data_path() + "cnn_3dobj/samples/data/3d_triplet_iter_20000.caffemodel"; + string network_forIMG = ts->get_data_path() + "cnn_3dobj/samples/data/3d_triplet_testIMG.prototxt"; + string mean_file = "no"; + string target_img = ts->get_data_path() + "cnn_3dobj/samples/data/images_all/2_24.png"; + string feature_blob = "feat"; + string device = "CPU"; + int dev_id = 0; + + cv::cnn_3dobj::descriptorExtractor descriptor; + bool set_succeed = descriptor.setNet(device, dev_id); + if (!set_succeed) { + ts->printf(cvtest::TS::LOG, "Net parameters which is GPU or CPU could not be set"); + ts->set_failed_test_info(cvtest::TS::FAIL_MISSING_TEST_DATA); + return; + } + int net_ready; + if (strcmp(mean_file.c_str(), "no") == 0) + net_ready = descriptor.loadNet(set_succeed, network_forIMG, caffemodel); + else + net_ready = descriptor.loadNet(set_succeed, network_forIMG, caffemodel, mean_file); + if (!net_ready) { + ts->printf(cvtest::TS::LOG, "No model loaded"); + ts->set_failed_test_info(cvtest::TS::FAIL_MISSING_TEST_DATA); + return; + } + cv::Mat img = cv::imread(target_img, -1); + if (img.empty()) { + ts->printf(cvtest::TS::LOG, "could not read image %s\n", target_img.c_str()); + ts->set_failed_test_info(cvtest::TS::FAIL_MISSING_TEST_DATA); + return; + } + cv::Mat feature_test; + descriptor.extract(net_ready, img, feature_test, feature_blob); + if (feature_test.empty()) { + ts->printf(cvtest::TS::LOG, "could not extract feature from image %s\n", target_img.c_str()); + ts->set_failed_test_info(cvtest::TS::FAIL_MISSING_TEST_DATA); + return; + } + +} + +TEST(VIDEO_BGSUBGMG, accuracy) { CV_CNN_Feature_Test test; test.safe_run(); } diff --git a/modules/cnn_3dobj/test/test_main.cpp b/modules/cnn_3dobj/test/test_main.cpp new file mode 100644 index 000000000..6b2499344 --- /dev/null +++ b/modules/cnn_3dobj/test/test_main.cpp @@ -0,0 +1,3 @@ +#include "test_precomp.hpp" + +CV_TEST_MAIN("cv") diff --git a/modules/cnn_3dobj/test/test_precomp.hpp b/modules/cnn_3dobj/test/test_precomp.hpp new file mode 100644 index 000000000..4d4227b67 --- /dev/null +++ b/modules/cnn_3dobj/test/test_precomp.hpp @@ -0,0 +1,18 @@ +#ifdef __GNUC__ +# pragma GCC diagnostic ignored "-Wmissing-declarations" +# if defined __clang__ || defined __APPLE__ +# pragma GCC diagnostic ignored "-Wmissing-prototypes" +# pragma GCC diagnostic ignored "-Wextra" +# endif +#endif + +#ifndef __OPENCV_TEST_PRECOMP_HPP__ +#define __OPENCV_TEST_PRECOMP_HPP__ + +#include +#include "opencv2/ts.hpp" +#include "opencv2/imgproc.hpp" +#include "opencv2/cnn_3dobj_config.hpp" +#include "opencv2/cnn_3dobj.hpp" + +#endif