Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
sqlite3* db = 0;
assert(SQLITE_OK == sqlite3_open(argv[2], &db));
const char create_table_qs[] =
"CREATE TABLE IF NOT EXISTS layer_quant "
"(layer INTEGER PRIMARY KEY ASC, quant BLOB);";
assert(SQLITE_OK == sqlite3_exec(db, create_table_qs, 0, 0, 0));
const char data_insert_qs[] =
"REPLACE INTO layer_quant "
"(layer, quant) VALUES ($layer, $quant);";
sqlite3_stmt* data_insert_stmt = 0;
assert(SQLITE_OK == sqlite3_prepare_v2(db, data_insert_qs, sizeof(data_insert_qs), &data_insert_stmt, 0));
for (f = 10; f < 11; f++)
{
int i, j, k;
ccv_convnet_layer_t *layer = convnet->layers + f;
char filename[256];
float kmean[256];
sprintf(filename, "lut-layer-%d-4bit.txt", f);
FILE *r = fopen(filename, "r");
for (i = 0; i < 0x10; i++)
{
union {
int i;
float f;
} v;
fscanf(r, "%d %d", &j, &v.i);
kmean[i] = v.f;
}
fclose(r);
for (i = 0; i < 0x10; i++)
{
k = i;
for (j = i + 1; j < 0x10; j++)
if (kmean[j] < kmean[k])
k = j;
float t = kmean[k];
kmean[k] = kmean[i];
kmean[i] = t;
}
sqlite3_bind_int(data_insert_stmt, 1, f);
uint16_t* km16 = (uint16_t*)ccmalloc(sizeof(uint16_t) * 0x10);
ccv_float_to_half_precision(kmean, km16, 0x10);
sqlite3_bind_blob(data_insert_stmt, 2, km16, sizeof(uint16_t) * 0x10, ccfree);
assert(SQLITE_DONE == sqlite3_step(data_insert_stmt));
sqlite3_reset(data_insert_stmt);
sqlite3_clear_bindings(data_insert_stmt);
ccv_dense_matrix_t* pngw = 0;
sprintf(filename, "layer-%d-4bit-crush.png", f);
ccv_read(filename, &pngw, CCV_IO_ANY_FILE | CCV_IO_GRAY);
for (i = 0; i < pngw->rows; i++)
for (j = 0; j < pngw->cols; j++)
layer->w[i * pngw->cols + j] = kmean[pngw->data.u8[i * pngw->cols + j]];
}
for (f = 11; f < 13; f++)
{
int i, j, k;
ccv_convnet_layer_t *layer = convnet->layers + f;
char filename[256];
float kmean[256];
sprintf(filename, "lut-layer-%d.txt", f);
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;
}
sqlite3_bind_int(data_insert_stmt, 1, f);
uint16_t* km16 = (uint16_t*)ccmalloc(sizeof(uint16_t) * 0x100);
ccv_float_to_half_precision(kmean, km16, 0x100);
sqlite3_bind_blob(data_insert_stmt, 2, km16, sizeof(uint16_t) * 0x100, ccfree);
assert(SQLITE_DONE == sqlite3_step(data_insert_stmt));
sqlite3_reset(data_insert_stmt);
sqlite3_clear_bindings(data_insert_stmt);
ccv_dense_matrix_t* pngw = 0;
sprintf(filename, "layer-%d-crush.png", f);
ccv_read(filename, &pngw, CCV_IO_ANY_FILE | CCV_IO_GRAY);
for (i = 0; i < pngw->rows; i++)
for (j = 0; j < pngw->cols; j++)
layer->w[i * pngw->cols + j] = kmean[pngw->data.u8[i * pngw->cols + j]];
}
sqlite3_finalize(data_insert_stmt);
sqlite3_close(db);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment