Skip to content

Instantly share code, notes, and snippets.

@liuliu
Last active August 29, 2015 14:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save liuliu/970d97db15f47c196454 to your computer and use it in GitHub Desktop.
Save liuliu/970d97db15f47c196454 to your computer and use it in GitHub Desktop.
sqlite3* db = 0;
int h[0x10000];
float kmean[0x100];
uint16_t tbl[0x10000];
int i, j, k;
for (i = 0; i < 0x10000; i++)
tbl[i] = i;
float lut[0x10000];
ccv_half_precision_to_float(tbl, lut, 0x10000);
ccv_convnet_t* convnet = ccv_convnet_read(0, argv[1]);
if (SQLITE_OK == sqlite3_open(argv[1], &db))
{
sqlite3_stmt* layer_data_stmt = 0;
const char layer_data_qs[] =
"SELECT layer, weight, bias, half_precision FROM layer_data;";
if (SQLITE_OK == sqlite3_prepare_v2(db, layer_data_qs, sizeof(layer_data_qs), &layer_data_stmt, 0))
{
while (sqlite3_step(layer_data_stmt) == SQLITE_ROW)
{
ccv_convnet_layer_t *layer = convnet->layers + sqlite3_column_int(layer_data_stmt, 0);
printf("layer %d\n", sqlite3_column_int(layer_data_stmt, 0));
if (sqlite3_column_int(layer_data_stmt, 0) < 10)
continue;
char filename[256];
sprintf(filename, "lut-layer-%d.txt", sqlite3_column_int(layer_data_stmt, 0));
FILE *r = fopen(filename, "r");
for (i = 0; i < 0x100; i++)
{
union {
int i;
float f;
} v;
fscanf(r, "%d %d", &j, &v.i);
kmean[i] = v.f;
}
fclose(r);
for (i = 0; i < 0x100; i++)
{
k = i;
for (j = i + 1; j < 0x100; j++)
if (kmean[j] < kmean[k])
k = j;
float t = kmean[k];
kmean[k] = kmean[i];
kmean[i] = t;
}
memset(h, 0, sizeof(h));
const uint16_t* w = sqlite3_column_blob(layer_data_stmt, 1);
ccv_dense_matrix_t* pngw = ccv_dense_matrix_new(layer->net.full_connect.count, layer->input.node.count, CCV_C1 | CCV_8U, 0, 0);
for (i = 0; i < pngw->rows; i++)
for (j = 0; j < pngw->cols; j++)
{
int t = 0;
for (k = 1; k < 0x100; k++)
if (fabs(kmean[k] - lut[w[j + i * pngw->cols]]) < fabs(kmean[t] - lut[w[j + i * pngw->cols]]))
t = k;
pngw->data.u8[i * pngw->cols + j] = t;
}
sprintf(filename, "layer-%d.png", sqlite3_column_int(layer_data_stmt, 0));
ccv_write(pngw, filename, 0, CCV_IO_PNG_FILE, 0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment