5ade81681ecd3e114da489ad9c38ec524d9a9295
[platform/upstream/opencv.git] / modules / softcascade / test / test_training.cpp
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) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2008-2013, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and / or other materials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 #if !defined(ANDROID)
44
45 #include <test_precomp.hpp>
46 #include <string>
47 #include <fstream>
48 #include <vector>
49
50 using namespace std;
51
52 namespace {
53
54 using namespace cv::softcascade;
55
56 typedef vector<cv::String> svector;
57 class ScaledDataset : public Dataset
58 {
59 public:
60     ScaledDataset(const string& path, const int octave);
61
62     virtual cv::Mat get(SampleType type, int idx) const;
63     virtual int available(SampleType type) const;
64     virtual ~ScaledDataset();
65
66 private:
67     svector pos;
68     svector neg;
69 };
70
71 ScaledDataset::ScaledDataset(const string& path, const int oct)
72 {
73     cv::glob(path + cv::format("/octave_%d/*.png", oct), pos);
74     cv::glob(path + "/*.png", neg);
75
76     // Check: files not empty
77     CV_Assert(pos.size() != size_t(0));
78     CV_Assert(neg.size() != size_t(0));
79 }
80
81 cv::Mat ScaledDataset::get(SampleType type, int idx) const
82 {
83     const std::string& src = (type == POSITIVE)? pos[idx]: neg[idx];
84     return cv::imread(src);
85 }
86
87 int ScaledDataset::available(SampleType type) const
88 {
89     return (int)((type == POSITIVE)? pos.size():neg.size());
90 }
91
92 ScaledDataset::~ScaledDataset(){}
93
94 }
95
96 TEST(SoftCascade, training)
97 {
98     // // 2. check and open output file
99     string outXmlPath = cv::tempfile(".xml");
100     cv::FileStorage fso(outXmlPath, cv::FileStorage::WRITE);
101
102     ASSERT_TRUE(fso.isOpened());
103
104     std::vector<int> octaves;
105     {
106         octaves.push_back(-1);
107         octaves.push_back(0);
108     }
109
110     fso << "regression-cascade"
111         << "{"
112         << "stageType"   << "BOOST"
113         << "featureType" << "ICF"
114         << "octavesNum"  << 2
115         << "width"       << 64
116         << "height"      << 128
117         << "shrinkage"   << 4
118         << "octaves"     << "[";
119
120     for (std::vector<int>::const_iterator it = octaves.begin(); it != octaves.end(); ++it)
121     {
122         int nfeatures  = 100;
123         int shrinkage = 4;
124         float octave = powf(2.f, (float)(*it));
125         cv::Size model = cv::Size( cvRound(64 * octave) / shrinkage, cvRound(128 * octave) / shrinkage );
126
127         cv::Ptr<FeaturePool> pool = FeaturePool::create(model, nfeatures, 10);
128         nfeatures = pool->size();
129         int npositives = 10;
130         int nnegatives = 20;
131
132         cv::Rect boundingBox = cv::Rect( cvRound(20 * octave), cvRound(20  * octave),
133                                          cvRound(64 * octave), cvRound(128 * octave));
134
135         cv::Ptr<ChannelFeatureBuilder> builder = ChannelFeatureBuilder::create("HOG6MagLuv");
136         cv::Ptr<Octave> boost = Octave::create(boundingBox, npositives, nnegatives, *it, shrinkage, builder);
137
138         std::string path = cvtest::TS::ptr()->get_data_path() + "cascadeandhog/sample_training_set";
139         ScaledDataset dataset(path, *it);
140
141         if (boost->train(&dataset, pool, 3, 2))
142         {
143             cv::Mat thresholds;
144             boost->setRejectThresholds(thresholds);
145             boost->write(fso, pool, thresholds);
146         }
147     }
148
149     fso << "]" << "}";
150     fso.release();
151
152
153     cv::FileStorage actual(outXmlPath, cv::FileStorage::READ);
154     cv::FileNode root = actual.getFirstTopLevelNode();
155
156     cv::FileNode fn = root["octaves"];
157     ASSERT_FALSE(fn.empty());
158 }
159
160 #endif