Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 163 lines (160 sloc) 5.361 kb
e580c48 @liuliu modified swt a little bit, disabled breakline option
authored
1 #include "ccv.h"
2 #include <sys/time.h>
3 #include <ctype.h>
4
5 double ccv_swt_evaluate(int n, ccv_dense_matrix_t** images, ccv_array_t** truth, double a, ccv_swt_param_t params)
6 {
7 int i, j, k;
8 double total_f = 0, total_precision = 0, total_recall = 0;
9 for (i = 0; i < n; i++)
10 {
11 ccv_array_t* words = ccv_swt_detect_words(images[i], params);
12 double f = 0, precision = 0, recall = 0;
13 for (j = 0; j < words->rnum; j++)
14 {
15 ccv_rect_t* estimate = (ccv_rect_t*)ccv_array_get(words, j);
16 int match = 0;
17 for (k = 0; k < truth[i]->rnum; k++)
18 {
19 ccv_rect_t* target = (ccv_rect_t*)ccv_array_get(truth[i], k);
20 match = ccv_max(match, ccv_max(ccv_min(target->x + target->width, estimate->x + estimate->width) - ccv_max(target->x, estimate->x), 0) * ccv_max(ccv_min(target->y + target->height, estimate->y + estimate->height) - ccv_max(target->y, estimate->y), 0));
21 }
22 precision += (double)match / (double)(estimate->width * estimate->height);
23 }
24 if (words->rnum > 0)
25 precision /= words->rnum;
26 for (j = 0; j < truth[i]->rnum; j++)
27 {
28 ccv_rect_t* target = (ccv_rect_t*)ccv_array_get(truth[i], j);
29 int match = 0;
30 for (k = 0; k < words->rnum; k++)
31 {
32 ccv_rect_t* estimate = (ccv_rect_t*)ccv_array_get(words, k);
33 match = ccv_max(match, ccv_max(ccv_min(target->x + target->width, estimate->x + estimate->width) - ccv_max(target->x, estimate->x), 0) * ccv_max(ccv_min(target->y + target->height, estimate->y + estimate->height) - ccv_max(target->y, estimate->y), 0));
34 }
35 recall += (double)match / (double)(target->width * target->height);
36 }
37 ccv_array_free(words);
38 if (truth[i]->rnum > 0)
39 recall /= truth[i]->rnum;
40 if (precision > 0 && recall > 0)
41 f = 1 / (a / precision + (1 - a) / recall);
42 total_f += f;
43 total_precision += precision;
44 total_recall += recall;
45 }
46 total_f /= n;
47 total_precision /= n;
48 total_recall /= n;
49 return total_f;
50 }
51
52 int main(int argc, char** argv)
53 {
54 FILE* r = fopen(argv[1], "rt");
55 if (argc == 3)
56 chdir(argv[2]);
57 int images;
58 fscanf(r, "%d", &images);
59 int i;
a5497b4 @liuliu fix several problems with cuckoo cache, still tracking a interesting one
authored
60 ccv_enable_default_cache();
e580c48 @liuliu modified swt a little bit, disabled breakline option
authored
61 ccv_dense_matrix_t** aof = (ccv_dense_matrix_t**)ccmalloc(sizeof(ccv_dense_matrix_t*) * images);
62 ccv_array_t** aow = (ccv_array_t**)ccmalloc(sizeof(ccv_array_t**) * images);
63 for (i = 0; i < images; i++)
64 {
65 char file[1000];
66 fscanf(r, "%s", file);
67 aof[i] = 0;
dca50eb @liuliu changed ccv_unserialize/ccv_serialize to a more sensible name
authored
68 ccv_read(file, aof + i, CCV_IO_GRAY | CCV_IO_ANY_FILE);
e580c48 @liuliu modified swt a little bit, disabled breakline option
authored
69 int locations;
70 fscanf(r, "%d", &locations);
71 int j;
72 aow[i] = ccv_array_new(locations, sizeof(ccv_rect_t));
73 for (j = 0; j < locations; j++)
74 {
75 double x, y, width, height;
76 fscanf(r, "%lf %lf %lf %lf", &x, &y, &width, &height);
77 ccv_rect_t rect = { .x = (int)x, .y = (int)y, .width = (int)width, .height = (int)height };
78 ccv_array_push(aow[i], &rect);
79 }
80 }
cd3546d @liuliu initial commit for dpm (deformable parts model)
authored
81 ccv_swt_param_t params = { .size = 5, .low_thresh = 93, .high_thresh = 279, .max_height = 300, .min_height = 10, .aspect_ratio = 10, .variance_ratio = 0.5, .thickness_ratio = 2, .height_ratio = 2, .intensity_thresh = 29, .distance_ratio = 3, .intersect_ratio = 2, .letter_thresh = 3, .elongate_ratio = 1.3, .breakdown = 1, .breakdown_ratio = 3 };
e580c48 @liuliu modified swt a little bit, disabled breakline option
authored
82 double best_f = 0;
83 ccv_swt_param_t best_params = params;
84 /*
85 for (i = 50; i < 200; i++)
86 {
87 params.low_thresh = i;
88 params.high_thresh = i * 3;
89 double f = ccv_swt_evaluate(images, aof, aow, 0.5, params);
90 if (f > best_f)
91 {
92 best_params = params;
93 best_f = f;
94 printf("best f = %lf at low_thresh = %d\n", best_f, i);
95 }
96 }
97 params = best_params;
98 for (i = params.low_thresh * 2; i < params.low_thresh * 4; i++)
99 {
100 params.high_thresh = i;
101 double f = ccv_swt_evaluate(images, aof, aow, 0.5, params);
102 if (f > best_f)
103 {
104 best_params = params;
105 best_f = f;
106 printf("best f = %lf at high_thresh = %d\n", best_f, i);
107 }
108 }
109 params = best_params;
110 for (i = 5; i < 30; i++)
111 {
112 params.intensity_thresh = i;
113 double f = ccv_swt_evaluate(images, aof, aow, 0.5, params);
114 if (f > best_f)
115 {
116 best_params = params;
117 best_f = f;
118 printf("best f = %lf at intensity_thresh = %d\n", best_f, i);
119 }
120 }
121 params = best_params;
122 for (i = 1; i <= 30; i++)
123 {
124 params.variance_ratio = i / 10.0;
125 double f = ccv_swt_evaluate(images, aof, aow, 0.5, params);
126 if (f > best_f)
127 {
128 best_params = params;
129 best_f = f;
130 printf("best f = %lf at variance_ratio = %lf\n", best_f, params.variance_ratio);
131 }
132 }
133 params = best_params;
134 for (i = 1; i <= 100; i++)
135 {
136 params.elongate_ratio = i / 10.0;
137 double f = ccv_swt_evaluate(images, aof, aow, 0.5, params);
138 if (f > best_f)
139 {
140 best_params = params;
141 best_f = f;
142 printf("best f = %lf at elongate_ratio = %lf\n", best_f, params.elongate_ratio);
143 }
144 }
145 */
146 for (i = 1; i <= 200; i++)
147 {
148 params.breakdown_ratio = i / 10.0;
149 double f = ccv_swt_evaluate(images, aof, aow, 0.5, params);
150 if (f > best_f)
151 {
152 best_params = params;
153 best_f = f;
154 printf("best f = %lf at breakdown_ratio = %lf\n", best_f, params.breakdown_ratio);
155 }
156 }
157 printf("best parameters for swt is:\n\tlow_thresh = %lf\n\thigh_thresh = %lf\n\tintensity_thresh = %d\n\tvariance_ratio = %lf\n\telongate_ratio = %lf\n\tbreakdown_ratio = %lf\n", best_params.low_thresh, best_params.high_thresh, best_params.intensity_thresh, best_params.variance_ratio, best_params.elongate_ratio, best_params.breakdown_ratio);
158 ccfree(aof);
159 ccfree(aow);
7d9cd97 @liuliu changed radix tries based cache to cuckoo hash based one now you don't
authored
160 ccv_disable_cache();
e580c48 @liuliu modified swt a little bit, disabled breakline option
authored
161 return 0;
162 }
Something went wrong with that request. Please try again.