REMOVED: the DrawModel function from classifierInterface (they all do exactly the...
[mldemos:baraks-mldemos.git] / _AlgorithmsPlugins / OpenCV / interfaceMLPClassifier.cpp
1 /*********************************************************************
2 MLDemos: A User-Friendly visualization toolkit for machine learning
3 Copyright (C) 2010  Basilio Noris
4 Contact: mldemos@b4silio.com
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public License,
8 version 3 as published by the Free Software Foundation.
9
10 This library is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free
17 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *********************************************************************/
19 #include "interfaceMLPClassifier.h"
20 #include <QPixmap>
21 #include <QBitmap>
22 #include <QPainter>
23
24 using namespace std;
25
26 ClassMLP::ClassMLP()
27 {
28         params = new Ui::ParametersMLP();
29         params->setupUi(widget = new QWidget());
30 }
31
32 void ClassMLP::SetParams(Classifier *classifier)
33 {
34         if(!classifier) return;
35         float alpha = params->mlpAlphaSpin->value();
36         float beta = params->mlpBetaSpin->value();
37         int layers = params->mlpLayerSpin->value();
38         int neurons = params->mlpNeuronSpin->value();
39         int activation = params->mlpFunctionCombo->currentIndex()+1; // 1: sigmoid, 2: gaussian
40
41         ((ClassifierMLP *)classifier)->SetParams(activation, neurons, layers, alpha, beta);
42 }
43
44 fvec ClassMLP::GetParams()
45 {
46     float alpha = params->mlpAlphaSpin->value();
47     float beta = params->mlpBetaSpin->value();
48     int layers = params->mlpLayerSpin->value();
49     int neurons = params->mlpNeuronSpin->value();
50     int activation = params->mlpFunctionCombo->currentIndex()+1; // 1: sigmoid, 2: gaussian
51
52     fvec par(5);
53     par[0] = alpha;
54     par[1] = beta;
55     par[2] = layers;
56     par[3] = neurons;
57     par[4] = activation;
58     return par;
59 }
60
61 void ClassMLP::SetParams(Classifier *classifier, fvec parameters)
62 {
63     if(!classifier) return;
64     float alpha = parameters.size() > 0 ? parameters[0] : 1;
65     float beta = parameters.size() > 1 ? parameters[1] : 1;
66     int layers = parameters.size() > 2 ? parameters[2] : 1;
67     int neurons = parameters.size() > 3 ? parameters[3] : 1;
68     int activation = parameters.size() > 4 ? parameters[4] : 0;
69
70     ((ClassifierMLP *)classifier)->SetParams(activation, neurons, layers, alpha, beta);
71 }
72
73 void ClassMLP::GetParameterList(std::vector<QString> &parameterNames,
74                              std::vector<QString> &parameterTypes,
75                              std::vector< std::vector<QString> > &parameterValues)
76 {
77     parameterNames.clear();
78     parameterTypes.clear();
79     parameterValues.clear();
80     parameterNames.push_back("Alpha");
81     parameterNames.push_back("Beta");
82     parameterNames.push_back("Hidden Layers");
83     parameterNames.push_back("Neurons per Layer");
84     parameterNames.push_back("Activation Function");
85     parameterTypes.push_back("Real");
86     parameterTypes.push_back("Real");
87     parameterTypes.push_back("Integer");
88     parameterTypes.push_back("Integer");
89     parameterTypes.push_back("List");
90     parameterValues.push_back(vector<QString>());
91     parameterValues.back().push_back("0.00000001f");
92     parameterValues.back().push_back("9999999.f");
93     parameterValues.push_back(vector<QString>());
94     parameterValues.back().push_back("0.00000001f");
95     parameterValues.back().push_back("9999999.f");
96     parameterValues.push_back(vector<QString>());
97     parameterValues.back().push_back("1");
98     parameterValues.back().push_back("999999");
99     parameterValues.push_back(vector<QString>());
100     parameterValues.back().push_back("1");
101     parameterValues.back().push_back("999999");
102     parameterValues.push_back(vector<QString>());
103     parameterValues.back().push_back("Hyperbolic Tangent");
104     parameterValues.back().push_back("Gaussian");
105 }
106
107 QString ClassMLP::GetAlgoString()
108 {
109         float alpha = params->mlpAlphaSpin->value();
110         float beta = params->mlpBetaSpin->value();
111         int layers = params->mlpLayerSpin->value();
112         int neurons = params->mlpNeuronSpin->value();
113         int activation = params->mlpFunctionCombo->currentIndex()+1; // 1: sigmoid, 2: gaussian
114
115         QString algo = QString("MLP %1 %2 %3 %4 %5").arg(neurons).arg(layers).arg(activation==1 ? "S" : "G").arg(alpha).arg(beta);
116         return algo;
117 }
118
119 Classifier *ClassMLP::GetClassifier()
120 {
121         ClassifierMLP *classifier = new ClassifierMLP();
122         SetParams(classifier);
123         return classifier;
124 }
125
126 void ClassMLP::SaveOptions(QSettings &settings)
127 {
128         settings.setValue("mlpNeuron", params->mlpNeuronSpin->value());
129         settings.setValue("mlpAlpha", params->mlpAlphaSpin->value());
130         settings.setValue("mlpBeta", params->mlpBetaSpin->value());
131         settings.setValue("mlpLayer", params->mlpLayerSpin->value());
132         settings.setValue("mlpFunction", params->mlpFunctionCombo->currentIndex());
133 }
134
135 bool ClassMLP::LoadOptions(QSettings &settings)
136 {
137         if(settings.contains("mlpNeuron")) params->mlpNeuronSpin->setValue(settings.value("mlpNeuron").toFloat());
138         if(settings.contains("mlpAlpha")) params->mlpAlphaSpin->setValue(settings.value("mlpAlpha").toFloat());
139         if(settings.contains("mlpBeta")) params->mlpBetaSpin->setValue(settings.value("mlpBeta").toFloat());
140         if(settings.contains("mlpLayer")) params->mlpLayerSpin->setValue(settings.value("mlpLayer").toFloat());
141         if(settings.contains("mlpFunction")) params->mlpFunctionCombo->setCurrentIndex(settings.value("mlpFunction").toInt());
142         return true;
143 }
144
145 void ClassMLP::SaveParams(QTextStream &file)
146 {
147         file << "classificationOptions" << ":" << "mlpNeuron" << " " << params->mlpNeuronSpin->value() << "\n";
148         file << "classificationOptions" << ":" << "mlpAlpha" << " " << params->mlpAlphaSpin->value() << "\n";
149         file << "classificationOptions" << ":" << "mlpBeta" << " " << params->mlpBetaSpin->value() << "\n";
150         file << "classificationOptions" << ":" << "mlpLayer" << " " << params->mlpLayerSpin->value() << "\n";
151         file << "classificationOptions" << ":" << "mlpFunction" << " " << params->mlpFunctionCombo->currentIndex() << "\n";
152 }
153
154 bool ClassMLP::LoadParams(QString name, float value)
155 {
156         if(name.endsWith("mlpNeuron")) params->mlpNeuronSpin->setValue((int)value);
157         if(name.endsWith("mlpAlpha")) params->mlpAlphaSpin->setValue(value);
158         if(name.endsWith("mlpBeta")) params->mlpBetaSpin->setValue(value);
159         if(name.endsWith("mlpLayer")) params->mlpLayerSpin->setValue((int)value);
160         if(name.endsWith("mlpFunction")) params->mlpFunctionCombo->setCurrentIndex((int)value);
161         return true;
162 }