From e01b3b603428fbebb0b40f614fb8c8287488f85d Mon Sep 17 00:00:00 2001 From: Julien Michel <julien.michel@orfeo-toolbox.org> Date: Fri, 20 Feb 2015 17:51:01 +0100 Subject: [PATCH] ENH: Replacing remaining calls to CalculateNumberOfStreamingDivisions by calls to StreamingManager, which is the up-to-date way of doing streaming --- .../app/otbComputeConfusionMatrix.cxx | 31 ++++++------- .../app/otbSOMClassification.cxx | 43 ++++++++----------- .../AppClassification/otb-module.cmake | 1 + .../AppImageUtils/otb-module.cmake | 1 + .../include/otbImageToPointSetFilter.h | 12 +++--- .../include/otbImageToPointSetFilter.txx | 19 +++----- Modules/Core/PointSet/otb-module.cmake | 1 + 7 files changed, 50 insertions(+), 58 deletions(-) diff --git a/Modules/Applications/AppClassification/app/otbComputeConfusionMatrix.cxx b/Modules/Applications/AppClassification/app/otbComputeConfusionMatrix.cxx index 65252ff5c6..3aed87bd66 100644 --- a/Modules/Applications/AppClassification/app/otbComputeConfusionMatrix.cxx +++ b/Modules/Applications/AppClassification/app/otbComputeConfusionMatrix.cxx @@ -22,8 +22,7 @@ #include "otbOGRDataSourceToLabelImageFilter.h" #include "itkImageRegionConstIterator.h" -#include "otbStreamingTraits.h" - +#include "otbRAMDrivenAdaptativeStreamingManager.h" #include "otbConfusionMatrixMeasurements.h" @@ -49,10 +48,9 @@ public: typedef itk::ImageRegionConstIterator<Int32ImageType> ImageIteratorType; typedef otb::OGRDataSourceToLabelImageFilter<Int32ImageType> RasterizeFilterType; - - typedef otb::StreamingTraits<Int32ImageType> StreamingTraitsType; - - typedef itk::ImageRegionSplitter<2> SplitterType; + + typedef RAMDrivenAdaptativeStreamingManager + <Int32ImageType> RAMDrivenAdaptativeStreamingManagerType; typedef Int32ImageType::RegionType RegionType; @@ -242,14 +240,17 @@ private: } // Prepare local streaming - SplitterType::Pointer splitter = SplitterType::New(); - unsigned int numberOfStreamDivisions = StreamingTraitsType::CalculateNumberOfStreamDivisions( - input, - input->GetLargestPossibleRegion(), - splitter, - otb::SET_BUFFER_MEMORY_SIZE, - 0, 1048576*GetParameterInt("ram"), 0); - RegionType streamRegion; + + RAMDrivenAdaptativeStreamingManagerType::Pointer + streamingManager = RAMDrivenAdaptativeStreamingManagerType::New(); + int availableRAM = GetParameterInt("ram"); + streamingManager->SetAvailableRAMInMB(availableRAM); + float bias = 2.0; // empiric value; + streamingManager->SetBias(bias); + + streamingManager->PrepareStreaming(input, input->GetLargestPossibleRegion()); + + unsigned long numberOfStreamDivisions = streamingManager->GetNumberOfSplits(); otbAppLogINFO("Number of stream divisions : "<<numberOfStreamDivisions); @@ -262,7 +263,7 @@ private: for (unsigned int index = 0; index < numberOfStreamDivisions; index++) { - streamRegion = splitter->GetSplit(index, numberOfStreamDivisions, reference->GetLargestPossibleRegion()); + RegionType streamRegion = streamingManager->GetSplit(index); input->SetRequestedRegion(streamRegion); input->PropagateRequestedRegion(); diff --git a/Modules/Applications/AppClassification/app/otbSOMClassification.cxx b/Modules/Applications/AppClassification/app/otbSOMClassification.cxx index aed041c0a6..87f9313cfc 100644 --- a/Modules/Applications/AppClassification/app/otbSOMClassification.cxx +++ b/Modules/Applications/AppClassification/app/otbSOMClassification.cxx @@ -21,7 +21,7 @@ #include "otbSOMMap.h" #include "otbSOM.h" #include "otbSOMImageClassificationFilter.h" -#include "otbStreamingTraits.h" +#include "otbRAMDrivenAdaptativeStreamingManager.h" #include "itkImageRegionConstIterator.h" #include "itkImageRandomNonRepeatingConstIteratorWithIndex.h" @@ -53,8 +53,9 @@ public: typedef itk::Statistics::ListSample<SampleType> ListSampleType; typedef otb::SOM<ListSampleType, SOMMapType> EstimatorType; - typedef otb::StreamingTraits<FloatVectorImageType> StreamingTraitsType; - typedef itk::ImageRegionSplitter<2> SplitterType; + typedef RAMDrivenAdaptativeStreamingManager + <FloatVectorImageType> RAMDrivenAdaptativeStreamingManagerType; + typedef FloatVectorImageType::RegionType RegionType; typedef itk::ImageRegionConstIterator<FloatVectorImageType> IteratorType; @@ -99,10 +100,6 @@ private: SetParameterDescription("ts", "Maximum training set size (in pixels)"); MandatoryOff("ts"); - AddParameter(ParameterType_Int, "sl", "StreamingLines"); - SetParameterDescription("sl", "Number of lines in each streaming block (used during data sampling)"); - MandatoryOff("sl"); - AddParameter(ParameterType_OutputImage, "som", "SOM Map"); SetParameterDescription("som","Output image containing the Self-Organizing Map"); MandatoryOff("som"); @@ -203,24 +200,18 @@ private: RegionType largestRegion = input->GetLargestPossibleRegion(); // Setting up local streaming capabilities - SplitterType::Pointer splitter = SplitterType::New(); - unsigned int numberOfStreamDivisions; - if (HasValue("sl")) - { - numberOfStreamDivisions = StreamingTraitsType::CalculateNumberOfStreamDivisions(input, - largestRegion, - splitter, - otb::SET_BUFFER_NUMBER_OF_LINES, - 0, 0, GetParameterInt("sl")); - } - else - { - numberOfStreamDivisions = StreamingTraitsType::CalculateNumberOfStreamDivisions(input, - largestRegion, - splitter, - otb::SET_BUFFER_MEMORY_SIZE, - 0, 1048576*GetParameterInt("ram"), 0); - } + + RAMDrivenAdaptativeStreamingManagerType::Pointer + streamingManager = RAMDrivenAdaptativeStreamingManagerType::New(); + int availableRAM = GetParameterInt("ram"); + streamingManager->SetAvailableRAMInMB(availableRAM); + float bias = 2.0; // empiric value; + streamingManager->SetBias(bias); + + streamingManager->PrepareStreaming(input, largestRegion); + + unsigned long numberOfStreamDivisions = streamingManager->GetNumberOfSplits(); + otbAppLogINFO("The images will be streamed into "<<numberOfStreamDivisions<<" parts."); @@ -258,7 +249,7 @@ private: unsigned int localNbSamples=0; piece = randPerm[index]; - streamingRegion = splitter->GetSplit(piece, numberOfStreamDivisions, largestRegion); + streamingRegion = streamingManager->GetSplit(piece); //otbAppLogINFO("Processing region: "<<streamingRegion); input->SetRequestedRegion(streamingRegion); diff --git a/Modules/Applications/AppClassification/otb-module.cmake b/Modules/Applications/AppClassification/otb-module.cmake index 453880987b..e40002ec7b 100644 --- a/Modules/Applications/AppClassification/otb-module.cmake +++ b/Modules/Applications/AppClassification/otb-module.cmake @@ -26,6 +26,7 @@ otb_module(OTBAppClassification OTBImageManipulation OTBObjectList OTBCommon + OTBStreaming TEST_DEPENDS OTBTestKernel OTBCommandLine diff --git a/Modules/Applications/AppImageUtils/otb-module.cmake b/Modules/Applications/AppImageUtils/otb-module.cmake index 95aadda68b..5aaa295188 100644 --- a/Modules/Applications/AppImageUtils/otb-module.cmake +++ b/Modules/Applications/AppImageUtils/otb-module.cmake @@ -18,6 +18,7 @@ otb_module(OTBAppImageUtils OTBKMZWriter OTBOSSIMAdapters OTBObjectList + OTBStreaming TEST_DEPENDS OTBTestKernel OTBCommandLine diff --git a/Modules/Core/PointSet/include/otbImageToPointSetFilter.h b/Modules/Core/PointSet/include/otbImageToPointSetFilter.h index bbea940233..c272fca339 100644 --- a/Modules/Core/PointSet/include/otbImageToPointSetFilter.h +++ b/Modules/Core/PointSet/include/otbImageToPointSetFilter.h @@ -19,7 +19,9 @@ #define __otbImageToPointSetFilter_h #include "otbPointSetSource.h" -#include "otbStreamingTraits.h" +#include "otbRAMDrivenAdaptativeStreamingManager.h" + +// TODO: This class needs a refactoring to become a persistent filter namespace otb { @@ -113,10 +115,10 @@ protected: /** End Multi-threading implementation */ /** Setup for streaming */ - typedef StreamingTraits<InputImageType> StreamingTraitsType; - typedef itk::ImageRegionSplitter<itkGetStaticConstMacro(InputImageDimension)> SplitterType; - typedef typename SplitterType::Pointer RegionSplitterPointer; - RegionSplitterPointer m_RegionSplitter; + typedef RAMDrivenAdaptativeStreamingManager<InputImageType> StreamingManagerType; + typedef typename StreamingManagerType::Pointer StreamingManagerPointer; + + StreamingManagerPointer m_StreamingManager; private: ImageToPointSetFilter(const ImageToPointSetFilter &); //purposely not implemented diff --git a/Modules/Core/PointSet/include/otbImageToPointSetFilter.txx b/Modules/Core/PointSet/include/otbImageToPointSetFilter.txx index 370f093e0f..3bd2b13444 100644 --- a/Modules/Core/PointSet/include/otbImageToPointSetFilter.txx +++ b/Modules/Core/PointSet/include/otbImageToPointSetFilter.txx @@ -42,8 +42,7 @@ ImageToPointSetFilter<TInputImage, TOutputPointSet> m_PointDataContainerPerThread.clear(); // create default region splitter - m_RegionSplitter = itk::ImageRegionSplitter<itkGetStaticConstMacro(InputImageDimension)>::New(); - + m_StreamingManager = StreamingManagerType::New(); } /** @@ -138,21 +137,17 @@ ImageToPointSetFilter<TInputImage, TOutputPointSet> PointDataContainerType * outputPointDataContainer = this->GetOutput()->GetPointData(); outputPointDataContainer->Initialize(); - + typename TInputImage::RegionType inputRegion = this->GetInput()->GetLargestPossibleRegion(); - unsigned int numDivisions; - numDivisions = StreamingTraitsType - ::CalculateNumberOfStreamDivisions(this->GetInput(), - this->GetInput()->GetLargestPossibleRegion(), - m_RegionSplitter, - SET_AUTOMATIC_NUMBER_OF_STREAM_DIVISIONS, - 0, 0, 0); - // Input is an image, cast away the constness so we can set // the requested region. InputImagePointer input = const_cast<TInputImage *> (this->GetInput()); + m_StreamingManager->PrepareStreaming(input,inputRegion); + + unsigned long numDivisions = m_StreamingManager->GetNumberOfSplits(); + /** * Loop over the number of pieces, execute the upstream pipeline on each * piece, and copy the results into the output image. @@ -163,7 +158,7 @@ ImageToPointSetFilter<TInputImage, TOutputPointSet> piece < numDivisions && !this->GetAbortGenerateData(); piece++) { - streamRegion = m_RegionSplitter->GetSplit(piece, numDivisions, inputRegion); + streamRegion = m_StreamingManager->GetSplit(piece); typedef itk::ImageToImageFilterDetail::ImageRegionCopier<itkGetStaticConstMacro(InputImageDimension), itkGetStaticConstMacro(InputImageDimension)> OutputToInputRegionCopierType; diff --git a/Modules/Core/PointSet/otb-module.cmake b/Modules/Core/PointSet/otb-module.cmake index 04c03982cb..2a7c22396e 100644 --- a/Modules/Core/PointSet/otb-module.cmake +++ b/Modules/Core/PointSet/otb-module.cmake @@ -10,6 +10,7 @@ otb_module(OTBPointSet DEPENDS OTBCommon OTBInterpolation + OTBStreaming TEST_DEPENDS OTBTestKernel -- GitLab