Skip to content

Commit

Permalink
added more unittests to convnet implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
liuliu committed Oct 23, 2013
1 parent f29b2d0 commit 05d68a2
Show file tree
Hide file tree
Showing 7 changed files with 694 additions and 68 deletions.
79 changes: 75 additions & 4 deletions bin/cifar-10.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ int main(int argc, char** argv)
ccv_convnet_param_t params[] = {
{
.type = CCV_CONVNET_CONVOLUTIONAL,
.bias = 1,
.bias = 0,
.sigma = 0.0001,
.dropout_rate = 0,
.input = {
.matrix = {
.rows = 31,
Expand Down Expand Up @@ -53,8 +54,9 @@ int main(int argc, char** argv)
},
{
.type = CCV_CONVNET_CONVOLUTIONAL,
.bias = 1,
.bias = 0,
.sigma = 0.01,
.dropout_rate = 0,
.input = {
.matrix = {
.rows = 15,
Expand Down Expand Up @@ -91,8 +93,9 @@ int main(int argc, char** argv)
},
{
.type = CCV_CONVNET_CONVOLUTIONAL,
.bias = 1,
.bias = 0,
.sigma = 0.01,
.dropout_rate = 0,
.input = {
.matrix = {
.rows = 7,
Expand Down Expand Up @@ -129,14 +132,18 @@ int main(int argc, char** argv)
},
{
.type = CCV_CONVNET_FULL_CONNECT,
.bias = 1,
.bias = 0,
.sigma = 0.01,
.dropout_rate = 0,
.input = {
.matrix = {
.rows = 3,
.cols = 3,
.channels = 64,
},
.node = {
.count = 3 * 3 * 64,
},
},
.output = {
.full_connect = {
Expand All @@ -146,6 +153,70 @@ int main(int argc, char** argv)
},
};
ccv_convnet_t* convnet = ccv_convnet_new(params, 7);
assert(argc == 5);
int num1 = atoi(argv[2]);
int num2 = atoi(argv[4]);
FILE* r1 = fopen(argv[1], "rb");
FILE* r2 = fopen(argv[3], "rb");
if (r1 && r2)
{
int i, j, k;
unsigned char bytes[32 * 32 + 1];
ccv_array_t* categorizeds = ccv_array_new(sizeof(ccv_categorized_t), num1, 0);
for (k = 0; k < num1; k++)
{
fread(bytes, 32 * 32 + 1, 1, r1);
ccv_categorized_t categorized;
categorized.c = bytes[0]; // the class
ccv_dense_matrix_t* a = ccv_dense_matrix_new(31, 31, CCV_32F | CCV_C3, 0, 0);
for (i = 0; i < 31; i++)
for (j = 0; j < 31; j++)
a->data.f32[(j + i * 31) * 3] = bytes[j + i * 32 + 1] / 255.0;
fread(bytes, 32 * 32, 1, r1);
for (i = 0; i < 31; i++)
for (j = 0; j < 31; j++)
a->data.f32[(j + i * 31) * 3 + 1] = bytes[j + i * 32] / 255.0;
fread(bytes, 32 * 32, 1, r1);
for (i = 0; i < 31; i++)
for (j = 0; j < 31; j++)
a->data.f32[(j + i * 31) * 3 + 2] = bytes[j + i * 32] / 255.0;
categorized.matrix = a;
ccv_array_push(categorizeds, &categorized);
}
ccv_array_t* tests = ccv_array_new(sizeof(ccv_categorized_t), num2, 0);
for (k = 0; k < num2; k++)
{
fread(bytes, 32 * 32 + 1, 1, r2);
ccv_categorized_t categorized;
categorized.c = bytes[0]; // the class
ccv_dense_matrix_t* a = ccv_dense_matrix_new(31, 31, CCV_32F | CCV_C3, 0, 0);
for (i = 0; i < 31; i++)
for (j = 0; j < 31; j++)
a->data.f32[(j + i * 31) * 3] = bytes[j + i * 32 + 1] / 255.0;
fread(bytes, 32 * 32, 1, r2);
for (i = 0; i < 31; i++)
for (j = 0; j < 31; j++)
a->data.f32[(j + i * 31) * 3 + 1] = bytes[j + i * 32] / 255.0;
fread(bytes, 32 * 32, 1, r2);
for (i = 0; i < 31; i++)
for (j = 0; j < 31; j++)
a->data.f32[(j + i * 31) * 3 + 2] = bytes[j + i * 32] / 255.0;
categorized.matrix = a;
ccv_array_push(tests, &categorized);
}
ccv_convnet_train_param_t params = {
.max_epoch = 100,
.mini_batch = 1,
.decay = 0.005,
.learn_rate = 0.001,
.momentum = 0.9,
};
ccv_convnet_supervised_train(convnet, categorizeds, tests, params);
}
if (r1)
fclose(r1);
if (r2)
fclose(r2);
ccv_convnet_free(convnet);
ccv_disable_cache();
return 0;
Expand Down
5 changes: 4 additions & 1 deletion lib/ccv.h
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,9 @@ typedef struct {
typedef struct {
int max_epoch;
int mini_batch;
double decay;
double learn_rate;
double momentum;
} ccv_convnet_train_param_t;

typedef struct {
Expand All @@ -1154,7 +1157,7 @@ typedef struct {
} ccv_categorized_t;

ccv_convnet_t* __attribute__((warn_unused_result)) ccv_convnet_new(ccv_convnet_param_t params[], int count);
void ccv_convnet_supervised_train(ccv_convnet_t* convnet, ccv_array_t* categorizeds, ccv_convnet_train_param_t params);
void ccv_convnet_supervised_train(ccv_convnet_t* convnet, ccv_array_t* categorizeds, ccv_array_t* tests, ccv_convnet_train_param_t params);
void ccv_convnet_encode(ccv_convnet_t* convnet, ccv_dense_matrix_t* a, ccv_dense_matrix_t** b, int type);
int ccv_convnet_classify(ccv_convnet_t* convnet, ccv_dense_matrix_t* a);
void ccv_convnet_free(ccv_convnet_t* convnet);
Expand Down
2 changes: 1 addition & 1 deletion lib/ccv_algebra.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ void ccv_gemm(ccv_matrix_t* a, ccv_matrix_t* b, double alpha, ccv_matrix_t* c, d
switch (CCV_GET_DATA_TYPE(dd->type))
{
case CCV_32F:
cblas_sgemm(CblasRowMajor, (transpose & CCV_A_TRANSPOSE) ? CblasTrans : CblasNoTrans, (transpose & CCV_B_TRANSPOSE) ? CblasTrans : CblasNoTrans, dd->rows, dd->cols, da->cols, alpha, da->data.f32, da->cols, db->data.f32, db->cols, beta, dd->data.f32, dd->cols);
cblas_sgemm(CblasRowMajor, (transpose & CCV_A_TRANSPOSE) ? CblasTrans : CblasNoTrans, (transpose & CCV_B_TRANSPOSE) ? CblasTrans : CblasNoTrans, dd->rows, dd->cols, (transpose & CCV_A_TRANSPOSE) ? da->rows : da->cols, alpha, da->data.f32, da->cols, db->data.f32, db->cols, beta, dd->data.f32, dd->cols);
break;
case CCV_64F:
cblas_dgemm(CblasRowMajor, (transpose & CCV_A_TRANSPOSE) ? CblasTrans : CblasNoTrans, (transpose & CCV_B_TRANSPOSE) ? CblasTrans : CblasNoTrans, dd->rows, dd->cols, (transpose & CCV_A_TRANSPOSE) ? da->rows : da->cols, alpha, da->data.f64, da->cols, db->data.f64, db->cols, beta, dd->data.f64, dd->cols);
Expand Down
Loading

0 comments on commit 05d68a2

Please sign in to comment.