diff --git a/app/cbDimensionalityReduction.cxx b/app/cbDimensionalityReduction.cxx index 106f609d48078a1e5cee94ed5276571dd589e61c..bc9a5754fec14e92c50e394ccc781d8b211bcf5b 100644 --- a/app/cbDimensionalityReduction.cxx +++ b/app/cbDimensionalityReduction.cxx @@ -222,7 +222,7 @@ private: } // Rescale vector image - m_Rescaler->SetScale(stddevMeasurementVector); + m_Rescaler->SetScale(stddevMeasurementVector*3); m_Rescaler->SetShift(meanMeasurementVector); m_Rescaler->SetInput(inImage); diff --git a/app/cbDimensionalityReductionTrainer.cxx b/app/cbDimensionalityReductionTrainer.cxx index aee6fb3ed966fb657980b5063ef4b35efdaa5e4e..55232ef871b527a00e5e3664737eb2bb4c9bdb1f 100644 --- a/app/cbDimensionalityReductionTrainer.cxx +++ b/app/cbDimensionalityReductionTrainer.cxx @@ -37,7 +37,7 @@ public: typedef Superclass::ListSampleType ListSampleType; typedef Superclass::SampleImageType SampleImageType; - typedef double ValueType; + typedef float ValueType; typedef itk::VariableLengthVector<ValueType> MeasurementType; typedef otb::StatisticsXMLFileReader<SampleType> StatisticsReader; @@ -81,8 +81,6 @@ private: void DoExecute() { - std::cout << "Appli Training!" << std::endl; - std::string shapefile = GetParameterString("io.vd"); otb::ogr::DataSource::Pointer source = otb::ogr::DataSource::New(shapefile, otb::ogr::DataSource::Modes::Read); @@ -103,7 +101,7 @@ private: } input->PushBack(mv); } - + MeasurementType meanMeasurementVector; MeasurementType stddevMeasurementVector; @@ -126,7 +124,7 @@ private: ShiftScaleFilterType::Pointer trainingShiftScaleFilter = ShiftScaleFilterType::New(); trainingShiftScaleFilter->SetInput(input); trainingShiftScaleFilter->SetShifts(meanMeasurementVector); - trainingShiftScaleFilter->SetScales(stddevMeasurementVector); + trainingShiftScaleFilter->SetScales(stddevMeasurementVector*3); trainingShiftScaleFilter->Update(); ListSampleType::Pointer trainingListSample= trainingShiftScaleFilter->GetOutput(); diff --git a/app/cbDimensionalityReductionVector.cxx b/app/cbDimensionalityReductionVector.cxx index ce4e1fa1ca35437a92762a8bab2297faba3fe3b3..cf2caed54850b84703a3a5d34670438f9c5c3a5f 100644 --- a/app/cbDimensionalityReductionVector.cxx +++ b/app/cbDimensionalityReductionVector.cxx @@ -57,7 +57,7 @@ class CbDimensionalityReductionVector : public Application /** Filters typedef */ - typedef double ValueType; + typedef float ValueType; typedef itk::VariableLengthVector<ValueType> InputSampleType; typedef itk::Statistics::ListSample<InputSampleType> ListSampleType; typedef MachineLearningModel<itk::VariableLengthVector<ValueType>, itk::VariableLengthVector<ValueType>> DimensionalityReductionModelType; @@ -136,6 +136,7 @@ class CbDimensionalityReductionVector : public Application if ( HasValue("in") ) { + std::string shapefile = GetParameterString("in"); otb::ogr::DataSource::Pointer ogrDS; OGRSpatialReference oSRS(""); @@ -150,29 +151,29 @@ class CbDimensionalityReductionVector : public Application { std::string item = layerDefn.GetFieldDefn(iField)->GetNameRef(); std::string key(item); + std::string::iterator end = std::remove_if( key.begin(), key.end(), IsNotAlphaNum ); + std::transform( key.begin(), end, key.begin(), tolower ); + /* key.erase( std::remove_if(key.begin(),key.end(),IsNotAlphaNum), key.end()); - std::transform(key.begin(), key.end(), key.begin(), tolower); + std::transform(key.begin(), key.end(), key.begin(), tolower);*/ OGRFieldType fieldType = layerDefn.GetFieldDefn(iField)->GetType(); - - if(fieldType == OFTInteger || ogr::version_proxy::IsOFTInteger64(fieldType) || fieldType == OFTReal) - { - std::string tmpKey="feat."+key; - AddChoice(tmpKey,item); - } - /* - if(fieldType == OFTInteger || ogr::version_proxy::IsOFTInteger64(fieldType) || fieldType == OFTReal) - { - std::string tmpKey="featout."+key; + /* if(fieldType == OFTInteger || ogr::version_proxy::IsOFTInteger64(fieldType) || fieldType == OFTReal) + {*/ + //std::string tmpKey="feat."+key; + std::string tmpKey = "feat." + key.substr( 0, static_cast<unsigned long>( end - key.begin() ) ); AddChoice(tmpKey,item); - }*/ + //} // this is the same as in otbVectorClassifier, but it doesnt work } + } + } void DoExecute() ITK_OVERRIDE { clock_t tic = clock(); + std::string shapefile = GetParameterString("in"); otb::ogr::DataSource::Pointer source = otb::ogr::DataSource::New(shapefile, otb::ogr::DataSource::Modes::Read); otb::ogr::Layer layer = source->GetLayer(0); @@ -187,12 +188,16 @@ class CbDimensionalityReductionVector : public Application { MeasurementType mv; mv.SetSize(nbFeatures); + for(int idx=0; idx < nbFeatures; ++idx) { - mv[idx] = (*it)[GetSelectedItems("feat")[idx]].GetValue<double>(); + mv[idx] = static_cast<float>( (*it)[GetSelectedItems("feat")[idx]].GetValue<double>() ); + } input->PushBack(mv); + } + /** Statistics for shift/scale */ @@ -218,7 +223,7 @@ class CbDimensionalityReductionVector : public Application ShiftScaleFilterType::Pointer trainingShiftScaleFilter = ShiftScaleFilterType::New(); trainingShiftScaleFilter->SetInput(input); trainingShiftScaleFilter->SetShifts(meanMeasurementVector); - trainingShiftScaleFilter->SetScales(stddevMeasurementVector); + trainingShiftScaleFilter->SetScales(stddevMeasurementVector*3); trainingShiftScaleFilter->Update(); otbAppLogINFO("mean used: " << meanMeasurementVector); otbAppLogINFO("standard deviation used: " << stddevMeasurementVector); @@ -246,9 +251,7 @@ class CbDimensionalityReductionVector : public Application /** Perform Dimensionality Reduction */ ListSampleType::Pointer listSample = trainingShiftScaleFilter->GetOutput(); - ListSampleType::Pointer target; - target = m_Model->PredictBatch(listSample); - + ListSampleType::Pointer target = m_Model->PredictBatch(listSample); /** Create/Update Output Shape file */ @@ -276,8 +279,8 @@ class CbDimensionalityReductionVector : public Application OGRFeatureDefn &inLayerDefn = layer.GetLayerDefn(); for (int k=0 ; k<inLayerDefn.GetFieldCount()-nbBands ; k++) // we don't copy the original bands { - OGRFieldDefn fieldDefn(inLayerDefn.GetFieldDefn(k)); - newLayer.CreateField(fieldDefn); + OGRFieldDefn fieldDefn(inLayerDefn.GetFieldDefn(k)); + newLayer.CreateField(fieldDefn); } } else if (GetParameterString("mode")=="update") @@ -306,20 +309,7 @@ class CbDimensionalityReductionVector : public Application } - /* - else - { - // Update mode - updateMode = true; - otbAppLogINFO("Update input vector data."); - // fill temporary buffer for the transfer - otb::ogr::Layer inputLayer = layer; - layer = buffer->CopyLayer(inputLayer, std::string("Buffer")); - // close input data source - source->Clear(); - // Re-open input data source in update mode - output = otb::ogr::DataSource::New(shapefile, otb::ogr::DataSource::Modes::Update_LayerUpdate); - }*/ + otb::ogr::Layer outLayer = output->GetLayer(0); @@ -351,34 +341,26 @@ class CbDimensionalityReductionVector : public Application } } - // Add an ID field. (The ID already contained in the layer refers to the polygon) - /* - OGRFieldDefn IDField("ID_point", OFTInteger); - ogr::FieldDefn IDFieldDef(IDField); - outLayer.CreateField(IDFieldDef); - */ + // Fill output layer unsigned int count=0; auto classfieldname = GetParameterStringList("featout"); it = layer.cbegin(); itEnd = layer.cend(); - int id=0; + for( ; it!=itEnd ; ++it, ++count) { ogr::Feature dstFeature(outLayer.GetLayerDefn()); - /* - if (GetParameterString("mode")=="overwrite") - {*/ - dstFeature.SetFrom( *it , TRUE); - dstFeature.SetFID(it->GetFID()); - //} + + dstFeature.SetFrom( *it , TRUE); + dstFeature.SetFID(it->GetFID()); + for (std::size_t i=0; i<classfieldname.size(); ++i){ - dstFeature[classfieldname[i]].SetValue<ValueType>(target->GetMeasurementVector(count)[i]); + dstFeature[classfieldname[i]].SetValue<double>(target->GetMeasurementVector(count)[i]); } - //dstFeature["ID_point"].SetValue<int>(id); if (updateMode) { outLayer.SetFeature(dstFeature); @@ -387,7 +369,6 @@ class CbDimensionalityReductionVector : public Application { outLayer.CreateFeature(dstFeature); } - id++; } if(outLayer.ogr().TestCapability("Transactions")) diff --git a/include/ImageDimensionalityReductionFilter.txx b/include/ImageDimensionalityReductionFilter.txx index 53a1739bd28fed73fe3f0623f1018dd12e124d56..e98a0b79f753624246d732d01e60a47b43f2595a 100644 --- a/include/ImageDimensionalityReductionFilter.txx +++ b/include/ImageDimensionalityReductionFilter.txx @@ -189,7 +189,7 @@ ImageDimensionalityReductionFilter<TInputImage, TOutputImage, TMaskImage> // This call is threadsafe labels = m_Model->PredictBatch(samples); - + // Set the output values typename TargetListSampleType::ConstIterator labIt = labels->Begin(); diff --git a/include/PCAModel.txx b/include/PCAModel.txx index e9ea1e2a2b94d3cc1331b65a2123f340087943f6..364dd8def35fa51373f3b0cf30e927622044d736 100644 --- a/include/PCAModel.txx +++ b/include/PCAModel.txx @@ -43,7 +43,6 @@ void PCAModel<TInputValue>::Train() shark::Data<shark::RealVector> inputSamples = shark::createDataFromRange( features ); m_pca.setData(inputSamples); m_pca.encoder(m_encoder, this->m_Dimension); - std::cout << m_encoder.matrix() << std::endl; m_pca.decoder(m_decoder, this->m_Dimension); } @@ -175,8 +174,6 @@ void PCAModel<TInputValue> //target[a]=1; //target.SetElement(a,p[a]); - - } //std::cout << p << std::endl; targets->SetMeasurementVector(id,target);