1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2008-2012, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
43 // Training application for Soft Cascades.
45 #include <sft/common.hpp>
47 #include <sft/dataset.hpp>
48 #include <sft/config.hpp>
50 #include <opencv2/core/core_c.h>
52 int main(int argc, char** argv)
57 "{help h usage ? | | print this message }"
58 "{config c | | path to configuration xml }"
61 cv::CommandLineParser parser(argc, argv, keys);
62 parser.about("Soft cascade training application.");
64 if (parser.has("help"))
66 parser.printMessage();
76 string configPath = parser.get<string>("config");
77 if (configPath.empty())
79 std::cout << "Configuration file is missing or empty. Could not start training." << std::endl;
83 std::cout << "Read configuration from file " << configPath << std::endl;
84 cv::FileStorage fs(configPath, cv::FileStorage::READ);
87 std::cout << "Configuration file " << configPath << " can't be opened." << std::endl;
94 std::cout << std::endl << "Training will be executed for configuration:" << std::endl << cfg << std::endl;
96 // 2. check and open output file
97 cv::FileStorage fso(cfg.outXmlPath, cv::FileStorage::WRITE);
100 std::cout << "Training stopped. Output classifier Xml file " << cfg.outXmlPath << " can't be opened." << std::endl;
104 fso << cfg.cascadeName
106 << "stageType" << "BOOST"
107 << "featureType" << cfg.featureType
108 << "octavesNum" << (int)cfg.octaves.size()
109 << "width" << cfg.modelWinSize.width
110 << "height" << cfg.modelWinSize.height
111 << "shrinkage" << cfg.shrinkage
114 // 3. Train all octaves
115 for (ivector::const_iterator it = cfg.octaves.begin(); it != cfg.octaves.end(); ++it)
117 // a. create random feature pool
118 int nfeatures = cfg.poolSize;
119 cv::Size model = cfg.model(it);
120 std::cout << "Model " << model << std::endl;
122 int nchannels = (cfg.featureType == "HOG6MagLuv") ? 10: 8;
124 std::cout << "number of feature channels is " << nchannels << std::endl;
126 cv::Ptr<cv::FeaturePool> pool = cv::FeaturePool::create(model, nfeatures, nchannels);
127 nfeatures = pool->size();
130 int npositives = cfg.positives;
131 int nnegatives = cfg.negatives;
132 int shrinkage = cfg.shrinkage;
133 cv::Rect boundingBox = cfg.bbox(it);
134 std::cout << "Object bounding box" << boundingBox << std::endl;
136 typedef cv::Octave Octave;
138 cv::Ptr<cv::ChannelFeatureBuilder> builder = cv::ChannelFeatureBuilder::create(cfg.featureType);
139 std::cout << "Channel builder " << builder->info()->name() << std::endl;
140 cv::Ptr<Octave> boost = Octave::create(boundingBox, npositives, nnegatives, *it, shrinkage, builder);
142 std::string path = cfg.trainPath;
143 sft::ScaledDataset dataset(path, *it);
145 if (boost->train(&dataset, pool, cfg.weaks, cfg.treeDepth))
147 CvFileStorage* fout = cvOpenFileStorage(cfg.resPath(it).c_str(), 0, CV_STORAGE_WRITE);
148 boost->write(fout, cfg.cascadeName);
150 cvReleaseFileStorage( &fout);
153 boost->setRejectThresholds(thresholds);
155 boost->write(fso, pool, thresholds);
157 cv::FileStorage tfs(("thresholds." + cfg.resPath(it)).c_str(), cv::FileStorage::WRITE);
158 tfs << "thresholds" << thresholds;
160 std::cout << "Octave " << *it << " was successfully trained..." << std::endl;
166 std::cout << "Training complete..." << std::endl;