OTB  6.7.0
Orfeo Toolbox
otbTrainImagesBase.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 #ifndef otbTrainImagesBase_h
21 #define otbTrainImagesBase_h
22 
26 
31 #include <string>
32 
33 namespace otb
34 {
35 namespace Wrapper
36 {
37 
48 {
49 public:
55 
57  itkTypeMacro( TrainImagesBase, Superclass );
58 
61 
63 
64 protected:
65 
66  typedef enum
67  {
70  struct SamplingRates;
71  class TrainFileNamesHandler;
72 
76  void InitIO();
77 
81  void InitSampling();
82 
85  void InitClassification();
88 
95  void ComputePolygonStatistics(FloatVectorImageListType *imageList, const std::vector<std::string> &vectorFileNames,
96  const std::vector<std::string> &statisticsFileNames);
97 
103  SamplingRates ComputeFinalMaximumSamplingRates(bool dedicatedValidation);
104 
105 
113  void ComputeSamplingRate(const std::vector<std::string> &statisticsFileNames,
114  const std::string &ratesFileName,
115  long maximum);
116 
123  void TrainModel(FloatVectorImageListType *imageList, const std::vector<std::string> &sampleTrainFileNames,
124  const std::vector<std::string> &sampleValidationFileNames);
125 
135  void SelectAndExtractSamples(FloatVectorImageType *image, std::string vectorFileName, std::string sampleFileName,
136  std::string statisticsFileName, std::string ratesFileName, SamplingStrategy strategy,
137  std::string selectedField = "");
138 
147  void SelectAndExtractTrainSamples(const TrainFileNamesHandler &fileNames, FloatVectorImageListType *imageList,
148  std::vector<std::string> vectorFileNames, SamplingStrategy strategy,
149  std::string selectedFieldName = "");
150 
151 
161  void SelectAndExtractValidationSamples(const TrainFileNamesHandler &fileNames, FloatVectorImageListType *imageList,
162  const std::vector<std::string> &validationVectorFileList = std::vector<std::string>());
163 
170  void SplitTrainingToValidationSamples(const TrainFileNamesHandler &fileNames, FloatVectorImageListType *imageList);
171 
172 private:
173 
182  void SplitTrainingAndValidationSamples(FloatVectorImageType *image, std::string sampleFileName,
183  std::string sampleTrainFileName, std::string sampleValidFileName,
184  std::string ratesTrainFileName);
185 
186 
187 protected:
188 
190  {
191  long int fmt;
192  long int fmv;
193  };
194 
202  {
203  public :
204  void CreateTemporaryFileNames(std::string outModel, size_t nbInputs, bool dedicatedValidation)
205  {
206 
207  if( dedicatedValidation )
208  {
209  rateTrainOut = outModel + "_ratesTrain.csv";
210  }
211  else
212  {
213  rateTrainOut = outModel + "_rates.csv";
214  }
215 
216  rateValidOut = outModel + "_ratesValid.csv";
217  for( unsigned int i = 0; i < nbInputs; i++ )
218  {
219  std::ostringstream oss;
220  oss << i + 1;
221  std::string strIndex( oss.str() );
222  if( dedicatedValidation )
223  {
224  polyStatTrainOutputs.push_back( outModel + "_statsTrain_" + strIndex + ".xml" );
225  polyStatValidOutputs.push_back( outModel + "_statsValid_" + strIndex + ".xml" );
226  ratesTrainOutputs.push_back( outModel + "_ratesTrain_" + strIndex + ".csv" );
227  ratesValidOutputs.push_back( outModel + "_ratesValid_" + strIndex + ".csv" );
228  sampleOutputs.push_back( outModel + "_samplesTrain_" + strIndex + ".shp" );
229  }
230  else
231  {
232  polyStatTrainOutputs.push_back( outModel + "_stats_" + strIndex + ".xml" );
233  ratesTrainOutputs.push_back( outModel + "_rates_" + strIndex + ".csv" );
234  sampleOutputs.push_back( outModel + "_samples_" + strIndex + ".shp" );
235  }
236  sampleTrainOutputs.push_back( outModel + "_samplesTrain_" + strIndex + ".shp" );
237  sampleValidOutputs.push_back( outModel + "_samplesValid_" + strIndex + ".shp" );
238  }
239  }
240 
241  void clear()
242  {
243  for( unsigned int i = 0; i < polyStatTrainOutputs.size(); i++ )
245  for( unsigned int i = 0; i < polyStatValidOutputs.size(); i++ )
247  for( unsigned int i = 0; i < ratesTrainOutputs.size(); i++ )
249  for( unsigned int i = 0; i < ratesValidOutputs.size(); i++ )
251  for( unsigned int i = 0; i < sampleOutputs.size(); i++ )
253  for( unsigned int i = 0; i < sampleTrainOutputs.size(); i++ )
255  for( unsigned int i = 0; i < sampleValidOutputs.size(); i++ )
257  for( unsigned int i = 0; i < tmpVectorFileList.size(); i++ )
259  }
260 
261  public:
262  std::vector<std::string> polyStatTrainOutputs;
263  std::vector<std::string> polyStatValidOutputs;
264  std::vector<std::string> ratesTrainOutputs;
265  std::vector<std::string> ratesValidOutputs;
266  std::vector<std::string> sampleOutputs;
267  std::vector<std::string> sampleTrainOutputs;
268  std::vector<std::string> sampleValidOutputs;
269  std::vector<std::string> tmpVectorFileList;
270  std::string rateValidOut;
271  std::string rateTrainOut;
272 
273  private:
274  bool RemoveFile(std::string &filePath)
275  {
276  bool res = true;
277  if( itksys::SystemTools::FileExists( filePath ) )
278  {
279  size_t posExt = filePath.rfind( '.' );
280  if( posExt != std::string::npos && filePath.compare( posExt, std::string::npos, ".shp" ) == 0 )
281  {
282  std::string shxPath = filePath.substr( 0, posExt ) + std::string( ".shx" );
283  std::string dbfPath = filePath.substr( 0, posExt ) + std::string( ".dbf" );
284  std::string prjPath = filePath.substr( 0, posExt ) + std::string( ".prj" );
285  RemoveFile( shxPath );
286  RemoveFile( dbfPath );
287  RemoveFile( prjPath );
288  }
289  res = itksys::SystemTools::RemoveFile( filePath );
290  if( !res )
291  {
292  //otbAppLogINFO( <<"Unable to remove file "<<filePath );
293  }
294  }
295  return res;
296  }
297  };
298 
299 };
300 
301 } // end namespace Wrapper
302 } // end namespace otb
303 
304 #ifndef OTB_MANUAL_INSTANTIATION
305 #include "otbTrainImagesBase.hxx"
306 #endif
307 
308 #endif //otbTrainImagesBase_h
void TrainModel(FloatVectorImageListType *imageList, const std::vector< std::string > &sampleTrainFileNames, const std::vector< std::string > &sampleValidationFileNames)
void CreateTemporaryFileNames(std::string outModel, vcl_size_t nbInputs, bool dedicatedValidation)
Creation of an "otb" vector image which contains metadata.
This class is a base class for composite applications.
void ComputeSamplingRate(const std::vector< std::string > &statisticsFileNames, const std::string &ratesFileName, long maximum)
otb::OGRDataToSamplePositionFilter< FloatVectorImageType, UInt8ImageType, otb::PeriodicSampler > PeriodicSamplerType
void ComputePolygonStatistics(FloatVectorImageListType *imageList, const std::vector< std::string > &vectorFileNames, const std::vector< std::string > &statisticsFileNames)
void SplitTrainingAndValidationSamples(FloatVectorImageType *image, std::string sampleFileName, std::string sampleTrainFileName, std::string sampleValidFileName, std::string ratesTrainFileName)
Extracts sample position from an image using a persistent filter.
otb::SamplingRateCalculator::MapRateType MapRateType
SamplingRates ComputeFinalMaximumSamplingRates(bool dedicatedValidation)
itk::SmartPointer< Self > Pointer
void SelectAndExtractTrainSamples(const TrainFileNamesHandler &fileNames, FloatVectorImageListType *imageList, std::vector< std::string > vectorFileNames, SamplingStrategy strategy, std::string selectedFieldName="")
This class is a generic all-purpose wrapping around an std::vector<itk::SmartPointer<ObjectType> >...
Definition: otbObjectList.h:40
void SplitTrainingToValidationSamples(const TrainFileNamesHandler &fileNames, FloatVectorImageListType *imageList)
void SelectAndExtractValidationSamples(const TrainFileNamesHandler &fileNames, FloatVectorImageListType *imageList, const std::vector< std::string > &validationVectorFileList=std::vector< std::string >())
std::map< std::string, TripletType > MapRateType
void SelectAndExtractSamples(FloatVectorImageType *image, std::string vectorFileName, std::string sampleFileName, std::string statisticsFileName, std::string ratesFileName, SamplingStrategy strategy, std::string selectedField="")
itk::SmartPointer< const Self > ConstPointer
CompositeApplication Superclass
Base class for the TrainImagesClassifier.