svm_model_matlab.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <string.h>
3 #include "../svm.h"
4 
5 #include "mex.h"
6 
7 #ifdef MX_API_VER
8 #if MX_API_VER < 0x07030000
9 typedef int mwIndex;
10 #endif
11 #endif
12 
13 #define NUM_OF_RETURN_FIELD 10
14 
15 #define Malloc(type,n) (type *)malloc((n)*sizeof(type))
16 
17 static const char *field_names[] = {
18  "Parameters",
19  "nr_class",
20  "totalSV",
21  "rho",
22  "Label",
23  "ProbA",
24  "ProbB",
25  "nSV",
26  "sv_coef",
27  "SVs"
28 };
29 
30 const char *model_to_matlab_structure(mxArray *plhs[], int num_of_feature, struct svm_model *model)
31 {
32  int i, j, n;
33  double *ptr;
34  mxArray *return_model, **rhs;
35  int out_id = 0;
36 
37  rhs = (mxArray **)mxMalloc(sizeof(mxArray *)*NUM_OF_RETURN_FIELD);
38 
39  // Parameters
40  rhs[out_id] = mxCreateDoubleMatrix(5, 1, mxREAL);
41  ptr = mxGetPr(rhs[out_id]);
42  ptr[0] = model->param.svm_type;
43  ptr[1] = model->param.kernel_type;
44  ptr[2] = model->param.degree;
45  ptr[3] = model->param.gamma;
46  ptr[4] = model->param.coef0;
47  out_id++;
48 
49  // nr_class
50  rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL);
51  ptr = mxGetPr(rhs[out_id]);
52  ptr[0] = model->nr_class;
53  out_id++;
54 
55  // total SV
56  rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL);
57  ptr = mxGetPr(rhs[out_id]);
58  ptr[0] = model->l;
59  out_id++;
60 
61  // rho
62  n = model->nr_class*(model->nr_class-1)/2;
63  rhs[out_id] = mxCreateDoubleMatrix(n, 1, mxREAL);
64  ptr = mxGetPr(rhs[out_id]);
65  for(i = 0; i < n; i++)
66  ptr[i] = model->rho[i];
67  out_id++;
68 
69  // Label
70  if(model->label)
71  {
72  rhs[out_id] = mxCreateDoubleMatrix(model->nr_class, 1, mxREAL);
73  ptr = mxGetPr(rhs[out_id]);
74  for(i = 0; i < model->nr_class; i++)
75  ptr[i] = model->label[i];
76  }
77  else
78  rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL);
79  out_id++;
80 
81  // probA
82  if(model->probA != NULL)
83  {
84  rhs[out_id] = mxCreateDoubleMatrix(n, 1, mxREAL);
85  ptr = mxGetPr(rhs[out_id]);
86  for(i = 0; i < n; i++)
87  ptr[i] = model->probA[i];
88  }
89  else
90  rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL);
91  out_id ++;
92 
93  // probB
94  if(model->probB != NULL)
95  {
96  rhs[out_id] = mxCreateDoubleMatrix(n, 1, mxREAL);
97  ptr = mxGetPr(rhs[out_id]);
98  for(i = 0; i < n; i++)
99  ptr[i] = model->probB[i];
100  }
101  else
102  rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL);
103  out_id++;
104 
105  // nSV
106  if(model->nSV)
107  {
108  rhs[out_id] = mxCreateDoubleMatrix(model->nr_class, 1, mxREAL);
109  ptr = mxGetPr(rhs[out_id]);
110  for(i = 0; i < model->nr_class; i++)
111  ptr[i] = model->nSV[i];
112  }
113  else
114  rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL);
115  out_id++;
116 
117  // sv_coef
118  rhs[out_id] = mxCreateDoubleMatrix(model->l, model->nr_class-1, mxREAL);
119  ptr = mxGetPr(rhs[out_id]);
120  for(i = 0; i < model->nr_class-1; i++)
121  for(j = 0; j < model->l; j++)
122  ptr[(i*(model->l))+j] = model->sv_coef[i][j];
123  out_id++;
124 
125  // SVs
126  {
127  int ir_index, nonzero_element;
128  mwIndex *ir, *jc;
129  mxArray *pprhs[1], *pplhs[1];
130 
131  if(model->param.kernel_type == PRECOMPUTED)
132  {
133  nonzero_element = model->l;
134  num_of_feature = 1;
135  }
136  else
137  {
138  nonzero_element = 0;
139  for(i = 0; i < model->l; i++) {
140  j = 0;
141  while(model->SV[i][j].index != -1)
142  {
143  nonzero_element++;
144  j++;
145  }
146  }
147  }
148 
149  // SV in column, easier accessing
150  rhs[out_id] = mxCreateSparse(num_of_feature, model->l, nonzero_element, mxREAL);
151  ir = mxGetIr(rhs[out_id]);
152  jc = mxGetJc(rhs[out_id]);
153  ptr = mxGetPr(rhs[out_id]);
154  jc[0] = ir_index = 0;
155  for(i = 0;i < model->l; i++)
156  {
157  if(model->param.kernel_type == PRECOMPUTED)
158  {
159  // make a (1 x model->l) matrix
160  ir[ir_index] = 0;
161  ptr[ir_index] = model->SV[i][0].value;
162  ir_index++;
163  jc[i+1] = jc[i] + 1;
164  }
165  else
166  {
167  int x_index = 0;
168  while (model->SV[i][x_index].index != -1)
169  {
170  ir[ir_index] = model->SV[i][x_index].index - 1;
171  ptr[ir_index] = model->SV[i][x_index].value;
172  ir_index++, x_index++;
173  }
174  jc[i+1] = jc[i] + x_index;
175  }
176  }
177  // transpose back to SV in row
178  pprhs[0] = rhs[out_id];
179  if(mexCallMATLAB(1, pplhs, 1, pprhs, "transpose"))
180  return "cannot transpose SV matrix";
181  rhs[out_id] = pplhs[0];
182  out_id++;
183  }
184 
185  /* Create a struct matrix contains NUM_OF_RETURN_FIELD fields */
186  return_model = mxCreateStructMatrix(1, 1, NUM_OF_RETURN_FIELD, field_names);
187 
188  /* Fill struct matrix with input arguments */
189  for(i = 0; i < NUM_OF_RETURN_FIELD; i++)
190  mxSetField(return_model,0,field_names[i],mxDuplicateArray(rhs[i]));
191  /* return */
192  plhs[0] = return_model;
193  mxFree(rhs);
194 
195  return NULL;
196 }
197 
198 struct svm_model *matlab_matrix_to_model(const mxArray *matlab_struct, const char **msg)
199 {
200  int i, j, n, num_of_fields;
201  double *ptr;
202  int id = 0;
203  struct svm_node *x_space;
204  struct svm_model *model;
205  mxArray **rhs;
206 
207  num_of_fields = mxGetNumberOfFields(matlab_struct);
208  if(num_of_fields != NUM_OF_RETURN_FIELD)
209  {
210  *msg = "number of return field is not correct";
211  return NULL;
212  }
213  rhs = (mxArray **) mxMalloc(sizeof(mxArray *)*num_of_fields);
214 
215  for(i=0;i<num_of_fields;i++)
216  rhs[i] = mxGetFieldByNumber(matlab_struct, 0, i);
217 
218  model = Malloc(struct svm_model, 1);
219  model->rho = NULL;
220  model->probA = NULL;
221  model->probB = NULL;
222  model->label = NULL;
223  model->nSV = NULL;
224  model->free_sv = 1; // XXX
225 
226  ptr = mxGetPr(rhs[id]);
227  model->param.svm_type = (int)ptr[0];
228  model->param.kernel_type = (int)ptr[1];
229  model->param.degree = (int)ptr[2];
230  model->param.gamma = ptr[3];
231  model->param.coef0 = ptr[4];
232  id++;
233 
234  ptr = mxGetPr(rhs[id]);
235  model->nr_class = (int)ptr[0];
236  id++;
237 
238  ptr = mxGetPr(rhs[id]);
239  model->l = (int)ptr[0];
240  id++;
241 
242  // rho
243  n = model->nr_class * (model->nr_class-1)/2;
244  model->rho = (double*) malloc(n*sizeof(double));
245  ptr = mxGetPr(rhs[id]);
246  for(i=0;i<n;i++)
247  model->rho[i] = ptr[i];
248  id++;
249 
250  // label
251  if(mxIsEmpty(rhs[id]) == 0)
252  {
253  model->label = (int*) malloc(model->nr_class*sizeof(int));
254  ptr = mxGetPr(rhs[id]);
255  for(i=0;i<model->nr_class;i++)
256  model->label[i] = (int)ptr[i];
257  }
258  id++;
259 
260  // probA
261  if(mxIsEmpty(rhs[id]) == 0)
262  {
263  model->probA = (double*) malloc(n*sizeof(double));
264  ptr = mxGetPr(rhs[id]);
265  for(i=0;i<n;i++)
266  model->probA[i] = ptr[i];
267  }
268  id++;
269 
270  // probB
271  if(mxIsEmpty(rhs[id]) == 0)
272  {
273  model->probB = (double*) malloc(n*sizeof(double));
274  ptr = mxGetPr(rhs[id]);
275  for(i=0;i<n;i++)
276  model->probB[i] = ptr[i];
277  }
278  id++;
279 
280  // nSV
281  if(mxIsEmpty(rhs[id]) == 0)
282  {
283  model->nSV = (int*) malloc(model->nr_class*sizeof(int));
284  ptr = mxGetPr(rhs[id]);
285  for(i=0;i<model->nr_class;i++)
286  model->nSV[i] = (int)ptr[i];
287  }
288  id++;
289 
290  // sv_coef
291  ptr = mxGetPr(rhs[id]);
292  model->sv_coef = (double**) malloc((model->nr_class-1)*sizeof(double));
293  for( i=0 ; i< model->nr_class -1 ; i++ )
294  model->sv_coef[i] = (double*) malloc((model->l)*sizeof(double));
295  for(i = 0; i < model->nr_class - 1; i++)
296  for(j = 0; j < model->l; j++)
297  model->sv_coef[i][j] = ptr[i*(model->l)+j];
298  id++;
299 
300  // SV
301  {
302  int sr, sc, elements;
303  int num_samples;
304  mwIndex *ir, *jc;
305  mxArray *pprhs[1], *pplhs[1];
306 
307  // transpose SV
308  pprhs[0] = rhs[id];
309  if(mexCallMATLAB(1, pplhs, 1, pprhs, "transpose"))
310  {
312  *msg = "cannot transpose SV matrix";
313  return NULL;
314  }
315  rhs[id] = pplhs[0];
316 
317  sr = (int)mxGetN(rhs[id]);
318  sc = (int)mxGetM(rhs[id]);
319 
320  ptr = mxGetPr(rhs[id]);
321  ir = mxGetIr(rhs[id]);
322  jc = mxGetJc(rhs[id]);
323 
324  num_samples = (int)mxGetNzmax(rhs[id]);
325 
326  elements = num_samples + sr;
327 
328  model->SV = (struct svm_node **) malloc(sr * sizeof(struct svm_node *));
329  x_space = (struct svm_node *)malloc(elements * sizeof(struct svm_node));
330 
331  // SV is in column
332  for(i=0;i<sr;i++)
333  {
334  int low = (int)jc[i], high = (int)jc[i+1];
335  int x_index = 0;
336  model->SV[i] = &x_space[low+i];
337  for(j=low;j<high;j++)
338  {
339  model->SV[i][x_index].index = (int)ir[j] + 1;
340  model->SV[i][x_index].value = ptr[j];
341  x_index++;
342  }
343  model->SV[i][x_index].index = -1;
344  }
345 
346  id++;
347  }
348  mxFree(rhs);
349 
350  return model;
351 }
int * nSV
Definition: svm.h:66
static const char * field_names[]
double value
Definition: svm.h:15
int l
Definition: svm.h:56
struct svm_node ** SV
Definition: svm.h:57
double * probB
Definition: svm.h:61
#define Malloc(type, n)
double * probA
Definition: svm.h:60
int nr_class
Definition: svm.h:55
Definition: svm.h:52
#define NUM_OF_RETURN_FIELD
struct svm_model * matlab_matrix_to_model(const mxArray *matlab_struct, const char **msg)
int * label
Definition: svm.h:65
void svm_free_and_destroy_model(svm_model **model_ptr_ptr)
Definition: svm.cpp:2961
struct svm_parameter param
Definition: svm.h:54
double * rho
Definition: svm.h:59
int index
Definition: svm.h:14
double ** sv_coef
Definition: svm.h:58
int degree
Definition: svm.h:32
int free_sv
Definition: svm.h:69
Definition: svm.h:12
double gamma
Definition: svm.h:33
int svm_type
Definition: svm.h:30
struct svm_model * model
Definition: svmtrain.c:58
double coef0
Definition: svm.h:34
const char * model_to_matlab_structure(mxArray *plhs[], int num_of_feature, struct svm_model *model)
int kernel_type
Definition: svm.h:31
struct svm_node * x_space
Definition: svmtrain.c:59


haf_grasping
Author(s): David Fischinger
autogenerated on Mon Jun 10 2019 13:28:43