test_cnn_3dobj_feature_extract.cpp 2.27 KB
Newer Older
1 2 3 4 5
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Author: Yida Wang
6 7
#include "test_precomp.hpp"

8
namespace opencv_test { namespace {
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
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)
{
29 30
    String caffemodel = cvtest::findDataFile("contrib/cnn_3dobj/3d_triplet_iter_30000.caffemodel");
    String network_forIMG = cvtest::findDataFile("contrib/cnn_3dobj/3d_triplet_testIMG.prototxt");
31 32
    String mean_file = "no";
    std::vector<String> ref_img;
33
    String target_img = cvtest::findDataFile("contrib/cnn_3dobj/4_78.png");
34 35
    String feature_blob = "feat";
    String device = "CPU";
36 37
    int dev_id = 0;

38 39 40 41 42 43 44
    cv::Mat img_base = cv::imread(target_img, -1);
    if (img_base.empty())
    {
        ts->printf(cvtest::TS::LOG, "could not read reference image %s\n", target_img.c_str(), "make sure the path of images are set properly.");
        ts->set_failed_test_info(cvtest::TS::FAIL_MISSING_TEST_DATA);
        return;
    }
45
    cv::cnn_3dobj::descriptorExtractor descriptor(device, dev_id);
46
    if (mean_file == "no")
47
        descriptor.loadNet(network_forIMG, caffemodel);
48
    else
49
        descriptor.loadNet(network_forIMG, caffemodel, mean_file);
50

51
    cv::Mat feature_test;
52
    descriptor.extract(img_base, feature_test, feature_blob);
53 54 55
    // Reference feature is computed by Caffe extract_features tool.
    // To generate values for different images, use extract_features with the resetted image list in prototxt.
    Mat feature_reference = (Mat_<float>(1,3) << -312.4805, 8.4768486, -224.98953);
56
    float dist = cvtest::norm(feature_test, feature_reference, NORM_L1);
57 58
    if (dist > 5) {
      ts->printf(cvtest::TS::LOG, "Extracted featrue is not the same from the one extracted from Caffe.");
59 60 61 62 63
      ts->set_failed_test_info(cvtest::TS::FAIL_MISSING_TEST_DATA);
      return;
    }
}

64
TEST(CNN_FEATURE, accuracy) { CV_CNN_Feature_Test test; test.safe_run(); }
65 66

}} // namespace