vl_bool CHIKMTree::vl_hikm_train(const vl_ikm_data *data, vl_uint32 N) { //char fileName[MAX_PATH]; //sprintf(fileName, "%s\\index\\info_train.txt", m_WorkSpace); //FILE* file = fopen(fileName, "wb"); //if (!file) { // return 0; //} m_VlHIKMTree->root = xmeans (m_VlHIKMTree, data, N, VL_MIN(m_VlHIKMTree->K, N), m_VlHIKMTree->depth, NULL); if (!m_VlHIKMTree->root) { return FALSE; } //fclose(file); return TRUE; }
static VlHIKMNode * xmeans (VlHIKMTree *tree, vl_uint8 const *data, int N, int K, int height) { VlHIKMNode *node = vl_malloc (sizeof(VlHIKMNode)) ; vl_uint *ids = vl_malloc (sizeof(vl_uint) * N) ; node-> filter = vl_ikm_new (tree -> method) ; node-> children = (height == 1) ? 0 : vl_malloc (sizeof(VlHIKMNode*) * K) ; vl_ikm_set_max_niters (node->filter, tree->max_niters) ; vl_ikm_set_verbosity (node->filter, tree->verb - 1 ) ; vl_ikm_init_rand_data (node->filter, data, tree->M, N, K) ; vl_ikm_train (node->filter, data, N) ; vl_ikm_push (node->filter, ids, data, N) ; /* recurse for each child */ if (height > 1) { int k ; for (k = 0 ; k < K ; k ++) { int partition_N ; int partition_K ; vl_uint8 *partition ; partition = vl_hikm_copy_subset (data, ids, N, tree->M, k, &partition_N) ; partition_K = VL_MIN (K, partition_N) ; node->children [k] = xmeans (tree, partition, partition_N, partition_K, height - 1) ; vl_free (partition) ; if (tree->verb > tree->depth - height) { VL_PRINTF("hikmeans: branch at depth %d: %6.1f %% completed\n", tree->depth - height, (double) (k+1) / K * 100) ; } } } vl_free (ids) ; return node ; }
static VlHIKMNode * xmeans (VlHIKMTree *tree, const vl_ikm_data *data, vl_uint32 N, vl_uint32 K, vl_uint32 height, FILE* file) { VlHIKMNode *node = (VlHIKMNode*)malloc (sizeof(VlHIKMNode)) ; vl_uint32 *ids = (vl_uint32*) malloc (sizeof(vl_uint32) * N); node-> filter = new CIKMTree(); node-> children = (height == 1) ? NULL : (VlHIKMNode**)malloc(sizeof(VlHIKMNode*) * K) ; node->filter->vl_ikm_new_tree(); node->filter->vl_ikm_set_max_niters(tree->max_niters); node->filter->vl_ikm_set_verbosity (1) ; node->filter->vl_ikm_init_rand_data(data, tree->M, N, K); node->filter->vl_ikm_train(data, N, file) ; node->filter->vl_ikm_push (ids, data, N) ; /* recurse for each child */ if (height > 1) { for (vl_uint32 k = 0 ; k < K ; k ++) { vl_uint32 partition_N ; vl_uint32 partition_K ; vl_ikm_data * partition ; partition = vl_hikm_copy_subset(data, ids, N, tree->M, k, &partition_N) ; partition_K = VL_MIN (K, partition_N); node->children [k] = xmeans(tree, partition, partition_N, partition_K, height - 1, file) ; free (partition) ; /* print some information */ if (tree->verb > (int)tree->depth - (int)height && file) { fprintf(file, "hikmeans: branch at depth %d: %6.1f %% completed\n", tree->depth - height, (double) (k+1) / K * 100) ; fflush(file); } } } free (ids) ; return node ; }
VL_EXPORT void vl_hikm_train (VlHIKMTree *f, vl_uint8 const *data, int N) { f -> root = xmeans (f, data, N, VL_MIN(f->K, N), f->depth) ; }