瀏覽代碼

added unittest for FPFeature VectorFeature

Johannes Ruehle 11 年之前
父節點
當前提交
a87476a6c2

+ 99 - 0
features/fpfeatures/tests/TestVectorFeature.cpp

@@ -0,0 +1,99 @@
+#ifdef NICE_USELIB_CPPUNIT
+
+#include <string>
+#include <exception>
+#include <iostream>
+#include <fstream>
+
+//----------
+
+#include "TestVectorFeature.h"
+
+#include "vislearning/cbaselib/FeaturePool.h"
+#include "../VectorFeature.h"
+
+const bool verbose = true;
+const bool verboseStartEnd = true;
+
+using namespace OBJREC;
+using namespace NICE;
+using namespace std;
+
+CPPUNIT_TEST_SUITE_REGISTRATION( TestVectorFeature );
+
+void TestVectorFeature::setUp() {
+}
+
+void TestVectorFeature::tearDown() {
+}
+void TestVectorFeature::testVectorFeature() 
+{
+    if (verboseStartEnd)
+        std::cerr << "================== TestVectorFeature::testVectorFeature ===================== " << std::endl;
+
+    Matrix mX;
+    Vector vY;
+    Vector vY_multi;
+
+    ifstream ifs ("toyExample1.data", ios::in);
+    //   ifstream ifs ("toyExampleLargeScale.data", ios::in);
+    //   ifstream ifs ("toyExampleLargeLargeScale.data", ios::in);
+    CPPUNIT_ASSERT ( ifs.good() );
+    ifs >> mX;
+    ifs >> vY;
+    ifs >> vY_multi;
+    ifs.close();
+
+    if (verbose)
+    {
+        std::cerr << "data loaded: mX" << std::endl;
+        std::cerr << mX << std::endl;
+        std::cerr << "vY: " << std::endl;
+        std::cerr << vY << std::endl;
+        std::cerr << "vY_multi: " << std::endl;
+        std::cerr << vY_multi << std::endl;
+    }
+
+    int iNumFeatureDimension = mX.cols();
+
+    FeaturePool fp;
+    VectorFeature *pVecFeature = new VectorFeature(iNumFeatureDimension);
+    pVecFeature->explode(fp);
+
+
+    // memory layout needs to be transposed into rows x column: features x samples
+    // features must lay next to each other in memory, so that each feature vector can
+    // be adressed by a starting pointer and the number of feature dimensions to come.
+    Matrix mX_transposed = mX.transpose();
+
+    Examples examples;
+
+    bool bSuccess = Examples::wrapExamplesAroundFeatureMatrix(mX_transposed, vY, examples);
+    CPPUNIT_ASSERT( bSuccess );
+
+    CPPUNIT_ASSERT( examples.size() == mX.rows() );
+
+    for(int i=0; i< examples.size(); i++)
+    {
+        Example &t_Example = examples[i].second;
+        NICE::Vector    t_FeatVector;
+        fp.calcFeatureVector(t_Example, t_FeatVector);
+        std::cerr << "Example " << i << " Features: " <<t_FeatVector << std::endl;
+
+        for(int f=0; f< iNumFeatureDimension;f++)
+        {
+            double t_f1 = t_FeatVector[f];
+            double t_f2 = mX(i,f);
+            CPPUNIT_ASSERT_DOUBLES_EQUAL( t_f1, t_f2, 0.001f );
+        }
+    }
+    examples.clean();
+    delete pVecFeature;
+
+    if (verboseStartEnd)
+        std::cerr << "================== TestFPCGPHIK::testFPCGPHIK done ===================== " << std::endl;
+
+}
+
+
+#endif

+ 26 - 0
features/fpfeatures/tests/TestVectorFeature.h

@@ -0,0 +1,26 @@
+#ifndef _TESTVECTORFEATURE_H
+#define _TESTVECTORFEATURE_H
+
+#include <cppunit/extensions/HelperMacros.h>
+
+/**
+ * CppUnit-Testcase. 
+ */
+class TestVectorFeature : public CppUnit::TestFixture {
+
+    CPPUNIT_TEST_SUITE( TestVectorFeature );
+    
+    CPPUNIT_TEST(testVectorFeature);
+
+    CPPUNIT_TEST_SUITE_END();
+  
+ private:
+ 
+ public:
+    void setUp();
+    void tearDown();
+
+    void testVectorFeature();
+};
+
+#endif // _TESTVECTORFEATURE_H

+ 42 - 0
features/fpfeatures/tests/toyExample1.data

@@ -0,0 +1,42 @@
+39 x 2
+0.1394    0.3699
+0.1210    0.3260
+0.1164    0.2588
+0.1210    0.2032
+0.1417    0.1886
+0.1624    0.2325
+0.1624    0.3319
+0.1509    0.3114
+0.1417    0.2412
+0.1417    0.2763
+0.1279    0.3173
+0.3537    0.3582
+0.3306    0.3056
+0.3306    0.2471
+0.3376    0.2061
+0.3583    0.1740
+0.3698    0.1564
+0.3790    0.2558
+0.3744    0.3173
+0.3698    0.3406
+0.3583    0.2646
+0.3629    0.1944
+0.3468    0.3173
+0.3329    0.2588
+0.3514    0.1974
+0.2224    0.3436
+0.2270    0.3348
+0.2293    0.2675
+0.2339    0.2237
+0.2316    0.1623
+0.2408    0.1857
+0.2615    0.2763
+0.2638    0.3436
+0.2592    0.3904
+0.2477    0.4284
+0.2224    0.3582
+0.2177    0.2909
+0.2224    0.2178
+0.2500    0.1213
+39 < 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 >
+39 < 0 0 0 0 0 0 0 0 0 0 0 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 >