File: brisque_trainer_livedb.cpp

package info (click to toggle)
opencv 4.5.1%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 268,248 kB
  • sloc: cpp: 969,170; xml: 682,525; python: 36,732; lisp: 30,170; java: 25,155; ansic: 7,927; javascript: 5,643; objc: 2,041; sh: 935; cs: 601; perl: 494; makefile: 145
file content (176 lines) | stat: -rw-r--r-- 7,299 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include  <sstream>
#include  <iostream>

#include "opencv2/quality.hpp"
#include "opencv2/quality/quality_utils.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/ml.hpp"

/*
BRISQUE Trainer using LIVE DB R2
http://live.ece.utexas.edu/research/Quality/subjective.htm
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 .
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.
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.
*/

/*
Copyright (c) 2011 The University of Texas at Austin
All rights reserved.

Permission is hereby granted, without written agreement and without license or royalty fees, to use, copy,
modify, and distribute this code (the source files) and its documentation for
any purpose, provided that the copyright notice in its entirety appear in all copies of this code, and the
original source of this code, Laboratory for Image and Video Engineering (LIVE, http://live.ece.utexas.edu)
and Center for Perceptual Systems (CPS, http://www.cps.utexas.edu) at the University of Texas at Austin (UT Austin,
http://www.utexas.edu), is acknowledged in any publication that reports research using this code. The research
is to be cited in the bibliography as:

1) A. Mittal, A. K. Moorthy and A. C. Bovik, "BRISQUE Software Release",
URL: http://live.ece.utexas.edu/research/quality/BRISQUE_release.zip, 2011

2) A. Mittal, A. K. Moorthy and A. C. Bovik, "No Reference Image Quality Assessment in the Spatial Domain"
submitted

IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT AUSTIN BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS DATABASE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF TEXAS
AT AUSTIN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

THE UNIVERSITY OF TEXAS AT AUSTIN SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE DATABASE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS,
AND THE UNIVERSITY OF TEXAS AT AUSTIN HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/

/* Original Paper: @cite Mittal2 and Original Implementation: @cite Mittal2_software */

namespace {

    #define CATEGORIES 5
    #define IMAGENUM   982
    #define JP2KNUM    227
    #define JPEGNUM    233
    #define WNNUM      174
    #define GBLURNUM   174
    #define FFNUM      174

    // collects training data from LIVE R2 database
    // returns {features, responses}, 1 row per image
    std::pair<cv::Mat, cv::Mat> collect_data_live_r2(const std::string& foldername)
    {
        FILE* fid = nullptr;

        //----------------------------------------------------
        // class is the distortion category, there are 982 images in LIVE database
        std::vector<std::string> distortionlabels;
        distortionlabels.push_back("jp2k");
        distortionlabels.push_back("jpeg");
        distortionlabels.push_back("wn");
        distortionlabels.push_back("gblur");
        distortionlabels.push_back("fastfading");

        int imnumber[5] = { 0,227,460,634,808 };

        std::vector<int>categorylabels;
        categorylabels.insert(categorylabels.end(), JP2KNUM, 0);
        categorylabels.insert(categorylabels.end(), JPEGNUM, 1);
        categorylabels.insert(categorylabels.end(), WNNUM, 2);
        categorylabels.insert(categorylabels.end(), GBLURNUM, 3);
        categorylabels.insert(categorylabels.end(), FFNUM, 4);

        int  iforg[IMAGENUM];
        fid = fopen((foldername + "orgs.txt").c_str(), "r");
        for (int itr = 0; itr < IMAGENUM; itr++)
            CV_Assert( fscanf(fid, "%d", iforg + itr) > 0);
        fclose(fid);

        float dmosscores[IMAGENUM];
        fid = fopen((foldername + "dmos.txt").c_str(), "r");
        for (int itr = 0; itr < IMAGENUM; itr++)
            CV_Assert( fscanf(fid, "%f", dmosscores + itr) > 0 );
        fclose(fid);

        // features vector, 1 row per image
        cv::Mat features(0, 0, CV_32FC1);

        // response vector, 1 row per image
        cv::Mat responses(0, 1, CV_32FC1);

        for (int itr = 0; itr < IMAGENUM; itr++)
        {
            //Dont compute features for original images
            if (iforg[itr])
                continue;

            // append dmos score
            float score = dmosscores[itr];
            responses.push_back(cv::Mat(1, 1, CV_32FC1, (void*)&score));

            // load image, calc features
            std::string imname = "";
            imname.append(foldername);
            imname.append("/");
            imname.append(distortionlabels[categorylabels[itr]].c_str());
            imname.append("/img");
            imname += std::to_string((itr - imnumber[categorylabels[itr]] + 1));
            imname.append(".bmp");

            cv::Mat im_features;
            cv::quality::QualityBRISQUE::computeFeatures(cv::imread(imname), im_features);    // outputs a row vector

            features.push_back(im_features.row(0)); // append row vector
        }

        return std::make_pair(std::move(features), std::move(responses));
    }    //    collect_data_live_r2
}

inline void printHelp()
{
    using namespace std;
    cout << "    Demo of training BRISQUE quality assessment model using LIVE R2 database." << endl;
    cout << "    A. Mittal, A. K. Moorthy and A. C. Bovik, 'No Reference Image Quality Assessment in the Spatial Domain'" << std::endl << std::endl;

    cout << "    Usage: program <live_r2_db_path> <output_model_path> <output_range_path>" << endl << endl;
}

int main(int argc, const char * argv[])
{
    using namespace cv::ml;

    if (argc != 4)
    {
        printHelp();
        exit(1);
    }

    std::cout << "Training BRISQUE on database at " << argv[1] << "..." << std::endl;

    // collect data from the data set
    auto data = collect_data_live_r2( std::string( argv[1] ) + "/" );

    // extract column ranges for features
    const auto range = cv::quality::quality_utils::get_column_range(data.first);

    // scale all features from -1 to 1
    cv::quality::quality_utils::scale<float>(data.first, range, -1.f, 1.f);

    // do training, output train file
    // libsvm call from original BRISQUE impl:   svm-train  -s 3 -g 0.05 -c 1024 -b 1 -q train_scale allmodel
    auto svm = SVM::create();
    svm->setType(SVM::Types::EPS_SVR);
    svm->setKernel(SVM::KernelTypes::RBF);
    svm->setGamma(0.05);
    svm->setC(1024.);
    svm->setTermCriteria(cv::TermCriteria(cv::TermCriteria::Type::EPS, 1000, 0.001));
    svm->setP(.1);// default p (epsilon) from libsvm

    svm->train(data.first, cv::ml::ROW_SAMPLE, data.second);
    svm->save( argv[2] );   // save to location specified in argv[2]

    // output scale file to argv[3]
    cv::Mat range_mat(range);
    cv::FileStorage fs(argv[3], cv::FileStorage::WRITE );
    fs << "range" << range_mat;

    return 0;
}