Commit e2ffaf62 authored by Jordi Inglada's avatar Jordi Inglada

ENH: resolve issues from code review

parent f81b2169
......@@ -133,13 +133,14 @@ private:
MandatoryOff("confmap");
AddParameter(ParameterType_OutputImage,"probamap", "Probability map");
SetParameterDescription("probamap","");
SetParameterDescription("probamap","Probability of each class for each pixel. This is an image having a number of bands equal to the number of classes in the model. This is only implemented for the Shark Random Forest classifier at this point.");
SetDefaultOutputPixelType("probamap",ImagePixelType_uint16);
MandatoryOff("probamap");
AddRAMParameter();
AddParameter(ParameterType_Int, "classe", "number of output classes");
SetDefaultParameterInt("classe", 20);
AddParameter(ParameterType_Int, "nbclasses", "Number of classes in the model");
SetDefaultParameterInt("nbclasses", 20);
SetParameterDescription("nbclasses","The number of classes is needed for the probamap output in order to set the number of output bands.");
// Doc example parameter settings
SetDocExampleParameterValue("in", "QB_1_ortho.tif");
......@@ -240,7 +241,7 @@ private:
m_ClassificationFilter->SetUseProbaMap(true);
if(m_Model->HasProbaIndex())
{
m_ClassificationFilter->SetNumberOfClasses(GetParameterInt("classe"));
m_ClassificationFilter->SetNumberOfClasses(GetParameterInt("nbclasses"));
SetParameterOutputImage<ProbaImageType>("probamap",m_ClassificationFilter->GetOutputProba());
}
else
......
......@@ -242,7 +242,7 @@ foreach(classifier ${classifierList})
-imstat ${INPUTDATA}/Classification/clImageStatisticsQB1${stat_input_format}
-out ${TEMP}/${OUTRASTER} ${raster_output_option}
-confmap ${TEMP}/${OUTCONFMAP}
-classe 4
-nbclasses 4
-probamap ${TEMP}/${OUTPROBAMAP}
VALID ${raster_comparison_three}
......
......@@ -88,14 +88,13 @@ typename ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
::GetOutputProba()
{
//std::cout << "Getoutprob" << std::endl;
if (this->GetNumberOfOutputs() < 2)
{
return ITK_NULLPTR;
return nullptr;
}
return static_cast<ProbaImageType *>(this->itk::ProcessObject::GetOutput(2));
}
template <class TInputImage, class TOutputImage, class TMaskImage>
void
ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
......@@ -117,16 +116,18 @@ ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
template <class TInputImage, class TOutputImage, class TMaskImage>
void
ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
::ClassicThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId)
::ClassicThreadedGenerateData(const OutputImageRegionType& outputRegionForThread,
itk::ThreadIdType threadId)
{
// Get the input pointers
// Get the input pointers
InputImageConstPointerType inputPtr = this->GetInput();
MaskImageConstPointerType inputMaskPtr = this->GetInputMask();
OutputImagePointerType outputPtr = this->GetOutput();
ConfidenceImagePointerType confidencePtr = this->GetOutputConfidence();
ProbaImagePointerType probaPtr = this->GetOutputProba();
// Progress reporting
itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
itk::ProgressReporter progress(this, threadId,
outputRegionForThread.GetNumberOfPixels());
// Define iterators
typedef itk::ImageRegionConstIterator<InputImageType> InputIteratorType;
......@@ -134,7 +135,7 @@ ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
typedef itk::ImageRegionIterator<OutputImageType> OutputIteratorType;
typedef itk::ImageRegionIterator<ConfidenceImageType> ConfidenceMapIteratorType;
typedef itk::ImageRegionIterator<ProbaImageType> ProbaMapIteratorType;
InputIteratorType inIt(inputPtr, outputRegionForThread);
OutputIteratorType outIt(outputPtr, outputRegionForThread);
......@@ -147,7 +148,8 @@ ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
}
// setup iterator for confidence map
bool computeConfidenceMap(m_UseConfidenceMap && m_Model->HasConfidenceIndex() && !m_Model->GetRegressionMode());
bool computeConfidenceMap(m_UseConfidenceMap && m_Model->HasConfidenceIndex() &&
!m_Model->GetRegressionMode());
ConfidenceMapIteratorType confidenceIt;
if (computeConfidenceMap)
{
......@@ -156,21 +158,23 @@ ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
}
// setup iterator for proba map
bool computeProbaMap(m_UseProbaMap && m_Model->HasProbaIndex() && !m_Model->GetRegressionMode());
bool computeProbaMap(m_UseProbaMap && m_Model->HasProbaIndex() &&
!m_Model->GetRegressionMode());
ProbaMapIteratorType probaIt;
if(computeProbaMap)
{
probaIt = ProbaMapIteratorType(probaPtr,outputRegionForThread);
probaIt.GoToBegin();
}
bool validPoint = true;
double confidenceIndex = 0.0;
ProbaSampleType probaVector{m_NumberOfClasses};
// Walk the part of the image
for (inIt.GoToBegin(), outIt.GoToBegin(); !inIt.IsAtEnd() && !outIt.IsAtEnd(); ++inIt, ++outIt)
for (inIt.GoToBegin(), outIt.GoToBegin(); !inIt.IsAtEnd() && !outIt.IsAtEnd();
++inIt, ++outIt)
{
// Check pixel validity
if (inputMaskPtr)
......@@ -180,22 +184,22 @@ ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
}
// If point is valid
if (validPoint)
{
{
// Classifify
if (computeProbaMap)
{
outIt.Set(m_Model->Predict(inIt.Get(),&confidenceIndex,&probaVector)[0]);
}
{
outIt.Set(m_Model->Predict(inIt.Get(),&confidenceIndex,
&probaVector)[0]);
}
else if (computeConfidenceMap)
{
{
outIt.Set(m_Model->Predict(inIt.Get(),&confidenceIndex)[0]);
}
}
else
{
{
outIt.Set(m_Model->Predict(inIt.Get())[0]);
}
}
}
else
{
// else, set default value
......@@ -208,17 +212,10 @@ ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
++confidenceIt;
}
if (computeProbaMap)
{
ProbaImageType::PixelType probVect{probaVector.Size()};
probVect.Fill(0.0);
for (size_t t =0; t < probaVector.Size();t++)
{
probVect[t] = probaVector[t];
std::cout << probVect[t] << '\t' << probaVector[t] << '\n';
}
probaIt.Set(probVect);
++probaIt;
}
{
probaIt.Set(probaVector);
++probaIt;
}
progress.CompletedPixel();
}
......@@ -227,23 +224,24 @@ ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
template <class TInputImage, class TOutputImage, class TMaskImage>
void
ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
::BatchThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId)
::BatchThreadedGenerateData(const OutputImageRegionType& outputRegionForThread,
itk::ThreadIdType threadId)
{
//std::cout << "batch mode" << std::endl;
bool computeConfidenceMap(m_UseConfidenceMap && m_Model->HasConfidenceIndex()
bool computeConfidenceMap(m_UseConfidenceMap && m_Model->HasConfidenceIndex()
&& !m_Model->GetRegressionMode());
bool computeProbaMap(m_UseProbaMap && m_Model->HasProbaIndex()
&& !m_Model->GetRegressionMode());
bool computeProbaMap(m_UseProbaMap && m_Model->HasProbaIndex()
&& !m_Model->GetRegressionMode());
// Get the input pointers
InputImageConstPointerType inputPtr = this->GetInput();
MaskImageConstPointerType inputMaskPtr = this->GetInputMask();
OutputImagePointerType outputPtr = this->GetOutput();
ConfidenceImagePointerType confidencePtr = this->GetOutputConfidence();
ProbaImagePointerType probaPtr = this->GetOutputProba();
ProbaImagePointerType probaPtr = this->GetOutputProba();
// Progress reporting
itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
itk::ProgressReporter progress(this, threadId,
outputRegionForThread.GetNumberOfPixels());
// Define iterators
typedef itk::ImageRegionConstIterator<InputImageType> InputIteratorType;
......@@ -262,14 +260,10 @@ ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
maskIt.GoToBegin();
}
// typedef typename ModelType::InputValueType InputValueType;
typedef typename ModelType::InputSampleType InputSampleType;
typedef typename ModelType::InputListSampleType InputListSampleType;
typedef typename ModelType::TargetValueType TargetValueType;
// typedef typename ModelType::TargetSampleType TargetSampleType;
typedef typename ModelType::TargetListSampleType TargetListSampleType;
// typedef typename ModelType::ConfidenceValueType ConfidenceValueType;
// typedef typename ModelType::ConfidenceSampleType ConfidenceSampleType;
typedef typename ModelType::ConfidenceListSampleType ConfidenceListSampleType;
typedef typename ModelType::ProbaListSampleType ProbaListSampleType;
typename InputListSampleType::Pointer samples = InputListSampleType::New();
......@@ -340,23 +334,20 @@ ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
if(computeConfidenceMap)
{
confidenceIndex = confidences->GetMeasurementVector(labIt.GetInstanceIdentifier())[0];
confidenceIndex =
confidences->GetMeasurementVector(labIt.GetInstanceIdentifier())[0];
}
if(computeProbaMap)
{
//std::cout << "imagecfilter before get" << std::endl;
probaValues = probas->GetMeasurementVector(labIt.GetInstanceIdentifier());
//std::cout << "imageclfilt after get" << std::endl;
}
++labIt;
{
probaValues = probas->GetMeasurementVector(labIt.GetInstanceIdentifier());
}
++labIt;
}
else
{
labelValue = m_DefaultLabel;
}
outIt.Set(labelValue);
if(computeConfidenceMap)
......@@ -365,17 +356,19 @@ ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
++confidenceIt;
}
if(computeProbaMap)
{
probaIt.Set(probaValues);
++probaIt;
}
{
probaIt.Set(probaValues);
++probaIt;
}
progress.CompletedPixel();
}
}
template <class TInputImage, class TOutputImage, class TMaskImage>
void
ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>
::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId)
ImageClassificationFilter<TInputImage, TOutputImage,
TMaskImage>::ThreadedGenerateData(
const OutputImageRegionType& outputRegionForThread,
itk::ThreadIdType threadId)
{
if(m_BatchMode)
{
......
......@@ -132,13 +132,8 @@ BoostMachineLearningModel<TInputValue,TOutputValue>
#endif
);
}
if (proba != ITK_NULLPTR)
{
if (!this->m_ProbaIndex)
{
itkExceptionMacro("Probability per class not available for this classifier !");
}
}
if (proba != nullptr && !this->m_ProbaIndex)
itkExceptionMacro("Probability per class not available for this classifier !");
target[0] = static_cast<TOutputValue>(result);
return target;
......
......@@ -140,15 +140,10 @@ DecisionTreeMachineLearningModel<TInputValue,TOutputValue>
itkExceptionMacro("Confidence index not available for this classifier !");
}
}
if (proba != ITK_NULLPTR)
{
if (!this->m_ProbaIndex)
{
itkExceptionMacro("Probability per class not available for this classifier !");
}
}
if (proba != nullptr && !this->m_ProbaIndex)
itkExceptionMacro("Probability per class not available for this classifier !");
return target;
return target;
}
template <class TInputValue, class TOutputValue>
......
......@@ -103,13 +103,8 @@ GradientBoostedTreeMachineLearningModel<TInputValue,TOutputValue>
itkExceptionMacro("Confidence index not available for this classifier !");
}
}
if (proba != ITK_NULLPTR)
{
if (!this->m_ProbaIndex)
{
itkExceptionMacro("Probability per class not available for this classifier !");
}
}
if (proba != nullptr && !m_ProbaIndex)
itkExceptionMacro("Probability per class not available for this classifier !");
return target;
}
......
......@@ -135,13 +135,8 @@ KNearestNeighborsMachineLearningModel<TInputValue,TTargetValue>
}
(*quality) = static_cast<ConfidenceValueType>(accuracy);
}
if (proba != ITK_NULLPTR)
{
if (!this->m_ProbaIndex)
{
if (proba != nullptr && !this->m_ProbaIndex)
itkExceptionMacro("Probability per class not available for this classifier !");
}
}
// Decision rule :
// VOTING is OpenCV default behaviour for classification
......
......@@ -129,13 +129,8 @@ LibSVMMachineLearningModel<TInputValue,TOutputValue>
// terminate node
x[input.Size()].index = -1;
x[input.Size()].value = 0;
if (proba != ITK_NULLPTR)
{
if (!this->m_ProbaIndex)
{
itkExceptionMacro("Probability per class not available for this classifier !");
}
}
if (proba != nullptr && !this->m_ProbaIndex)
itkExceptionMacro("Probability per class not available for this classifier !");
if (quality != nullptr)
{
......
......@@ -282,13 +282,8 @@ typename NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::TargetSam
{
(*quality) = static_cast<ConfidenceValueType>(maxResponse) - static_cast<ConfidenceValueType>(secondResponse);
}
if (proba != ITK_NULLPTR)
{
if (!this->m_ProbaIndex)
{
itkExceptionMacro("Probability per class not available for this classifier !");
}
}
if (proba != nullptr && !this->m_ProbaIndex)
itkExceptionMacro("Probability per class not available for this classifier !");
return target;
}
......
......@@ -107,13 +107,8 @@ NormalBayesMachineLearningModel<TInputValue,TOutputValue>
itkExceptionMacro("Confidence index not available for this classifier !");
}
}
if (proba != ITK_NULLPTR)
{
if (!this->m_ProbaIndex)
{
itkExceptionMacro("Probability per class not available for this classifier !");
}
}
if (proba != nullptr && !this->m_ProbaIndex)
itkExceptionMacro("Probability per class not available for this classifier !");
return target;
}
......
......@@ -198,14 +198,10 @@ SVMMachineLearningModel<TInputValue,TOutputValue>
(*quality) = m_SVMModel->predict(sample,true);
#endif
}
if (proba != ITK_NULLPTR)
{
if (!this->m_ProbaIndex)
{
itkExceptionMacro("Probability per class not available for this classifier !");
}
}
return target;
if (proba != nullptr && !this->m_ProbaIndex)
itkExceptionMacro("Probability per class not available for this classifier !");
return target;
}
template <class TInputValue, class TOutputValue>
......
......@@ -154,9 +154,9 @@ protected:
virtual ~SharkRandomForestsMachineLearningModel();
/** Predict values using the model */
virtual TargetSampleType DoPredict(const InputSampleType& input, ConfidenceValueType *quality=nullptr, ProbaSampleType *proba=nullptr) const override;
TargetSampleType DoPredict(const InputSampleType& input, ConfidenceValueType *quality=nullptr, ProbaSampleType *proba=nullptr) const override;
virtual void DoPredictBatch(const InputListSampleType *, const unsigned int & startIndex, const unsigned int & size, TargetListSampleType *, ConfidenceListSampleType * = nullptr, ProbaListSampleType * = nullptr) const override;
void DoPredictBatch(const InputListSampleType *, const unsigned int & startIndex, const unsigned int & size, TargetListSampleType *, ConfidenceListSampleType * = nullptr, ProbaListSampleType * = nullptr) const override;
/** PrintSelf method */
void PrintSelf(std::ostream& os, itk::Indent indent) const override;
......
......@@ -106,7 +106,7 @@ SharkRandomForestsMachineLearningModel<TInputValue,TOutputValue>
{
std::nth_element(probas.begin(), probas.begin()+1,
probas.end(), std::greater<double>());
conf = static_cast<ConfidenceValueType>((probas[0]-probas[1]));
conf = static_cast<ConfidenceValueType>(probas[0]-probas[1]);
}
else
{
......@@ -128,24 +128,23 @@ SharkRandomForestsMachineLearningModel<TInputValue,TOutputValue>
{
samples.push_back(value[i]);
}
if (quality != nullptr)
{
if (quality != nullptr || proba != nullptr)
{
shark::RealVector probas = m_RFModel.decisionFunction()(samples);
(*quality) = ComputeConfidence(probas, m_ComputeMargin);
if (quality != nullptr)
{
(*quality) = ComputeConfidence(probas, m_ComputeMargin);
}
if (proba != nullptr)
if (proba != nullptr)
{
shark::RealVector probas = m_RFModel.decisionFunction()(samples);
ProbaSampleType prob{(unsigned int)probas.size()};
ProbaSampleType prob{static_cast<unsigned int>(probas.size())};
for(size_t i =0; i< probas.size();i++)
{
//probas contain the N class probability indexed between 0 and N-1
prob[i] = static_cast<unsigned int>(probas[i]*1000);
}
(*proba) = prob;
{
//probas contain the N class probability indexed between 0 and N-1
(*proba)[i] = static_cast<unsigned int>(probas[i]*1000);
}
}
}
unsigned int res{0};
m_RFModel.eval(samples, res);
......@@ -186,24 +185,25 @@ SharkRandomForestsMachineLearningModel<TInputValue,TOutputValue>
omp_set_num_threads(itk::MultiThreader::GetGlobalDefaultNumberOfThreads());
#endif
if( proba !=nullptr || quality != nullptr)
{
shark::Data<shark::RealVector> probas = m_RFModel.decisionFunction()(inputSamples);
if( proba !=nullptr)
{
unsigned int id = startIndex;
for(shark::RealVector && p : probas.elements())
{
unsigned int id = startIndex;
shark::Data<shark::RealVector> probas = m_RFModel.decisionFunction()(inputSamples);
for(shark::RealVector && p : probas.elements())
ProbaSampleType prob{(unsigned int)p.size()};
for(size_t i =0; i< p.size();i++)
{
ProbaSampleType prob{(unsigned int)p.size()};
for(size_t i =0; i< p.size();i++)
{
prob[i] =p[i]*1000;
}
proba->SetMeasurementVector(id,prob);
++id;
prob[i] =p[i]*1000;
}
proba->SetMeasurementVector(id,prob);
++id;
}
}
if(quality != nullptr)
{
shark::Data<shark::RealVector> probas = m_RFModel.decisionFunction()(inputSamples);
unsigned int id = startIndex;
for(shark::RealVector && p : probas.elements())
{
......@@ -214,7 +214,8 @@ SharkRandomForestsMachineLearningModel<TInputValue,TOutputValue>
++id;
}
}
}
auto prediction = m_RFModel(inputSamples);
unsigned int id = startIndex;
for(const auto& p : prediction.elements())
......
......@@ -117,7 +117,7 @@ SharkKMeansMachineLearningModel<TInputValue, TOutputValue>
( *quality ) = ConfidenceValueType( 1.);
}
if (proba != ITK_NULLPTR)
if (proba != nullptr)
{
if (!this->m_ProbaIndex)
{
......@@ -188,7 +188,7 @@ SharkKMeansMachineLearningModel<TInputValue, TOutputValue>
quality->SetMeasurementVector( qid, static_cast<ConfidenceValueType>(1.) );
}
}
if (proba !=ITK_NULLPTR)
if (proba !=nullptr && !this->m_ProbaIndex)
{
itkExceptionMacro("Probability per class not available for this classifier !");
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment