1 /*
2 Copyright (c) 2019 Ferhat Kurtulmuş
3 Boost Software License - Version 1.0 - August 17th, 2003
4 Permission is hereby granted, free of charge, to any person or organization
5 obtaining a copy of the software and accompanying documentation covered by
6 this license (the "Software") to use, reproduce, display, distribute,
7 execute, and transmit the Software, and to prepare derivative works of the
8 Software, and to permit third-parties to whom the Software is furnished to
9 do so, all subject to the following:
10 The copyright notices in the Software and this entire statement, including
11 the above license grant, this restriction and the following disclaimer,
12 must be included in all copies of the Software, in whole or in part, and
13 all derivative works of the Software, unless such copies or derivative
14 works are solely in the form of machine-executable object code generated by
15 a source language processor.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
19 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
20 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
21 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24 
25 module opencvd.ml;
26 
27 import opencvd.cvcore;
28 
29 private extern (C){
30     SVM SVM_Create();
31     void SVM_Close(SVM svm);
32     double SVM_GetC(SVM svm);
33     Mat SVM_GetClassWeights(SVM svm);
34     double SVM_GetCoef0(SVM svm);
35     double SVM_GetDecisionFunction(SVM svm, int i, Mat alpha, Mat svidx);
36     double SVM_GetDegree(SVM svm);
37     double SVM_GetGamma(SVM svm);
38     int SVM_GetKernelType(SVM svm);
39     double SVM_GetNu(SVM svm);
40     double SVM_GetP(SVM svm);
41     Mat SVM_GetSupportVectors(SVM svm);
42     TermCriteria SVM_GetTermCriteria(SVM svm);
43     int SVM_GetType(SVM svm);
44     Mat SVM_GetUncompressedSupportVectors(SVM svm);
45     void SVM_SetC (SVM svm, double val);
46     void SVM_SetClassWeights(SVM svm, Mat val);
47     void SVM_SetCoef0 (SVM svm, double val);
48     void SVM_SetDegree (SVM svm, double val);
49     void SVM_SetGamma (SVM svm, double val);
50     void SVM_SetKernel (SVM svm, int kernelType);
51     void SVM_SetNu (SVM svm, double val);
52     void SVM_SetP (SVM svm, double val);
53     void SVM_SetTermCriteria(SVM svm, TermCriteria val);
54     void SVM_SetType(SVM svm, int val);
55     ParamGrid SVM_GetDefaultGridPtr(int param_id);
56     bool SVM_TrainAuto0(SVM svm, Mat samples, int layout, Mat responses, int kFold, ParamGrid Cgrid,
57         ParamGrid gammaGrid, ParamGrid pGrid, ParamGrid nuGrid, ParamGrid coeffGrid,
58         ParamGrid degreeGrid, bool balanced);
59 
60     ParamGrid ParamGrid_Create (double minVal, double maxVal, double logstep);
61     double ParamGrid_MinVal (ParamGrid pg);
62     double ParamGrid_MaxVal (ParamGrid pg);
63     double ParamGrid_LogStep (ParamGrid pg);
64 }
65 
66 enum: int { // cv::ml::SampleTypes
67     ROW_SAMPLE = 0, 
68     COL_SAMPLE = 1 
69 }
70 
71 struct SVM {
72     void* p;
73     
74     enum: int { // KernelTypes
75         CUSTOM =-1, 
76         LINEAR =0, 
77         POLY =1, 
78         RBF =2, 
79         SIGMOID =3, 
80         CHI2 =4, 
81         INTER =5 
82     }
83     enum: int { // ParamTypes
84         C =0, 
85         GAMMA =1, 
86         P =2, 
87         NU =3, 
88         COEF =4, 
89         DEGREE =5 
90     }
91     
92     enum: int { // Types
93         C_SVC =100, 
94         NU_SVC =101, 
95         ONE_CLASS =102, 
96         EPS_SVR =103, 
97         NU_SVR =104 
98     }
99     
100     static SVM opCall(){
101         return SVM_Create();
102     }
103     static SVM create(){
104         return SVM_Create();
105     }
106     
107     double getC(){
108         return SVM_GetC(this);
109     }
110     
111     Mat getClassWeights(){
112         return SVM_GetClassWeights(this);
113     }
114     
115     double getCoef0(){
116         return SVM_GetCoef0(this);
117     }
118     
119     double getDecisionFunction(int i, Mat alpha, Mat svidx){
120         return SVM_GetDecisionFunction(this, i, alpha, svidx);
121     }
122     
123     double getDegree(){
124         return SVM_GetDegree(this);
125     }
126     
127     double getGamma(){
128         return SVM_GetGamma(this);
129     }
130     
131     int getKernelType(){
132         return SVM_GetKernelType(this);
133     }
134     
135     double getNu(){
136         return SVM_GetNu(this);
137     }
138     
139     double getP(){
140         return SVM_GetP(this);
141     }
142     
143     Mat getSupportVectors(){
144         return SVM_GetSupportVectors(this);
145     }
146     
147     TermCriteria getTermCriteria(){
148         return SVM_GetTermCriteria(this);
149     }
150     
151     int getType(){
152         return SVM_GetType(this);
153     }
154     
155     Mat getUncompressedSupportVectors(){
156         return SVM_GetUncompressedSupportVectors(this);
157     }
158     
159     void setC(double val){
160         SVM_SetC(this, val);
161     }
162     
163     void setClassWeights(Mat val){
164         SVM_SetClassWeights(this, val);
165     }
166     
167     void setCoef0 (double val){
168         SVM_SetCoef0 (this, val);
169     }
170     
171     void setDegree(double val){
172         SVM_SetDegree (this, val);
173     }
174     
175     void setGamma(double val){
176         SVM_SetGamma(this, val);
177     }
178     
179     void setKernel (int kernelType){
180         SVM_SetKernel (this, kernelType);
181     }
182     
183     void setNu (double val){
184         SVM_SetNu (this, val);
185     }
186     
187     void setP (double val){
188         SVM_SetP (this, val);
189     }
190     
191     void setTermCriteria( TermCriteria val){
192         SVM_SetTermCriteria(this, val);
193     }
194     
195     void setType( int val){
196         SVM_SetType(this, val);
197     }
198     
199     static ParamGrid getDefaultGridPtr(int param_id){
200         return SVM_GetDefaultGridPtr(param_id);
201     }
202     
203     bool trainAuto( Mat samples, int layout, Mat responses, int kFold = 10,
204         ParamGrid Cgrid = SVM.getDefaultGridPtr(SVM.C),
205         ParamGrid gammaGrid = SVM.getDefaultGridPtr(SVM.GAMMA),
206         ParamGrid pGrid = SVM.getDefaultGridPtr(SVM.P),
207         ParamGrid nuGrid = SVM.getDefaultGridPtr(SVM.NU),
208         ParamGrid coeffGrid = SVM.getDefaultGridPtr(SVM.COEF),
209         ParamGrid degreeGrid = SVM.getDefaultGridPtr(SVM.DEGREE),
210         bool balanced = false){
211         
212         return SVM_TrainAuto0(this, samples, layout, responses, kFold, Cgrid,
213             gammaGrid, pGrid, nuGrid, coeffGrid,
214             degreeGrid, balanced);
215     }
216 }
217 
218 void Destroy(SVM svm){
219     SVM_Close(svm);
220 }
221 
222 struct ParamGrid {
223     void* p;
224     
225     static ParamGrid opCall(double minVal = 0, double maxVal = 0, double logstep = 1.0){
226         return ParamGrid_Create (minVal, maxVal, logstep);
227     }
228     
229     double minVal(){
230         return ParamGrid_MinVal (this);
231     }
232     
233     double maxVal (){
234         return ParamGrid_MaxVal (this);
235     }
236     
237     double logStep (){
238         return ParamGrid_LogStep (this);
239     }
240 }