00001 #ifndef Impala_Core_Training_PrecomputeKernelMatrix_h
00002 #define Impala_Core_Training_PrecomputeKernelMatrix_h
00003
00004 #include "Util/PropertySet.h"
00005 #include "Core/Vector/Apply.h"
00006 #include "Core/Training/KernelFunctions.h"
00007 #include "Core/Array/PixSum.h"
00008 #include "Core/Array/Pattern/PatMPixOp.h"
00009 #include "Core/Feature/FeatureTable.h"
00010
00011 namespace Impala
00012 {
00013 namespace Core
00014 {
00015 namespace Training
00016 {
00017
00018 Matrix::Mat*
00019 PrecomputeKernelMatrix(std::vector<Feature::FeatureTable*> features,
00020 std::vector<double> weights,
00021 Util::PropertySet* properties)
00022 {
00023 ILOG_VAR(Training.PrecomputeKernelMatrix);
00024 if(!(weights.size() == features.size()))
00025 {
00026 ILOG_ERROR("size of list does not match!");
00027 return 0;
00028 }
00029
00030 std::vector<Matrix::Mat*> DistanceMatrices;
00031 std::vector<double> AList;
00032
00033 std::vector<Feature::FeatureTable*>::iterator it;
00034 for(it=features.begin() ; it!=features.end() ; ++it)
00035 {
00036 Feature::FeatureTable* f = *it;
00037
00038
00039 Matrix::Mat* m = Apply(&Training::Chi2Distance, f->GetColumn2(), f->GetColumn2());
00040 DistanceMatrices.push_back(m);
00041
00042
00043 double a = Array::PixSum(m);
00044 a /= m->W() * m->H();
00045 AList.push_back(a);
00046 }
00047
00048 if(!(weights.size() == AList.size() == DistanceMatrices.size()))
00049 {
00050 ILOG_ERROR("size of list does not match!");
00051 return 0;
00052 }
00053
00054 MpoChi2Kernel kernel(weights, AList);
00055 Array::Pattern::PatMPixOp(DistanceMatrices[0], DistanceMatrices, kernel);
00056
00057 for(int i=1 ; i<DistanceMatrices.size() ; ++i)
00058 delete DistanceMatrices[i];
00059 return DistanceMatrices[0];
00060 }
00061
00062 Matrix::Mat*
00063 PrecomputeKernelMatrix(Feature::FeatureTable* features,
00064 Util::PropertySet* properties)
00065 {
00066 std::vector<Feature::FeatureTable*> f;
00067 std::vector<double> w;
00068 f.push_back(features);
00069 w.push_back(1);
00070 return PrecomputeKernelMatrix(f,w,properties);
00071 }
00072
00073 }
00074 }
00075 }
00076
00077
00078 #endif