ar_hmdb_benchmark.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2014, Itseez Inc, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Itseez Inc or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include "opencv2/datasets/ar_hmdb.hpp"
  42. #include "opencv2/datasets/util.hpp"
  43. #include <opencv2/core.hpp>
  44. #include <opencv2/flann.hpp>
  45. #include <opencv2/ml.hpp>
  46. #include <cstdio>
  47. #include <string>
  48. #include <vector>
  49. #include <fstream>
  50. using namespace std;
  51. using namespace cv;
  52. using namespace cv::datasets;
  53. using namespace cv::flann;
  54. using namespace cv::ml;
  55. void fillData(const string &path, vector< Ptr<Object> > &curr, Index &flann_index, Mat1f &data, Mat1i &labels);
  56. void fillData(const string &path, vector< Ptr<Object> > &curr, Index &flann_index, Mat1f &data, Mat1i &labels)
  57. {
  58. const unsigned int descriptorNum = 162;
  59. Mat1f sample(1, descriptorNum);
  60. Mat1i nresps(1, 1);
  61. Mat1f dists(1, 1);
  62. unsigned int numFiles = 0;
  63. for (unsigned int i=0; i<curr.size(); ++i)
  64. {
  65. AR_hmdbObj *example = static_cast<AR_hmdbObj *>(curr[i].get());
  66. string featuresFullPath = path + "hmdb51_org_stips/" + example->name + "/" + example->videoName + ".txt";
  67. ifstream infile(featuresFullPath.c_str());
  68. string line;
  69. // skip header
  70. for (unsigned int j=0; j<3; ++j)
  71. {
  72. getline(infile, line);
  73. }
  74. while (getline(infile, line))
  75. {
  76. // 7 skip, hog+hof: 72+90 read
  77. vector<string> elems;
  78. split(line, elems, '\t');
  79. for (unsigned int j=0; j<descriptorNum; ++j)
  80. {
  81. sample(0, j) = (float)atof(elems[j+7].c_str());
  82. }
  83. flann_index.knnSearch(sample, nresps, dists, 1, SearchParams());
  84. data(numFiles, nresps(0, 0)) ++;
  85. }
  86. labels(numFiles, 0) = example->id;
  87. numFiles++;
  88. }
  89. }
  90. int main(int argc, char *argv[])
  91. {
  92. const char *keys =
  93. "{ help h usage ? | | show this message }"
  94. "{ path p |true| path to dataset }";
  95. CommandLineParser parser(argc, argv, keys);
  96. string path(parser.get<string>("path"));
  97. if (parser.has("help") || path=="true")
  98. {
  99. parser.printMessage();
  100. return -1;
  101. }
  102. // loading dataset
  103. Ptr<AR_hmdb> dataset = AR_hmdb::create();
  104. dataset->load(path);
  105. int numSplits = dataset->getNumSplits();
  106. printf("splits number: %u\n", numSplits);
  107. const unsigned int descriptorNum = 162;
  108. const unsigned int clusterNum = 4000;
  109. const unsigned int sampleNum = 5613856; // max for all 3 splits
  110. vector<double> res;
  111. for (int currSplit=0; currSplit<numSplits; ++currSplit)
  112. {
  113. Mat1f samples(sampleNum, descriptorNum);
  114. unsigned int currSample = 0;
  115. vector< Ptr<Object> > &curr = dataset->getTrain(currSplit);
  116. unsigned int numFeatures = 0;
  117. for (unsigned int i=0; i<curr.size(); ++i)
  118. {
  119. AR_hmdbObj *example = static_cast<AR_hmdbObj *>(curr[i].get());
  120. string featuresFullPath = path + "hmdb51_org_stips/" + example->name + "/" + example->videoName + ".txt";
  121. ifstream infile(featuresFullPath.c_str());
  122. string line;
  123. // skip header
  124. for (unsigned int j=0; j<3; ++j)
  125. {
  126. getline(infile, line);
  127. }
  128. while (getline(infile, line))
  129. {
  130. numFeatures++;
  131. if (currSample < sampleNum)
  132. {
  133. // 7 skip, hog+hof: 72+90 read
  134. vector<string> elems;
  135. split(line, elems, '\t');
  136. for (unsigned int j=0; j<descriptorNum; ++j)
  137. {
  138. samples(currSample, j) = (float)atof(elems[j+7].c_str());
  139. }
  140. currSample++;
  141. }
  142. }
  143. }
  144. printf("split %u, train features number: %u, samples number: %u\n", currSplit, numFeatures, currSample);
  145. // clustering
  146. Mat1f centers(clusterNum, descriptorNum);
  147. ::cvflann::KMeansIndexParams kmean_params;
  148. unsigned int resultClusters = hierarchicalClustering< L2<float> >(samples, centers, kmean_params);
  149. if (resultClusters < clusterNum)
  150. {
  151. centers = centers.rowRange(Range(0, resultClusters));
  152. }
  153. Index flann_index(centers, KDTreeIndexParams());
  154. printf("resulted clusters number: %u\n", resultClusters);
  155. unsigned int numTrainFiles = curr.size();
  156. Mat1f trainData(numTrainFiles, resultClusters);
  157. Mat1i trainLabels(numTrainFiles, 1);
  158. for (unsigned int i=0; i<numTrainFiles; ++i)
  159. {
  160. for (unsigned int j=0; j<resultClusters; ++j)
  161. {
  162. trainData(i, j) = 0;
  163. }
  164. }
  165. printf("calculating train histograms\n");
  166. fillData(path, curr, flann_index, trainData, trainLabels);
  167. printf("train svm\n");
  168. Ptr<SVM> svm = SVM::create();
  169. svm->setType(SVM::C_SVC);
  170. svm->setKernel(SVM::POLY); //SVM::RBF;
  171. svm->setDegree(0.5);
  172. svm->setGamma(1);
  173. svm->setCoef0(1);
  174. svm->setC(1);
  175. svm->setNu(0.5);
  176. svm->setP(0);
  177. svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER+TermCriteria::EPS, 1000, 0.01));
  178. svm->train(trainData, ROW_SAMPLE, trainLabels);
  179. // prepare to predict
  180. curr = dataset->getTest(currSplit);
  181. unsigned int numTestFiles = curr.size();
  182. Mat1f testData(numTestFiles, resultClusters);
  183. Mat1i testLabels(numTestFiles, 1); // ground true
  184. for (unsigned int i=0; i<numTestFiles; ++i)
  185. {
  186. for (unsigned int j=0; j<resultClusters; ++j)
  187. {
  188. testData(i, j) = 0;
  189. }
  190. }
  191. printf("calculating test histograms\n");
  192. fillData(path, curr, flann_index, testData, testLabels);
  193. printf("predicting\n");
  194. Mat1f testPredicted(numTestFiles, 1);
  195. svm->predict(testData, testPredicted);
  196. unsigned int correct = 0;
  197. for (unsigned int i=0; i<numTestFiles; ++i)
  198. {
  199. if ((int)testPredicted(i, 0) == testLabels(i, 0))
  200. {
  201. correct++;
  202. }
  203. }
  204. double accuracy = 1.0*correct/numTestFiles;
  205. printf("correctly recognized actions: %f\n", accuracy);
  206. res.push_back(accuracy);
  207. }
  208. double accuracy = 0.0;
  209. for (unsigned int i=0; i<res.size(); ++i)
  210. {
  211. accuracy += res[i];
  212. }
  213. printf("average: %f\n", accuracy/res.size());
  214. return 0;
  215. }