double closestPointsf(vector<R3> & src, vector<R3> & dst, vector<int> & indexes, vector<double> & distances) { distances.clear(); indexes.clear(); #ifndef USE_KD Mat srcm = asMatRows(src), dstm = asMatRows(dst); vector<float> dists; double error = (double) knn(dstm, srcm, indexes, dists); for(int i = 0; i < dists.size(); i++) distances.push_back((double)dists[i]); double divisor = src.size() > 0 ? (double)(src.size()) : 1.0; return error / divisor; #else Pixel3DSet p1(src); Pixel3DSet p2(dst); LKDNode n; n.init(p2); n.split(p2); double error = 0.0; for(int i = 0; i < src.size(); i++) { R3 p = src[i]; R3 w; int index = 0; n.NN(p2,p, index, w); indexes.push_back(index); float D = p.dist(p2[index]); distances.push_back(D); error += (D*D) / (float) src.size(); } return error; #endif }
void TEST_knn(char *file, int size_database, int quantity_atributes, int k) { int size_class_values = 2; struct database *data = mount_database(file, size_database, quantity_atributes); assert(data); data->class_values = (double *)calloc(size_class_values, sizeof(double)); assert(data->class_values); data->size_class_values = size_class_values; data->class_values[0] = 1.0; data->class_values[1] = 0.0; int **confusion_matrix = create_confusion_matrix(); knn(data, k, confusion_matrix); printf("TP:%d, FN:%d\nFP:%d, TN:%d\n", confusion_matrix[0][0], confusion_matrix[0][1], confusion_matrix[1][0], confusion_matrix[1][1]); double i = (double)(confusion_matrix[0][0] + confusion_matrix[1][1]); double j = (double)(confusion_matrix[0][0] + confusion_matrix[0][1] + confusion_matrix[1][0] + confusion_matrix[1][1]); printf("k:%d\naccuracy: %f\n", k, (double)(i/j)); free_database(data); free_confusion_matrix(confusion_matrix); }
static int knn_class(flt *x, struct codebook *cb, int nclass, int k) { int *tbl = 0; int *ctab = 0; int i,n,c; tbl=(int*)malloc(k*sizeof(int)); ctab=(int*)malloc(nclass*sizeof(int)); ifn ( tbl && ctab) { if (tbl) free(tbl); if (ctab) free(ctab); error(NIL,"No memory",NIL); } knn(x, cb, k, tbl); for (i=0; i<nclass; i++) ctab[i] = 0; for (i=0; i<k; i++) { c = cb->code[tbl[i]].label; if (c>=0 && c<nclass) ctab[c]++; } n = c = -1; for (i=0; i<nclass; i++) if (ctab[i]>n) { c = i; n = ctab[i]; } else if (ctab[i]==n) { c = -1; } if (tbl) free(tbl); if (ctab) free(ctab); return c; }
void MLearn::runML(){ Mat trainingData ( numTrainingPoints , 2 , CV_32FC1 ) ; Mat testData ( numTestPoints , 2 , CV_32FC1 ) ; randu ( trainingData ,0 ,1) ; randu ( testData ,0 ,1) ; Mat trainingClasses = labelData ( trainingData , eq ) ; Mat testClasses = labelData ( testData , eq ) ; plot_binary ( trainingData , trainingClasses , " Training Data " ) ; plot_binary ( testData , testClasses , " Test Data " ) ; svm ( trainingData , trainingClasses , testData , testClasses ) ; mlp ( trainingData , trainingClasses , testData , testClasses ) ; knn ( trainingData , trainingClasses , testData , testClasses , 3) ; bayes ( trainingData , trainingClasses , testData , testClasses ) ; decisiontree ( trainingData , trainingClasses , testData , testClasses ) ; waitKey () ; }
double closestPointsf(Pixel3DSet & src, Pixel3DSet & dst, vector<int> & indexes, vector<double> & distances) { //cout << "timing " << endl; double error; //LTimer t; t.start(); distances.clear(); indexes.clear(); #ifndef USE_KD Mat srcm = asMatRows(src), dstm = asMatRows(dst); vector<float> dists; error = (double) knn(dstm, srcm, indexes, dists); for(int i = 0; i < dists.size(); i++) distances.push_back((double)dists[i]); double divisor = src.size() > 0 ? (double)(src.size()) : 1.0; error /= divisor; #else LKDNode n; n.init(dst); n.split(dst, 0, 15); //cout << n.averageLeafSize() << endl; error = 0.0; for(int i = 0; i < src.size(); i++) { R3 p = src[i]; R3 w; int index = 0; n.NN(dst,p, index, w); //if(i%1000 == 0 )cout << i << " / " << src.size() << endl; indexes.push_back(index); float D = p.dist(dst[index]); distances.push_back(D); error += (D*D) / (float) src.size(); } #endif //t.stop(); //cout << "took " << t.getSeconds() << endl; return error; }
void main() { int movie,rating,ex,usr,user,cnt,ind; for(i=0;i<100000;i++) { scanf("%d%d%d%d",&usr,&movie,&rating,&ex); r[usr-1][movie-1]=rating; } root=knn(root); //display(); node ptr; ptr=root; user=2; printf("the recommended items for %d are:\n",user); while(1) { if(ptr->head==user) break; ptr=ptr->next; } for(j=0;j<1682;j++) { cnt=0; for(k=0;k<10;k++) { ind=ptr->array[k]; if((r[ind][j]!=0)&&(r[user][j]==0)) { cnt++; } } if(cnt>6) printf("%d\t",j); } }
/* NEW VERSION */ void linsched_create_normal_task_binary(struct elf_binary* binary, int niceval) { struct sched_param params; unsigned long long time_slice; int query[3]; query[0] = binary->size; query[1] = binary->bss; query[2] = binary->input_size; /* If we cannot support any more tasks, return. */ if (curr_task_id >= LINSCHED_MAX_TASKS) return; time_start = sched_clock(); sim_param = (query[0]+query[1]+query[2])/(30*2); time_slice = knn(&query[0], 3); time_slice *= 1000000; //time_slice = 500000000; /* Create "normal" task and set its nice value. */ __linsched_tasks[curr_task_id] = __linsched_create_task_binary(binary->callback, time_slice); params.sched_priority = 0; sys_sched_setscheduler(__linsched_tasks[curr_task_id], SCHED_NORMAL, ¶ms); set_user_nice(__linsched_tasks[curr_task_id], niceval); /* Print message. */ printf("Created normal task 0x%x with nice value %d.\n", (unsigned int)__linsched_tasks[curr_task_id], niceval); /* Increment task id. */ curr_task_id++; }
int main(int argc, char** argv) { CoverTree<float, std::vector<float>, float (*const)(const std::vector<float>&, const std::vector<float>&)> tree(&euclidian); PointContainer data = generate(VECTORLENGTH); boost::posix_time::ptime time = boost::posix_time::microsec_clock::local_time(); for(PointContainer::const_iterator it = data.begin(); it != data.end(); ++it) { tree.insert(*it); } std::cout << "Build time " << (boost::posix_time::microsec_clock::local_time() - time) << std::endl; std::ofstream stream("dump.txt"); tree.dump(stream); Point zero(2, 0.f); time = boost::posix_time::microsec_clock::local_time(); PointContainer result = knn(data, zero, KNNSIZE); std::cout << "Out time (linear) " << (boost::posix_time::microsec_clock::local_time() - time) << std::endl; time = boost::posix_time::microsec_clock::local_time(); PointContainer result2 = tree.knn(zero, KNNSIZE); std::cout << "Out time (cover_tree) " << (boost::posix_time::microsec_clock::local_time() - time) << std::endl; bool boolean = true; for(int i = 0; i < std::min(result.size(), result2.size()); ++i) { for(int j = 0; j < POINTSIZE; ++j) { boolean = boolean && (result[i][j] == result2[i][j]); } std::cout << i << std::endl << result[i] << result2[i]; } std::cout << "Result " << (boolean && (result.size() == result2.size())) << std::endl; return EXIT_SUCCESS; }
/* Compute the nearest neighbors */ void SMOTE::computeNeighbors(cv::Mat minority, int nearestNeighbors, cv::Mat *neighbors) { cv::Size s = minority.size(); int i, k = nearestNeighbors, max, index = 0; cv::Mat classes(s.height, 1, CV_32FC1); for (i = 0; i < s.height; i++) { classes.at<float>(i, 0) = index; index++; } cv::KNearest knn(minority, classes); max = (minority.rows > knn.get_max_k() ? knn.get_max_k() : minority.rows); knn.find_nearest(minority, (k > max ? max : k), 0, 0, neighbors, 0); // for (i = 0; i < (*neighbors).size().height; i++){ // std::cout << std::endl << nearestNeighbors << " vizinhos de i: " << i << std::endl; // for (int x = 0; x < (*neighbors).size().width; x++){ // std::cout << (*neighbors).at<float>(i, x) << " "; // } // } classes.release(); }
void Apply_knn:: getAll_knn(int sizeOfMat,std::vector<float> label_bucket[],std::vector<float> values_of_bucket[]) { /** * Begin Apply knn to the Mat * **/ std::vector<float> container[sizeOfMat]; int k=15; cv::Mat result=Mat(sizeOfMat,k,CV_32F); /** * Load data for knn **/ CvKNearest knn(&trainningMat,&labelMat); /** * Loading complet **/ knn.find_nearest(&testingMat,k,&result); cout<<"get max_k\t"<<knn.get_max_k()<<"\n"; cout<<"get total trainn samples\t"<<knn.get_sample_count()<<"\n"; cout<<result; cout<<"\n"; /** * End Apply knn to the Mat * **/ /** * Put values in the bucket appropriately begins * **/ cv::Mat tempMat=cv::Mat(&testingMat); for(int _i=0;_i<result.rows;_i++) { /** * _i points to the first row of result which corresponds to the first row of testing sample. * So points at testing sample of _i must be put to the same index of index_to_bucket **/ for(int _j=0;_j<result.cols;_j++) { int key_of_map=result.at<float>(_i);/**here will be change jth index needs to be added. So check cols of result**/ int index_to_bucket=vect_indexMap[key_of_map]; cout<<index_to_bucket<<"\t"<<key_of_map<<"\n"; /** index of testing sample serially from begin to end*/ int index_to_testingSample=testingIndexMat.at<int>(_i); (label_bucket[index_to_bucket]).push_back(index_to_testingSample); float _x=tempMat.at<float>(_i,0); float _y=tempMat.at<float>(_i,1); (values_of_bucket[index_to_bucket]).push_back(_x); (values_of_bucket[index_to_bucket]).push_back(_y); }/**check this again**/ } /** * Put values in the bucket appropriately ends * **/ /** * Printing the values in the buckets begin * **/ for(int _j=0;_j<result.rows-1;_j++) { //std::vector<float>::iterator it_to_label_bucket; cout<<"Contains of bucket:\t"<<(label_bucket[_j]).at(0)<<"\n"; for(int _k=0;_k<(label_bucket[_j]).size();_k++) { cout<<(label_bucket[_j]).at(_k)<<"\t:"; cout<<"_x\t:"<<(values_of_bucket[_j]).at(_k*2)<<"\t"; cout<<"_y\t:"<<(values_of_bucket[_j]).at(_k*2+1); cout<<"\n"; } cout<<"\n\n"; } /** * Printing the values in the buckets end * **/ WriteGroupFile wgrF; wgrF.setFileName("gr_1nnD.txt"); wgrF.openFile(); wgrF.imageNumber=((label_bucket[0]).at(0))/500+1; if(wgrF.imageNumber==1) { /** * Saving all contents of lable_bucket in **/ cout<<"here\n"; int total_groups=0; if(result.rows%2==0) total_groups=result.rows; else total_groups=result.rows-1; cout<<"Total groups:\t"<<total_groups<<"\n"; getFirstImageVector(label_bucket,total_groups); } wgrF.setTotalBuckets(sizeOfMat); wgrF.writeGroups(label_bucket,values_of_bucket); wgrF.closeFile(); // return listOfMat; }
static std::pair<int,int> leave_one_out(KnnObject* o, int stop_threshold, int* selection_vector = 0, double* weight_vector = 0, std::vector<long>* indexes = 0) { int* selections = selection_vector; if (selections == 0) { selections = o->selection_vector; } double* weights = weight_vector; if (weights == 0) { weights = o->weight_vector; } assert(o->feature_vectors != 0); kNearestNeighbors<char*, ltstr, eqstr> knn(o->num_k); int total_correct = 0; int total_queries = 0; if (indexes == 0) { for (size_t i = 0; i < o->feature_vectors->size(); ++i/*, cur += o->num_features*/) { // We don't want to do the calculation if there is no // hope that kNN will return the correct answer (because // there aren't enough examples in the database). if (o->id_name_histogram[i] < int((o->num_k + 0.5) / 2)) { continue; } double* current_known; double* unknown = (*o->feature_vectors)[i]; for (size_t j = 0; j < o->feature_vectors->size(); ++j) { current_known = (*o->feature_vectors)[j]; if (i == j) continue; double distance; compute_distance(o->distance_type, current_known, o->num_features, unknown, &distance, selections, weights); knn.add(o->id_names[j], distance); } knn.majority(); if (strcmp(knn.answer[0].first, o->id_names[i]) == 0) { total_correct++; } knn.reset(); total_queries++; if (total_queries - total_correct > stop_threshold) return std::make_pair(total_correct, total_queries); } } else { for (size_t i = 0; i < o->feature_vectors->size(); ++i) { if (o->id_name_histogram[i] < int((o->num_k + 0.5) / 2)) continue; double* current_known; double* unknown = (*o->feature_vectors)[i]; for (size_t j = 0; j < o->feature_vectors->size(); ++j) { current_known = (*o->feature_vectors)[j]; if (i == j) continue; double distance; if (o->distance_type == CITY_BLOCK) { distance = city_block_distance_skip(current_known, unknown, selections, weights, indexes->begin(), indexes->end()); } else if (o->distance_type == FAST_EUCLIDEAN) { distance = fast_euclidean_distance_skip(current_known, unknown, selections, weights, indexes->begin(), indexes->end()); } else { distance = euclidean_distance_skip(current_known, unknown, selections, weights, indexes->begin(), indexes->end()); } knn.add(o->id_names[j], distance); } knn.majority(); if (strcmp(knn.answer[0].first, o->id_names[i]) == 0) { total_correct++; } knn.reset(); total_queries++; if (total_queries - total_correct > stop_threshold) return std::make_pair(total_correct, total_queries); } } return std::make_pair(total_correct, total_queries); }
void mexFunction(int nout, mxArray *pout[], int nin, const mxArray *pin[]) { FILE *f = fopen("log.txt", "wt"); fclose(f); if (nin < 2) { mexErrMsgTxt("nnmex called with < 2 input arguments"); } const mxArray *A = pin[0], *B = pin[1]; const mxArray *ANN_PREV = NULL, *ANN_WINDOW = NULL, *AWINSIZE = NULL; int aw = -1, ah = -1, bw = -1, bh = -1; BITMAP *a = NULL, *b = NULL, *ann_prev = NULL, *ann_window = NULL, *awinsize = NULL; VECBITMAP<unsigned char> *ab = NULL, *bb = NULL; VECBITMAP<float> *af = NULL, *bf = NULL; if (mxGetNumberOfDimensions(A) != 3 || mxGetNumberOfDimensions(B) != 3) { mexErrMsgTxt("dims != 3"); } if (mxGetDimensions(A)[2] != mxGetDimensions(B)[2]) { mexErrMsgTxt("3rd dimension not same size"); } int mode = MODE_IMAGE; if (mxGetDimensions(A)[2] != 3) { // a discriptor rather than rgb if (mxIsUint8(A) && mxIsUint8(B)) { mode = MODE_VECB; } else if (is_float(A) && is_float(B)) { mode = MODE_VECF; } else { mexErrMsgTxt("input not uint8, single, or double"); } } Params *p = new Params(); RecomposeParams *rp = new RecomposeParams(); BITMAP *borig = NULL; if (mode == MODE_IMAGE) { a = convert_bitmap(A); b = convert_bitmap(B); borig = b; aw = a->w; ah = a->h; bw = b->w; bh = b->h; } else if (mode == MODE_VECB) { ab = convert_vecbitmap<unsigned char>(A); bb = convert_vecbitmap<unsigned char>(B); if (ab->n != bb->n) { mexErrMsgTxt("3rd dimension differs"); } aw = ab->w; ah = ab->h; bw = bb->w; bh = bb->h; p->vec_len = ab->n; } else if (mode == MODE_VECF) { af = convert_vecbitmap<float>(A); bf = convert_vecbitmap<float>(B); if (af->n != bf->n) { mexErrMsgTxt("3rd dimension differs"); } aw = af->w; ah = af->h; bw = bf->w; bh = bf->h; p->vec_len = af->n; } double *win_size = NULL; BITMAP *amask = NULL, *bmask = NULL; double scalemin = 0.5, scalemax = 2.0; // The product of these must be one. /* parse parameters */ int i = 2; int sim_mode = 0; int knn_chosen = -1; p->algo = ALGO_CPU; int enrich_mode = 0; if (nin > i && !mxIsEmpty(pin[i])) { if (mxStringEquals(pin[i], "cpu")) { p->algo = ALGO_CPU; } else if (mxStringEquals(pin[i], "gpucpu")) { p->algo = ALGO_GPUCPU; } else if (mxStringEquals(pin[i], "cputiled")) { p->algo = ALGO_CPUTILED; } else if (mxStringEquals(pin[i], "rotscale")) { sim_mode = 1; } else if (mxStringEquals(pin[i], "enrich")) { p->algo = ALGO_CPUTILED; enrich_mode = 1; } else { mexErrMsgTxt("Unknown algorithm"); } } i++; if (nin > i && !mxIsEmpty(pin[i])) { p->patch_w = int(mxGetScalar(pin[i])); } i++; if (nin > i && !mxIsEmpty(pin[i])) { p->nn_iters = int(mxGetScalar(pin[i])); } i++; if (nin > i && !mxIsEmpty(pin[i])) { p->rs_max = int(mxGetScalar(pin[i])); } i++; if (nin > i && !mxIsEmpty(pin[i])) { p->rs_min = int(mxGetScalar(pin[i])); } i++; if (nin > i && !mxIsEmpty(pin[i])) { p->rs_ratio = mxGetScalar(pin[i]); } i++; if (nin > i && !mxIsEmpty(pin[i])) { p->rs_iters = mxGetScalar(pin[i]); } i++; if (nin > i && !mxIsEmpty(pin[i])) { p->cores = int(mxGetScalar(pin[i])); } i++; if (nin > i && !mxIsEmpty(pin[i])) { bmask = convert_bitmap(pin[i]); } i++; // XC+ if (nin > i && !mxIsEmpty(pin[i])) { if (!mxIsDouble(pin[i])) { mexErrMsgTxt("\nwin_size should be of type double."); } win_size = (double*)mxGetData(pin[i]); if (mxGetNumberOfElements(pin[i])==1) { p->window_h = p->window_w = int(win_size[0]); } else if (mxGetNumberOfElements(pin[i])==2) { p->window_h = int(win_size[0]); p->window_w = int(win_size[1]); } else { mexErrMsgTxt("\nwin_size should be a scalar for square window or [h w] for a rectangular one."); } } i++; /* continue parsing parameters */ // [ann_prev=NULL], [ann_window=NULL], [awinsize=NULL], if (nin > i && !mxIsEmpty(pin[i])) { ANN_PREV = pin[i]; int clip_count = 0; ann_prev = convert_field(p, ANN_PREV, bw, bh, clip_count); // Bug fixed by Connelly } i++; if (nin > i && !mxIsEmpty(pin[i])) { ANN_WINDOW = pin[i]; int clip_count = 0; ann_window = convert_field(p, ANN_WINDOW, bw, bh, clip_count); } i++; if (nin > i && !mxIsEmpty(pin[i])) { AWINSIZE = pin[i]; awinsize = convert_winsize_field(p, AWINSIZE, aw, ah); if (p->window_w==INT_MAX||p->window_h==INT_MAX) { p->window_w = -1; p->window_h = -1; } } i++; if (nin > i && !mxIsEmpty(pin[i])) { knn_chosen = int(mxGetScalar(pin[i])); if (knn_chosen == 1) { knn_chosen = -1; } if (knn_chosen <= 0) { mexErrMsgTxt("\nknn is less than zero"); } } i++; if (nin > i && !mxIsEmpty(pin[i])) { scalemax = mxGetScalar(pin[i]); if (scalemax <= 0) { mexErrMsgTxt("\nscalerange is less than zero"); } scalemin = 1.0/scalemax; if (scalemax < scalemin) { double temp = scalemax; scalemax = scalemin; scalemin = temp; } } i++; if (ann_window&&!awinsize&&!win_size) { mexErrMsgTxt("\nUsing ann_window - either awinsize or win_size should be defined.\n"); } if (enrich_mode) { int nn_iters = p->nn_iters; p->enrich_iters = nn_iters/2; p->nn_iters = 2; if (A != B) { mexErrMsgTxt("\nOur implementation of enrichment requires that image A = image B.\n"); } if (mode == MODE_IMAGE) { b = a; } else { mexErrMsgTxt("\nEnrichment only implemented for 3 channel uint8 inputs.\n"); } } init_params(p); if (sim_mode) { init_xform_tables(scalemin, scalemax, 1); } RegionMasks *amaskm = amask ? new RegionMasks(p, amask): NULL; BITMAP *ann = NULL; // NN field BITMAP *annd_final = NULL; // NN patch distance field BITMAP *ann_sim_final = NULL; VBMP *vann_sim = NULL; VBMP *vann = NULL; VBMP *vannd = NULL; if (mode == MODE_IMAGE) { // input as RGB image if (!a || !b) { mexErrMsgTxt("internal error: no a or b image"); } if (knn_chosen > 1) { p->knn = knn_chosen; if (sim_mode) { mexErrMsgTxt("rotating+scaling patches not implemented with knn (actually it is implemented it is not exposed by the wrapper)"); } PRINCIPAL_ANGLE *pa = NULL; vann_sim = NULL; vann = knn_init_nn(p, a, b, vann_sim, pa); vannd = knn_init_dist(p, a, b, vann, vann_sim); knn(p, a, b, vann, vann_sim, vannd, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, pa); // sort_knn(p, vann, vann_sim, vannd); } else if (sim_mode) { BITMAP *ann_sim = NULL; ann = sim_init_nn(p, a, b, ann_sim); BITMAP *annd = sim_init_dist(p, a, b, ann, ann_sim); sim_nn(p, a, b, ann, ann_sim, annd); if (ann_prev) { mexErrMsgTxt("when searching over rotations+scales, previous guess is not supported"); } annd_final = annd; ann_sim_final = ann_sim; } else { ann = init_nn(p, a, b, bmask, NULL, amaskm, 1, ann_window, awinsize); BITMAP *annd = init_dist(p, a, b, ann, bmask, NULL, amaskm); nn(p, a, b, ann, annd, amaskm, bmask, 0, 0, rp, 0, 0, 0, NULL, p->cores, ann_window, awinsize); if (ann_prev) minnn(p, a, b, ann, annd, ann_prev, bmask, 0, 0, rp, NULL, amaskm, p->cores); annd_final = annd; } } /* else if (mode == MODE_VECB) { // mexPrintf("mode vecb %dx%dx%d, %dx%dx%d\n", ab->w, ab->h, ab->n, bb->w, bb->h, bb->n); // mexPrintf(" %d %d %d %d\n", ab->get(0,0)[0], ab->get(1,0)[0], ab->get(0,1)[0], ab->get(0,0)[1]); if (!ab || !bb) { mexErrMsgTxt("internal error: no a or b image"); } ann = vec_init_nn<unsigned char>(p, ab, bb, bmask, NULL, amaskm); VECBITMAP<int> *annd = vec_init_dist<unsigned char, int>(p, ab, bb, ann, bmask, NULL, amaskm); // mexPrintf(" %d %d %d %p %p\n", annd->get(0,0)[0], annd->get(1,0)[0], annd->get(0,1)[0], amaskm, bmask); vec_nn<unsigned char, int>(p, ab, bb, ann, annd, amaskm, bmask, 0, 0, rp, 0, 0, 0, NULL, p->cores); if (ann_prev) vec_minnn<unsigned char, int>(p, ab, bb, ann, annd, ann_prev, bmask, 0, 0, rp, NULL, amaskm, p->cores); annd_final = vecbitmap_to_bitmap(annd); delete annd; } else if (mode == MODE_VECF) { // mexPrintf("mode vecf %dx%dx%d, %dx%dx%d\n", af->w, af->h, af->n, bf->w, bf->h, bf->n); // mexPrintf(" %f %f %f %f\n", af->get(0,0)[0], af->get(1,0)[0], af->get(0,1)[0], af->get(0,0)[1]); if (!af || !bf) { mexErrMsgTxt("internal error: no a or b image"); } ann = vec_init_nn<float>(p, af, bf, bmask, NULL, amaskm); VECBITMAP<float> *annd = vec_init_dist<float, float>(p, af, bf, ann, bmask, NULL, amaskm); vec_nn<float, float>(p, af, bf, ann, annd, amaskm, bmask, 0, 0, rp, 0, 0, 0, NULL, p->cores); if (ann_prev) vec_minnn<float, float>(p, af, bf, ann, annd, ann_prev, bmask, 0, 0, rp, NULL, amaskm, p->cores); annd_final = create_bitmap(annd->w, annd->h); clear(annd_final); delete annd; } */ else if(mode == MODE_VECB) { if (sim_mode) { mexErrMsgTxt("internal error: rotation+scales not implemented with descriptor mode"); } if (knn_chosen > 1) { mexErrMsgTxt("internal error: kNN not implemented with descriptor mode"); } // mexPrintf("mode vecb_xc %dx%dx%d, %dx%dx%d\n", ab->w, ab->h, ab->n, bb->w, bb->h, bb->n); // input as uint8 discriptors per pixel if (!ab || !bb) { mexErrMsgTxt("internal error: no a or b image"); } ann = XCvec_init_nn<unsigned char>(p, ab, bb, bmask, NULL, amaskm); VECBITMAP<int> *annd = XCvec_init_dist<unsigned char, int>(p, ab, bb, ann, bmask, NULL, amaskm); XCvec_nn<unsigned char, int>(p, ab, bb, ann, annd, amaskm, bmask, 0, 0, rp, 0, 0, 0, NULL, p->cores); if (ann_prev) XCvec_minnn<unsigned char, int>(p, ab, bb, ann, annd, ann_prev, bmask, 0, 0, rp, NULL, amaskm, p->cores); annd_final = vecbitmap_to_bitmap(annd); delete annd; } else if(mode == MODE_VECF) { if (sim_mode) { mexErrMsgTxt("internal error: rotation+scales not implemented with descriptor mode"); } if (knn_chosen > 1) { mexErrMsgTxt("internal error: kNN not implemented with descriptor mode"); } // input as float/double discriptors per pixel if (!af || !bf) { mexErrMsgTxt("internal error: no a or b image"); } ann = XCvec_init_nn<float>(p, af, bf, bmask, NULL, amaskm); VECBITMAP<float> *annd = XCvec_init_dist<float, float>(p, af, bf, ann, bmask, NULL, amaskm); XCvec_nn<float, float>(p, af, bf, ann, annd, amaskm, bmask, 0, 0, rp, 0, 0, 0, NULL, p->cores); if (ann_prev) XCvec_minnn<float, float>(p, af, bf, ann, annd, ann_prev, bmask, 0, 0, rp, NULL, amaskm, p->cores); annd_final = create_bitmap(annd->w, annd->h); clear(annd_final); delete annd; } destroy_region_masks(amaskm); // output ann: x | y | patch_distance if(nout >= 1) { mxArray *ans = NULL; if (knn_chosen > 1) { if (sim_mode) { mexErrMsgTxt("rotating+scaling patches return value not implemented with knn"); } mwSize dims[4] = { ah, aw, 3, knn_chosen }; ans = mxCreateNumericArray(4, dims, mxINT32_CLASS, mxREAL); int *data = (int *) mxGetData(ans); for (int kval = 0; kval < knn_chosen; kval++) { int *xchan = &data[aw*ah*3*kval+0]; int *ychan = &data[aw*ah*3*kval+aw*ah]; int *dchan = &data[aw*ah*3*kval+2*aw*ah]; for (int y = 0; y < ah; y++) { // int *ann_row = (int *) ann->line[y]; // int *annd_row = (int *) annd_final->line[y]; for (int x = 0; x < aw; x++) { // int pp = ann_row[x]; int pp = vann->get(x, y)[kval]; int pos = y + x * ah; xchan[pos] = INT_TO_X(pp); ychan[pos] = INT_TO_Y(pp); dchan[pos] = vannd->get(x, y)[kval]; } } } } else if (ann_sim_final) { mwSize dims[3] = { ah, aw, 5 }; ans = mxCreateNumericArray(3, dims, mxSINGLE_CLASS, mxREAL); float *data = (float *) mxGetData(ans); float *xchan = &data[0]; float *ychan = &data[aw*ah]; float *dchan = &data[2*aw*ah]; float *tchan = &data[3*aw*ah]; float *schan = &data[4*aw*ah]; double angle_scale = 2.0*M_PI/NUM_ANGLES; for (int y = 0; y < ah; y++) { int *ann_row = (int *) ann->line[y]; int *annd_row = (int *) annd_final->line[y]; int *ann_sim_row = ann_sim_final ? (int *) ann_sim_final->line[y]: NULL; for (int x = 0; x < aw; x++) { int pp = ann_row[x]; int pos = y + x * ah; xchan[pos] = INT_TO_X(pp); ychan[pos] = INT_TO_Y(pp); dchan[pos] = annd_row[x]; if (ann_sim_final) { int v = ann_sim_row[x]; int tval = INT_TO_Y(v)&(NUM_ANGLES-1); int sval = INT_TO_X(v); tchan[pos] = tval*angle_scale; schan[pos] = xform_scale_table[sval]*(1.0/65536.0); } } } } else { mwSize dims[3] = { ah, aw, 3 }; ans = mxCreateNumericArray(3, dims, mxINT32_CLASS, mxREAL); int *data = (int *) mxGetData(ans); int *xchan = &data[0]; int *ychan = &data[aw*ah]; int *dchan = &data[2*aw*ah]; for (int y = 0; y < ah; y++) { int *ann_row = (int *) ann->line[y]; int *annd_row = (int *) annd_final->line[y]; for (int x = 0; x < aw; x++) { int pp = ann_row[x]; int pos = y + x * ah; xchan[pos] = INT_TO_X(pp); ychan[pos] = INT_TO_Y(pp); dchan[pos] = annd_row[x]; } } } pout[0] = ans; } // clean up delete vann; delete vann_sim; delete vannd; delete p; delete rp; destroy_bitmap(a); destroy_bitmap(borig); delete ab; delete bb; delete af; delete bf; destroy_bitmap(ann); destroy_bitmap(annd_final); destroy_bitmap(ann_sim_final); if (ann_prev) destroy_bitmap(ann_prev); if (ann_window) destroy_bitmap(ann_window); if (awinsize) destroy_bitmap(awinsize); }
int main (){ char file[50];//name of file int nclus;//number of clusters //float *Cone, *Ctwo, *temp, *temp2; /*-------------------------------------------------------------------------- -------------get input parameters --------------------------------------------------------------------------*/ //get name of file to open printf("what is the name of your file?"); if (scanf("%s", &file[0]) != 1){ //read in value and make sure it is valid name printf("error: filemane invalid!\n"); exit(EXIT_FAILURE); } printf("filename is %s\n", file); //get number of clusters printf("how many clusters do you want to find?"); if (scanf("%d", &nclus) != 1){ //read in value and make sure it is valid number printf("error: number invalid!\n"); exit(EXIT_FAILURE); } printf("number of k clusters is %d\n", nclus); /*-------------------------------------------------------------------------- -------------open and read input file --------------------------------------------------------------------------*/ //open input file FILE *fp; if ((fp = fopen(file,"r")) == NULL){ printf("Error:cannot read the file %s\n", file); }else{ printf("success opening file %s\n", file); } //read number of items and attributes int items, attributes; fscanf(fp,"%d",&items); fscanf(fp,"%d",&attributes); printf ("items %d attributes %d\n", items, attributes); getchar(); //Create nodes Node *head = (Node *) malloc (sizeof(Node)); Node *node = (Node *) malloc (sizeof(Node)); float *temp = (float *)malloc(items*attributes*sizeof(float)); //read file printf("reading file\n"); int i, j; //counters for (i=0; i < items; i++){//for each item printf("reading item %d\n",i); for (j=0; j < attributes; j++){ fscanf(fp,"%f",&temp[(attributes)*i+j]); //printf("read attribute %d, %f\n",j,temp[(attributes)*i+j]); } node = addData(nclus+1,&temp[(attributes)*i]); append(head,node); } /*-------------------------------------------------------------------------- -------------get initial means --------------------------------------------------------------------------*/ float *means = (float *)malloc(nclus*attributes*sizeof(float)); //float *means_b = (float *)malloc(nclus*attributes*sizeof(float)); int index; node = head; //printf("node...%f %f\n", node->att[0], node->att[1]); for (i=0;i<nclus;i++){ node = node->next; //printf("node...%f %f\n", node->att[0], node->att[1]); printf("\n\ninitial center for cluster %d:\n",i); for (j=0;j<attributes;j++){ index = (attributes)*(i)+j; means[index] = node->att[j]; printf("attribute %d: %f\n", j, node->att[j]); } } printf("press enter\n"); getchar(); /*-------------------------------------------------------------------------- -------------run k-means algorithm --------------------------------------------------------------------------*/ //declare variables for k-means algo float *att_sum = (float *)malloc(nclus*attributes*sizeof(float)); //sum of each column for each cluster float *sqerr_sum = (float *)malloc(nclus*sizeof(float)); //keep track of rmse float *rmse = (float *)malloc(nclus*sizeof(float)); //keep track of rmse float *rmsediff = (float *)malloc(nclus*sizeof(float)); //differences in rmse int *cnt = (int *)malloc(nclus*sizeof(int)); //number of elements in each cluster float *last_rmse = (float *)malloc(nclus*sizeof(float)); int test = 0; int count = 0; do{//repeat until test = 1 (RMSE converges) test = kmeans(head,&means[0],nclus,&last_rmse[0], attributes, &att_sum[0], &sqerr_sum[0], &rmse[0], &rmsediff[0], &cnt[0]); printf("press enter for next iteration\n"); getchar(); //printf("test %d\n", test); count ++; if (count > 100) test = 1; }while(test == 0); /*-------------------------------------------------------------------------- -------------print results in output file --------------------------------------------------------------------------*/ FILE *out; if ((out = fopen("output_gbm.csv","w")) == NULL){ printf("Error:cannot read the file output.txt\n"); }else{ printf("success opening output file output.txt for output\n"); } node = head; while(node->next != NULL){ node = node->next; for (j=0;j<attributes;j++) { fprintf(out, "%f,", node->att[j]); } fprintf(out,"%d\n",node->kclus); } fclose(out); fclose (fp) ; /*-------------------------------------------------------------------------- -------------run k-nearest neighbor algorithm --------------------------------------------------------------------------*/ int response; printf("if you want to run k-nearest neighbor type the number 1 and press enter, otherwise press any other key: "); if (scanf("%d", &response) != 1){ //read in value and make sure it is valid number printf("error: number invalid!\n"); exit(EXIT_FAILURE); } printf("response is %d\n", response); //run k-nn algorithm if (response == 1){ knn(head, nclus); //knn call function } free(last_rmse); free(means); free(att_sum); free(sqerr_sum); free(rmse); free(rmsediff); free(cnt); return 1; }
bool ccGriddedTools::ComputeNormals(ccPointCloud* cloud, const std::vector<int>& indexGrid, int width, int height, bool* canceledByUser/*=0*/, int kernelWidth/*=3*/ ) { //init bool result = true; if (canceledByUser) *canceledByUser = false; //try to compute normals if (cloud->reserveTheNormsTable()) { //progress dialog ccProgressDialog pdlg(true); CCLib::NormalizedProgress nprogress(&pdlg,cloud->size()); pdlg.setMethodTitle("Compute normals"); pdlg.setInfo(qPrintable(QString("Number of points: %1").arg(cloud->size()))); pdlg.start(); const int* _indexGrid = &(indexGrid[0]); CCLib::ReferenceCloud knn(cloud); //neighborhood 'half-width' (total width = 1 + 2*kernelWidth) //max number of neighbours: (1+2*nw)^2 knn.reserve((1+2*kernelWidth)*(1+2*kernelWidth)); //for each grid cell for (int j=0; j<height; ++j) { for (int i=0; i<width; ++i, ++_indexGrid) { if (*_indexGrid >= 0) { unsigned pointIndex = static_cast<unsigned>(*_indexGrid); //add the point itself knn.clear(false); knn.addPointIndex(pointIndex); //warning: indexes are shifted (0 = no point) const CCVector3* P = cloud->getPoint(pointIndex); //look for neighbors for (int v=std::max(0,j-kernelWidth); v<std::min<int>(height,j+kernelWidth); ++v) { if (v != j) { for (int u=std::max(0,i-kernelWidth); u<std::min<int>(width,i+kernelWidth); ++u) { if (u != i) { int indexN = indexGrid[v*width + u]; if (indexN >= 0) { //we don't consider points with a too much different depth than the central point const CCVector3* Pn = cloud->getPoint(static_cast<unsigned>(indexN)); if (fabs(Pn->z - P->z) <= std::max(fabs(Pn->x - P->x),fabs(Pn->y - P->y))) knn.addPointIndex(static_cast<unsigned>(indexN)); //warning: indexes are shifted (0 = no point) } } } } } CCVector3 N(0,0,1); if (knn.size() >= 3) { CCLib::Neighbourhood Z(&knn); //compute normal with quadratic func. (if we have enough points) if (false/*knn.size() >= 6*/) { uchar hfDims[3]; const PointCoordinateType* h = Z.getHeightFunction(hfDims); if (h) { const CCVector3* gv = Z.getGravityCenter(); assert(gv); const uchar& iX = hfDims[0]; const uchar& iY = hfDims[1]; const uchar& iZ = hfDims[2]; PointCoordinateType lX = P->u[iX] - gv->u[iX]; PointCoordinateType lY = P->u[iY] - gv->u[iY]; N.u[iX] = h[1] + (2 * h[3] * lX) + (h[4] * lY); N.u[iY] = h[2] + (2 * h[5] * lY) + (h[4] * lX); N.u[iZ] = -PC_ONE; N.normalize(); } } else #define USE_LSQ_PLANE #ifdef USE_LSQ_PLANE { //compute normal with best fit plane const CCVector3* _N = Z.getLSQPlaneNormal(); if (_N) N = *_N; } #else { //compute normals with 2D1/2 triangulation CCLib::GenericIndexedMesh* theMesh = Z.triangulateOnPlane(); if (theMesh) { unsigned faceCount = theMesh->size(); N.z = 0; //for all triangles theMesh->placeIteratorAtBegining(); for (unsigned j=0; j<faceCount; ++j) { const CCLib::TriangleSummitsIndexes* tsi = theMesh->getNextTriangleIndexes(); //we look if the central point is one of the triangle's vertices if (tsi->i1 == 0 || tsi->i2 == 0|| tsi->i3 == 0) { const CCVector3 *A = knn.getPoint(tsi->i1); const CCVector3 *B = knn.getPoint(tsi->i2); const CCVector3 *C = knn.getPoint(tsi->i3); CCVector3 no = (*B - *A).cross(*C - *A); //no.normalize(); N += no; } } delete theMesh; theMesh = 0; //normalize the 'mean' vector N.normalize(); } } #endif } //check normal vector sign CCVector3 viewVector = *P /*- cloudTrans.getTranslationAsVec3D()*/; //clouds are still in their local coordinate system! if (viewVector.dot(N) > 0) N *= -PC_ONE; cloud->addNorm(N); //progress if (!nprogress.oneStep()) { result = false; if (canceledByUser) *canceledByUser = true; ccLog::Warning("[ComputeNormals] Process canceled by user!"); //early stop j = height; break; } } } } if (!result) { cloud->unallocateNorms(); } } else { ccLog::Warning("[ComputeNormals] Not enough memory!"); } return result; }
float knn(Mat& m_destinations, Mat& m_object, vector<int>& ptpairs) { vector<float> dists; return knn(m_destinations, m_object, ptpairs, dists); }
int main (int argc, const char * argv[]) { // choose which zero of autocorrelation should be set as delay parameter int delay_choice =2; int embed_dimension =3; int num_neighbours = 15; long num_of_points_retained=500; int percentile =70; long output_len=2*(((float)percentile)/100)*num_of_points_retained; printf("%ld output_len =",output_len); float *output = malloc(output_len* sizeof(float)); printf("Density based subsampling running \n"); if (argc != 2) { fprintf(stderr, "Expecting wav file as argument\n"); return 1; } // Open sound file that comes in as a command line argument SF_INFO sndInfo; SNDFILE *sndFile = sf_open(argv[1], SFM_READ, &sndInfo); if (sndFile == NULL) { fprintf(stderr, "Error reading source file '%s': %s\n", argv[1], sf_strerror(sndFile)); return 1; } // Check format - 16bit PCM if (sndInfo.format != (SF_FORMAT_WAV | SF_FORMAT_PCM_16)) { fprintf(stderr, "Input should be 16bit Wav\n"); sf_close(sndFile); return 1; } // Now we know the length of the wav file, we can create a buffer for it, in this case, we are creating a double array. // Allocate memory float *buffer = malloc(sndInfo.frames * sizeof(float)* sndInfo.channels); if (buffer == NULL) { fprintf(stderr, "Could not allocate memory for data\n"); sf_close(sndFile); return 1; } // This next step, actually reads in the wav file data. This function automatically converts the whichever format the audio file data is in to doubles. The library can convert to a number of different formats. // Load data long numFrames = sf_readf_float(sndFile, buffer, sndInfo.frames); // Check correct number of samples loaded if (numFrames != sndInfo.frames) { fprintf(stderr, "Did not read enough samples for source\n"); sf_close(sndFile); free(buffer); return 1; } // Now just output some info about the wav file printf("Read %ld samples from %s, Sample rate: %d, Length: %fs\n", numFrames, argv[1], sndInfo.samplerate, (float)numFrames/sndInfo.samplerate); // extract a single channel out float *buffer_singlechannel = malloc(sndInfo.frames * sizeof(float)); long i=0; for (long f=0 ; f<numFrames ; f++) { buffer_singlechannel[f]= buffer[i]; // Channel 1 i+=sndInfo.channels; } // free the buffer with multiple channel input free(buffer); // array to store the autocorrelation function double *buffer_autocorrelation = malloc((2*sndInfo.frames +1)* sizeof(double)); // calculate autocorrelation xcorr( buffer_singlechannel, buffer_singlechannel, buffer_autocorrelation, sndInfo.frames, 0,sndInfo.frames, 1, 1, sndInfo.frames,sndInfo.frames, sndInfo.frames, sndInfo.frames); // choose delay to be second zero of auto correlation long long delay = zeroCrossing(buffer_autocorrelation, 2*sndInfo.frames +1, delay_choice); printf("Delay chosen = %lld\n",delay); long long delay_embd_length= (sndInfo.frames -(delay*embed_dimension)+2)* embed_dimension; //create delay embedding and store in a linear array in row major form float *buffer_delayembedding =malloc(delay_embd_length*sizeof(float)); long long k1=0; for (long long l=(delay*embed_dimension)-2; l<sndInfo.frames; l=l+1) { buffer_delayembedding[k1]=buffer_singlechannel[l]; buffer_delayembedding[k1+1]=buffer_singlechannel[l-delay]; buffer_delayembedding[k1+2]=buffer_singlechannel[l-2*delay]; // printf("%f %f %f\n", buffer_delayembedding[k1],buffer_delayembedding[k1+1],buffer_delayembedding[k1+2]); k1=k1+embed_dimension; } int *array =malloc(num_of_points_retained *sizeof(int)); random_selector( delay_embd_length/ embed_dimension ,num_of_points_retained , array); for (int i=0; i<num_of_points_retained; i++) { // printf("%d\n",array[i]); } float *buffer_delayembedding_selec =malloc(num_of_points_retained*embed_dimension*sizeof(float)); float * density_vals =malloc(num_of_points_retained* sizeof(float)); printf("total len = %lld\n ", delay_embd_length/3 ); printf("buffer len = %ld\n", num_of_points_retained*embed_dimension); printf("num_of_points_retained = %ld\n", num_of_points_retained); for (int i=0; i<num_of_points_retained; i=i+1) { int loc=(array[i]-1)*embed_dimension; // printf("i :%d loc=%d array[i]: %d \n",i, loc, array[i]); buffer_delayembedding_selec[3*i]=buffer_delayembedding[loc]; buffer_delayembedding_selec[3*i+1]=buffer_delayembedding[loc+1]; buffer_delayembedding_selec[3*i+2]=buffer_delayembedding[loc+2]; // printf("buffer selec [%d] = %f ", 3*i, buffer_delayembedding_selec[3*i]); // printf("[%d] = %f ", 3*i+1, buffer_delayembedding_selec[3*i+1]); // printf("[%d] = %f \n", 3*i+2, buffer_delayembedding_selec[3*i+2]); } // for (int i=0; i<delay_embd_length/ embed_dimension ; i++) // { // printf("% d th original :%f %f %f\n",i, buffer_delayembedding [3*i],buffer_delayembedding [3*i+1],buffer_delayembedding [3*i+2]); // } knn (buffer_delayembedding_selec, density_vals , (num_of_points_retained), num_neighbours); // free all arrays used till now except the single channel wave file and close the sndFile object sf_close(sndFile); //desnity based subsampling float * density_vals_temp =malloc(num_of_points_retained* sizeof(float)); memcpy(density_vals_temp,density_vals,num_of_points_retained* sizeof(float)); qsort(density_vals_temp, num_of_points_retained, sizeof(float), cmpfunc); float key=findNumber(100-percentile,density_vals_temp,500); // Function call and the print statement // printf(" \n %d percentile: %.4lf \n", percentile, key); printf(" \n the linear array output contains the subsampled point cloud \n"); //for (int kk=0; kk<num_of_points_retained; kk++) { // printf("%d index %f \n", kk,density_vals_temp[kk]); //} free(density_vals_temp); int counter2=0; for( int counter = 0 ; counter < num_of_points_retained; counter++) { if (density_vals[counter]>=key && counter2<output_len/2) { output[2*counter2]=buffer_delayembedding_selec[3*counter]; output[2*counter2+1]=buffer_delayembedding_selec[3*counter+1]; //printf("%d output %.4lf , %.4lf\n", counter2,output[2*counter2],output[2*counter2+1]); //printf("%d output %.4lf , %.4lf\n", counter,buffer_delayembedding_selec[3*counter],buffer_delayembedding_selec[3*counter+1]); counter2++; } } free(buffer_singlechannel); free(buffer_autocorrelation); free(buffer_delayembedding); free(array); free(buffer_delayembedding_selec); free(density_vals); printf("Density based subsampling End\n"); free(output); return 0; }
void vlad_compute(int k, int d, const float *centroids, int n, const float *v,int flags, float *desc) { int i,j,l,n_quantile,i0,i1,ai,a,ma,ni; int *perm ; float un , diff; float *tab,*u,*avg,*sum,*mom2,*dists; int *hist,*assign; if(flags<11 || flags>=13) { assign=ivec_new(n); nn(n,k,d,centroids,v,assign,NULL,NULL); if(flags==6 || flags==7) { n_quantile = flags==6 ? 3 : 1; fvec_0(desc,k*d*n_quantile); perm = ivec_new(n); tab = fvec_new(n); ivec_sort_index(assign,n,perm); i0=0; for(i=0;i<k;i++) { i1=i0; while(i1<n && assign[perm[i1]]==i) { i1++; } if(i1==i0) continue; for(j=0;j<d;j++) { for(l=i0;l<i1;l++) { tab[l-i0]=v[perm[l]*d+j]; } ni=i1-i0; fvec_sort(tab,ni); for(l=0;l<n_quantile;l++) { desc[(i*d+j)*n_quantile+l]=(tab[(l*ni+ni/2)/n_quantile]-centroids[i*d+j])*ni; } } i0=i1; } free(perm); free(tab); } else if(flags==5) { fvec_0(desc,k*d); for(i=0;i<n;i++) { for(j=0;j<d;j++) { desc[assign[i]*d+j]+=v[i*d+j]; } } } else if(flags==8 || flags==9) { fvec_0(desc,k*d); u = fvec_new(d); for(i=0;i<n;i++) { fvec_cpy(u,v+i*d,d); fvec_sub(u,centroids+assign[i]*d,d); un=(float)sqrt(fvec_norm2sqr(u,d)); if(un==0) continue; if(flags==8) { fvec_div_by(u,d,un); } else if(flags==9) { fvec_div_by(u,d,sqrt(un)); } fvec_add(desc+assign[i]*d,u,d); } free(u); } else if(flags==10) { fvec_0(desc,k*d); for(i=0;i<n;i++) { for(j=0;j<d;j++) { desc[assign[i]*d+j]+=v[i*d+j]; } } for(i=0;i<k;i++) { fvec_normalize(desc+i*d,d,2.0); } } else if(flags==13) { fvec_0(desc,k*d); for(i=0;i<n;i++) { for(j=0;j<d;j++) { desc[assign[i]*d+j]+=(float)sqr(v[i*d+j]-centroids[assign[i]*d+j]); } } } else if(flags==14) { avg = fvec_new_0(k*d); for(i=0;i<n;i++) { for(j=0;j<d;j++) { avg[assign[i]*d+j]+=v[i*d+j]-centroids[assign[i]*d+j]; } } hist=ivec_new_histogram(k,assign,n); for(i=0;i<k;i++) { if(hist[i]>0) { for(j=0;j<d;j++) { avg[i*d+j]/=hist[i]; } } } free(hist); fvec_0(desc,k*d); for(i=0;i<n;i++) { for(j=0;j<d;j++) { desc[assign[i]*d+j]+=(float)(sqr(v[i*d+j]-centroids[assign[i]*d+j]-avg[assign[i]*d+j])); } } fvec_sqrt(desc,k*d); free(avg); } else if(flags==15) { fvec_0(desc,k*d*2); sum = desc; for(i=0;i<n;i++) { for(j=0;j<d;j++) { sum[assign[i]*d+j]+=v[i*d+j]-centroids[assign[i]*d+j]; } } hist = ivec_new_histogram(k,assign,n); mom2 = desc+k*d; for(i=0;i<n;i++) { ai=assign[i]; for(j=0;j<d;j++) { mom2[ai*d+j]+=(float)(sqr(v[i*d+j]-centroids[ai*d+j]-sum[ai*d+j]/hist[ai])); } } fvec_sqrt(mom2,k*d); free(hist); } else if(flags==17) { fvec_0(desc,k*d*2); for(i=0;i<n;i++) { for(j=0;j<d;j++) { diff=v[i*d+j]-centroids[assign[i]*d+j]; if(diff>0) { desc[assign[i]*d+j]+=diff; } else { desc[assign[i]*d+j+k*d]-=diff; } } } } else { fvec_0(desc,k*d); for(i=0;i<n;i++) { for(j=0;j<d;j++) { desc[assign[i]*d+j]+=v[i*d+j]-centroids[assign[i]*d+j]; } } if(flags==1) { hist=ivec_new_histogram(k,assign,n); /* printf("unbalance factor=%g\n",ivec_unbalanced_factor(hist,k)); */ for(i=0;i<k;i++) { for(j=0;j<d;j++) { desc[i*d+j]/=hist[i]; } } free(hist); } if(flags==2) { for(i=0;i<k;i++) { fvec_normalize(desc+i*d,d,2.0); } } if(flags==3 || flags==4) { assert(!"not implemented"); } if(flags==16) { hist=ivec_new_histogram(k,assign,n); for(i=0;i<k;i++) { if(hist[i]>0) { fvec_norm(desc+i*d,d,2); fvec_mul_by(desc+i*d,d,sqrt(hist[i])); } } free(hist); } } free(assign); } else if(flags==11 || flags==12) { ma=flags==11 ? 4 : 2; assign=ivec_new(n*ma); dists=knn(n,k,d,ma,centroids,v,assign,NULL,NULL); fvec_0(desc,k*d); for(i=0;i<n;i++) { for(j=0;j<d;j++) { for(a=0;a<ma;a++) { desc[assign[ma*i+a]*d+j]+=v[i*d+j]-centroids[assign[ma*i+a]*d+j]; } } } free(dists); free(assign); } }