Home || Visual Search || Applications || Architecture || Important Messages || OGL || Src

FikSvmRepositoryInFile.h

Go to the documentation of this file.
00001 #ifndef Impala_Persistency_FikSvmRepositoryInFile_h
00002 #define Impala_Persistency_FikSvmRepositoryInFile_h
00003 
00004 #include "Core/Training/FikSvm.h"
00005 #include "Persistency/RepositoryInFileSystem.h"
00006 #include "Persistency/ModelLocator.h"
00007 #include "Core/Array/ReadRaw.h"
00008 #include "Core/Array/WriteRaw.h"
00009 
00010 namespace Impala
00011 {
00012 namespace Persistency
00013 {
00014 
00015 
00016 class FikSvmRepositoryInFile
00017 {
00018 public:
00019 
00020     typedef Core::Training::FikSvm FikSvm;
00021     typedef Core::Feature::WeightedFeatureList WeightedFeatureList;
00022 
00023     FikSvmRepositoryInFile()
00024     {
00025     }
00026 
00027     bool
00028     Exists(const ModelLocator& loc)
00029     {
00030         String dir = GetDir(loc, false);
00031         File file = RepFS().GetFile(loc, dir, loc.GetConcept() + ".props",
00032                                     false, true);
00033         return file.Valid();
00034     }
00035 
00036     FikSvm*
00037     Get(const ModelLocator& loc)
00038     {
00039         String dir = GetDir(loc, false);
00040         File file = RepFS().GetFile(loc, dir, loc.GetConcept() + ".props",
00041                                     false, false);
00042         if (!file.Valid())
00043             return 0;
00044 
00045         Util::PropertySet ps;
00046         Read(&ps, file);
00047 
00048         File featFile = RepFS().GetFile(loc, dir, loc.GetConcept() + ".features",
00049                                         false, false);
00050         WeightedFeatureList features;
00051         Read(&features, featFile);
00052 
00053         FikSvm* svm = new FikSvm(features, ps.GetInt("nrBins"),
00054                                  ps.GetInt("nrSV"), ps.GetDouble("rho"),
00055                                  ps.GetDouble("probA"), ps.GetDouble("probB"),
00056                                  ps.GetInt("probIndex"));
00057 
00058         for (int i=0 ; i<svm->GetNrFeatures() ; i++)
00059         {
00060             File rawFile = RepFS().GetFile(loc, dir, MakeString(i) + ".raw",
00061                                            false, false);
00062             Util::IOBuffer* buf = rawFile.GetReadBuffer();
00063             Core::Matrix::Mat* h = 0;
00064             ReadRaw(h, buf);
00065             Core::Matrix::Mat* a = 0;
00066             ReadRaw(a, buf);
00067             Core::Matrix::Mat* b = 0;
00068             ReadRaw(b, buf);
00069             svm->ImportSvm(h, a, b);
00070             delete b;
00071             delete a;
00072             delete h;
00073             delete buf;
00074         }
00075         return svm;
00076     }
00077 
00078     void
00079     Add(const ModelLocator& loc, FikSvm* svm)
00080     {
00081         String dir = GetDir(loc, false);
00082         File file = RepFS().GetFile(loc, dir, loc.GetConcept() + ".props",
00083                                     true, false);
00084         if (!file.Valid())
00085             return;
00086 
00087         Util::PropertySet ps;
00088         ps.Add("nrBins", svm->GetNrBins());
00089         ps.Add("nrSV", svm->GetNrSV());
00090         ps.Add("rho", svm->GetRho());
00091         ps.Add("probA", svm->GetProbA());
00092         ps.Add("probB", svm->GetProbB());
00093         ps.Add("probIndex", svm->GetProbIndex());
00094         Write(&ps, file);
00095 
00096         File featFile = RepFS().GetFile(loc, dir, loc.GetConcept() + ".features",
00097                                         true, false);
00098         Write(svm->GetFeatureList(), featFile);
00099 
00100         for (int i=0 ; i<svm->GetNrFeatures() ; i++)
00101         {
00102             File rawFile = RepFS().GetFile(loc, dir, MakeString(i) + ".raw",
00103                                            true, false);
00104             Util::IOBuffer* buf = rawFile.GetWriteBuffer();
00105             Core::Matrix::Mat* mat = svm->GetH(i);
00106             WriteRaw(mat, buf, 1);
00107             delete mat;
00108             mat = svm->GetAMat(i);
00109             WriteRaw(mat, buf, 1);
00110             delete mat;
00111             mat = svm->GetBMat(i);
00112             WriteRaw(mat, buf, 1);
00113             delete mat;
00114             delete buf;
00115         }
00116     }
00117 
00118 private:
00119 
00120     RepositoryInFileSystem&
00121     RepFS()
00122     {
00123         return RepositoryInFileSystem::GetInstance();
00124     }
00125 
00126     String
00127     GetDir(const ModelLocator& loc, bool forAll)
00128     {
00129         String dir = "ConceptModels";
00130 
00131         if (forAll)
00132             return dir;
00133 
00134         dir = FileNameConcat(dir, loc.GetConceptSet());
00135         dir = FileNameConcat(dir, loc.GetModel());
00136         dir = FileNameConcat(dir, loc.GetFeature());
00137         dir = FileNameConcat(dir, loc.GetConcept());
00138         return dir;
00139     }
00140 
00141     ILOG_VAR_DEC;
00142 };
00143 
00144 ILOG_VAR_INIT(FikSvmRepositoryInFile, Impala.Persistency);
00145 
00146 } // namespace Persistency
00147 } // namespace Impala
00148 
00149 #endif

Generated on Thu Jan 13 09:05:02 2011 for ImpalaSrc by  doxygen 1.5.1