brisque_trainer_livedb.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include <sstream>
  2. #include <iostream>
  3. #include "opencv2/quality.hpp"
  4. #include "opencv2/quality/quality_utils.hpp"
  5. #include "opencv2/imgcodecs.hpp"
  6. #include "opencv2/ml.hpp"
  7. /*
  8. BRISQUE Trainer using LIVE DB R2
  9. http://live.ece.utexas.edu/research/Quality/subjective.htm
  10. H.R. Sheikh, Z.Wang, L. Cormack and A.C. Bovik, "LIVE Image Quality Assessment Database Release 2", http://live.ece.utexas.edu/research/quality .
  11. H.R. Sheikh, M.F. Sabir and A.C. Bovik, "A statistical evaluation of recent full reference image quality assessment algorithms", IEEE Transactions on Image Processing, vol. 15, no. 11, pp. 3440-3451, Nov. 2006.
  12. Z. Wang, A.C. Bovik, H.R. Sheikh and E.P. Simoncelli, "Image quality assessment: from error visibility to structural similarity," IEEE Transactions on Image Processing , vol.13, no.4, pp. 600- 612, April 2004.
  13. */
  14. /*
  15. Copyright (c) 2011 The University of Texas at Austin
  16. All rights reserved.
  17. Permission is hereby granted, without written agreement and without license or royalty fees, to use, copy,
  18. modify, and distribute this code (the source files) and its documentation for
  19. any purpose, provided that the copyright notice in its entirety appear in all copies of this code, and the
  20. original source of this code, Laboratory for Image and Video Engineering (LIVE, http://live.ece.utexas.edu)
  21. and Center for Perceptual Systems (CPS, http://www.cps.utexas.edu) at the University of Texas at Austin (UT Austin,
  22. http://www.utexas.edu), is acknowledged in any publication that reports research using this code. The research
  23. is to be cited in the bibliography as:
  24. 1) A. Mittal, A. K. Moorthy and A. C. Bovik, "BRISQUE Software Release",
  25. URL: http://live.ece.utexas.edu/research/quality/BRISQUE_release.zip, 2011
  26. 2) A. Mittal, A. K. Moorthy and A. C. Bovik, "No Reference Image Quality Assessment in the Spatial Domain"
  27. submitted
  28. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT AUSTIN BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL,
  29. OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS DATABASE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF TEXAS
  30. AT AUSTIN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. THE UNIVERSITY OF TEXAS AT AUSTIN SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  32. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE DATABASE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS,
  33. AND THE UNIVERSITY OF TEXAS AT AUSTIN HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  34. */
  35. /* Original Paper: @cite Mittal2 and Original Implementation: @cite Mittal2_software */
  36. namespace {
  37. #define CATEGORIES 5
  38. #define IMAGENUM 982
  39. #define JP2KNUM 227
  40. #define JPEGNUM 233
  41. #define WNNUM 174
  42. #define GBLURNUM 174
  43. #define FFNUM 174
  44. // collects training data from LIVE R2 database
  45. // returns {features, responses}, 1 row per image
  46. std::pair<cv::Mat, cv::Mat> collect_data_live_r2(const std::string& foldername)
  47. {
  48. FILE* fid = nullptr;
  49. //----------------------------------------------------
  50. // class is the distortion category, there are 982 images in LIVE database
  51. std::vector<std::string> distortionlabels;
  52. distortionlabels.push_back("jp2k");
  53. distortionlabels.push_back("jpeg");
  54. distortionlabels.push_back("wn");
  55. distortionlabels.push_back("gblur");
  56. distortionlabels.push_back("fastfading");
  57. int imnumber[5] = { 0,227,460,634,808 };
  58. std::vector<int>categorylabels;
  59. categorylabels.insert(categorylabels.end(), JP2KNUM, 0);
  60. categorylabels.insert(categorylabels.end(), JPEGNUM, 1);
  61. categorylabels.insert(categorylabels.end(), WNNUM, 2);
  62. categorylabels.insert(categorylabels.end(), GBLURNUM, 3);
  63. categorylabels.insert(categorylabels.end(), FFNUM, 4);
  64. int iforg[IMAGENUM];
  65. fid = fopen((foldername + "orgs.txt").c_str(), "r");
  66. for (int itr = 0; itr < IMAGENUM; itr++)
  67. CV_Assert( fscanf(fid, "%d", iforg + itr) > 0);
  68. fclose(fid);
  69. float dmosscores[IMAGENUM];
  70. fid = fopen((foldername + "dmos.txt").c_str(), "r");
  71. for (int itr = 0; itr < IMAGENUM; itr++)
  72. CV_Assert( fscanf(fid, "%f", dmosscores + itr) > 0 );
  73. fclose(fid);
  74. // features vector, 1 row per image
  75. cv::Mat features(0, 0, CV_32FC1);
  76. // response vector, 1 row per image
  77. cv::Mat responses(0, 1, CV_32FC1);
  78. for (int itr = 0; itr < IMAGENUM; itr++)
  79. {
  80. //Dont compute features for original images
  81. if (iforg[itr])
  82. continue;
  83. // append dmos score
  84. float score = dmosscores[itr];
  85. responses.push_back(cv::Mat(1, 1, CV_32FC1, (void*)&score));
  86. // load image, calc features
  87. std::string imname = "";
  88. imname.append(foldername);
  89. imname.append("/");
  90. imname.append(distortionlabels[categorylabels[itr]].c_str());
  91. imname.append("/img");
  92. imname += std::to_string((itr - imnumber[categorylabels[itr]] + 1));
  93. imname.append(".bmp");
  94. cv::Mat im_features;
  95. cv::quality::QualityBRISQUE::computeFeatures(cv::imread(imname), im_features); // outputs a row vector
  96. features.push_back(im_features.row(0)); // append row vector
  97. }
  98. return std::make_pair(std::move(features), std::move(responses));
  99. } // collect_data_live_r2
  100. }
  101. inline void printHelp()
  102. {
  103. using namespace std;
  104. cout << " Demo of training BRISQUE quality assessment model using LIVE R2 database." << endl;
  105. cout << " A. Mittal, A. K. Moorthy and A. C. Bovik, 'No Reference Image Quality Assessment in the Spatial Domain'" << std::endl << std::endl;
  106. cout << " Usage: program <live_r2_db_path> <output_model_path> <output_range_path>" << endl << endl;
  107. }
  108. int main(int argc, const char * argv[])
  109. {
  110. using namespace cv::ml;
  111. if (argc != 4)
  112. {
  113. printHelp();
  114. exit(1);
  115. }
  116. std::cout << "Training BRISQUE on database at " << argv[1] << "..." << std::endl;
  117. // collect data from the data set
  118. auto data = collect_data_live_r2( std::string( argv[1] ) + "/" );
  119. // extract column ranges for features
  120. const auto range = cv::quality::quality_utils::get_column_range(data.first);
  121. // scale all features from -1 to 1
  122. cv::quality::quality_utils::scale<float>(data.first, range, -1.f, 1.f);
  123. // do training, output train file
  124. // libsvm call from original BRISQUE impl: svm-train -s 3 -g 0.05 -c 1024 -b 1 -q train_scale allmodel
  125. auto svm = SVM::create();
  126. svm->setType(SVM::Types::EPS_SVR);
  127. svm->setKernel(SVM::KernelTypes::RBF);
  128. svm->setGamma(0.05);
  129. svm->setC(1024.);
  130. svm->setTermCriteria(cv::TermCriteria(cv::TermCriteria::Type::EPS, 1000, 0.001));
  131. svm->setP(.1);// default p (epsilon) from libsvm
  132. svm->train(data.first, cv::ml::ROW_SAMPLE, data.second);
  133. svm->save( argv[2] ); // save to location specified in argv[2]
  134. // output scale file to argv[3]
  135. cv::Mat range_mat(range);
  136. cv::FileStorage fs(argv[3], cv::FileStorage::WRITE );
  137. fs << "range" << range_mat;
  138. return 0;
  139. }