51 template <
typename SingleClassModel,
typename ModelType>
88 throw std::runtime_error(
89 "Cannot copy: source model is still training");
91 for (
auto& model :
models) {
92 model.second.training_events.removeListeners();
93 model.second.training_events.addListener(
103 explicit Model(Json::Value
const& root)
112 for (
auto p : root[
"models"]) {
113 std::string l = p[
"label"].asString();
114 models.insert(std::pair<std::string, SingleClassModel>(
116 models[l].training_events.removeListeners();
117 models[l].training_events.addListener(
131 throw std::runtime_error(
132 "Cannot copy: target model is still training");
134 throw std::runtime_error(
135 "Cannot copy: source model is still training");
147 for (
auto& model : this->
models) {
148 model.second.training_events.removeListeners();
149 model.second.training_events.addListener(
173 return static_cast<unsigned int>(
models.size());
183 return (
models.count(label) > 0);
196 if (m.first == label) {
211 auto it =
models.find(label);
213 throw std::out_of_range(
"Class " + label +
" does not exist");
247 if (!trainingSet)
return;
265 for (
auto it =
models.begin(); it !=
models.end(); ++it) {
266 if (trainingSet->
labels().count(it->first) == 0) {
273 for (
typename std::set<std::string>::iterator it =
274 trainingSet->
labels().begin();
275 it != trainingSet->
labels().end(); ++it) {
280 for (
auto it = this->
models.begin(); it != this->
models.end(); ++it) {
281 it->second.is_training_ =
true;
282 it->second.cancel_training_ =
false;
289 std::thread(&SingleClassModel::train, &it->second,
313 if (!trainingSet)
return;
314 if (trainingSet->
labels().count(label) == 0)
315 throw std::out_of_range(
"Class " + label +
" does not exist");
327 throw std::runtime_error(
328 "Dimensions of the new Training Set do not match existing " 345 models[label].is_training_ =
true;
346 models[label].cancel_training_ =
false;
352 std::thread(&SingleClassModel::train, &(this->
models[label]),
368 for (
auto& it : this->
models) {
370 it.second.cancelTraining();
371 while (it.second.isTraining()) {
386 models[label].cancelTraining();
387 while (
models[label].isTraining()) {
407 for (
auto it = this->
models.begin(); it != this->
models.end(); ++it) {
418 virtual void filter(std::vector<float>
const& observation) {
436 root[
"models"].resize(static_cast<Json::ArrayIndex>(
size()));
437 Json::ArrayIndex modelIndex(0);
439 root[
"models"][modelIndex++] = it.second.toJson();
480 std::map<std::string, SingleClassModel>
models;
489 bool expectedState(
false);
490 if (
is_joining_.compare_exchange_weak(expectedState,
true)) {
495 bool classesUntrained(
true);
496 while (classesUntrained) {
497 classesUntrained =
false;
498 for (
auto it = this->models.begin(); it != this->models.end();
500 if (it->second.training_status.status ==
501 TrainingEvent::Status::Cancel ||
502 it->second.training_status.status ==
503 TrainingEvent::Status::Error) {
505 classesUntrained =
true;
513 TrainingEvent event(
this,
"", TrainingEvent::Status::Alldone);
529 if (e.
status != TrainingEvent::Status::Run) {
531 ((SingleClassModel*)e.
model)->is_training_ =
false;
553 if (
is_training_)
throw std::runtime_error(
"The Model is training");
563 bool configChanged(
false);
564 if (configuration.
changed) configChanged =
true;
566 if (config.second.changed) configChanged =
true;
569 throw std::runtime_error(
570 "Configuration has changed, models need to be trained.");
580 if (models.count(label) > 0 && models[label].isTraining())
581 throw std::runtime_error(
"The Model is already training");
583 if (models.find(label) == models.end()) {
584 models.insert(std::pair<std::string, SingleClassModel>(
585 label, shared_parameters));
586 models[label].training_events.addListener(
589 models[label].label = label;
virtual void train(TrainingSet *trainingSet)
Train all classes from the training set passed in argument.
Definition: xmmModel.hpp:246
Model(Json::Value const &root)
Constructor from Json Structure.
Definition: xmmModel.hpp:103
virtual void removeClass(std::string const &label)
Remove a specific class by label.
Definition: xmmModel.hpp:207
virtual void addModelForClass(std::string const &label)
Update training set for a specific label.
Definition: xmmModel.hpp:579
T get() const
get the attribute's current value
Definition: xmmAttribute.hpp:203
bool changed
specifies if parameters have changed (model is invalid)
Definition: xmmModelParameters.hpp:76
Shared Parameters for models with multiple classes.
Definition: xmmModelSharedParameters.hpp:53
Attribute< unsigned int > dimension
total dimension of the training data
Definition: xmmTrainingSet.hpp:298
Model(Model< SingleClassModel, ModelType > const &src)
Copy Constructor.
Definition: xmmModel.hpp:78
virtual void joinTraining()
Finishes the background training process by joining threads and deleting the models which training fa...
Definition: xmmModel.hpp:487
virtual Json::Value toJson() const
Write the object to a JSON Structure.
Definition: xmmModel.hpp:432
Configuration< ModelType > configuration
Configuration (default and class-specific parameters)
Definition: xmmModel.hpp:470
void checkConfigurationChanges() const
Look for configuration changes and throws an exception if the model is not up to date.
Definition: xmmModel.hpp:562
std::atomic< bool > is_training_
locks the Model while the models are training Used in cancel method
Definition: xmmModel.hpp:613
MultithreadingMode multithreading
Multithreading Training Mode.
Definition: xmmModelConfiguration.hpp:210
virtual void clear()
Remove all classes.
Definition: xmmModel.hpp:221
void notifyListeners(EventType &e) const
Propagates the event to all listeners.
Definition: xmmEvents.hpp:99
Probabilistic machine learning model for multiclass recognition and regression.
Definition: xmmModel.hpp:52
void cancelTraining()
Cancels the training of all models.
Definition: xmmModel.hpp:366
int getIndex(std::string const &label) const
Checks if a class exists.
Definition: xmmModel.hpp:191
void * model
Source Model.
Definition: xmmModelSingleClass.hpp:140
unsigned int size() const
Get the number of classes in the model.
Definition: xmmModel.hpp:172
std::map< std::string, std::thread > training_threads_
Training Threads.
Definition: xmmModel.hpp:601
std::atomic< bool > cancel_required_
locks the Model while the models are training Used in cancel method
Definition: xmmModel.hpp:607
virtual ~Model()
Destructor.
Definition: xmmModel.hpp:160
virtual void filter(std::vector< float > const &observation)
filters a incoming observation (performs recognition or regression)
Definition: xmmModel.hpp:418
No multithreading: all classes are trained sequentially.
TrainingSet * getPhrasesOfClass(std::string const &label)
get the pointer to the sub-training set containing all phrases with a given label ...
Definition: xmmTrainingSet.cpp:276
Model configuration.
Definition: xmmModelConfiguration.hpp:89
const std::set< std::string > & labels() const
get the list of labels currently in the training set
Definition: xmmTrainingSet.hpp:234
void onTrainingEvent(TrainingEvent const &e)
Monitors the training of each Model of the group.
Definition: xmmModel.hpp:524
std::mutex event_mutex_
Mutex that prevents concurrent calls to onEvent()
Definition: xmmModel.hpp:629
Status status
Status of the training process.
Definition: xmmModelSingleClass.hpp:150
Multithreading in Background: all classes are trained in parallel in different threads. the train function returns after the training has started.
unsigned int models_still_training_
Number of models that are still training.
Definition: xmmModel.hpp:624
std::map< std::string, SingleClassModel > models
models stored in a map. Each Model is associated with a label
Definition: xmmModel.hpp:480
Base class for the definition of training sets.
Definition: xmmTrainingSet.hpp:46
Generator class for a specific type of events.
Definition: xmmEvents.hpp:47
Attribute< std::vector< std::string > > column_names
labels of the columns of the training set (e.g. descriptor names)
Definition: xmmTrainingSet.hpp:308
bool hasClass(std::string const &label) const
Checks if a class exists.
Definition: xmmModel.hpp:181
EventGenerator< TrainingEvent > training_events
Generator for training process events.
Definition: xmmModel.hpp:475
Abstract class for handling JSON + File I/O.
Definition: xmmJson.hpp:50
Multithreading: all classes are trained in parallel in different threads. the train function returns ...
Model(bool bimodal=false)
Constructor.
Definition: xmmModel.hpp:58
Model< SingleClassModel, ModelType > & operator=(Model< SingleClassModel, ModelType > const &src)
Assignment.
Definition: xmmModel.hpp:127
bool trained() const
Checks if the model is trained (training finished and not empty)
Definition: xmmModel.hpp:235
Exception class for handling JSON parsing errors.
Definition: xmmJson.hpp:127
void cancelTraining(std::string const &label)
Cancels the training of a given class.
Definition: xmmModel.hpp:383
std::vector< std::string > get() const
get the attribute's current value
Definition: xmmAttribute.hpp:546
virtual void reset()
Resets the fitering process (recognition or regression)
Definition: xmmModel.hpp:404
Event for monitoring the training process.
Definition: xmmModelSingleClass.hpp:49
void checkTraining() const
Checks if the Model is still training.
Definition: xmmModel.hpp:552
Definition: xmmAttribute.hpp:42
std::shared_ptr< SharedParameters > shared_parameters
Set of Parameters shared among classes.
Definition: xmmModel.hpp:465
std::map< std::string, ClassParameters< ModelType > > class_parameters_
Parameters for each class.
Definition: xmmModelConfiguration.hpp:222
virtual void train(TrainingSet *trainingSet, std::string const &label)
Train a specific class from the training set passed in argument.
Definition: xmmModel.hpp:312
Attribute< unsigned int > dimension_input
dimension of the input modality in bimodal mode
Definition: xmmTrainingSet.hpp:303
bool training() const
Checks if the model is still training.
Definition: xmmModel.hpp:240
std::atomic< bool > is_joining_
specifies if a thread for joining the training process has been launched.
Definition: xmmModel.hpp:619
void fromJson(Json::Value const &root)
Read the object from a JSON Structure.
Definition: xmmModel.hpp:449