/usr/include/OTB-5.8/otbNeuralNetworkMachineLearningModel.txx is in libotb-dev 5.8.0+dfsg-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | /*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef otbNeuralNetworkMachineLearningModel_txx
#define otbNeuralNetworkMachineLearningModel_txx
#include <fstream>
#include "otbNeuralNetworkMachineLearningModel.h"
#include "otbOpenCVUtils.h"
#include "itkMacro.h" // itkExceptionMacro
namespace otb
{
template<class TInputValue, class TOutputValue>
NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::NeuralNetworkMachineLearningModel() :
m_ANNModel (new CvANN_MLP),
m_TrainMethod(CvANN_MLP_TrainParams::RPROP),
m_ActivateFunction(CvANN_MLP::SIGMOID_SYM),
m_Alpha(1.),
m_Beta(1.),
m_BackPropDWScale(0.1),
m_BackPropMomentScale(0.1),
m_RegPropDW0(0.1),
m_RegPropDWMin(FLT_EPSILON),
m_TermCriteriaType(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS),
m_MaxIter(1000),
m_Epsilon(0.01),
m_CvMatOfLabels(ITK_NULLPTR)
{
this->m_ConfidenceIndex = true;
this->m_IsRegressionSupported = true;
}
template<class TInputValue, class TOutputValue>
NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::~NeuralNetworkMachineLearningModel()
{
delete m_ANNModel;
cvReleaseMat(&m_CvMatOfLabels);
}
/** Sets the topology of the NN */
template<class TInputValue, class TOutputValue>
void NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::SetLayerSizes(const std::vector<unsigned int> layers)
{
const unsigned int nbLayers = layers.size();
if (nbLayers < 3)
itkExceptionMacro(<< "Number of layers in the Neural Network must be >= 3")
m_LayerSizes = layers;
}
/** Converts a ListSample of VariableLengthVector to a CvMat. The user
* is responsible for freeing the output pointer with the
* cvReleaseMat function. A null pointer is resturned in case the
* conversion failed.
*/
template<class TInputValue, class TOutputValue>
void NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::LabelsToMat(const TargetListSampleType * labels,
cv::Mat & output)
{
unsigned int nbSamples = 0;
if (labels != ITK_NULLPTR)
{
nbSamples = labels->Size();
}
// Check for valid listSample
if (nbSamples > 0)
{
// Build an iterator
typename TargetListSampleType::ConstIterator labelSampleIt = labels->Begin();
TargetValueType classLabel;
for (; labelSampleIt != labels->End(); ++labelSampleIt)
{
// Retrieve labelSample
typename TargetListSampleType::MeasurementVectorType labelSample = labelSampleIt.GetMeasurementVector();
classLabel = labelSample[0];
if (m_MapOfLabels.count(classLabel) == 0)
{
m_MapOfLabels[classLabel] = -1;
}
}
unsigned int nbClasses = m_MapOfLabels.size();
typename MapOfLabelsType::iterator itMapOfLabels = m_MapOfLabels.begin();
unsigned itLabel = 0;
for (; itMapOfLabels != m_MapOfLabels.end(); ++itMapOfLabels)
{
classLabel = itMapOfLabels->first;
m_MapOfLabels[classLabel] = itLabel;
if (itLabel == 0)
{
if (m_CvMatOfLabels)
{
cvReleaseMat(&m_CvMatOfLabels);
}
m_CvMatOfLabels = cvCreateMat(1, nbClasses, CV_32SC1);
}
m_CvMatOfLabels->data.i[itLabel] = classLabel;
++itLabel;
}
// Allocate CvMat
// Sample index
unsigned int sampleIdx = 0;
labelSampleIt = labels->Begin();
output.create(nbSamples, nbClasses, CV_32FC1);
output.setTo(-m_Beta);
// Fill the cv matrix
for (; labelSampleIt != labels->End(); ++labelSampleIt, ++sampleIdx)
{
// Retrieve labelSample
typename TargetListSampleType::MeasurementVectorType labelSample = labelSampleIt.GetMeasurementVector();
classLabel = labelSample[0];
unsigned int indexLabel = m_MapOfLabels[classLabel];
output.at<float> (sampleIdx, indexLabel) = m_Beta;
}
}
}
template<class TInputValue, class TOutputValue>
void NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::CreateNetwork()
{
//Create the neural network
const unsigned int nbLayers = m_LayerSizes.size();
if ( nbLayers == 0 )
itkExceptionMacro(<< "Number of layers in the Neural Network must be >= 3")
cv::Mat layers = cv::Mat(nbLayers, 1, CV_32SC1);
for (unsigned int i = 0; i < nbLayers; i++)
{
layers.row(i) = m_LayerSizes[i];
}
m_ANNModel->create(layers, m_ActivateFunction, m_Alpha, m_Beta);
}
template<class TInputValue, class TOutputValue>
CvANN_MLP_TrainParams NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::SetNetworkParameters()
{
CvANN_MLP_TrainParams params;
params.train_method = m_TrainMethod;
params.bp_dw_scale = m_BackPropDWScale;
params.bp_moment_scale = m_BackPropMomentScale;
params.rp_dw0 = m_RegPropDW0;
params.rp_dw_min = m_RegPropDWMin;
CvTermCriteria term_crit = cvTermCriteria(m_TermCriteriaType, m_MaxIter, m_Epsilon);
params.term_crit = term_crit;
return params;
}
template<class TInputValue, class TOutputValue>
void NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::SetupNetworkAndTrain(cv::Mat& labels)
{
//convert listsample to opencv matrix
cv::Mat samples;
otb::ListSampleToMat<InputListSampleType>(this->GetInputListSample(), samples);
this->CreateNetwork();
CvANN_MLP_TrainParams params = this->SetNetworkParameters();
//train the Neural network model
m_ANNModel->train(samples, labels, cv::Mat(), cv::Mat(), params);
}
/** Train the machine learning model for classification*/
template<class TInputValue, class TOutputValue>
void NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::Train()
{
//Transform the targets into a matrix of labels
cv::Mat matOutputANN;
if (this->m_RegressionMode)
{
// MODE REGRESSION
otb::ListSampleToMat<TargetListSampleType>(this->GetTargetListSample(), matOutputANN);
}
else
{
// MODE CLASSIFICATION : store the map between internal labels and output labels
LabelsToMat(this->GetTargetListSample(), matOutputANN);
}
this->SetupNetworkAndTrain(matOutputANN);
}
template<class TInputValue, class TOutputValue>
typename NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::TargetSampleType NeuralNetworkMachineLearningModel<
TInputValue, TOutputValue>::DoPredict(const InputSampleType & input, ConfidenceValueType *quality) const
{
//convert listsample to Mat
cv::Mat sample;
otb::SampleToMat<InputSampleType>(input, sample);
cv::Mat response; //(1, 1, CV_32FC1);
m_ANNModel->predict(sample, response);
TargetSampleType target;
float currentResponse = 0;
float maxResponse = response.at<float> (0, 0);
if (this->m_RegressionMode)
{
// MODE REGRESSION : only output first response
target[0] = maxResponse;
return target;
}
// MODE CLASSIFICATION : find the highest response
float secondResponse = -1e10;
target[0] = m_CvMatOfLabels->data.i[0];
unsigned int nbClasses = m_CvMatOfLabels->cols;
for (unsigned itLabel = 1; itLabel < nbClasses; ++itLabel)
{
currentResponse = response.at<float> (0, itLabel);
if (currentResponse > maxResponse)
{
secondResponse = maxResponse;
maxResponse = currentResponse;
target[0] = m_CvMatOfLabels->data.i[itLabel];
}
else
{
if (currentResponse > secondResponse)
{
secondResponse = currentResponse;
}
}
}
if (quality != ITK_NULLPTR)
{
(*quality) = static_cast<ConfidenceValueType>(maxResponse) - static_cast<ConfidenceValueType>(secondResponse);
}
return target;
}
template<class TInputValue, class TOutputValue>
void NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::Save(const std::string & filename,
const std::string & name)
{
const char* lname = "my_nn";
if ( !name.empty() )
lname = name.c_str();
CvFileStorage* fs = ITK_NULLPTR;
fs = cvOpenFileStorage(filename.c_str(), ITK_NULLPTR, CV_STORAGE_WRITE);
if ( !fs )
{
itkExceptionMacro("Could not open the file " << filename << " for writing");
}
m_ANNModel->write(fs, lname);
if (m_CvMatOfLabels != ITK_NULLPTR)
cvWrite(fs, "class_labels", m_CvMatOfLabels);
cvReleaseFileStorage(&fs);
}
template<class TInputValue, class TOutputValue>
void NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::Load(const std::string & filename,
const std::string & name)
{
const char* lname = ITK_NULLPTR;
if ( !name.empty() )
lname = name.c_str();
cv::FileNode model_node;
cv::FileStorage fs(filename,cv::FileStorage::READ);
if (!fs.isOpened())
{
itkExceptionMacro("Could not open the file " << filename << " for reading");
}
if( lname )
model_node = fs[lname];
else
{
cv::FileNode root = fs.root();
if ( root.size() > 0)
{
model_node = *(root.begin());
}
}
m_ANNModel->read(*fs,*model_node);
m_CvMatOfLabels = (CvMat*)cvReadByName( *fs, *model_node, "class_labels" );
fs.release();
}
template<class TInputValue, class TOutputValue>
bool NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::CanReadFile(const std::string & file)
{
std::ifstream ifs;
ifs.open(file.c_str());
if (!ifs)
{
std::cerr << "Could not read file " << file << std::endl;
return false;
}
while (!ifs.eof())
{
std::string line;
std::getline(ifs, line);
if (line.find(CV_TYPE_NAME_ML_ANN_MLP) != std::string::npos)
{
//std::cout << "Reading a " << CV_TYPE_NAME_ML_ANN_MLP << " model" << std::endl;
return true;
}
}
ifs.close();
return false;
}
template<class TInputValue, class TOutputValue>
bool NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::CanWriteFile(const std::string & itkNotUsed(file))
{
return false;
}
template<class TInputValue, class TOutputValue>
void NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::PrintSelf(std::ostream& os, itk::Indent indent) const
{
// Call superclass implementation
Superclass::PrintSelf(os, indent);
}
} //end namespace otb
#endif
|