diff --git a/Code/Learning/otbSVMClassifier.txx b/Code/Learning/otbSVMClassifier.txx index 2e534e74ed04acddf7959fdfa4f5ab22e68c81cf..fe8e2bf773ae77fc6762484a2e20667ceeb26bf2 100644 --- a/Code/Learning/otbSVMClassifier.txx +++ b/Code/Learning/otbSVMClassifier.txx @@ -180,6 +180,7 @@ SVMClassifier< TSample, TLabel > typename OutputType::ConstIterator endO = m_Output->End() ; typename TSample::MeasurementVectorType measurements ; + int numberOfComponentsPerSample = iter.GetMeasurementVector().Size() ;//this->GetSample().GetMeasurementVectorSize();// int max_line_len = 1024; @@ -187,13 +188,16 @@ SVMClassifier< TSample, TLabel > int max_nr_attr = 64; bool predict_probability = 1; + const struct svm_model* model = m_Model->GetModel(); // char* line = (char *) malloc(max_line_len*sizeof(char)); // x = (struct svm_node *) malloc(max_nr_attr*sizeof(struct // svm_node)); - m_Model->AllocateProblem(1, numberOfComponentsPerSample); - x = m_Model->GetXSpace(); + +/* m_Model->AllocateProblem(1, numberOfComponentsPerSample);*/ + + x = new svm_node[numberOfComponentsPerSample+1];//m_Model->GetXSpace(); //std::cout << "XSpace Allocated" << std::endl; if(svm_check_probability_model(model)==0) @@ -208,11 +212,14 @@ SVMClassifier< TSample, TLabel > int total = 0; double error = 0; double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0; - + + int svm_type=svm_get_svm_type(model); //std::cout << "SVM Type = " << svm_type << std::endl; + int nr_class=svm_get_nr_class(model); //std::cout << "SVM nr_class = " << nr_class << std::endl; + int *labels=(int *) malloc(nr_class*sizeof(int)); double *prob_estimates=NULL; int j; @@ -223,17 +230,19 @@ SVMClassifier< TSample, TLabel > printf("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%g\n",svm_get_svr_probability(model)); else { + svm_get_labels(model,labels); + prob_estimates = (double *) malloc(nr_class*sizeof(double)); + } /*fprintf(output,"labels"); for(j=0;j<nr_class;j++) fprintf(output," %d",labels[j]); fprintf(output,"\n");*/ - } } // while(1) - //std::cout << "Starting iterations " << std::endl; +// std::cout << "Starting iterations " << std::endl; while (iter != end && iterO != endO) { @@ -329,7 +338,8 @@ if(predict_probability) //std::cout << "End of iterations and free" << std::endl; // free(x); - + +delete [] x; } } // end of namespace itk diff --git a/Code/Learning/otbSVMModel.h b/Code/Learning/otbSVMModel.h index ea37fc27ae5a519c62cc3875496604ac33576c8c..d4fc25084be21eb6af02a9d54b6cbf71c202719a 100644 --- a/Code/Learning/otbSVMModel.h +++ b/Code/Learning/otbSVMModel.h @@ -145,6 +145,7 @@ public: /** Allocates the problem */ void AllocateProblem(int l, long int elements); + /** Sets the model */ void SetModel(struct svm_model* aModel); diff --git a/Examples/Data/ROI_mask_multi.png b/Examples/Data/ROI_mask_multi.png index 7322cfc2294637951d68ae78986acd066f814175..3a3de297566e508c8eb9162bd71cc70724c242e1 100644 Binary files a/Examples/Data/ROI_mask_multi.png and b/Examples/Data/ROI_mask_multi.png differ diff --git a/Examples/Learning/GenerateTrainingImageExample.cxx b/Examples/Learning/GenerateTrainingImageExample.cxx index 525c497cf9ff22266f1a00bade4e95e16c9388e8..81175718553de4da93a19b6bc69769dbb1e932ac 100644 --- a/Examples/Learning/GenerateTrainingImageExample.cxx +++ b/Examples/Learning/GenerateTrainingImageExample.cxx @@ -116,14 +116,15 @@ int main( int argc, char ** argv ) --nbRois; OutputPixelType label = 0; - unsigned long xUL, yUL, xBR, yBR = 0; + unsigned long xUL, yUL, xBR, yBR, tmp_label = 0; - roisFile >> label; + roisFile >> tmp_label; roisFile >> xUL; roisFile >> yUL; roisFile >> xBR; roisFile >> yBR; + label = static_cast<OutputPixelType>(tmp_label); std::cout << "Label : " << int(label) << std::endl; std::cout << "( " << xUL << " , " << yUL << " )" << std::endl; @@ -151,6 +152,7 @@ int main( int argc, char ** argv ) while(!it.IsAtEnd()) { + it.Set(static_cast<OutputPixelType>(label)); //std::cout << static_cast<OutputPixelType>(label) << " -- "; diff --git a/Examples/Learning/SVMImageEstimatorClassificationMultiExample.cxx b/Examples/Learning/SVMImageEstimatorClassificationMultiExample.cxx index 9384a997010e756c1f4649de2143cc6993b3157f..268dce5913f8a5e7805662dc49eae5ee1cffcce6 100644 --- a/Examples/Learning/SVMImageEstimatorClassificationMultiExample.cxx +++ b/Examples/Learning/SVMImageEstimatorClassificationMultiExample.cxx @@ -152,6 +152,7 @@ int main( int argc, char* argv[] ) // Software Guide : BeginCodeSnippet svmEstimator->SaveModel(outputModelFileName); + // Software Guide : EndCodeSnippet @@ -176,11 +177,9 @@ int main( int argc, char* argv[] ) ClassifyReaderType::Pointer cReader = ClassifyReaderType::New(); - cReader->SetFileName( inputImageFileName ); cReader->Update(); - // Software Guide : BeginLatex // @@ -197,7 +196,6 @@ int main( int argc, char* argv[] ) typedef itk::Statistics::ImageToListAdaptor< ClassifyImageType > SampleType; SampleType::Pointer sample = SampleType::New(); - // Software Guide : EndCodeSnippet // Software Guide : BeginLatex @@ -211,7 +209,6 @@ int main( int argc, char* argv[] ) sample->SetImage(cReader->GetOutput()); - // Software Guide : EndCodeSnippet // Software Guide : BeginLatex @@ -230,7 +227,7 @@ int main( int argc, char* argv[] ) typedef otb::SVMModel< InputPixelType, LabelPixelType > ModelType; ModelType::Pointer model = svmEstimator->GetModel(); - + //model->LoadModel(outputModelFileName); // Software Guide : EndCodeSnippet @@ -260,13 +257,17 @@ int main( int argc, char* argv[] ) // Software Guide : EndLatex // Software Guide : BeginCodeSnippet - - int numberOfClasses = model->GetNumberOfClasses(); + std::cout << "GNC" << std::endl; + int numberOfClasses = model->GetNumberOfClasses(); + std::cout << "SNC = "<< numberOfClasses << std::endl; classifier->SetNumberOfClasses(numberOfClasses) ; + std::cout << "SM" << std::endl; classifier->SetModel( model ); + std::cout << "SS" << std::endl; classifier->SetSample(sample.GetPointer()) ; + std::cout << "Up" << std::endl; classifier->Update() ; - + std::cout << "---" << std::endl; // Software Guide : EndCodeSnippet // Software Guide : BeginLatex @@ -288,7 +289,7 @@ int main( int argc, char* argv[] ) typedef otb::Image< OutputPixelType, Dimension > OutputImageType; OutputImageType::Pointer outputImage = OutputImageType::New(); - + std::cout << "---" << std::endl; // Software Guide : EndCodeSnippet // Software Guide : BeginLatex @@ -320,7 +321,7 @@ int main( int argc, char* argv[] ) outputImage->SetRegions( region ); outputImage->Allocate(); - + std::cout << "---" << std::endl; // Software Guide : EndCodeSnippet // Software Guide : BeginLatex @@ -360,14 +361,14 @@ int main( int argc, char* argv[] ) // Software Guide : BeginCodeSnippet - + std::cout << "---" << std::endl; while (m_iter != m_last && !outIt.IsAtEnd()) { outIt.Set(m_iter.GetClassLabel()); ++m_iter ; ++outIt; } - + std::cout << "---" << std::endl; // Software Guide : EndCodeSnippet // Software Guide : BeginLatex @@ -388,12 +389,12 @@ int main( int argc, char* argv[] ) FileImageType > RescalerType; RescalerType::Pointer rescaler = RescalerType::New(); - + std::cout << "---" << std::endl; rescaler->SetOutputMinimum( itk::NumericTraits< unsigned char >::min()); rescaler->SetOutputMaximum( itk::NumericTraits< unsigned char >::max()); rescaler->SetInput( outputImage ); - + std::cout << "---" << std::endl; // Software Guide : EndCodeSnippet // Software Guide : BeginLatex