Commit 0e64c2be15f9b8632c3a657752f4d9cc4b8e5bb8

Authored by Josh Klontz
1 parent fa3e5586

removed mlp

openbr/plugins/classification/mlp.cpp deleted
1 -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *  
2 - * Copyright 2012 The MITRE Corporation *  
3 - * *  
4 - * Licensed under the Apache License, Version 2.0 (the "License"); *  
5 - * you may not use this file except in compliance with the License. *  
6 - * You may obtain a copy of the License at *  
7 - * *  
8 - * http://www.apache.org/licenses/LICENSE-2.0 *  
9 - * *  
10 - * Unless required by applicable law or agreed to in writing, software *  
11 - * distributed under the License is distributed on an "AS IS" BASIS, *  
12 - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *  
13 - * See the License for the specific language governing permissions and *  
14 - * limitations under the License. *  
15 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */  
16 -  
17 -#include <opencv2/ml/ml.hpp>  
18 -  
19 -#include <openbr/plugins/openbr_internal.h>  
20 -#include <openbr/core/opencvutils.h>  
21 -  
22 -using namespace cv;  
23 -  
24 -namespace br  
25 -{  
26 -  
27 -/*!  
28 - * \ingroup transforms  
29 - * \brief Wraps OpenCV's multi-layer perceptron framework  
30 - * \author Scott Klum \cite sklum  
31 - * \br_link http://docs.opencv.org/modules/ml/doc/neural_networks.html  
32 - * \br_property enum kernel Type of MLP kernel to use. Options are Identity, Sigmoid, Gaussian. Default is Sigmoid.  
33 - * \br_property float alpha Determines activation function for neural network. See OpenCV documentation for more details. Default is 1.  
34 - * \br_property float beta Determines activation function for neural network. See OpenCV documentation for more details. Default is 1.  
35 - * \br_property QStringList inputVariables Metadata keys for the labels associated with each template. There should be the same number of keys in the list as there are neurons in the final layer. Default is QStringList().  
36 - * \br_property QStringList outputVariables Metadata keys to store the output of the neural network. There should be the same number of keys in the list as there are neurons in the final layer. Default is QStringList().  
37 - * \br_property QList<int> neuronsPerLayer The number of neurons in each layer of the net. Default is QList<int>() << 1 << 1.  
38 - */  
39 -class MLPTransform : public MetaTransform  
40 -{  
41 - Q_OBJECT  
42 -  
43 - Q_ENUMS(Kernel)  
44 - Q_PROPERTY(Kernel kernel READ get_kernel WRITE set_kernel RESET reset_kernel STORED false)  
45 - Q_PROPERTY(float alpha READ get_alpha WRITE set_alpha RESET reset_alpha STORED false)  
46 - Q_PROPERTY(float beta READ get_beta WRITE set_beta RESET reset_beta STORED false)  
47 - Q_PROPERTY(QStringList inputVariables READ get_inputVariables WRITE set_inputVariables RESET reset_inputVariables STORED false)  
48 - Q_PROPERTY(QStringList outputVariables READ get_outputVariables WRITE set_outputVariables RESET reset_outputVariables STORED false)  
49 - Q_PROPERTY(QList<int> neuronsPerLayer READ get_neuronsPerLayer WRITE set_neuronsPerLayer RESET reset_neuronsPerLayer STORED false)  
50 -  
51 -public:  
52 -  
53 - enum Kernel { Identity = ml::ANN_MLP::IDENTITY,  
54 - Sigmoid = ml::ANN_MLP::SIGMOID_SYM,  
55 - Gaussian = ml::ANN_MLP::GAUSSIAN,  
56 - ReLU = ml::ANN_MLP::RELU,  
57 - LeakyReLU = ml::ANN_MLP::LEAKYRELU};  
58 -  
59 -private:  
60 - BR_PROPERTY(Kernel, kernel, Sigmoid)  
61 - BR_PROPERTY(float, alpha, 1)  
62 - BR_PROPERTY(float, beta, 1)  
63 - BR_PROPERTY(QStringList, inputVariables, QStringList())  
64 - BR_PROPERTY(QStringList, outputVariables, QStringList())  
65 - BR_PROPERTY(QList<int>, neuronsPerLayer, QList<int>() << 1 << 1)  
66 -  
67 - Ptr<ml::ANN_MLP> mlp;  
68 -  
69 - void init()  
70 - {  
71 - if (kernel == Gaussian)  
72 - qWarning("The OpenCV documentation warns that the Gaussian kernel, \"is not completely supported at the moment\"");  
73 -  
74 - Mat layers = Mat(neuronsPerLayer.size(), 1, CV_32SC1);  
75 - for (int i=0; i<neuronsPerLayer.size(); i++)  
76 - layers.row(i) = Scalar(neuronsPerLayer.at(i));  
77 -  
78 - mlp = ml::ANN_MLP::create();  
79 - mlp->setLayerSizes(layers);  
80 - mlp->setActivationFunction(kernel, alpha, beta);  
81 - }  
82 -  
83 - void train(const TemplateList &data)  
84 - {  
85 - Mat _data = OpenCVUtils::toMat(data.data());  
86 -  
87 - // Assuming data has n templates  
88 - // _data needs to be n x size of input layer  
89 - // Labels needs to be a n x outputs matrix  
90 - // For the time being we're going to assume a single output  
91 - Mat labels = Mat::zeros(data.size(),inputVariables.size(),CV_32F);  
92 - for (int i=0; i<inputVariables.size(); i++)  
93 - labels.col(i) += OpenCVUtils::toMat(File::get<float>(data, inputVariables.at(i)));  
94 -  
95 - mlp->train(_data, ml::ROW_SAMPLE, labels);  
96 -  
97 - if (Globals->verbose)  
98 - for (int i=0; i<neuronsPerLayer.size(); i++) qDebug() << mlp->getWeights(i);  
99 - }  
100 -  
101 - void project(const Template &src, Template &dst) const  
102 - {  
103 - dst = src;  
104 -  
105 - // See above for response dimensionality  
106 - Mat response(outputVariables.size(), 1, CV_32FC1);  
107 - mlp->predict(src.m().reshape(1,1), response);  
108 -  
109 - // Apparently mlp.predict reshapes the response matrix?  
110 - for (int i=0; i<outputVariables.size(); i++) dst.file.set(outputVariables.at(i),response.at<float>(0,i));  
111 - }  
112 -  
113 - void load(QDataStream &stream)  
114 - {  
115 - OpenCVUtils::loadModel(mlp, stream);  
116 - }  
117 -  
118 - void store(QDataStream &stream) const  
119 - {  
120 - OpenCVUtils::storeModel(mlp, stream);  
121 - }  
122 -};  
123 -  
124 -BR_REGISTER(Transform, MLPTransform)  
125 -  
126 -} // namespace br  
127 -  
128 -#include "classification/mlp.moc"