XMM - Probabilistic Models for Motion Recognition and Mapping

xmmModelSingleClass.hpp
Go to the documentation of this file.
1 /*
2  * xmmModelSingleClass.hpp
3  *
4  * Abstract class for Single-class Probabilistic Machine learning models based
5  * on the EM algorithm
6  *
7  * Contact:
8  * - Jules Francoise <jules.francoise@ircam.fr>
9  *
10  * This code has been initially authored by Jules Francoise
11  * <http://julesfrancoise.com> during his PhD thesis, supervised by Frederic
12  * Bevilacqua <href="http://frederic-bevilacqua.net>, in the Sound Music
13  * Movement Interaction team <http://ismm.ircam.fr> of the
14  * STMS Lab - IRCAM, CNRS, UPMC (2011-2015).
15  *
16  * Copyright (C) 2015 UPMC, Ircam-Centre Pompidou.
17  *
18  * This File is part of XMM.
19  *
20  * XMM is free software: you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation, either version 3 of the License, or
23  * (at your option) any later version.
24  *
25  * XMM is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28  * GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with XMM. If not, see <http://www.gnu.org/licenses/>.
32  */
33 
34 #ifndef xmmModelSingleClass_h
35 #define xmmModelSingleClass_h
36 
37 #include "../common/xmmCircularbuffer.hpp"
38 #include "../common/xmmEvents.hpp"
39 #include "../trainingset/xmmTrainingSet.hpp"
41 #include <memory>
42 #include <mutex>
43 
44 namespace xmm {
50  public:
54  enum Status {
58  Run,
59 
64 
69 
74 
79  };
80 
84  enum class ErrorType {
92  };
93 
100  TrainingEvent(void* model_, std::string label_,
101  Status status_ = Status::Run)
102  : model(model_),
103  label(label_),
104  status(status_),
105  progression(0),
106  log_likelihood(0),
107  iterations(0) {}
108 
114  : model(src.model),
115  label(src.label),
116  status(src.status),
119  iterations(src.iterations) {}
120 
126  if (this != &src) {
127  model = src.model;
128  label = src.label;
129  status = src.status;
130  progression = src.progression;
132  iterations = src.iterations;
133  }
134  return *this;
135  }
136 
140  void* model;
141 
145  std::string label;
146 
151 
155  float progression;
156 
162 
166  double iterations;
167 };
168 
175  public:
180  SingleClassProbabilisticModel(std::shared_ptr<SharedParameters> p = NULL);
181 
187 
193  explicit SingleClassProbabilisticModel(std::shared_ptr<SharedParameters> p,
194  Json::Value const& root);
195 
201  SingleClassProbabilisticModel const& src);
202 
207 
209 
215  bool isTraining() const;
216 
226  void train(TrainingSet* trainingSet);
227 
236  void cancelTraining();
237 
239 
241 
246  virtual void reset();
247 
255  virtual double filter(std::vector<float> const& observation) = 0;
256 
258 
260 
266  virtual Json::Value toJson() const;
267 
273  virtual void fromJson(Json::Value const& root) = 0;
274 
276 
280  std::string label;
281 
285  std::shared_ptr<SharedParameters> shared_parameters;
286 
291 
297 
298  protected:
303  virtual void allocate() = 0;
304 
308  virtual void emAlgorithmInit(TrainingSet* trainingSet) = 0;
309 
316  virtual double emAlgorithmUpdate(TrainingSet* trainingSet) = 0;
317 
321  virtual void emAlgorithmTerminate();
322 
331  bool emAlgorithmHasConverged(int step, double log_prob,
332  double old_log_prob) const;
333 
339  bool cancelTrainingIfRequested();
340 
345  inline void check_training() const {
346  if (this->isTraining())
347  throw std::runtime_error("The model is training");
348  }
349 
354 
358  std::mutex training_mutex_;
359 
364 
369 };
370 }
371 
372 #endif
TrainingEvent(TrainingEvent const &src)
Copy constructor.
Definition: xmmModelSingleClass.hpp:113
CircularBuffer< double > likelihood_buffer_
Likelihood buffer used for smoothing.
Definition: xmmModelSingleClass.hpp:353
std::string label
Label of the source model, if any.
Definition: xmmModelSingleClass.hpp:145
bool is_training_
defines if the model is being trained.
Definition: xmmModelSingleClass.hpp:363
float progression
progression within the training algorithm
Definition: xmmModelSingleClass.hpp:155
void * model
Source Model.
Definition: xmmModelSingleClass.hpp:140
std::mutex training_mutex_
Mutex used in Concurrent Mode.
Definition: xmmModelSingleClass.hpp:358
Status
Status of the training process.
Definition: xmmModelSingleClass.hpp:54
bool cancel_training_
defines if the model received a request to cancel training
Definition: xmmModelSingleClass.hpp:368
ErrorType
Type of Training Error.
Definition: xmmModelSingleClass.hpp:84
EventGenerator< TrainingEvent > training_events
Generator for events monitoring the training process.
Definition: xmmModelSingleClass.hpp:290
Generic Template for Machine Learning Probabilistic models based on the EM algorithm.
Definition: xmmModelSingleClass.hpp:174
TrainingEvent training_status
Event containing information on the current status of the training process.
Definition: xmmModelSingleClass.hpp:296
Status status
Status of the training process.
Definition: xmmModelSingleClass.hpp:150
Base class for the definition of training sets.
Definition: xmmTrainingSet.hpp:46
Generator class for a specific type of events.
Definition: xmmEvents.hpp:47
std::string label
label associated with the given model
Definition: xmmModelSingleClass.hpp:280
Abstract class for handling JSON + File I/O.
Definition: xmmJson.hpp:50
An error occured during training.
Definition: xmmModelSingleClass.hpp:63
double iterations
Number of EM iterations.
Definition: xmmModelSingleClass.hpp:166
The training of all classes has finished.
Definition: xmmModelSingleClass.hpp:78
TrainingEvent(void *model_, std::string label_, Status status_=Status::Run)
Constructor.
Definition: xmmModelSingleClass.hpp:100
Event for monitoring the training process.
Definition: xmmModelSingleClass.hpp:49
Definition: xmmAttribute.hpp:42
TrainingEvent & operator=(TrainingEvent const &src)
Assignment operator.
Definition: xmmModelSingleClass.hpp:125
Training is done without error.
Definition: xmmModelSingleClass.hpp:73
Training is still running.
Definition: xmmModelSingleClass.hpp:58
std::shared_ptr< SharedParameters > shared_parameters
Pointer to the shared parameters owned by a multi-class model.
Definition: xmmModelSingleClass.hpp:285
void check_training() const
Checks if the model is still training.
Definition: xmmModelSingleClass.hpp:345
The training has been cancelled.
Definition: xmmModelSingleClass.hpp:68
Convergence Errors (numerical errors in the training process).
double log_likelihood
Log-likelihood of the data given the model&#39;s parameters at the en of training.
Definition: xmmModelSingleClass.hpp:161