Skip to content

Commit

Permalink
add max_dimension to REST interface
Browse files Browse the repository at this point in the history
  • Loading branch information
liuliu committed Mar 12, 2014
1 parent 7d254ad commit f009aa3
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 80 deletions.
4 changes: 2 additions & 2 deletions bin/image-net.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,10 @@ int main(int argc, char** argv)
for (i = 0; i < 13; i++)
{
layer_params[i].w.decay = 0.0005;
layer_params[i].w.learn_rate = 0.00001;
layer_params[i].w.learn_rate = 0.01;
layer_params[i].w.momentum = 0.9;
layer_params[i].bias.decay = 0;
layer_params[i].bias.learn_rate = 0.00001;
layer_params[i].bias.learn_rate = 0.01;
layer_params[i].bias.momentum = 0.9;
}
layer_params[10].dor = 0.5;
Expand Down
4 changes: 2 additions & 2 deletions lib/cuda/cwc_convnet.cu
Original file line number Diff line number Diff line change
Expand Up @@ -1701,8 +1701,8 @@ static void _cwc_convnet_batch_formation(gsl_rng* rng, ccv_array_t* categorizeds
ccv_dense_matrix_t* input = 0;
if (image->cols != dim.width || image->rows != dim.height)
{
int x = rng ? gsl_rng_uniform_int(rng, image->cols - dim.width + 1) : (image->cols - dim.width + 1) / 2;
int y = rng ? gsl_rng_uniform_int(rng, image->rows - dim.height + 1) : (image->rows - dim.height + 1) / 2;
int x = (image->cols - dim.width + 1) / 2;
int y = (image->rows - dim.height + 1) / 2;
assert(x == 0 || y == 0);
ccv_slice(image, (ccv_matrix_t**)&input, CCV_32F, y, x, dim.height, dim.width);
} else
Expand Down
48 changes: 31 additions & 17 deletions serve/bbf.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,38 @@
static void uri_bbf_on_model_string(void* context, char* string);
static void uri_bbf_on_source_blob(void* context, ebb_buf data);

typedef struct {
ccv_bbf_param_t params;
int max_dimension;
} ccv_bbf_uri_param_t;

static const param_dispatch_t param_map[] = {
{
.property = "accurate",
.type = PARAM_TYPE_BOOL,
.offset = offsetof(ccv_bbf_param_t, accurate),
.offset = offsetof(ccv_bbf_uri_param_t, params) + offsetof(ccv_bbf_param_t, accurate),
},
{
.property = "interval",
.type = PARAM_TYPE_INT,
.offset = offsetof(ccv_bbf_param_t, interval),
.offset = offsetof(ccv_bbf_uri_param_t, params) + offsetof(ccv_bbf_param_t, interval),
},
{
.property = "max_dimension",
.type = PARAM_TYPE_INT,
.offset = offsetof(ccv_bbf_uri_param_t, max_dimension),
},
{
.property = "min_neighbors",
.type = PARAM_TYPE_INT,
.offset = offsetof(ccv_bbf_param_t, min_neighbors),
.offset = offsetof(ccv_bbf_uri_param_t, params) + offsetof(ccv_bbf_param_t, min_neighbors),
},
{
.property = "model",
.type = PARAM_TYPE_STRING,
.on_string = uri_bbf_on_model_string,
.offset = 0,
},
{
.property = "size",
.type = PARAM_TYPE_INT,
.offset = offsetof(ccv_bbf_param_t, min_neighbors),
},
{
.property = "source",
.type = PARAM_TYPE_BODY,
Expand All @@ -50,15 +55,16 @@ typedef struct {
typedef struct {
param_parser_t param_parser;
bbf_context_t* context;
ccv_bbf_param_t params;
ccv_bbf_uri_param_t params;
ccv_bbf_classifier_cascade_t* cascade;
ebb_buf source;
} bbf_param_parser_t;

static void uri_bbf_param_parser_init(bbf_param_parser_t* parser)
{
param_parser_init(&parser->param_parser, param_map, sizeof(param_map) / sizeof(param_dispatch_t), &parser->params, parser);
parser->params = ccv_bbf_default_params;
parser->params.params = ccv_bbf_default_params;
parser->params.max_dimension = 0;
parser->cascade = 0;
parser->source.data = 0;
}
Expand Down Expand Up @@ -108,10 +114,10 @@ void* uri_bbf_detect_objects_init(void)
assert(param_parser_map_alphabet(param_map, sizeof(param_map) / sizeof(param_dispatch_t)) == 0);
context->desc = param_parser_map_http_body(param_map, sizeof(param_map) / sizeof(param_dispatch_t),
"[{"
"\"x\":\"integer\","
"\"y\":\"integer\","
"\"width\":\"integer\","
"\"height\":\"integer\","
"\"x\":\"number\","
"\"y\":\"number\","
"\"width\":\"number\","
"\"height\":\"number\","
"\"confidence\":\"number\""
"}]");
return context;
Expand Down Expand Up @@ -158,8 +164,16 @@ int uri_bbf_detect_objects(const void* context, const void* parsed, ebb_buf* buf
free(parser);
return -1;
}
ccv_array_t* seq = ccv_bbf_detect_objects(image, &parser->cascade, 1, parser->params);
ccv_matrix_free(image);
ccv_dense_matrix_t* resize = 0;
if (parser->params.max_dimension > 0 && (image->rows > parser->params.max_dimension || image->cols > parser->params.max_dimension))
{
ccv_resample(image, &resize, 0, ccv_max(parser->params.max_dimension, (int)(image->rows * (float)parser->params.max_dimension / image->cols + 0.5)), ccv_max(parser->params.max_dimension, (int)(image->cols * (float)parser->params.max_dimension / image->rows + 0.5)), CCV_INTER_AREA);
ccv_matrix_free(image);
} else
resize = image;
ccv_array_t* seq = ccv_bbf_detect_objects(resize, &parser->cascade, 1, parser->params.params);
float width = resize->cols, height = resize->rows;
ccv_matrix_free(resize);
if (seq == 0)
{
free(parser);
Expand All @@ -176,7 +190,7 @@ int uri_bbf_detect_objects(const void* context, const void* parsed, ebb_buf* buf
{
char cell[128];
ccv_comp_t* comp = (ccv_comp_t*)ccv_array_get(seq, i);
snprintf(cell, 128, "{\"x\":%d,\"y\":%d,\"width\":%d,\"height\":%d,\"confidence\":%f}", comp->rect.x, comp->rect.y, comp->rect.width, comp->rect.height, comp->classification.confidence);
snprintf(cell, 128, "{\"x\":%f,\"y\":%f,\"width\":%f,\"height\":%f,\"confidence\":%f}", comp->rect.x / width, comp->rect.y / height, comp->rect.width / width, comp->rect.height / height, comp->classification.confidence);
size_t len = strnlen(cell, 128);
while (buf->written + len + 1 >= buf->len)
{
Expand Down
53 changes: 36 additions & 17 deletions serve/dpm.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,26 @@
static void uri_dpm_on_model_string(void* context, char* string);
static void uri_dpm_on_source_blob(void* context, ebb_buf data);

typedef struct {
ccv_dpm_param_t params;
int max_dimension;
} ccv_dpm_uri_param_t;

static const param_dispatch_t param_map[] = {
{
.property = "interval",
.type = PARAM_TYPE_INT,
.offset = offsetof(ccv_dpm_param_t, interval),
.offset = offsetof(ccv_dpm_uri_param_t, params) + offsetof(ccv_dpm_param_t, interval),
},
{
.property = "max_dimension",
.type = PARAM_TYPE_INT,
.offset = offsetof(ccv_dpm_uri_param_t, max_dimension),
},
{
.property = "min_neighbors",
.type = PARAM_TYPE_INT,
.offset = offsetof(ccv_dpm_param_t, min_neighbors),
.offset = offsetof(ccv_dpm_uri_param_t, params) + offsetof(ccv_dpm_param_t, min_neighbors),
},
{
.property = "model",
Expand All @@ -33,7 +43,7 @@ static const param_dispatch_t param_map[] = {
{
.property = "threshold",
.type = PARAM_TYPE_FLOAT,
.offset = offsetof(ccv_dpm_param_t, threshold),
.offset = offsetof(ccv_dpm_uri_param_t, params) + offsetof(ccv_dpm_param_t, threshold),
},
};

Expand All @@ -46,15 +56,16 @@ typedef struct {
typedef struct {
param_parser_t param_parser;
dpm_context_t* context;
ccv_dpm_param_t params;
ccv_dpm_uri_param_t params;
ccv_dpm_mixture_model_t* mixture_model;
ebb_buf source;
} dpm_param_parser_t;

static void uri_dpm_param_parser_init(dpm_param_parser_t* parser)
{
param_parser_init(&parser->param_parser, param_map, sizeof(param_map) / sizeof(param_dispatch_t), &parser->params, parser);
parser->params = ccv_dpm_default_params;
parser->params.params = ccv_dpm_default_params;
parser->params.max_dimension = 0;
parser->mixture_model = 0;
parser->source.data = 0;
}
Expand Down Expand Up @@ -107,16 +118,16 @@ void* uri_dpm_detect_objects_init(void)
assert(param_parser_map_alphabet(param_map, sizeof(param_map) / sizeof(param_dispatch_t)) == 0);
context->desc = param_parser_map_http_body(param_map, sizeof(param_map) / sizeof(param_dispatch_t),
"[{"
"\"x\":\"integer\","
"\"y\":\"integer\","
"\"width\":\"integer\","
"\"height\":\"integer\","
"\"x\":\"number\","
"\"y\":\"number\","
"\"width\":\"number\","
"\"height\":\"number\","
"\"confidence\":\"number\","
"\"parts\":[{"
"\"x\":\"integer\","
"\"y\":\"integer\","
"\"width\":\"integer\","
"\"height\":\"integer\","
"\"x\":\"number\","
"\"y\":\"number\","
"\"width\":\"number\","
"\"height\":\"number\","
"\"confidence\":\"number\""
"}]"
"}]");
Expand Down Expand Up @@ -165,8 +176,16 @@ int uri_dpm_detect_objects(const void* context, const void* parsed, ebb_buf* buf
free(parser);
return -1;
}
ccv_array_t* seq = ccv_dpm_detect_objects(image, &parser->mixture_model, 1, parser->params);
ccv_matrix_free(image);
ccv_dense_matrix_t* resize = 0;
if (parser->params.max_dimension > 0 && (image->rows > parser->params.max_dimension || image->cols > parser->params.max_dimension))
{
ccv_resample(image, &resize, 0, ccv_max(parser->params.max_dimension, (int)(image->rows * (float)parser->params.max_dimension / image->cols + 0.5)), ccv_max(parser->params.max_dimension, (int)(image->cols * (float)parser->params.max_dimension / image->rows + 0.5)), CCV_INTER_AREA);
ccv_matrix_free(image);
} else
resize = image;
ccv_array_t* seq = ccv_dpm_detect_objects(resize, &parser->mixture_model, 1, parser->params.params);
float width = resize->cols, height = resize->rows;
ccv_matrix_free(resize);
if (seq == 0)
{
free(parser);
Expand All @@ -183,7 +202,7 @@ int uri_dpm_detect_objects(const void* context, const void* parsed, ebb_buf* buf
{
char cell[128];
ccv_root_comp_t* comp = (ccv_root_comp_t*)ccv_array_get(seq, i);
snprintf(cell, 128, "{\"x\":%d,\"y\":%d,\"width\":%d,\"height\":%d,\"confidence\":%f,\"parts\":[", comp->rect.x, comp->rect.y, comp->rect.width, comp->rect.height, comp->classification.confidence);
snprintf(cell, 128, "{\"x\":%f,\"y\":%f,\"width\":%f,\"height\":%f,\"confidence\":%f,\"parts\":[", comp->rect.x / width, comp->rect.y / height, comp->rect.width / width, comp->rect.height / height, comp->classification.confidence);
size_t len = strnlen(cell, 128);
while (buf->written + len >= buf->len)
{
Expand All @@ -194,7 +213,7 @@ int uri_dpm_detect_objects(const void* context, const void* parsed, ebb_buf* buf
buf->written += len;
for (j = 0; j < comp->pnum; j++)
{
snprintf(cell, 128, "{\"x\":%d,\"y\":%d,\"width\":%d,\"height\":%d,\"confidence\":%f}", comp->part[j].rect.x, comp->part[j].rect.y, comp->part[j].rect.width, comp->part[j].rect.height, comp->part[j].classification.confidence);
snprintf(cell, 128, "{\"x\":%f,\"y\":%f,\"width\":%f,\"height\":%f,\"confidence\":%f}", comp->part[j].rect.x / width, comp->part[j].rect.y / height, comp->part[j].rect.width / width, comp->part[j].rect.height / height, comp->part[j].classification.confidence);
len = strnlen(cell, 128);
while (buf->written + len + 3 >= buf->len)
{
Expand Down
43 changes: 31 additions & 12 deletions serve/icf.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,26 @@
static void uri_icf_on_model_string(void* context, char* string);
static void uri_icf_on_source_blob(void* context, ebb_buf data);

typedef struct {
ccv_icf_param_t params;
int max_dimension;
} ccv_icf_uri_param_t;

static const param_dispatch_t param_map[] = {
{
.property = "interval",
.type = PARAM_TYPE_INT,
.offset = offsetof(ccv_icf_param_t, interval),
.offset = offsetof(ccv_icf_uri_param_t, params) + offsetof(ccv_icf_param_t, interval),
},
{
.property = "max_dimension",
.type = PARAM_TYPE_INT,
.offset = offsetof(ccv_icf_uri_param_t, max_dimension),
},
{
.property = "min_neighbors",
.type = PARAM_TYPE_INT,
.offset = offsetof(ccv_icf_param_t, min_neighbors),
.offset = offsetof(ccv_icf_uri_param_t, params) + offsetof(ccv_icf_param_t, min_neighbors),
},
{
.property = "model",
Expand All @@ -33,7 +43,7 @@ static const param_dispatch_t param_map[] = {
{
.property = "step_through",
.type = PARAM_TYPE_INT,
.offset = offsetof(ccv_icf_param_t, step_through),
.offset = offsetof(ccv_icf_uri_param_t, params) + offsetof(ccv_icf_param_t, step_through),
},
};

Expand All @@ -45,15 +55,16 @@ typedef struct {
typedef struct {
param_parser_t param_parser;
icf_context_t* context;
ccv_icf_param_t params;
ccv_icf_uri_param_t params;
ccv_icf_classifier_cascade_t* cascade;
ebb_buf source;
} icf_param_parser_t;

static void uri_icf_param_parser_init(icf_param_parser_t* parser)
{
param_parser_init(&parser->param_parser, param_map, sizeof(param_map) / sizeof(param_dispatch_t), &parser->params, parser);
parser->params = ccv_icf_default_params;
parser->params.params = ccv_icf_default_params;
parser->params.max_dimension = 0;
parser->cascade = 0;
parser->source.data = 0;
}
Expand Down Expand Up @@ -103,10 +114,10 @@ void* uri_icf_detect_objects_init(void)
assert(param_parser_map_alphabet(param_map, sizeof(param_map) / sizeof(param_dispatch_t)) == 0);
context->desc = param_parser_map_http_body(param_map, sizeof(param_map) / sizeof(param_dispatch_t),
"[{"
"\"x\":\"integer\","
"\"y\":\"integer\","
"\"width\":\"integer\","
"\"height\":\"integer\","
"\"x\":\"number\","
"\"y\":\"number\","
"\"width\":\"number\","
"\"height\":\"number\","
"\"confidence\":\"number\""
"}]");
return context;
Expand Down Expand Up @@ -153,8 +164,16 @@ int uri_icf_detect_objects(const void* context, const void* parsed, ebb_buf* buf
free(parser);
return -1;
}
ccv_array_t* seq = ccv_icf_detect_objects(image, &parser->cascade, 1, parser->params);
ccv_matrix_free(image);
ccv_dense_matrix_t* resize = 0;
if (parser->params.max_dimension > 0 && (image->rows > parser->params.max_dimension || image->cols > parser->params.max_dimension))
{
ccv_resample(image, &resize, 0, ccv_max(parser->params.max_dimension, (int)(image->rows * (float)parser->params.max_dimension / image->cols + 0.5)), ccv_max(parser->params.max_dimension, (int)(image->cols * (float)parser->params.max_dimension / image->rows + 0.5)), CCV_INTER_AREA);
ccv_matrix_free(image);
} else
resize = image;
ccv_array_t* seq = ccv_icf_detect_objects(resize, &parser->cascade, 1, parser->params.params);
float width = resize->cols, height = resize->rows;
ccv_matrix_free(resize);
if (seq == 0)
{
free(parser);
Expand All @@ -171,7 +190,7 @@ int uri_icf_detect_objects(const void* context, const void* parsed, ebb_buf* buf
{
char cell[128];
ccv_comp_t* comp = (ccv_comp_t*)ccv_array_get(seq, i);
snprintf(cell, 128, "{\"x\":%d,\"y\":%d,\"width\":%d,\"height\":%d,\"confidence\":%f}", comp->rect.x, comp->rect.y, comp->rect.width, comp->rect.height, comp->classification.confidence);
snprintf(cell, 128, "{\"x\":%f,\"y\":%f,\"width\":%f,\"height\":%f,\"confidence\":%f}", comp->rect.x / width, comp->rect.y / height, comp->rect.width / width, comp->rect.height / height, comp->classification.confidence);
size_t len = strnlen(cell, 128);
while (buf->written + len + 1 >= buf->len)
{
Expand Down
Loading

0 comments on commit f009aa3

Please sign in to comment.