00001 #ifndef Impala_Core_Trec_Concept_h
00002 #define Impala_Core_Trec_Concept_h
00003
00004 #include <cstdio>
00005 #include "Basis/String.h"
00006 #include "Core/Matrix/Vecs.h"
00007 #include "Core/Array/Pattern/PatM12PixOp.h"
00008 #include "Core/Array/Pattern/PatM21PixOp.h"
00009 #include "Core/Array/Pattern/PatM22PixOp.h"
00010 #include "Core/Trec/TrecSVM.h"
00011
00012 namespace Impala
00013 {
00014 namespace Core
00015 {
00016 namespace Trec
00017 {
00018
00019
00020 template<class DstArrayT, class SrcArrayT>
00021 class M21poConSVM
00022 {
00023 public:
00025 typedef Array::Pattern::TagTransInVar TransVarianceCategory;
00026
00028 typedef Array::Pattern::TagCallValue CallCategory;
00029
00030 typedef typename DstArrayT::StorType DstStorT;
00031 typedef typename DstArrayT::ArithType DstArithT;
00032 typedef typename SrcArrayT::ArithType SrcArithT;
00033
00035 M21poConSVM(TrecSVM* theSVM, int nrConcepts)
00036 {
00037 _theSVM = theSVM;
00038 _numConcepts = nrConcepts;
00039 _hist = Matrix::VecCreate<Matrix::VecScalarInt32>(_numConcepts);
00040 _histPtr = Matrix::VecE(_hist, 0);
00041 for (int i=0 ; i<_numConcepts ; i++)
00042 _histPtr[i] = 0;
00043 }
00044
00046 DstArithT
00047 DoIt(const SrcArithT& s1, const SrcArithT& s2, const SrcArithT& s3,
00048 const SrcArithT& s4, const SrcArithT& s5, const SrcArithT& s6,
00049 const SrcArithT& s7, const SrcArithT& s8, const SrcArithT& s9,
00050 const SrcArithT& s10, const SrcArithT& s11, const SrcArithT& s12,
00051 const SrcArithT& s13, const SrcArithT& s14, const SrcArithT& s15,
00052 const SrcArithT& s16, const SrcArithT& s17, const SrcArithT& s18,
00053 const SrcArithT& s19, const SrcArithT& s20, const SrcArithT& s21)
00054 {
00055 Int32 rank = _theSVM->Predict(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10,
00056 s11, s12, s13, s14, s15, s16, s17, s18,
00057 s19, s20, s21);
00058 _histPtr[rank]++;
00059 return rank;
00060 }
00061
00062
00063
00064 TrecSVM* _theSVM;
00065 int _numConcepts;
00066 Matrix::VecScalarInt32* _hist;
00067 int* _histPtr;
00068 };
00069
00070
00071 template<class DstArrayT, class SrcArrayT>
00072 class M22poConSVM
00073 {
00074 public:
00076 typedef Array::Pattern::TagTransInVar TransVarianceCategory;
00077
00079 typedef Array::Pattern::TagCallValue CallCategory;
00080
00081 typedef typename DstArrayT::StorType DstStorT;
00082 typedef typename DstArrayT::ArithType DstArithT;
00083 typedef typename SrcArrayT::ArithType SrcArithT;
00084
00086 M22poConSVM(TrecSVM* theSVM, int nrConcepts)
00087 {
00088 _theSVM = theSVM;
00089 _numConcepts = nrConcepts;
00090 _hist = Matrix::VecCreate<Matrix::VecScalarInt32>(_numConcepts);
00091 _histPtr = Matrix::VecE(_hist, 0);
00092 for (int i=0 ; i<_numConcepts ; i++)
00093 _histPtr[i] = 0;
00094 }
00095
00097 DstArithT
00098 DoIt(const SrcArithT& s1, const SrcArithT& s2, const SrcArithT& s3,
00099 const SrcArithT& s4, const SrcArithT& s5, const SrcArithT& s6,
00100 const SrcArithT& s7, const SrcArithT& s8, const SrcArithT& s9,
00101 const SrcArithT& s10, const SrcArithT& s11, const SrcArithT& s12,
00102 const SrcArithT& s13, const SrcArithT& s14, const SrcArithT& s15,
00103 const SrcArithT& s16, const SrcArithT& s17, const SrcArithT& s18,
00104 const SrcArithT& s19, const SrcArithT& s20, const SrcArithT& s21,
00105 const SrcArithT& mask)
00106 {
00107 if (mask < 0)
00108 return 0;
00109 Int32 rank = _theSVM->Predict(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10,
00110 s11, s12, s13, s14, s15, s16, s17, s18,
00111 s19, s20, s21);
00112 _histPtr[rank]++;
00113 return rank;
00114 }
00115
00116
00117
00118 TrecSVM* _theSVM;
00119 int _numConcepts;
00120 Matrix::VecScalarInt32* _hist;
00121 int* _histPtr;
00122 };
00123
00124
00125 void
00126 PrintConceptVector(std::string fileName, int frameNr,
00127 Matrix::VecScalarInt32* histo, int numPix)
00128 {
00129 FILE *file;
00130 if (! (file = fopen(fileName.c_str(), "a"))) {
00131 std::cout << "PrintConceptVector: error append to file " << fileName
00132 << std::endl;
00133 return;
00134 }
00135
00136
00137 int n = Matrix::VecNrElem(histo);
00138 int* hPtr = Matrix::VecE(histo, 0);
00139 double eps = 0.0001;
00140 fprintf(file, "%d", frameNr);
00141 for (int i=0 ; i<n ; i++)
00142 {
00143 double sz = (double) hPtr[i] / numPix;
00144 if (sz > eps)
00145 fprintf(file, " %d:%f", i+1, sz);
00146 }
00147 fprintf(file, "\n");
00148
00149 fclose(file);
00150 }
00151
00152 }
00153 }
00154 }
00155
00156 #endif