-
Notifications
You must be signed in to change notification settings - Fork 0
/
svm_struct_latent_api.c
561 lines (461 loc) · 16.1 KB
/
svm_struct_latent_api.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/************************************************************************/
/* */
/* svm_struct_latent_api.c */
/* */
/* API function definitions for Latent SVM^struct */
/* */
/* Author: Chun-Nam Yu */
/* Date: 17.Dec.08 */
/* */
/* This software is available for non-commercial use only. It must */
/* not be modified and distributed without prior permission of the */
/* author. The author is not responsible for implications from the */
/* use of this software. */
/* */
/************************************************************************/
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "svm_struct_latent_api_types.h"
#include <errno.h>
#include "maxflow-v3.02.src/maxflowwrap.hpp"
#define MAX_INPUT_LINE_LENGTH 10000
void die(const char *message)
{
if(errno) {
perror(message);
} else {
printf("ERROR: %s\n", message);
}
exit(1);
}
SVECTOR *read_sparse_vector(char *file_name, int object_id, STRUCT_LEARN_PARM *sparm){
int scanned;
WORD *words = NULL;
char feature_file[1000];
sprintf(feature_file, "%s_%d.feature", file_name, object_id);
FILE *fp = fopen(feature_file, "r");
int length = 0;
while(!feof(fp)){
length++;
words = (WORD *) realloc(words, length*sizeof(WORD));
if(!words) die("Memory error.");
scanned = fscanf(fp, " %d:%f", &words[length-1].wnum, &words[length-1].weight);
if(scanned < 2) {
words[length-1].wnum = 0;
words[length-1].weight = 0.0;
}
}
fclose(fp);
if(words[length-1].wnum) {
length++;
words = (WORD *) realloc(words,length*sizeof(WORD));
words[length-1].wnum = 0;
words[length-1].weight = 0.0;
}
SVECTOR *fvec = create_svector(words,"",1);
free(words);
return fvec;
}
SVECTOR *read_sparse_phi2(char *file_name, STRUCT_LEARN_PARM *sparm){
int scanned;
WORD *words = NULL;
char feature_file[1000];
sprintf(feature_file, "%s", file_name);
FILE *fp = fopen(feature_file, "r");
int length = 0;
while(!feof(fp)){
length++;
words = (WORD *) realloc(words, length*sizeof(WORD));
if(!words) die("Memory error.");
scanned = fscanf(fp, " %d:%f", &words[length-1].wnum, &words[length-1].weight);
if(scanned < 2) {
words[length-1].wnum = 0;
words[length-1].weight = 0.0;
}
}
fclose(fp);
if(words[length-1].wnum) {
length++;
words = (WORD *) realloc(words,length*sizeof(WORD));
words[length-1].wnum = 0;
words[length-1].weight = 0.0;
}
SVECTOR *fvec = create_svector(words,"",1);
free(words);
return fvec;
}
SAMPLE read_struct_examples(char *file, STRUCT_LEARN_PARM *sparm) {
/*
Read input examples {(x_1,y_1),...,(x_n,y_n)} from file.
The type of pattern x and label y has to follow the definition in
svm_struct_latent_api_types.h.
*/
SAMPLE sample;
int i, j;
SVECTOR *temp_sub=NULL;
double vecDistance;
long n_neighbors=0;
// open the file containing candidate bounding box dimensions/labels/featurePath and image label
FILE *fp = fopen(file, "r");
if(fp==NULL){
printf("Error: Cannot open input file %s\n",file);
exit(1);
}
sample.n = 1;
sample.examples = (EXAMPLE *) malloc(sample.n*sizeof(EXAMPLE));
if(!sample.examples) die("Memory error.");
sample.examples[0].x.n_pos = 0;
sample.examples[0].x.n_neg = 0;
fscanf(fp,"%d", &sample.examples[0].n_imgs);
// Initialise pattern
sample.examples[0].x.example_cost = 1;
sample.examples[0].x.x_is = (SUB_PATTERN *) malloc(sample.examples[0].n_imgs*sizeof(SUB_PATTERN));
if(!sample.examples[0].x.x_is) die("Memory error.");
sample.examples[0].y.labels = (int *) malloc(sample.examples[0].n_imgs*sizeof(int));
if(!sample.examples[0].y.labels) die("Memory error.");
SVECTOR *temp=NULL;
for(i = 0; i < sample.examples[0].n_imgs; i++){
fscanf(fp,"%s",sample.examples[0].x.x_is[i].phi1_file_name);
fscanf(fp,"%s",sample.examples[0].x.x_is[i].phi2_file_name);
fscanf(fp, "%d", &sample.examples[0].x.x_is[i].id);
fscanf(fp, "%d", &sample.examples[0].y.labels[i]);
sample.examples[0].x.x_is[i].phi1 = read_sparse_vector(sample.examples[0].x.x_is[i].phi1_file_name, sample.examples[0].x.x_is[i].id, sparm);
sample.examples[0].x.x_is[i].phi2 = read_sparse_phi2(sample.examples[0].x.x_is[i].phi2_file_name, sparm);
temp = create_svector_with_index(sample.examples[0].x.x_is[i].phi2->words, "", 1, sparm->phi1_size);
sample.examples[0].x.x_is[i].phi1phi2_pos = add_ss(sample.examples[0].x.x_is[i].phi1, temp);
free_svector(temp);
sample.examples[0].x.x_is[i].phi1phi2_neg = create_svector_with_index(sample.examples[0].x.x_is[i].phi1phi2_pos->words, "", 1, (sparm->phi1_size+sparm->phi2_size));
sample.examples[0].x.x_is[i].phi1phi2_shift = create_svector_with_index(sample.examples[0].x.x_is[i].phi1phi2_pos->words, "", 1, (sparm->phi1_size+sparm->phi2_size)*2);
if(sample.examples[0].y.labels[i] == 1) {
sample.examples[0].x.n_pos++;
}
else{
sample.examples[0].x.n_neg++;
}
}
sample.examples[0].y.n_pos = sample.examples[0].x.n_pos;
sample.examples[0].y.n_neg = sample.examples[0].x.n_neg;
sample.examples[0].x.neighbors = (int **) malloc(sample.examples[0].n_imgs*sizeof(int*));
sample.examples[0].x.n_neighbors=0;
for (i = 0; i < sample.examples[0].n_imgs; i++){
sample.examples[0].x.neighbors[i] = (int *) malloc(sample.examples[0].n_imgs*sizeof(int));
for (j=(i+1); j < sample.examples[0].n_imgs; j++){
temp_sub = sub_ss(sample.examples[0].x.x_is[i].phi2, sample.examples[0].x.x_is[j].phi2);
vecDistance = sprod_ss(temp_sub, temp_sub);
free_svector(temp_sub);
if(vecDistance < sparm->pairwise_threshold){
sample.examples[0].x.neighbors[i][j]=1;
sample.examples[0].x.n_neighbors++;
}
else{
sample.examples[0].x.neighbors[i][j]=0;
}
}
}
printf("No of neighbors = %d\n",sample.examples[0].x.n_neighbors);
fflush(stdout);
return(sample);
}
void init_struct_model(SAMPLE sample, STRUCTMODEL *sm, STRUCT_LEARN_PARM *sparm, LEARN_PARM *lparm, KERNEL_PARM *kparm) {
/*
Initialize parameters in STRUCTMODEL sm. Set the diminension
of the feature space sm->sizePsi. Can also initialize your own
variables in sm here.
*/
sm->n = sample.n;
// \psi is concatanation of \psi1 and \psi2. Dimension is the sum of dimensions of \psi1 and \psi2
sm->sizePsi = (sparm->phi1_size+sparm->phi2_size)*2 + (sparm->phi1_size+sparm->phi2_size);
}
SVECTOR *psi(PATTERN x, LABEL y, STRUCTMODEL *sm, STRUCT_LEARN_PARM *sparm) {
/*
Creates the feature vector \Psi(x,y) and return a pointer to
sparse vector SVECTOR in SVM^light format. The dimension of the
feature vector returned has to agree with the dimension in sm->sizePsi.
*/
SVECTOR *fvec=NULL;
SVECTOR *psi1=NULL;
SVECTOR *psi2=NULL;
SVECTOR *temp_psi=NULL;
SVECTOR *temp_sub=NULL;
WORD *words = NULL;
words = (WORD *) malloc(sizeof(WORD));
if(!words) die("Memory error.");
words[0].wnum = 0;
words[0].weight = 0;
fvec = create_svector(words,"",1);
psi1 = create_svector(words,"",1);
psi2 = create_svector(words,"",1);
free(words);
int i,j = 0;
for (i = 0; i < (x.n_pos+x.n_neg); i++){
if(y.labels[i] == 1){
temp_psi = add_ss(psi1, x.x_is[i].phi1phi2_pos);
}
else{
temp_psi = add_ss(psi1, x.x_is[i].phi1phi2_neg);
}
free_svector(psi1);
psi1 = temp_psi;
for (j=(i+1); j < (x.n_pos+x.n_neg); j++){
if(x.neighbors[i][j]){
if(y.labels[i] != y.labels[j]){
temp_sub = sub_ss_sq(x.x_is[i].phi1phi2_pos, x.x_is[j].phi1phi2_pos);
temp_psi = add_ss(psi2, temp_sub);
free_svector(temp_sub);
free_svector(psi2);
psi2 = temp_psi;
}
}
}
}
// scale w1 by 1/n
temp_psi = smult_s(psi1, (float)1/(float)(x.n_pos+x.n_neg));
free_svector(psi1);
psi1 = temp_psi;
// scale w2 by 1/n^2
if (x.n_neighbors){
temp_psi = smult_s(psi2, (float)1/(float)x.n_neighbors);
free_svector(psi2);
psi2 = temp_psi;
}
// concatenate psi1, psi2
temp_psi = create_svector_with_index(psi2->words, "", 1, (sparm->phi1_size+sparm->phi2_size)*2);
free_svector(psi2);
fvec = add_ss(psi1, temp_psi);
free_svector(temp_psi);
free_svector(psi1);
return(fvec);
}
void classify_struct_example(PATTERN x, LABEL *y, STRUCTMODEL *sm, STRUCT_LEARN_PARM *sparm) {
/*
Makes prediction with input pattern x with weight vector in sm->w,
i.e., computing argmax_{(y)} <w,psi(x,y)>.
Output pair (y) is stored at location pointed to by
pointers *y.
*/
int i,j;
SVECTOR *temp_sub=NULL;
double *unary_pos = (double*)malloc((x.n_pos+x.n_neg)*sizeof(double));
double *unary_neg = (double*)malloc((x.n_pos+x.n_neg)*sizeof(double));
double **binary = (double**)malloc((x.n_pos+x.n_neg)*sizeof(double *));
for (i = 0; i < (x.n_pos+x.n_neg); i++){
binary[i] = (double*)malloc((x.n_pos+x.n_neg)*sizeof(double));
// compute unary potential for ybar.labels[i] == 1
unary_pos[i] = sprod_ns(sm->w, x.x_is[i].phi1phi2_pos);
if(unary_pos[i] != 0){
unary_pos[i] = (float)(-1*unary_pos[i])/(float)(x.n_pos+x.n_neg);
}
// compute unary potential for ybar.labels[i] == -1
unary_neg[i] = sprod_ns(sm->w, x.x_is[i].phi1phi2_neg);
if(unary_neg[i] != 0){
unary_neg[i] = (float)(-1*unary_neg[i])/(float)(x.n_pos+x.n_neg);
}
for (j = (i+1); j < (x.n_pos+x.n_neg); j++){
if(x.neighbors[i][j]){
temp_sub = sub_ss_sq(x.x_is[i].phi1phi2_shift, x.x_is[j].phi1phi2_shift);
binary[i][j] = sprod_ns(sm->w, temp_sub);
assert(binary[i][j] <= 0);
free_svector(temp_sub);
}
else{
binary[i][j] = 0;
}
}
}
if (x.n_neighbors){
for (i = 0; i < (x.n_pos+x.n_neg); i++){
for (j = (i+1); j < (x.n_pos+x.n_neg); j++){
if(binary[i][j] != 0){
binary[i][j] = (double)(-1*binary[i][j])/(double)x.n_neighbors;
}
}
}
}
y->labels = maxflowwrapper(unary_pos, unary_neg, binary, x.n_pos, x.n_neg);
free(unary_pos);
free(unary_neg);
for (i = 0; i < (x.n_pos+x.n_neg); i++){
free(binary[i]);
}
free(binary);
return;
}
void find_most_violated_constraint_marginrescaling(PATTERN x, LABEL y, LABEL *ybar, STRUCTMODEL *sm, STRUCT_LEARN_PARM *sparm) {
/*
Finds the most violated constraint (loss-augmented inference), i.e.,
computing argmax_{(ybar,hbar)} [<w,psi(x,ybar,hbar)> + loss(y,ybar,hbar)].
The output (ybar,hbar) are stored at location pointed by
pointers *ybar and *hbar.
*/
int i, j;
SVECTOR *temp_sub=NULL;
double *unary_pos = (double*)malloc((x.n_pos+x.n_neg)*sizeof(double));
double *unary_neg = (double*)malloc((x.n_pos+x.n_neg)*sizeof(double));
double **binary = (double**)malloc((x.n_pos+x.n_neg)*sizeof(double *));
for (i = 0; i < (x.n_pos+x.n_neg); i++){
binary[i] = (double*)malloc((x.n_pos+x.n_neg)*sizeof(double));
// compute unary potential for ybar.labels[i] == 1
unary_pos[i] = sprod_ns(sm->w, x.x_is[i].phi1phi2_pos);
if(unary_pos[i] != 0){
unary_pos[i] = (float)(-1*unary_pos[i])/(float)(x.n_pos+x.n_neg);
}
// compute unary potential for ybar.labels[i] == -1
unary_neg[i] = sprod_ns(sm->w, x.x_is[i].phi1phi2_neg);
if(unary_neg[i] != 0){
unary_neg[i] = (float)(-1*unary_neg[i])/(float)(x.n_pos+x.n_neg);
}
if(y.labels[i] == 1){
// add 1/n to 'ybar == -1' unary term
unary_neg[i] -= (float)1/(float)(x.n_pos+x.n_neg);
}
else{
// add 1/n to 'ybar == 1' unary term
unary_pos[i] -= (float)1/(float)(x.n_pos+x.n_neg);
}
for (j = (i+1); j < (x.n_pos+x.n_neg); j++){
if(x.neighbors[i][j]){
temp_sub = sub_ss_sq(x.x_is[i].phi1phi2_shift, x.x_is[j].phi1phi2_shift);
binary[i][j] = sprod_ns(sm->w, temp_sub);
assert(binary[i][j] <= 0);
free_svector(temp_sub);
}
else{
binary[i][j] = 0;
}
}
}
if (x.n_neighbors){
for (i = 0; i < (x.n_pos+x.n_neg); i++){
for (j = (i+1); j < (x.n_pos+x.n_neg); j++){
if(binary[i][j] != 0){
binary[i][j] = (double)(-1*binary[i][j])/(double)x.n_neighbors;
}
}
}
}
ybar->labels = maxflowwrapper(unary_pos, unary_neg, binary, x.n_pos, x.n_neg);
free(unary_pos);
free(unary_neg);
for (i = 0; i < (x.n_pos+x.n_neg); i++){
free(binary[i]);
}
free(binary);
return;
}
double loss(LABEL y, LABEL ybar, STRUCT_LEARN_PARM *sparm) {
/*
Computes the loss of prediction (ybar,hbar) against the
correct label y.
*/
double l = 0;
int i;
for (i = 0; i < (y.n_pos+y.n_neg); i++){
if (y.labels[i] != ybar.labels[i]){
l++;
}
}
l = l/(double)(y.n_pos+y.n_neg);
return(l);
}
void write_struct_model(char *file, STRUCTMODEL *sm, STRUCT_LEARN_PARM *sparm) {
/*
Writes the learned weight vector sm->w to file after training.
*/
FILE *modelfl;
int i;
modelfl = fopen(file,"w");
if (modelfl==NULL) {
printf("Cannot open model file %s for output!", file);
exit(1);
}
for (i=1;i<sm->sizePsi+1;i++) {
fprintf(modelfl, "%d:%.16g\n", i, sm->w[i]);
}
fclose(modelfl);
}
STRUCTMODEL read_struct_model(char *file, STRUCT_LEARN_PARM *sparm) {
/*
Reads in the learned model parameters from file into STRUCTMODEL sm.
The input file format has to agree with the format in write_struct_model().
*/
STRUCTMODEL sm;
FILE *modelfl;
int sizePsi,i, fnum;
double fweight;
modelfl = fopen(file,"r");
if (modelfl==NULL) {
printf("Cannot open model file %s for input!", file);
exit(1);
}
sizePsi = 1;
sm.w = (double*)malloc((sizePsi+1)*sizeof(double));
for (i=0;i<sizePsi+1;i++) {
sm.w[i] = 0.0;
}
while (!feof(modelfl)) {
fscanf(modelfl, "%d:%lf", &fnum, &fweight);
if(fnum > sizePsi) {
sizePsi = fnum;
sm.w = (double *)realloc(sm.w,(sizePsi+1)*sizeof(double));
}
sm.w[fnum] = fweight;
}
fclose(modelfl);
sm.sizePsi = sizePsi;
return(sm);
}
void free_struct_model(STRUCTMODEL sm, STRUCT_LEARN_PARM *sparm) {
/*
Free any memory malloc'ed in STRUCTMODEL sm after training.
*/
free(sm.w);
}
void free_pattern(PATTERN x) {
/*
Free any memory malloc'ed when creating pattern x.
*/
}
void free_label(LABEL y) {
/*
Free any memory malloc'ed when creating label y.
*/
free(y.labels);
}
void free_struct_sample(SAMPLE s) {
/*
Free the whole training sample.
*/
int i;
for (i=0;i<s.n;i++) {
free_pattern(s.examples[i].x);
free_label(s.examples[i].y);
}
free(s.examples);
}
void parse_struct_parameters(STRUCT_LEARN_PARM *sparm) {
/*
Parse parameters for structured output learning passed
via the command line.
*/
int i;
/* set default */
sparm->phi1_size=24004;
sparm->phi2_size=512;
sparm->pairwise_threshold=0;
//sparm->phi1_size=3;
//sparm->phi2_size=2;
for (i=0;(i<sparm->custom_argc)&&((sparm->custom_argv[i])[0]=='-');i++) {
switch ((sparm->custom_argv[i])[2]) {
/* your code here */
case 't': i++; sparm->pairwise_threshold = atof(sparm->custom_argv[i]); break;
default: printf("\nUnrecognized option %s!\n\n", sparm->custom_argv[i]); exit(0);
}
}
}
void copy_label(LABEL l1, LABEL *l2)
{
}
void print_label(LABEL l, FILE *flabel)
{
}