Commit 00fb23c7d453546b164c82e1a8cba6cec05b9d9f

Authored by Cédric Traizet
1 parent 916ac460

vector app work in progress

app/cbDimensionalityReductionVector.cxx
... ... @@ -35,10 +35,11 @@ namespace Wrapper
35 35 {
36 36  
37 37 /** Utility function to negate std::isalnum */
38   -/*bool IsNotAlphaNum(char c)
  38 +bool IsNotAlphaNum(char c)
39 39 {
40 40 return !std::isalnum(c);
41   -}*/
  41 +}
  42 +
42 43 class CbDimensionalityReductionVector : public Application
43 44 {
44 45 public:
... ... @@ -97,8 +98,8 @@ class CbDimensionalityReductionVector : public Application
97 98 AddParameter(ParameterType_ListView, "feat", "Field names to be calculated."); //
98 99 SetParameterDescription("feat","List of field names in the input vector data used as features for training."); //
99 100  
100   - AddParameter(ParameterType_ListView, "feat_out", "Field names to be calculated."); //
101   - SetParameterDescription("feat_out","List of field names in the input vector data used as features for training."); //
  101 + AddParameter(ParameterType_StringList, "featout", "Field names to be calculated."); //
  102 + SetParameterDescription("featout","List of field names in the input vector data used as features for training."); //
102 103  
103 104 AddParameter(ParameterType_OutputFilename, "out", "Output vector data file containing the reduced vector");
104 105 SetParameterDescription("out","Output vector data file storing sample values (OGR format)."
... ... @@ -111,40 +112,46 @@ class CbDimensionalityReductionVector : public Application
111 112 SetDocExampleParameterValue("model", "model.txt");
112 113 SetDocExampleParameterValue("out", "vectorDataOut.shp");
113 114 SetDocExampleParameterValue("feat", "perimeter area width");
114   - SetDocExampleParameterValue("feat_out", "perimeter area width");
  115 + SetDocExampleParameterValue("featout", "perimeter area width");
115 116 //SetOfficialDocLink();
116 117 }
117 118  
118 119 void DoUpdateParameters() ITK_OVERRIDE
119 120 {
120   - /** I don't know what this does */
121   - /*
122   - if ( HasValue("in") )
123   - {
124   - std::string shapefile = GetParameterString("in");
125   - otb::ogr::DataSource::Pointer ogrDS;
126   - OGRSpatialReference oSRS("");
127   - std::vector<std::string> options;
128   - ogrDS = otb::ogr::DataSource::New(shapefile, otb::ogr::DataSource::Modes::Read);
129   - otb::ogr::Layer layer = ogrDS->GetLayer(0);
130   - OGRFeatureDefn &layerDefn = layer.GetLayerDefn();
131   - ClearChoices("feat");
132   -
133   - for(int iField=0; iField< layerDefn.GetFieldCount(); iField++)
134   - {
135   - std::string item = layerDefn.GetFieldDefn(iField)->GetNameRef();
136   - std::string key(item);
137   - key.erase( std::remove_if(key.begin(),key.end(),IsNotAlphaNum), key.end());
138   - std::transform(key.begin(), key.end(), key.begin(), tolower);
139   - OGRFieldType fieldType = layerDefn.GetFieldDefn(iField)->GetType();
140   -
141   - if(fieldType == OFTInteger || ogr::version_proxy::IsOFTInteger64(fieldType) || fieldType == OFTReal)
142   - {
143   - std::string tmpKey="feat."+key;
144   - AddChoice(tmpKey,item);
145   - }
146   - }
147   - }*/
  121 +
  122 + if ( HasValue("in") )
  123 + {
  124 + std::string shapefile = GetParameterString("in");
  125 + otb::ogr::DataSource::Pointer ogrDS;
  126 + OGRSpatialReference oSRS("");
  127 + std::vector<std::string> options;
  128 + ogrDS = otb::ogr::DataSource::New(shapefile, otb::ogr::DataSource::Modes::Read);
  129 + otb::ogr::Layer layer = ogrDS->GetLayer(0);
  130 + OGRFeatureDefn &layerDefn = layer.GetLayerDefn();
  131 + ClearChoices("feat");
  132 + //ClearChoices("featout");
  133 +
  134 + for(int iField=0; iField< layerDefn.GetFieldCount(); iField++)
  135 + {
  136 + std::string item = layerDefn.GetFieldDefn(iField)->GetNameRef();
  137 + std::string key(item);
  138 + key.erase( std::remove_if(key.begin(),key.end(),IsNotAlphaNum), key.end());
  139 + std::transform(key.begin(), key.end(), key.begin(), tolower);
  140 + OGRFieldType fieldType = layerDefn.GetFieldDefn(iField)->GetType();
  141 +
  142 + if(fieldType == OFTInteger || ogr::version_proxy::IsOFTInteger64(fieldType) || fieldType == OFTReal)
  143 + {
  144 + std::string tmpKey="feat."+key;
  145 + AddChoice(tmpKey,item);
  146 + }
  147 + /*
  148 + if(fieldType == OFTInteger || ogr::version_proxy::IsOFTInteger64(fieldType) || fieldType == OFTReal)
  149 + {
  150 + std::string tmpKey="featout."+key;
  151 + AddChoice(tmpKey,item);
  152 + }*/
  153 + }
  154 + }
148 155 }
149 156  
150 157 void DoExecute() ITK_OVERRIDE
... ... @@ -222,25 +229,30 @@ class CbDimensionalityReductionVector : public Application
222 229  
223 230 /** Create/Update Output Shape file */
224 231  
  232 + std::cout << GetParameterStringList("featout").size() << std::endl;
  233 +
225 234 ogr::DataSource::Pointer output;
226 235 ogr::DataSource::Pointer buffer = ogr::DataSource::New();
227 236 bool updateMode = false;
  237 +
  238 +
228 239 if (IsParameterEnabled("out") && HasValue("out"))
229 240 {
230 241 // Create new OGRDataSource
231 242 output = ogr::DataSource::New(GetParameterString("out"), ogr::DataSource::Modes::Overwrite);
232   - otb::ogr::Layer newLayer = output->CreateLayer(
233   - GetParameterString("out"),
234   - const_cast<OGRSpatialReference*>(layer.GetSpatialRef()),
235   - layer.GetGeomType());
  243 + otb::ogr::Layer newLayer = output->CreateLayer(GetParameterString("out"),
  244 + const_cast<OGRSpatialReference*>(layer.GetSpatialRef()),
  245 + layer.GetGeomType());
236 246 // Copy existing fields
237 247 OGRFeatureDefn &inLayerDefn = layer.GetLayerDefn();
238   - for (int k=0 ; k<inLayerDefn.GetFieldCount() ; k++)
  248 + for (int k=0 ; k<inLayerDefn.GetFieldCount()-nbFeatures ; k++) // we don't copy the original bands
239 249 {
240 250 OGRFieldDefn fieldDefn(inLayerDefn.GetFieldDefn(k));
241 251 newLayer.CreateField(fieldDefn);
242 252 }
243 253 }
  254 +
  255 + /*
244 256 else
245 257 {
246 258 // Update mode
... ... @@ -253,11 +265,13 @@ class CbDimensionalityReductionVector : public Application
253 265 source->Clear();
254 266 // Re-open input data source in update mode
255 267 output = otb::ogr::DataSource::New(shapefile, otb::ogr::DataSource::Modes::Update_LayerUpdate);
256   - }
  268 + }*/
  269 +
  270 +
257 271  
258 272 otb::ogr::Layer outLayer = output->GetLayer(0);
259 273 OGRErr errStart = outLayer.ogr().StartTransaction();
260   -
  274 + /*
261 275 if (errStart != OGRERR_NONE)
262 276 {
263 277 itkExceptionMacro(<< "Unable to start transaction for OGR layer " << outLayer.ogr().GetName() << ".");
... ... @@ -266,16 +280,16 @@ class CbDimensionalityReductionVector : public Application
266 280 // Add the field of prediction in the output layer if field not exist
267 281  
268 282 OGRFeatureDefn &layerDefn = layer.GetLayerDefn();
269   - int idx = layerDefn.GetFieldIndex(GetParameterString("feat_out").c_str());
  283 + int idx = layerDefn.GetFieldIndex(GetParameterStringList("featout").c_str());
270 284  
271 285 if (idx >= 0)
272 286 {
273 287 if (layerDefn.GetFieldDefn(idx)->GetType() != OFTInteger)
274   - itkExceptionMacro("Field name "<< GetParameterString("feat_out") << " already exists with a different type!");
  288 + itkExceptionMacro("Field name "<< GetParameterStringList("featout") << " already exists with a different type!");
275 289 }
276 290 else
277 291 {
278   - OGRFieldDefn predictedField(GetParameterString("feat_out").c_str(), OFTInteger);
  292 + OGRFieldDefn predictedField(GetParameterStringList("featout").c_str(), OFTInteger);
279 293 ogr::FieldDefn predictedFieldDef(predictedField);
280 294 outLayer.CreateField(predictedFieldDef);
281 295 }
... ... @@ -283,7 +297,7 @@ class CbDimensionalityReductionVector : public Application
283 297 // Fill output layer
284 298  
285 299 unsigned int count=0;
286   - std::string classfieldname = GetParameterString("feat_out");
  300 + std::string classfieldname = GetParameterStringList("featout");
287 301 it = layer.cbegin();
288 302 itEnd = layer.cend();
289 303 for( ; it!=itEnd ; ++it, ++count)
... ... @@ -312,7 +326,7 @@ class CbDimensionalityReductionVector : public Application
312 326 }
313 327 output->SyncToDisk();
314 328 clock_t toc = clock();
315   - otbAppLogINFO( "Elapsed: "<< ((double)(toc - tic) / CLOCKS_PER_SEC)<<" seconds.");
  329 + otbAppLogINFO( "Elapsed: "<< ((double)(toc - tic) / CLOCKS_PER_SEC)<<" seconds.");*/
316 330 }
317 331  
318 332 ModelPointerType m_Model;
... ...
include/AutoencoderModel.h
... ... @@ -40,6 +40,12 @@ public:
40 40 itkGetMacro(Noise,double);
41 41 itkSetMacro(Noise,double);
42 42  
  43 + itkGetMacro(rho,double);
  44 + itkSetMacro(rho,double);
  45 +
  46 + itkGetMacro(beta,double);
  47 + itkSetMacro(beta,double);
  48 +
43 49 bool CanReadFile(const std::string & filename);
44 50 bool CanWriteFile(const std::string & filename);
45 51  
... ... @@ -67,6 +73,8 @@ private:
67 73 unsigned int m_NumberOfIterations;
68 74 double m_Regularization; // L2 Regularization parameter
69 75 double m_Noise; // probability for an input to be set to 0 (denosing autoencoder)
  76 + double m_rho; // Sparsity parameter
  77 + double m_beta; // Sparsity regularization parameter
70 78 };
71 79 } // end namespace otb
72 80  
... ...
include/AutoencoderModel.txx
... ... @@ -9,6 +9,8 @@
9 9  
10 10 //include train function
11 11 #include <shark/ObjectiveFunctions/ErrorFunction.h>
  12 +#include <shark/ObjectiveFunctions/SparseAutoencoderError.h>//the error function performing the regularisation of the hidden neurons
  13 +
12 14 #include <shark/Algorithms/GradientDescent/Rprop.h>// the RProp optimization algorithm
13 15 #include <shark/ObjectiveFunctions/Loss/SquaredLoss.h> // squared loss used for regression
14 16 #include <shark/ObjectiveFunctions/Regularizer.h> //L2 regulariziation
... ... @@ -62,6 +64,7 @@ void AutoencoderModel&lt;TInputValue,AutoencoderType&gt;::TrainOneLayer(unsigned int n
62 64 shark::LabeledData<shark::RealVector,shark::RealVector> trainSet(samples,samples);//labels identical to inputs
63 65 shark::SquaredLoss<shark::RealVector> loss;
64 66 shark::ErrorFunction error(trainSet, &model, &loss);
  67 + //shark::SparseAutoencoderError error(data,&model, &loss, m_rho, m_beta);
65 68 shark::TwoNormRegularizer regularizer(error.numberOfVariables());
66 69 error.setRegularizer(m_Regularization,&regularizer);
67 70  
... ...
include/DimensionalityReductionModelFactory.txx
... ... @@ -54,6 +54,7 @@ using SOM4DModelFactory = SOMModelFactory&lt;TInputValue, TTargetValue, 4&gt; ;
54 54 template <class TInputValue, class TTargetValue>
55 55 using SOM5DModelFactory = SOMModelFactory<TInputValue, TTargetValue, 5> ;
56 56  
  57 +
57 58 template <class TInputValue, class TOutputValue>
58 59 typename DimensionalityReductionModel<TInputValue,TOutputValue>::Pointer
59 60 DimensionalityReductionModelFactory<TInputValue,TOutputValue>
... ...
include/cbTrainAutoencoder.txx
... ... @@ -111,7 +111,7 @@ void cbLearningApplicationBaseDR&lt;TInputValue,TOutputValue&gt;
111 111 dimredTrainer->SetNumberOfHiddenNeurons(nb_neuron);
112 112 dimredTrainer->SetNumberOfIterations(GetParameterInt("model.autoencoder.nbiter"));
113 113 dimredTrainer->SetRegularization(GetParameterFloat("model.autoencoder.regularization"));
114   - dimredTrainer->SetRegularization(GetParameterFloat("model.autoencoder.noise"));
  114 + dimredTrainer->SetNoise(GetParameterFloat("model.autoencoder.noise"));
115 115 dimredTrainer->SetInputListSample(trainingListSample);
116 116 std::cout << "before train" << std::endl;
117 117 dimredTrainer->Train();
... ...