Skip to content

Commit

Permalink
added ccv_set_output_levels so that convnet training misc can be put …
Browse files Browse the repository at this point in the history
…in verbose
  • Loading branch information
liuliu committed Sep 22, 2014
1 parent 60ca04c commit 72a33c8
Show file tree
Hide file tree
Showing 41 changed files with 259 additions and 244 deletions.
18 changes: 18 additions & 0 deletions lib/ccv.h
Original file line number Diff line number Diff line change
Expand Up @@ -1236,4 +1236,22 @@ void ccv_convnet_write(ccv_convnet_t* convnet, const char* filename, ccv_convnet
void ccv_convnet_compact(ccv_convnet_t* convnet); // remove unused resources
void ccv_convnet_free(ccv_convnet_t* convnet);

/* add for command-line outputs, b/c ccv's training steps has a ton of outputs,
* and in the future, it can be piped into callback functions for critical information
* (such as the on-going missing rate, or iterations etc.) */

enum {
CCV_CLI_ERROR = 1 << 2,
CCV_CLI_INFO = 1 << 1,
CCV_CLI_VERBOSE = 1,
CCV_CLI_NONE = 0,
};


int ccv_cli_output_level_and_above(int level);
void ccv_set_cli_output_levels(int level);
int ccv_get_cli_output_levels(void);

#define CCV_CLI_OUTPUT_LEVEL_IS(a) (a & ccv_get_cli_output_levels())

#endif
50 changes: 25 additions & 25 deletions lib/ccv_bbf.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ static int _ccv_prepare_background_data(ccv_bbf_classifier_cascade_t* cascade, c
int rneg = negtotal;
for (t = 0; negtotal < negnum; t++)
{
printf("preparing negative data ... 0%%");
PRINT(CCV_CLI_INFO, "preparing negative data ... 0%%");
for (i = 0; i < bgnum; i++)
{
negperbg = (t < 2) ? (negnum - negtotal) / (bgnum - i) + 1 : negnum - negtotal;
Expand All @@ -184,7 +184,7 @@ static int _ccv_prepare_background_data(ccv_bbf_classifier_cascade_t* cascade, c
assert((image->type & CCV_C1) && (image->type & CCV_8U));
if (image == 0)
{
printf("\n%s file corrupted\n", bgfiles[i]);
PRINT(CCV_CLI_ERROR, "\n%s file corrupted\n", bgfiles[i]);
continue;
}
if (t % 2 != 0)
Expand Down Expand Up @@ -267,26 +267,26 @@ static int _ccv_prepare_background_data(ccv_bbf_classifier_cascade_t* cascade, c
ccv_array_free(detected);
ccv_matrix_free(image);
ccv_drain_cache();
printf("\rpreparing negative data ... %2d%%", 100 * negtotal / negnum);
PRINT(CCV_CLI_INFO, "\rpreparing negative data ... %2d%%", 100 * negtotal / negnum);
fflush(0);
if (negtotal >= negnum)
break;
}
if (rneg == negtotal)
break;
rneg = negtotal;
printf("\nentering additional round %d\n", t + 1);
PRINT(CCV_CLI_INFO, "\nentering additional round %d\n", t + 1);
}
gsl_rng_free(rng);
ccfree(idcheck);
ccv_drain_cache();
printf("\n");
PRINT(CCV_CLI_INFO, "\n");
return negtotal;
}

static void _ccv_prepare_positive_data(ccv_dense_matrix_t** posimg, unsigned char** posdata, ccv_size_t size, int posnum)
{
printf("preparing positive data ... 0%%");
PRINT(CCV_CLI_INFO, "preparing positive data ... 0%%");
int i;
for (i = 0; i < posnum; i++)
{
Expand All @@ -305,14 +305,14 @@ static void _ccv_prepare_positive_data(ccv_dense_matrix_t** posimg, unsigned cha
memcpy(posdata[i] + isizs0, imgs1->data.u8, isizs1);
memcpy(posdata[i] + isizs0 + isizs1, imgs2->data.u8, isizs2);

printf("\rpreparing positive data ... %2d%%", 100 * (i + 1) / posnum);
PRINT(CCV_CLI_INFO, "\rpreparing positive data ... %2d%%", 100 * (i + 1) / posnum);
fflush(0);

ccv_matrix_free(imgs1);
ccv_matrix_free(imgs2);
}
ccv_drain_cache();
printf("\n");
PRINT(CCV_CLI_INFO, "\n");
}

typedef struct {
Expand Down Expand Up @@ -454,16 +454,16 @@ static ccv_bbf_feature_t _ccv_bbf_genetic_optimize(unsigned char** posdata, int
{
best_err = min_err;
memcpy(&best, &gene[min_id].feature, sizeof(best));
printf("best bbf feature with error %f\n|-size: %d\n|-positive point: ", best_err, best.size);
PRINT(CCV_CLI_INFO, "best bbf feature with error %f\n|-size: %d\n|-positive point: ", best_err, best.size);
for (i = 0; i < best.size; i++)
printf("(%d %d %d), ", best.px[i], best.py[i], best.pz[i]);
printf("\n|-negative point: ");
PRINT(CCV_CLI_INFO, "(%d %d %d), ", best.px[i], best.py[i], best.pz[i]);
PRINT(CCV_CLI_INFO, "\n|-negative point: ");
for (i = 0; i < best.size; i++)
printf("(%d %d %d), ", best.nx[i], best.ny[i], best.nz[i]);
printf("\n");
PRINT(CCV_CLI_INFO, "(%d %d %d), ", best.nx[i], best.ny[i], best.nz[i]);
PRINT(CCV_CLI_INFO, "\n");
it = 0;
}
printf("minimum error achieved in round %d(%d) : %f with %d ms\n", t, it, min_err, timer / 1000);
PRINT(CCV_CLI_INFO, "minimum error achieved in round %d(%d) : %f with %d ms\n", t, it, min_err, timer / 1000);
_ccv_bbf_genetic_qsort(gene, pnum, 0);
for (i = 0; i < ftnum; i++)
++gene[i].age;
Expand Down Expand Up @@ -602,13 +602,13 @@ static ccv_bbf_gene_t _ccv_bbf_best_gene(ccv_bbf_gene_t* gene, int pnum, int poi
min_err = gene[i].error;
break;
}
printf("local best bbf feature with error %f\n|-size: %d\n|-positive point: ", min_err, gene[min_id].feature.size);
PRINT(CCV_CLI_INFO, "local best bbf feature with error %f\n|-size: %d\n|-positive point: ", min_err, gene[min_id].feature.size);
for (i = 0; i < gene[min_id].feature.size; i++)
printf("(%d %d %d), ", gene[min_id].feature.px[i], gene[min_id].feature.py[i], gene[min_id].feature.pz[i]);
printf("\n|-negative point: ");
PRINT(CCV_CLI_INFO, "(%d %d %d), ", gene[min_id].feature.px[i], gene[min_id].feature.py[i], gene[min_id].feature.pz[i]);
PRINT(CCV_CLI_INFO, "\n|-negative point: ");
for (i = 0; i < gene[min_id].feature.size; i++)
printf("(%d %d %d), ", gene[min_id].feature.nx[i], gene[min_id].feature.ny[i], gene[min_id].feature.nz[i]);
printf("\nthe computation takes %d ms\n", timer / 1000);
PRINT(CCV_CLI_INFO, "(%d %d %d), ", gene[min_id].feature.nx[i], gene[min_id].feature.ny[i], gene[min_id].feature.nz[i]);
PRINT(CCV_CLI_INFO, "\nthe computation takes %d ms\n", timer / 1000);
return gene[min_id];
}

Expand Down Expand Up @@ -672,7 +672,7 @@ static ccv_bbf_feature_t _ccv_bbf_convex_optimize(unsigned char** posdata, int p
g++;
}
}
printf("bootstrapping round : %d\n", t);
PRINT(CCV_CLI_INFO, "bootstrapping round : %d\n", t);
ccv_bbf_gene_t local_gene = _ccv_bbf_best_gene(gene, g, 2, posdata, posnum, negdata, negnum, size, pw, nw);
if (local_gene.error >= best_gene.error - 1e-10)
break;
Expand Down Expand Up @@ -824,7 +824,7 @@ static ccv_bbf_feature_t _ccv_bbf_convex_optimize(unsigned char** posdata, int p
}
gene[g] = best_gene;
g++;
printf("float search round : %d\n", t);
PRINT(CCV_CLI_INFO, "float search round : %d\n", t);
ccv_bbf_gene_t local_gene = _ccv_bbf_best_gene(gene, g, CCV_BBF_POINT_MIN, posdata, posnum, negdata, negnum, size, pw, nw);
if (local_gene.error >= best_gene.error - 1e-10)
break;
Expand Down Expand Up @@ -1026,7 +1026,7 @@ void ccv_bbf_classifier_cascade_new(ccv_dense_matrix_t** posimg, int posnum, cha
}
_ccv_prepare_positive_data(posimg, posdata, cascade->size, posnum);
rpos = _ccv_prune_positive_data(cascade, posdata, posnum, cascade->size);
printf("%d postivie data and %d negative data in training\n", rpos, rneg);
PRINT(CCV_CLI_INFO, "%d postivie data and %d negative data in training\n", rpos, rneg);
/* reweight to 1.00 */
totalw = 0;
for (j = 0; j < rpos; j++)
Expand Down Expand Up @@ -1060,8 +1060,8 @@ void ccv_bbf_classifier_cascade_new(ccv_dense_matrix_t** posimg, int posnum, cha
++efp;
}
fp /= rneg; efp /= rneg;
printf("stage classifier real TP rate : %f, FP rate : %f\n", tp, fp);
printf("stage classifier TP rate : %f, FP rate : %f at threshold : %f\n", etp, efp, classifier.threshold);
PRINT(CCV_CLI_INFO, "stage classifier real TP rate : %f, FP rate : %f\n", tp, fp);
PRINT(CCV_CLI_INFO, "stage classifier TP rate : %f, FP rate : %f at threshold : %f\n", etp, efp, classifier.threshold);
if (k > 0)
{
/* save classifier state */
Expand Down Expand Up @@ -1110,7 +1110,7 @@ void ccv_bbf_classifier_cascade_new(ccv_dense_matrix_t** posimg, int posnum, cha
for (j = 0; j < rneg; j++)
nw[j] = nw[j] / totalw;
double c = log(rw);
printf("coefficient of feature %d: %f\n", k + 1, c);
PRINT(CCV_CLI_INFO, "coefficient of feature %d: %f\n", k + 1, c);
classifier.count = k + 1;
/* resizing classifier */
if (k >= cacheK)
Expand Down
6 changes: 3 additions & 3 deletions lib/ccv_convnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,7 @@ void ccv_convnet_supervised_train(ccv_convnet_t* convnet, ccv_array_t* categoriz
_ccv_convnet_propagate_loss(convnet, categorized->matrix, softmax, update_params);
if ((i + 1) % params.mini_batch == 0)
{
FLUSH(" - at epoch %03d / %d => stochastic gradient descent at %d / %d", t + 1, params.max_epoch, (i + 1) / params.mini_batch, aligned_rnum / params.mini_batch);
FLUSH(CCV_CLI_INFO, " - at epoch %03d / %d => stochastic gradient descent at %d / %d", t + 1, params.max_epoch, (i + 1) / params.mini_batch, aligned_rnum / params.mini_batch);
// update weights
_ccv_convnet_update(convnet, params.mini_batch, momentum, update_params, params.layer_params);
_ccv_convnet_update_zero(update_params);
Expand All @@ -1336,14 +1336,14 @@ void ccv_convnet_supervised_train(ccv_convnet_t* convnet, ccv_array_t* categoriz
int miss = 0;
for (i = 0; i < tests->rnum; i++)
{
FLUSH(" - at epoch %03d / %d => going through %d / %d for tests", t + 1, params.max_epoch, i + 1, tests->rnum);
FLUSH(CCV_CLI_INFO, " - at epoch %03d / %d => going through %d / %d for tests", t + 1, params.max_epoch, i + 1, tests->rnum);
ccv_categorized_t* test = (ccv_categorized_t*)ccv_array_get(tests, i);
int c = 0;
_ccv_convnet_classify(convnet, &test->matrix, &c, 1);
if (c != test->c)
++miss;
}
FLUSH(" - at epoch %03d / %d => with miss rate %.2f%%\n", t + 1, params.max_epoch, miss * 100.0f / tests->rnum);
FLUSH(CCV_CLI_INFO, " - at epoch %03d / %d => with miss rate %.2f%%\n", t + 1, params.max_epoch, miss * 100.0f / tests->rnum);
if (t + 1 < params.max_epoch)
{
// reshuffle the parts we visited and move the rest to the beginning
Expand Down
Loading

0 comments on commit 72a33c8

Please sign in to comment.