Skip to content

Commit

Permalink
the "collect from positive" does pan out :-(
Browse files Browse the repository at this point in the history
  • Loading branch information
liuliu committed Jun 20, 2012
1 parent 3cfead3 commit 4dbf477
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 76 deletions.
8 changes: 4 additions & 4 deletions bin/dpmcreate.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void exit_with_help()
"\t--alpha-ratio : decrease the step size for each iteration [DEFAULT TO 0.9]\n"
"\t--margin-c : the famous C in SVM [DEFAULT TO 0.002]\n"
"\t--balance : to balance the weight of positive examples and negative examples [DEFAULT TO 1.5]\n"
"\t--negative-cache-size : the cache size for negative examples it should be smaller than negative-count and larger than 100 [DEFAULT TO 1000]\n"
"\t--negative-cache-size : the cache size for negative examples it should be smaller than negative-count and larger than 100 [DEFAULT TO 500]\n"
"\t--include-overlap : the percentage of overlap between expected bounding box and the bounding box from detection. Beyond this threshold, it is ensured to be the same object [DEFAULT TO 0.7]\n"
"\t--exclude-overlap : the percentage of overlap between expected bounding box and the bounding box from detection. Below this threshold, it is ensured to not be the same object [DEFAULT TO 0.5]\n"
"\t--grayscale : 0 or 1, whether to exploit color in a given image [DEFAULT TO 1]\n"
Expand Down Expand Up @@ -75,13 +75,13 @@ int main(int argc, char** argv)
.alpha = 0.1,
.balance = 1.5,
.alpha_ratio = 0.9,
.iterations = 15,
.iterations = 20,
.relabels = 5,
.negative_cache_size = 1000,
.negative_cache_size = 500,
.C = 0.002,
.percentile_breakdown = 0.05,
.include_overlap = 0.7,
.exclude_overlap = 0.5,
.exclude_overlap = 0.1,
.grayscale = 0 };
int i, k;
while (getopt_long_only(argc, argv, "", dpm_options, &k) != -1)
Expand Down
2 changes: 1 addition & 1 deletion bin/dpmdetect.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int main(int argc, char** argv)
ccv_dense_matrix_t* image = 0;
ccv_read(argv[1], &image, CCV_IO_ANY_FILE);
ccv_dpm_mixture_model_t* model = ccv_load_dpm_mixture_model(argv[2]);
ccv_dpm_param_t params = { .interval = 8, .min_neighbors = 1, .flags = 0, .threshold = -0.3 };
ccv_dpm_param_t params = { .interval = 8, .min_neighbors = 1, .flags = 0, .threshold = 1.1 };
if (image != 0)
{
unsigned int elapsed_time = get_current_time();
Expand Down
87 changes: 16 additions & 71 deletions lib/ccv_dpm.c
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ static ccv_dpm_feature_vector_t* _ccv_dpm_collect_best(ccv_dense_matrix_t* image
return v;
}

static ccv_array_t* _ccv_dpm_collect_all(gsl_rng* rng, ccv_dense_matrix_t* image, ccv_dpm_mixture_model_t* model, ccv_rect_t *bboxes, int bnum, float overlap, ccv_dpm_param_t params, float threshold)
static ccv_array_t* _ccv_dpm_collect_all(gsl_rng* rng, ccv_dense_matrix_t* image, ccv_dpm_mixture_model_t* model, float overlap, ccv_dpm_param_t params, float threshold)
{
int i, j, k, x, y;
double scale = pow(2.0, 1.0 / (params.interval + 1.0));
Expand Down Expand Up @@ -863,20 +863,6 @@ static ccv_array_t* _ccv_dpm_collect_all(gsl_rng* rng, ccv_dense_matrix_t* image
for (y = rwh; y < root_feature->rows - rwh; y++)
{
for (x = rww; x < root_feature->cols - rww; x++)
{
ccv_rect_t rect = ccv_rect((int)((x - rww) * CCV_DPM_WINDOW_SIZE * scale_x + 0.5), (int)((y - rwh) * CCV_DPM_WINDOW_SIZE * scale_y + 0.5), (int)(root_classifier->root.w->cols * CCV_DPM_WINDOW_SIZE * scale_x + 0.5), (int)(root_classifier->root.w->rows * CCV_DPM_WINDOW_SIZE * scale_y + 0.5));
int flag = 0;
if (bboxes)
for (k = 0; k < bnum; k++)
if ((double)(ccv_max(0, ccv_min(rect.x + rect.width, bboxes[k].x + bboxes[k].width) - ccv_max(rect.x, bboxes[k].x)) *
ccv_max(0, ccv_min(rect.y + rect.height, bboxes[k].y + bboxes[k].height) - ccv_max(rect.y, bboxes[k].y))) /
(double)ccv_max(rect.width * rect.height, bboxes[k].width * bboxes[k].height) >= overlap)
{
flag = 1;
break;
}
if (flag)
continue;
if (f_ptr[x] + root_classifier->beta > threshold)
{
// initialize v
Expand All @@ -887,7 +873,6 @@ static ccv_array_t* _ccv_dpm_collect_all(gsl_rng* rng, ccv_dense_matrix_t* image
if (av->rnum >= enough * (i + 1))
break;
}
}
f_ptr += root_feature->cols;
if (av->rnum >= enough * (i + 1))
break;
Expand All @@ -910,10 +895,10 @@ static ccv_array_t* _ccv_dpm_collect_all(gsl_rng* rng, ccv_dense_matrix_t* image
return av;
}

static void _ccv_dpm_collect_from_background_and_positive(ccv_array_t* av, gsl_rng* rng, char** bgfiles, int bgnum, char** posfiles, int posnum, ccv_rect_t* bboxes, ccv_dpm_mixture_model_t* model, ccv_dpm_new_param_t params, float threshold)
static void _ccv_dpm_collect_from_background(ccv_array_t* av, gsl_rng* rng, char** bgfiles, int bgnum, ccv_dpm_mixture_model_t* model, ccv_dpm_new_param_t params, float threshold)
{
int i, j, k;
int* order = (int*)ccmalloc(sizeof(int) * ccv_max(bgnum, posnum));
int i, j;
int* order = (int*)ccmalloc(sizeof(int) * bgnum);
for (i = 0; i < bgnum; i++)
order[i] = i;
gsl_ran_shuffle(rng, order, bgnum, sizeof(int));
Expand All @@ -923,7 +908,7 @@ static void _ccv_dpm_collect_from_background_and_positive(ccv_array_t* av, gsl_r
fflush(stdout);
ccv_dense_matrix_t* image = 0;
ccv_read(bgfiles[order[i]], &image, (params.grayscale ? CCV_IO_GRAY : 0) | CCV_IO_ANY_FILE);
ccv_array_t* at = _ccv_dpm_collect_all(rng, image, model, 0, 0, params.exclude_overlap, params.detector, threshold);
ccv_array_t* at = _ccv_dpm_collect_all(rng, image, model, params.exclude_overlap, params.detector, threshold);
if (at)
{
for (j = 0; j < at->rnum; j++)
Expand All @@ -934,50 +919,6 @@ static void _ccv_dpm_collect_from_background_and_positive(ccv_array_t* av, gsl_r
if (av->rnum >= params.negative_cache_size)
break;
}
if (av->rnum < params.negative_cache_size)
{
// collect negatives from positive images (I am smart!)
for (i = 0; i < posnum; i++)
order[i] = i;
gsl_ran_shuffle(rng, order, posnum, sizeof(int));
int* pos_processed = (int*)ccmalloc(sizeof(int) * posnum);
memset(pos_processed, 0, sizeof(int) * posnum);
ccv_rect_t* bboxes_in_file = (ccv_rect_t*)ccmalloc(sizeof(ccv_rect_t) * posnum);
for (i = 0; i < posnum; i++)
{
if (pos_processed[order[i]])
continue;
printf("\b\b\b\b\b\b%3d%%) ", av->rnum * 100 / params.negative_cache_size);
fflush(stdout);
ccv_dense_matrix_t* image = 0;
// find the same images with all their bounding boxes
bboxes_in_file[0] = bboxes[order[i]];
k = 1;
for (j = 0; j < posnum; j++)
if (j != order[i] &&
!pos_processed[j] &&
strncmp(posfiles[order[i]], posfiles[j], 1024) == 0)
{
pos_processed[j] = 1;
bboxes_in_file[k] = bboxes[j];
k++;
}
pos_processed[order[i]] = 1;
ccv_read(posfiles[order[i]], &image, (params.grayscale ? CCV_IO_GRAY : 0) | CCV_IO_ANY_FILE);
ccv_array_t* at = _ccv_dpm_collect_all(rng, image, model, bboxes_in_file, k, params.exclude_overlap, params.detector, threshold);
if (at)
{
for (j = 0; j < at->rnum; j++)
ccv_array_push(av, ccv_array_get(at, j));
ccv_array_free(at);
}
ccv_matrix_free(image);
if (av->rnum >= params.negative_cache_size)
break;
}
ccfree(bboxes_in_file);
ccfree(pos_processed);
}
ccfree(order);
}

Expand Down Expand Up @@ -1358,7 +1299,7 @@ static void _ccv_dpm_check_params(ccv_dpm_new_param_t params)
assert(params.relabels > 0);
assert(params.negative_cache_size > 0);
assert(params.include_overlap > 0.1);
assert(params.exclude_overlap > 0.1);
assert(params.exclude_overlap >= 0.0);
assert(params.alpha > 0 && params.alpha < 1);
assert(params.alpha_ratio > 0 && params.alpha_ratio < 1);
assert(params.C > 0);
Expand Down Expand Up @@ -1540,8 +1481,6 @@ void ccv_dpm_mixture_model_new(char** posfiles, ccv_rect_t* bboxes, int posnum,
int negi = 0;
int abort = 0;
int* negk = (int*)ccmalloc(params.negative_cache_size * sizeof(int));
for (i = 0; i < params.negative_cache_size; i++)
negk[i] = i;
if (0 == _ccv_dpm_read_negative_feature_vectors(&negv, negk, params.negative_cache_size, neg_vector_checkpoint))
printf(" - read collected negative responses from last interrupted process\n");
_ccv_dpm_read_gradient_descent_progress(&c, &t, &r, &previous_loss, &positive_loss, &negative_loss, &loss, &pos_prog, &neg_prog, &negi, order, posnum + negnum, gradient_progress_checkpoint);
Expand Down Expand Up @@ -1609,7 +1548,7 @@ void ccv_dpm_mixture_model_new(char** posfiles, ccv_rect_t* bboxes, int posnum,
}
} else {
// the cache is used up now, collect again
if ((neg_prog % params.negative_cache_size) == 0)
if (!negv || negi >= ccv_min(negv->rnum, params.negative_cache_size))
{
if (negv)
{
Expand All @@ -1633,13 +1572,20 @@ void ccv_dpm_mixture_model_new(char** posfiles, ccv_rect_t* bboxes, int posnum,
last_update = printf(" - pause, collecting negative examples, current loss %.5lf (positive %.5lf, negative %.5f) --- %2d%% ( 0%%) ", loss / ccv_max(1, pos_prog + neg_prog), positive_loss / ccv_max(1, pos_prog), negative_loss / ccv_max(1, neg_prog), (i + 1) * 100 / (posnum + negnum));
fflush(stdout);
if (negv->rnum < params.negative_cache_size)
_ccv_dpm_collect_from_background_and_positive(negv, rng, bgfiles, bgnum, posfiles, posnum, bboxes, model, params, 0);
_ccv_dpm_collect_from_background(negv, rng, bgfiles, bgnum, model, params, 0);
negi = 0;
gsl_ran_shuffle(rng, negk, params.negative_cache_size, sizeof(int));
for (j = 0; j < ccv_min(negv->rnum, params.negative_cache_size); j++)
negk[j] = j;
gsl_ran_shuffle(rng, negk, ccv_min(negv->rnum, params.negative_cache_size), sizeof(int));
_ccv_dpm_write_negative_feature_vectors(negv, negk, params.negative_cache_size, neg_vector_checkpoint);
_ccv_dpm_write_gradient_descent_progress(c, t, i, previous_loss, positive_loss, negative_loss, loss, pos_prog, neg_prog, negi, order, posnum + negnum, gradient_progress_checkpoint);
_ccv_dpm_write_checkpoint(_model, subcheckpoint);
_ccv_dpm_write_checkpoint(model, checkpoint);
if (negv->rnum <= ccv_min(params.negative_cache_size / 2, ccv_min(REGQ, MINI_BATCH))) // we cannot get sufficient negatives, abort
{
abort = 1;
break;
}
}
if (negv && negv->rnum > 0)
{
Expand Down Expand Up @@ -1749,7 +1695,6 @@ void ccv_dpm_mixture_model_new(char** posfiles, ccv_rect_t* bboxes, int posnum,
ccv_array_free(negv);
negv = 0;
negi = 0;
gsl_ran_shuffle(rng, negk, params.negative_cache_size, sizeof(int));
remove(neg_vector_checkpoint);
}
t = 0;
Expand Down

0 comments on commit 4dbf477

Please sign in to comment.