XMM - Probabilistic Models for Motion Recognition and Mapping

xmmModelConfiguration.hpp
Go to the documentation of this file.
1 /*
2  * xmmModelConfiguration.hpp
3  *
4  * Configuration for probabilistic models with multiple classes
5  *
6  * Contact:
7  * - Jules Francoise <jules.francoise@ircam.fr>
8  *
9  * This code has been initially authored by Jules Francoise
10  * <http://julesfrancoise.com> during his PhD thesis, supervised by Frederic
11  * Bevilacqua <href="http://frederic-bevilacqua.net>, in the Sound Music
12  * Movement Interaction team <http://ismm.ircam.fr> of the
13  * STMS Lab - IRCAM, CNRS, UPMC (2011-2015).
14  *
15  * Copyright (C) 2015 UPMC, Ircam-Centre Pompidou.
16  *
17  * This File is part of XMM.
18  *
19  * XMM is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * XMM is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with XMM. If not, see <http://www.gnu.org/licenses/>.
31  */
32 
33 #ifndef xmmModelConfiguration_h
34 #define xmmModelConfiguration_h
35 
36 #include "xmmModelParameters.hpp"
37 #include <map>
38 
39 namespace xmm {
47  Likeliest = 0,
48 
53  Mixture = 1
54 };
55 
59 enum class MultithreadingMode {
63  Sequential,
64 
70  Parallel,
71 
80 };
81 
88 template <typename ModelType>
89 class Configuration : public ClassParameters<ModelType> {
90  public:
91  template <typename SingleClassModel, typename ModelType_>
92  friend class Model;
93 
98  : multithreading(MultithreadingMode::Parallel),
99  multiClass_regression_estimator(
101 
107  : ClassParameters<ModelType>(src),
108  multithreading(src.multithreading),
109  multiClass_regression_estimator(src.multiClass_regression_estimator),
110  class_parameters_(src.class_parameters_) {}
111 
116  explicit Configuration(Json::Value const& root) {
117  ClassParameters<ModelType>::fromJson(root["default_parameters"]);
118  multithreading = static_cast<MultithreadingMode>(
119  root.get("multithreading", 0).asInt());
120  multiClass_regression_estimator =
121  static_cast<MultiClassRegressionEstimator>(
122  root.get("multiClass_regression_estimator", 0).asInt());
123  class_parameters_.clear();
124  for (auto p : root["class_parameters"]) {
125  class_parameters_[p["label"].asString()].fromJson(p);
126  }
127  }
128 
134  if (this != &src) {
136  class_parameters_ = src.class_parameters_;
137  multithreading = src.multithreading;
138  multiClass_regression_estimator =
140  }
141  return *this;
142  }
143 
153  if (class_parameters_.count(label) == 0) {
154  class_parameters_[label] = *this;
155  }
156  return class_parameters_[label];
157  }
158 
162  void reset() { class_parameters_.clear(); }
163 
168  void reset(std::string label) {
169  if (class_parameters_.count(label) > 0) {
170  class_parameters_.erase(label);
171  }
172  }
173 
178  virtual Json::Value toJson() const {
179  Json::Value root;
180  root["multithreading"] = static_cast<int>(multithreading);
181  root["multiClass_regression_estimator"] =
182  static_cast<int>(multiClass_regression_estimator);
183  root["default_parameters"] = ClassParameters<ModelType>::toJson();
184  int i = 0;
185  for (auto p : class_parameters_) {
186  root["class_parameters"][i] = p.second.toJson();
187  root["class_parameters"][i]["label"] = p.first;
188  i++;
189  }
190  return root;
191  }
192 
198  virtual void fromJson(Json::Value const& root) {
199  try {
200  Configuration<ModelType> tmp(root);
201  *this = tmp;
202  } catch (JsonException& e) {
203  throw e;
204  }
205  }
206 
211 
217 
218  protected:
222  std::map<std::string, ClassParameters<ModelType>> class_parameters_;
223 };
224 }
225 
226 #endif
MultiClassRegressionEstimator multiClass_regression_estimator
Regression mode for multiple class (prediction from likeliest class vs interpolation) ...
Definition: xmmModelConfiguration.hpp:216
void reset(std::string label)
Reset the parameters of a given classes to default.
Definition: xmmModelConfiguration.hpp:168
void reset()
Reset the parameters of all classes to default.
Definition: xmmModelConfiguration.hpp:162
Configuration(Json::Value const &root)
Constructor from Json Structure.
Definition: xmmModelConfiguration.hpp:116
ClassParameters & operator=(ClassParameters const &src)
Assignment.
Definition: xmmModelParameters.hpp:71
MultithreadingMode multithreading
Multithreading Training Mode.
Definition: xmmModelConfiguration.hpp:210
Probabilistic machine learning model for multiclass recognition and regression.
Definition: xmmModel.hpp:52
virtual void fromJson(Json::Value const &root)
Read the object from a JSON Structure.
Definition: xmmModelConfiguration.hpp:198
Configuration()
Default Constructor.
Definition: xmmModelConfiguration.hpp:97
No multithreading: all classes are trained sequentially.
MultiClassRegressionEstimator
Regression estimator for multiclass models.
Definition: xmmModelConfiguration.hpp:43
the output is estimated as the output values of the likeliest class
Model configuration.
Definition: xmmModelConfiguration.hpp:89
virtual Json::Value toJson() const =0
Write the object to a JSON Structure.
the output is estimated as a weight sum of the output values of each class
MultithreadingMode
Multithreading mode for multiple-class training.
Definition: xmmModelConfiguration.hpp:59
Multithreading in Background: all classes are trained in parallel in different threads. the train function returns after the training has started.
Multithreading: all classes are trained in parallel in different threads. the train function returns ...
Configuration(Configuration const &src)
Copy Constructor.
Definition: xmmModelConfiguration.hpp:106
virtual Json::Value toJson() const
Write the object to a JSON Structure.
Definition: xmmModelConfiguration.hpp:178
Exception class for handling JSON parsing errors.
Definition: xmmJson.hpp:127
Definition: xmmAttribute.hpp:42
Configuration & operator=(Configuration const &src)
Assignment.
Definition: xmmModelConfiguration.hpp:133
virtual void fromJson(Json::Value const &root)=0
Read the object from a JSON Structure.
std::map< std::string, ClassParameters< ModelType > > class_parameters_
Parameters for each class.
Definition: xmmModelConfiguration.hpp:222
ClassParameters< ModelType > & operator[](std::string label)
access the parameters of a given class by label
Definition: xmmModelConfiguration.hpp:152
Class-specific Model Parameters.
Definition: xmmModelParameters.hpp:48