00001 #ifndef Impala_Core_ImageSet_ComputeKernelData_h
00002 #define Impala_Core_ImageSet_ComputeKernelData_h
00003
00004 #include "Core/ImageSet/Reporter.h"
00005 #include "Sandbox/koen/cuda/projects/precomputeMatrix/ComputeKernelMatrix.h"
00006
00007 namespace Impala
00008 {
00009 namespace Core
00010 {
00011 namespace ImageSet
00012 {
00013
00014 class ComputeKernelData : public Listener
00015 {
00016 public:
00017 ComputeKernelData(Reporter* reporter, CmdOptions& options, ImageSet* is)
00018 {
00019 mReporter = reporter;
00020 if (options.GetNrArg() < 4)
00021 {
00022 ILOG_ERROR("Missing argument");
00023 return;
00024 }
00025 String name2 = options.GetArg(2);
00026 mBaseSet = Database::MakeRawDataSet(name2, true);
00027 mKernelMatrixName = options.GetArg(3);
00028 }
00029
00030 virtual
00031 ~ComputeKernelData()
00032 {
00033 delete mBaseSet;
00034 }
00035
00036 virtual void
00037 HandleNewWalk(ImageSet* is, String walkType)
00038 {
00039 mWalkType = walkType;
00040 }
00041
00042
00044
00046 class PrecomputeTask
00047 {
00048 public:
00049 PrecomputeTask(String inputDefinitionFilename, String averagesFilename,
00050 String outputFilename, Database::RawDataSet* dataSet,
00051 Database::RawDataSet* mBaseSet, int dirId)
00052 : mOutputFilename(outputFilename), mTotalFeatureWeight(0),
00053 mWriteAsFeatureTable(true)
00054 {
00055 ILOG_INFO("Loading precompute definition (" << inputDefinitionFilename
00056 << ")...");
00057 std::ifstream ifs(inputDefinitionFilename.c_str());
00058 std::ifstream fs(averagesFilename.c_str(), std::ios::binary);
00059 if(fs.fail() || fs.eof())
00060 {
00061 ILOG_ERROR("Could not load averages from " << averagesFilename);
00062 }
00063
00064 if (mBaseSet->GetDatabase()->GetDataChannel() || dataSet->GetDatabase()->GetDataChannel())
00065 {
00066 ILOG_ERROR("Does not work with data channel");
00067 throw("bye");
00068 }
00069
00070 double weight = 0.0;
00071 String name;
00072 while(ifs >> weight)
00073 {
00074 ifs >> name;
00075 int i = mFeatureNames.size();
00076 mFeatureNames.push_back(name);
00077 mFeatureWeights.push_back(weight);
00078
00079 double q = 0.0;
00080 fs.read(reinterpret_cast<char *>(&q), sizeof(q));
00081 mFeatureAverages.push_back(q);
00082 ILOG_INFO(i << " " << mFeatureAverages[i] << " (" << mFeatureWeights[i] << "*" << mFeatureNames[i] << ")");
00083 mTotalFeatureWeight += weight;
00084
00085 String tableA = mBaseSet->GetFilePathFeatureIndex(mFeatureNames[i], "", false, false);
00086 String tableB = dataSet->GetFilePathFeatureData(
00087 Feature::FeatureDefinition(mFeatureNames[i]), dirId, false, -1, false, false);
00088 mTableNamesA.push_back(tableA);
00089 mTableNamesB.push_back(tableB);
00090 }
00091 fs.close();
00092 ifs.close();
00093 if(Size() == 0)
00094 ILOG_ERROR("Empty input definition?");
00095 }
00096
00097 inline size_t
00098 Size()
00099 {
00100 return mFeatureNames.size();
00101 }
00102
00103 String
00104 GetTableFilenameA(int index)
00105 {
00106 return mTableNamesA[index];
00107 }
00108
00109 String
00110 GetTableFilenameB(int index)
00111 {
00112 return mTableNamesB[index];
00113 }
00114
00115 KernelWriter*
00116 GetKernelWriter(int vectorCountA, int vectorCountB)
00117 {
00118 KernelWriter* kernelWriter = new KernelWriter(mOutputFilename, vectorCountA, vectorCountB, mWriteAsFeatureTable);
00119
00120
00121 std::ifstream fs(mTableNamesB[0].c_str(), std::ios::binary);
00122 int n = 200 + 8 * vectorCountB;
00123 char* buffer = new char[n];
00124 fs.seekg(1024 + 128);
00125 fs.read(buffer, n);
00126 fs.close();
00127
00128 kernelWriter->outputStream.seekp(1024 + 128);
00129 kernelWriter->outputStream.write(buffer, n);
00130 delete buffer;
00131
00132 return kernelWriter;
00133 }
00134
00135 std::vector<String> mFeatureNames;
00136 std::vector<String> mTableNamesA;
00137 std::vector<String> mTableNamesB;
00138 std::vector<double> mFeatureWeights;
00139 std::vector<double> mFeatureAverages;
00140 bool mWriteAsFeatureTable;
00141 String mOutputFilename;
00142 double mTotalFeatureWeight;
00143 };
00144
00145
00146 virtual void
00147 HandleDoneDir(ImageSet* is, int dirId)
00148 {
00149 String kernelDataCacheFilename = is->GetFilePathKernelData(
00150 mWalkType,
00151 mBaseSet->GetSetName(),
00152 mKernelMatrixName,
00153 dirId, false, true);
00154 if(!kernelDataCacheFilename.empty())
00155 {
00156 ILOG_INFO("Skipping, KernelData already exists: " <<
00157 kernelDataCacheFilename);
00158 return;
00159 }
00160
00161 String storageFilename = is->GetFilePathKernelData(
00162 mWalkType,
00163 mBaseSet->GetSetName(),
00164 mKernelMatrixName,
00165 dirId, true, false);
00166
00167 String inputDefinition = mBaseSet->GetFilePathPrecomputedKernels
00168 (mKernelMatrixName + ".input.txt", "", false, false);
00169
00170 String averagesFilename = mBaseSet->GetFilePathPrecomputedKernels
00171 (mKernelMatrixName + ".averages.raw", "", false, false);
00172
00173
00174 #ifdef CUDA
00175 bool GPU = Link::Cuda::CudaUsed();
00176 #else
00177 bool GPU = false;
00178 #endif
00179 int setting_slabWidth = 1024;
00180 PrecomputeTask* pt = new PrecomputeTask(inputDefinition, averagesFilename, storageFilename, is, mBaseSet, dirId);
00181 ILOG_INFO("GPU mode: " << GPU);
00182 if(GPU)
00183 {
00184 ComputeKernelMatrix<float, true>(pt, setting_slabWidth);
00185 }
00186 else
00187 {
00188 #ifdef CUDA
00189
00190 ComputeKernelMatrix<float, false>(pt, setting_slabWidth);
00191 #else
00192 ComputeKernelMatrix<double, false>(pt, setting_slabWidth);
00193 #endif
00194 }
00195 delete pt;
00196 }
00197
00198 private:
00199
00200 Reporter* mReporter;
00201 Database::RawDataSet* mBaseSet;
00202 String mWalkType;
00203 String mKernelMatrixName;
00204
00205 ILOG_VAR_DEC;
00206 };
00207
00208 ILOG_VAR_INIT(ComputeKernelData, Impala.Core.ImageSet);
00209
00210 }
00211 }
00212 }
00213
00214 #endif