-
Notifications
You must be signed in to change notification settings - Fork 0
/
svm_struct_latent_classify.c
222 lines (182 loc) · 8.07 KB
/
svm_struct_latent_classify.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
/************************************************************************/
/* */
/* svm_struct_latent_classify.c */
/* */
/* Classification Code for Latent SVM^struct */
/* */
/* Author: Chun-Nam Yu */
/* Date: 9.Nov.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 <assert.h>
#include <math.h>
#include <stdio.h>
#include <sys/time.h>
#include "svm_struct_latent_api.h"
#include "./svm_light/svm_learn.h"
#define KERNEL_INFO_FILE "data/kernel_info.txt"
#define max(x,y) ( ((x)>(y)) ? (x) : (y))
void read_input_parameters(int argc, char **argv, char *testfile, char *modelfile, char *labelfile, char *latentfile,char *scorefile, char* kernel_info_file, char* filestub, STRUCT_LEARN_PARM *sparm);
double get_hinge_l_from_pos_score(double pos_score, LABEL gt)
{
return max(1 - 2*((double)gt.label-.5)*pos_score,0);
}
void get_class_scores(PATTERN x, IMAGE_KERNEL_CACHE ** cached_images, double * scores, STRUCTMODEL * sm, STRUCT_LEARN_PARM * sparm) {
int j;
LABEL y;
for (j = 0; j < sparm->n_classes; ++j) {
y.label = j;
scores[j] = get_classifier_score(x, y, cached_images, sm, sparm);
}
}
double regularizaton_cost(double* w, long num_entries)
{
long k;
double cost = 0;
for(k=0; k<=num_entries;k++) {
cost += w[k]*w[k]*.5;
}
return cost;
}
int main(int argc, char* argv[]) {
double avghingeloss;
LABEL y;
long i, correct;
double weighted_correct;
char testfile[1024];
char modelfile[1024];
char labelfile[1024];
char latentfile[1024];
char scorefile[1024];
FILE *flabel;
FILE *flatent;
FILE *fscore;
STRUCTMODEL model;
STRUCT_LEARN_PARM sparm;
SAMPLE testsample;
/* read input parameters */
read_input_parameters(argc,argv,testfile,modelfile,labelfile,latentfile,scorefile,model.kernel_info_file,model.filestub, &sparm);
printf("C: %f\n",sparm.C);
flabel = fopen(labelfile,"w");
flatent = fopen(latentfile,"w");
fscore = fopen(scorefile, "w");
init_struct_model(model.kernel_info_file, &model, &sparm);
read_struct_model(modelfile, &model);
/* read test examples */
printf("Reading test examples..."); fflush(stdout);
testsample = read_struct_examples(testfile, &model, &sparm);
printf("done.\n");
IMAGE_KERNEL_CACHE ** cached_images = init_cached_images(testsample.examples,&model);
avghingeloss = 0.0;
correct = 0;
weighted_correct=0.0;
int *valid_example_kernel = (int *) malloc(5*sizeof(int));
for(i = 0; i < model.num_kernels; i++)
valid_example_kernel[i] = 1;
double total_example_weight = 0;
int num_distinct_examples = 0;
int last_image_id = -1;
LATENT_VAR h = make_latent_var(&model);
double * scores = (double *)calloc(sparm.n_classes, sizeof(double));
for (i=0;i<testsample.n;i++) {
while (testsample.examples[i].x.image_id == last_image_id) i++;
last_image_id = testsample.examples[i].x.image_id;
num_distinct_examples++;
// if(finlatent) {
// read_latent_var(&h,finlatent);
//printf("%d %d\n",h.position_x,h.position_y);
// }
//printf("%f\n",sparm.C);
struct timeval start_time;
struct timeval finish_time;
gettimeofday(&start_time, NULL);
classify_struct_example(testsample.examples[i].x,&y,&h,cached_images,&model,&sparm,1);
gettimeofday(&finish_time, NULL);
double microseconds = 1e6 * (finish_time.tv_sec - start_time.tv_sec) + (finish_time.tv_usec - start_time.tv_usec);
//printf("This ESS call took %f milliseconds.\n", microseconds/1e3);
total_example_weight += testsample.examples[i].x.example_cost;
//double hinge_l = get_hinge_l_from_pos_score(pos_score,testsample.examples[i].y);
//printf("with a pos_score of %f, a label of %d we get a hinge_l of %f\n", pos_score, testsample.examples[i].y.label, hinge_l);
// double weighted_hinge_l = hinge_l * testsample.examples[i].x.example_cost;
//avghingeloss += weighted_hinge_l;
//if (hinge_l<1) {
//A classification is considered "correct" if it guesses one of the objects in the image
if (y.label == testsample.examples[i].y.label || testsample.examples[i].x.also_correct[y.label]) {
correct++;
weighted_correct+=testsample.examples[i].x.example_cost;
}
print_label(y, flabel);
fprintf(flabel,"\n"); fflush(flabel);
print_latent_var(testsample.examples[i].x, h, flatent);
get_class_scores(testsample.examples[i].x, cached_images, scores, &model, &sparm);
fprintf(fscore, "%s ", testsample.examples[i].x.image_path);
for (int j = 0; j < sparm.n_classes; ++j) {
fprintf(fscore, "%f ", scores[j]);
}
fprintf(fscore, "\n");
}
free_latent_var(h);
fclose(flabel);
fclose(flatent);
free(scores);
//double w_cost = regularizaton_cost(model.w_curr.get_vec(), model.sizePsi);
//avghingeloss = avghingeloss/testsample.n;
printf("\n");
//printf("Objective Value with C=%f is %f\n\n\n", sparm.C, (sparm.C * avghingeloss) + w_cost);
//printf("Average hinge loss on dataset: %.4f\n", avghingeloss);
printf("Zero/one error on test set: %.4f\n", 1.0 - ((float) correct) / (1.0 * num_distinct_examples));
printf("Weighted zero/one error on the test set %.4f\n", 1.0 - (weighted_correct/total_example_weight));
printf("zeroone %.4f weightedzeroone %.4f\n", 1.0 - ((float) correct) / (1.0 * num_distinct_examples), 1.0 - (weighted_correct/total_example_weight));
fclose(fscore);
free_cached_images(cached_images, &model);
//free_struct_sample(testsample);
free_struct_model(model,&sparm);
return(0);
}
void read_input_parameters(int argc, char **argv, char *testfile, char *modelfile, char *labelfile, char *latentfile,char *scorefile, char* kernel_info_file, char* filestub, STRUCT_LEARN_PARM *sparm) {
long i;
/* set default */
strcpy(modelfile, "lssvm_model");
strcpy(labelfile, "lssvm_label");
strcpy(latentfile, "lssvm_latent");
strcpy(scorefile, "lssvm_score");
strcpy(kernel_info_file, "lssvm_kernelconfig");
strcpy(filestub, "lssvm_filestub");
sparm->custom_argc = 0;
for (i=1;(i<argc)&&((argv[i])[0]=='-');i++) {
switch ((argv[i])[1]) {
case '-': strcpy(sparm->custom_argv[sparm->custom_argc++],argv[i]);i++; strcpy(sparm->custom_argv[sparm->custom_argc++],argv[i]);break;
default: printf("\nUnrecognized option %s!\n\n",argv[i]); exit(0);
}
}
if (i>=argc) {
printf("\nNot enough input parameters!\n\n");
exit(0);
}
strcpy(testfile, argv[i]);
if(i+1<argc)
strcpy(modelfile, argv[i+1]);
if(i+2<argc)
strcpy(labelfile,argv[i+2]);
if(i+3<argc)
strcpy(latentfile,argv[i+3]);
if(i+4<argc)
strcpy(scorefile,argv[i+4]);
if(i+5<argc)
strcpy(filestub,argv[i+5]);
if(i+6<argc)
strcpy(kernel_info_file,argv[i+6]);
printf("1 is %s\n", modelfile);
printf("2 is %s\n", labelfile);
printf("3 is %s\n", latentfile);
printf("4 is %s\n", scorefile);
printf("5 is %s\n", filestub);
printf("6 is %s\n", kernel_info_file);
fflush(stdout);
parse_struct_parameters(sparm);
}