Skip to content

Commit

Permalink
fix a few compilation errors and run valgrind against icfdetect
Browse files Browse the repository at this point in the history
  • Loading branch information
liuliu committed Sep 3, 2013
1 parent 0cc48c2 commit 5902640
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 33 deletions.
4 changes: 2 additions & 2 deletions doc/icf.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ The validation script is available at ./bin/icfvldtr.rb.

76.23% (52)

Which has slightly higher recall than DPM implementation provided in ccv, with higher
false alarms too.
Which has roughly the same recall as DPM implementation provided in ccv, with roughly
the same false alarms too.

How to train my own detector?
-----------------------------
Expand Down
65 changes: 38 additions & 27 deletions lib/ccv_icf.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,31 @@ void ccv_icf(ccv_dense_matrix_t* a, ccv_dense_matrix_t** b, int type)
ccv_matrix_free(mg);
}

static inline float _ccv_icf_run_feature(ccv_icf_feature_t* feature, float* ptr, int cols, int ch, int x, int y)
{
float c = feature->beta;
int q;
for (q = 0; q < feature->count; q++)
c += (ptr[(feature->sat[q * 2 + 1].x + x + 1 + (feature->sat[q * 2 + 1].y + y + 1) * cols) * ch + feature->channel[q]] - ptr[(feature->sat[q * 2].x + x + (feature->sat[q * 2 + 1].y + y + 1) * cols) * ch + feature->channel[q]] + ptr[(feature->sat[q * 2].x + x + (feature->sat[q * 2].y + y) * cols) * ch + feature->channel[q]] - ptr[(feature->sat[q * 2 + 1].x + x + 1 + (feature->sat[q * 2].y + y) * cols) * ch + feature->channel[q]]) * feature->alpha[q];
return c;
}

static inline int _ccv_icf_run_weak_classifier(ccv_icf_decision_tree_t* weak_classifier, float* ptr, int cols, int ch, int x, int y)
{
float c = _ccv_icf_run_feature(weak_classifier->features, ptr, cols, ch, x, y);
if (c > 0)
{
if (!(weak_classifier->pass & 0x1))
return 1;
return _ccv_icf_run_feature(weak_classifier->features + 2, ptr, cols, ch, x, y) > 0;
} else {
if (!(weak_classifier->pass & 0x2))
return 0;
return _ccv_icf_run_feature(weak_classifier->features + 1, ptr, cols, ch, x, y) > 0;
}
}

#ifdef HAVE_GSL
static void _ccv_icf_randomize_feature(gsl_rng* rng, ccv_size_t size, int minimum, ccv_icf_feature_t* feature, int grayscale)
{
feature->count = gsl_rng_uniform_int(rng, CCV_ICF_SAT_MAX) + 1;
Expand Down Expand Up @@ -456,15 +481,6 @@ static void _ccv_icf_read_classifier_cascade_state(const char* directory, ccv_ic
static CCV_IMPLEMENT_QSORT(_ccv_icf_precomputed_ordering, ccv_icf_value_index_t, less_than)
#undef less_than

static inline float _ccv_icf_run_feature(ccv_icf_feature_t* feature, float* ptr, int cols, int ch, int x, int y)
{
float c = feature->beta;
int q;
for (q = 0; q < feature->count; q++)
c += (ptr[(feature->sat[q * 2 + 1].x + x + 1 + (feature->sat[q * 2 + 1].y + y + 1) * cols) * ch + feature->channel[q]] - ptr[(feature->sat[q * 2].x + x + (feature->sat[q * 2 + 1].y + y + 1) * cols) * ch + feature->channel[q]] + ptr[(feature->sat[q * 2].x + x + (feature->sat[q * 2].y + y) * cols) * ch + feature->channel[q]] - ptr[(feature->sat[q * 2 + 1].x + x + 1 + (feature->sat[q * 2].y + y) * cols) * ch + feature->channel[q]]) * feature->alpha[q];
return c;
}

static inline void _ccv_icf_3_uint8_to_1_uint1_1_uint23(uint8_t* u8, uint8_t* u1, uint32_t* uint23)
{
*u1 = (u8[0] >> 7);
Expand Down Expand Up @@ -851,21 +867,6 @@ static double _ccv_icf_find_best_weak_classifier(ccv_icf_feature_t* features, in
return rate;
}

static inline int _ccv_icf_run_weak_classifier(ccv_icf_decision_tree_t* weak_classifier, float* ptr, int cols, int ch, int x, int y)
{
float c = _ccv_icf_run_feature(weak_classifier->features, ptr, cols, ch, x, y);
if (c > 0)
{
if (!(weak_classifier->pass & 0x1))
return 1;
return _ccv_icf_run_feature(weak_classifier->features + 2, ptr, cols, ch, x, y) > 0;
} else {
if (!(weak_classifier->pass & 0x2))
return 0;
return _ccv_icf_run_feature(weak_classifier->features + 1, ptr, cols, ch, x, y) > 0;
}
}

static ccv_array_t* _ccv_icf_collect_validates(gsl_rng* rng, ccv_size_t size, ccv_margin_t margin, ccv_array_t* validatefiles, int grayscale)
{
ccv_array_t* validates = ccv_array_new(ccv_compute_dense_matrix_size(size.height + margin.top + margin.bottom + 2, size.width + margin.left + margin.right + 2, CCV_8U | (grayscale ? CCV_C1 : CCV_C3)), validatefiles->rnum, 0);
Expand Down Expand Up @@ -1361,9 +1362,11 @@ static double _ccv_icf_rate_weak_classifier(ccv_icf_decision_tree_t* weak_classi
return rate;
}
#endif
#endif

ccv_icf_classifier_cascade_t* ccv_icf_classifier_cascade_new(ccv_array_t* posfiles, int posnum, ccv_array_t* bgfiles, int negnum, ccv_array_t* validatefiles, const char* dir, ccv_icf_new_param_t params)
{
#ifdef HAVE_GSL
_ccv_icf_check_params(params);
assert(posfiles->rnum > 0);
assert(bgfiles->rnum > 0);
Expand Down Expand Up @@ -1530,10 +1533,15 @@ ccv_icf_classifier_cascade_t* ccv_icf_classifier_cascade_new(ccv_array_t* posfil
gsl_rng_free(rng);
ccv_function_state_finish();
return z.classifier;
#else
assert(0 && "ccv_icf_classifier_cascade_new requires GSL library support");
return 0;
#endif
}

void ccv_icf_classifier_cascade_soft(ccv_icf_classifier_cascade_t* cascade, ccv_array_t* posfiles, double acceptance)
{
#ifdef HAVE_GSL
printf("with %d positive examples\n"
"going to accept %.2lf%% positive examples\n",
posfiles->rnum, acceptance * 100);
Expand Down Expand Up @@ -1567,6 +1575,9 @@ void ccv_icf_classifier_cascade_soft(ccv_icf_classifier_cascade_t* cascade, ccv_
_ccv_icf_classifier_cascade_soft_with_validates(validates, cascade, acceptance);
ccv_array_free(validates);
gsl_rng_free(rng);
#else
assert(0 && "ccv_icf_classifier_cascade_soft requires GSL library support");
#endif
}

static void _ccv_icf_read_classifier_cascade_with_fd(FILE* r, ccv_icf_classifier_cascade_t* cascade)
Expand Down Expand Up @@ -1754,6 +1765,8 @@ static void _ccv_icf_detect_objects_with_classifier_cascade(ccv_dense_matrix_t*
{
int rows = (int)(pyr[i]->rows / scale + 0.5);
int cols = (int)(pyr[i]->cols / scale + 0.5);
if (rows < cascade->size.height || cols < cascade->size.width)
break;
ccv_dense_matrix_t* image = k == 0 ? pyr[i] : 0;
if (k > 0)
ccv_resample(pyr[i], &image, 0, rows, cols, CCV_INTER_AREA);
Expand All @@ -1769,8 +1782,6 @@ static void _ccv_icf_detect_objects_with_classifier_cascade(ccv_dense_matrix_t*
ccv_dense_matrix_t* sat = 0;
ccv_sat(icf, &sat, 0, CCV_PADDING_ZERO);
ccv_matrix_free(icf);
if (rows < cascade->size.height || cols < cascade->size.width)
break;
int ch = CCV_GET_CHANNEL(sat->type);
float* ptr = sat->data.f32;
for (y = 0; y < rows; y += params.step_through)
Expand Down Expand Up @@ -1966,7 +1977,7 @@ ccv_array_t* ccv_icf_detect_objects(ccv_dense_matrix_t* a, void* cascade, int co
// count number of neighbors
for(i = 0; i < seq[k]->rnum; i++)
{
ccv_root_comp_t r1 = *(ccv_root_comp_t*)ccv_array_get(seq[k], i);
ccv_comp_t r1 = *(ccv_comp_t*)ccv_array_get(seq[k], i);
int idx = *(int*)ccv_array_get(idx_seq, i);

comps[idx].id = r1.id;
Expand Down
12 changes: 8 additions & 4 deletions lib/ccv_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,20 +217,24 @@ int ccv_write(ccv_dense_matrix_t* mat, char* out, int* len, int type, void* conf
}
switch (type)
{
#ifdef HAVE_LIBJPEG
case CCV_IO_JPEG_FILE:
#ifdef HAVE_LIBJPEG
_ccv_write_jpeg_fd(mat, fd, conf);
if (len != 0)
*len = 0;
break;
#else
assert(0 && "ccv_write requires libjpeg support for JPEG format");
#endif
#ifdef HAVE_LIBPNG
break;
case CCV_IO_PNG_FILE:
#ifdef HAVE_LIBPNG
_ccv_write_png_fd(mat, fd, conf);
if (len != 0)
*len = 0;
break;
#else
assert(0 && "ccv_write requires libpng support for PNG format");
#endif
break;
case CCV_IO_BINARY_FILE:
_ccv_write_binary_fd(mat, fd, conf);
if (len != 0)
Expand Down
7 changes: 7 additions & 0 deletions lib/configure
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ else
;;
* ) ;;
esac
echo -ne " Run \033[4msanity assertion\033[m along side [y/N] ? "
read -n 1 yn ; if [ ! -z $yn ] ; then echo ; fi
case $yn in
[yY]* ) CFLAGS="$CFLAGS-D USE_SANITY_ASSERTION "
;;
* ) ;;
esac
echo -ne " With \033[4mavcodec\033[m [y/N] ? "
read -n 1 yn ; if [ ! -z $yn ] ; then echo ; fi
case $yn in
Expand Down

0 comments on commit 5902640

Please sign in to comment.