From 2b1fb3dfb7c7fab9aebc4d64f47ea2273e16bcc3 Mon Sep 17 00:00:00 2001 From: Clement Farabet Date: Tue, 25 Oct 2011 07:53:35 -0400 Subject: [PATCH] Added a couple for missing mem frees. --- generic/imgraph.c | 51 +++---- pink/kruskal.c | 368 +++++++++++++++++++++++----------------------- 2 files changed, 206 insertions(+), 213 deletions(-) diff --git a/generic/imgraph.c b/generic/imgraph.c index 3ef9423..a912087 100644 --- a/generic/imgraph.c +++ b/generic/imgraph.c @@ -602,7 +602,7 @@ static int imgraph_(watershed)(lua_State *L) { int32_t *labels_data = SLONGDATA(labels_xv); int i; for (i = 0; i < (rowsize(filtered_xv)*colsize(filtered_xv)); i++) - if (labels_data[i]) filtered_data[i] = NDG_MAX; + if (labels_data[i]) filtered_data[i] = NDG_MAX; else filtered_data[i] = NDG_MIN; imgraph_(xv2tensor)(filtered_xv, input); @@ -636,7 +636,7 @@ static int imgraph_(mergetree)(lua_State *L) { // compute labels struct xvimage *labels = allocimage(NULL,rs,cs,1,VFF_TYP_4_BYTE); int32_t *labels_data = SLONGDATA(labels); - int i; + int i; for (i=0; irs = rs; t->altitudes = altitudes; - - + + return 1; } @@ -684,7 +684,7 @@ static int imgraph_(filtertree)(lua_State *L) { attribute = surfaceMergeTree(t->tree->CT,t->rag); break; case 1: - attribute = dynaMergeTree(t->tree->CT,t->rag); + attribute = dynaMergeTree(t->tree->CT,t->rag); break; case 2: attribute = volumeMergeTree(t->tree->CT,t->rag); @@ -717,25 +717,26 @@ static int imgraph_(filtertree)(lua_State *L) { return 1; } - static int imgraph_(cuttree)(lua_State *L) { // get args MergeTree *t = lua_toMergeTree(L, 1); - - //calling the labeling method on the merge tree - list * cut = MSF_Kruskal(t); - - // export list into lua table - lua_newtable(L); int tb = lua_gettop(L); - int id = 1; - while(cut) { - lua_pushnumber(L, cut->index+1); - lua_rawseti(L, tb, id++); - cut = cut->next; - } - - // done - return 1; + + //calling the labeling method on the merge tree + list * cut = MSF_Kruskal(t); + + // export list into lua table + lua_newtable(L); int tb = lua_gettop(L); + int id = 1; + while(cut) { + lua_pushnumber(L, cut->index+1); + lua_rawseti(L, tb, id++); + list *cur = cut; + cut = cut->next; + free(cur); + } + + // done + return 1; } static int imgraph_(weighttree)(lua_State *L) { @@ -754,7 +755,7 @@ static int imgraph_(weighttree)(lua_State *L) { t->weights[id++] = lua_tonumber(L, -1); lua_pop(L,1); } - if (id < t->tree->CT->nbnodes) + if (id < t->tree->CT->nbnodes) printf(" WARNING: not enough weights provided, padding with zeros\n"); for (; idtree->CT->nbnodes; id++) t->weights[id] = 0; @@ -834,7 +835,7 @@ int imgraph_(tree2components)(lua_State *L) { // get entry for son long sonid = son->son + 1; lua_rawgeti(L, table_comps, sonid); - THTensor *sonentry = luaT_toudata(L, -1, torch_(Tensor_id)); + THTensor *sonentry = luaT_toudata(L, -1, torch_(Tensor_id)); lua_pop(L,1); // update parent's structure @@ -1085,7 +1086,7 @@ int imgraph_(histpooling)(lua_State *L) { real minConfidence = lua_tonumber(L, 5); // check dims - if ((vectors->nDimension != 3) || (segm->nDimension != 2) + if ((vectors->nDimension != 3) || (segm->nDimension != 2) || (segm->size[0] != vectors->size[1]) || (segm->size[1] != vectors->size[2])) THError(" vectors must be KxHxW and segm HxW"); @@ -1345,7 +1346,7 @@ int imgraph_(segm2components)(lua_State *L) { } else { // retrieve entry - THTensor *entry = luaT_toudata(L, -1, torch_(Tensor_id)); + THTensor *entry = luaT_toudata(L, -1, torch_(Tensor_id)); lua_pop(L,1); // update content diff --git a/pink/kruskal.c b/pink/kruskal.c index 8b1d705..470615a 100644 --- a/pink/kruskal.c +++ b/pink/kruskal.c @@ -1,10 +1,9 @@ /* -Kruskal algorithm for maximum spanning forest computation -author: Camille Couprie -21 oct. 2011 + Kruskal algorithm for maximum spanning forest computation + author: Camille Couprie + 21 oct. 2011 */ - #include #include #include @@ -23,47 +22,47 @@ author: Camille Couprie #define true 1 void Insert(list **sl, int index) - { - list *tmp = NULL; - list *csl = *sl; - list *elem = malloc(sizeof(list)); - if(!elem) exit(EXIT_FAILURE); - elem->index = index; - while(csl) - { - tmp = csl; - csl = csl->next; - } - elem->next = csl; - if(tmp) tmp->next = elem; - else *sl = elem; - } +{ + list *tmp = NULL; + list *csl = *sl; + list *elem = malloc(sizeof(list)); + if(!elem) exit(EXIT_FAILURE); + elem->index = index; + while(csl) + { + tmp = csl; + csl = csl->next; + } + elem->next = csl; + if(tmp) tmp->next = elem; + else *sl = elem; +} /*=====================================================================================*/ - list * MSF_Kruskal(MergeTree * MT) // max_weight /* maximum weight value */ ) +list * MSF_Kruskal(MergeTree * MT) // max_weight /* maximum weight value */ ) /*=====================================================================================*/ -/*Segment a tree into two components. - Returns a list of nodes correspunding to the Max Spanning Forest cut, +/*Segment a tree into two components. + Returns a list of nodes correspunding to the Max Spanning Forest cut, computed using Kruskal's algorithm */ -{ - int i, j, k, x, y, e1, e2; - int nb_markers; int nb_leafs; - long N, M; +{ + int i, j, k, x, y, e1, e2; + int nb_markers; int nb_leafs; + long N, M; - mtree * T= MT->tree; - float * W = MT->weights; - // mergeTreePrint(T); - JCctree *CT = T->CT; - int root_node = CT->root; + mtree * T= MT->tree; + float * W = MT->weights; + // mergeTreePrint(T); + JCctree *CT = T->CT; + int root_node = CT->root; //nb nodes - M = CT->nbnodes; + M = CT->nbnodes; - // nb_edges - nb_leafs = 0; - for (i = 0; i < M; i++) - if (CT->tabnodes[i].nbsons == 0) - nb_leafs++; + // nb_edges + nb_leafs = 0; + for (i = 0; i < M; i++) + if (CT->tabnodes[i].nbsons == 0) + nb_leafs++; nb_markers = nb_leafs+1; N=M+nb_markers; @@ -71,8 +70,8 @@ void Insert(list **sl, int index) //printf("Nb nodes:%d Nb edges: %d Nb leafs :%d \n", N, M, nb_leafs); // indexes of edges : son's nodes indexes - //Memory allocation of temporary arrays for Krukal's algorithm - Lifo * LIFO; + //Memory allocation of temporary arrays for Krukal's algorithm + Lifo * LIFO; LIFO = CreeLifoVide(M); if (LIFO == NULL) { fprintf(stderr, "kruskal : CreeLifoVide failed\n"); exit(0); } @@ -83,68 +82,66 @@ void Insert(list **sl, int index) if (SeededNodes == NULL) { fprintf(stderr, "kruskal : malloc failed\n"); exit(0); } // markers - SeededNodes[0]= M; j=1; - for (i = 0; i < CT->nbnodes; i++) - if (CT->tabnodes[i].nbsons == 0) + for (i = 0; i < CT->nbnodes; i++) + if (CT->tabnodes[i].nbsons == 0) { - SeededNodes[j]= i+CT->nbnodes; - Mrk[SeededNodes[j]] = 1; - //printf("seeded node %d \n", SeededNodes[j]); - j++; + SeededNodes[j]= i+CT->nbnodes; + Mrk[SeededNodes[j]] = 1; + //printf("seeded node %d \n", SeededNodes[j]); + j++; } Mrk[M] = 0; - - + uint32_t * Rnk = (uint32_t*)calloc(N, sizeof(uint32_t)); if (Rnk == NULL) { fprintf(stderr, "kruskal : malloc failed\n"); exit(0); } uint32_t * Fth = (uint32_t*)malloc(N*sizeof(uint32_t)); if (Fth == NULL) { fprintf(stderr, "kruskal : malloc failed\n"); exit(0); } for(k=0;knbnodes+k]=0.5; - //fprintf(stderr,"%f\n", sorted_weights[CT->nbnodes+k]); - } - TriRapideStochastique_dec(sorted_weights,Es, 0, M-1); - free(sorted_weights); - + for(k=0;knbnodes+k]=0.5; + //fprintf(stderr,"%f\n", sorted_weights[CT->nbnodes+k]); + } + TriRapideStochastique_dec(sorted_weights,Es, 0, M-1); + free(sorted_weights); + long nb_arete = 0; int e_max, root; long cpt_aretes = 0; - // beginning of main loop + // beginning of main loop while (nb_arete < N-nb_markers+1) { e_max=Es[cpt_aretes]; cpt_aretes=cpt_aretes+1; - e1= e_max; // e1 = Edges[0][e_max]; + e1= e_max; // e1 = Edges[0][e_max]; if (e_maxnbnodes) e2= CT->tabnodes[e_max].father; else e2= e_max-CT->nbnodes; if (e2==-1)e2=M; //e2 = Edges[1][e_max]; x = element_find(e1, Fth ); y = element_find(e2, Fth ); if ((x != y) && (!(Mrk[x]>=1 && Mrk[y]>=1))) - { - root = element_link( x,y, Rnk, Fth); - nb_arete=nb_arete+1; - if ( Mrk[x]>=1) Mrk[root]= Mrk[x]; - else if ( Mrk[y]>=1) Mrk[root]= Mrk[y]; - } + { + root = element_link( x,y, Rnk, Fth); + nb_arete=nb_arete+1; + if ( Mrk[x]>=1) Mrk[root]= Mrk[x]; + else if ( Mrk[y]>=1) Mrk[root]= Mrk[y]; + } } - - //building the labeling for each individual markers in map + + //building the labeling for each individual markers in map // (find the root vertex of each tree) int * Map2 = (int *)malloc(N*sizeof(int)); int * Map = (int *)malloc(N*sizeof(int)); @@ -152,80 +149,80 @@ void Insert(list **sl, int index) Map2[i] = element_find(i, Fth); //printf("Map2[%d]=%d \n",i,Map2[i]); } - - // Compute the binary labeling in Map - for (i = 1; i < nb_markers; i++) + + // Compute the binary labeling in Map + for (i = 1; i < nb_markers; i++) Map[SeededNodes[i]] = 1; - Map[M]=0; + Map[M]=0; for (i=0;inbnodes; i++) - { - // nodes having a different value than their father are in the cut - if( Map[CT->tabnodes[i].father] != Map[i]) - Insert(&cut, i); - // leafs having the same label as the root are in the cut - if ((CT->tabnodes[i].nbsons == 0) && (Map[i]==0)) - Insert(&cut, i); + for (i = 0; i < CT->nbnodes; i++) + { + // nodes having a different value than their father are in the cut + if( Map[CT->tabnodes[i].father] != Map[i]) + Insert(&cut, i); + // leafs having the same label as the root are in the cut + if ((CT->tabnodes[i].nbsons == 0) && (Map[i]==0)) + Insert(&cut, i); } - - //PrintList(cut); - - - LifoTermine(LIFO); - free(Mrk); - free(Rnk); - free(Es); - free(Map); - free(Map2); - free(Fth); - return cut; + + //PrintList(cut); + + LifoTermine(LIFO); + free(Mrk); + free(SeededNodes); + free(Rnk); + free(Fth); + free(Es); + free(Map); + free(Map2); + return cut; } /*================================================*/ void PrintList(list *sl) /*================================================*/ { -while(sl) - { - printf("%d\n",sl->index); - sl = sl->next; - } + while(sl) + { + printf("%d\n",sl->index); + sl = sl->next; + } } - + /*================================================*/ int nb_neighbors(int x, JCctree *CT, int nb_leafs) /*================================================*/ @@ -252,20 +249,20 @@ int neighbor(int x, int k, JCctree *CT, int nb_leafs, int * SeededNodes) { tmp = CT->tabnodes[x].nbsons; if (tmp==0) - { if (k==0) return CT->tabnodes[x].father; - return SeededNodes[x+1]; - } + { if (k==0) return CT->tabnodes[x].father; + return SeededNodes[x+1]; + } else if (k<=tmp) - { - if (k==tmp) return CT->tabnodes[x].father; - s = CT->tabnodes[x].sonlist; - for (i=0;inext; // INEFFICACE A REFAIRE - return s->son; - } + { + if (k==tmp) return CT->tabnodes[x].father; + s = CT->tabnodes[x].sonlist; + for (i=0;inext; // INEFFICACE A REFAIRE + return s->son; + } } - else if (xnbnodes+nb_leafs) + else if (xnbnodes+nb_leafs) return x-CT->nbnodes; - else + else { return CT->root; } @@ -273,10 +270,10 @@ int neighbor(int x, int k, JCctree *CT, int nb_leafs, int * SeededNodes) /*================================================*/ int element_link( int x,int y, uint32_t *Rnk, uint32_t *Fth) -/*================================================*/ +/*================================================*/ { if( Rnk[x] > Rnk[y]) - { + { int t; t=x; x=y; @@ -290,37 +287,33 @@ int element_link( int x,int y, uint32_t *Rnk, uint32_t *Fth) return y; } - - /*===============================*/ int element_find(int x, uint32_t *Fth ) /*===============================*/ -{ - if (Fth[x] != x) - Fth[x] = element_find(Fth[x], Fth); +{ + if (Fth[x] != x) + Fth[x] = element_find(Fth[x], Fth); return Fth[x]; } - /* // Printing the merge tree - int32_t i; - JCsoncell *s; - JCctree *CT = MT->CT; - printf("root = %d ; nbnodes: %d ; nbsoncells: %d\n", CT->root, CT->nbnodes, CT->nbsoncells); - for (i = 0; i < CT->nbnodes; i++) - { - printf("node: %d ; level %d ; nbsons: %d ; father: %d ; mergeEdge %d", - i, CT->tabnodes[i].data, CT->tabnodes[i].nbsons, CT->tabnodes[i].father, MT->mergeEdge[i]); +/* // Printing the merge tree + int32_t i; + JCsoncell *s; + JCctree *CT = MT->CT; + printf("root = %d ; nbnodes: %d ; nbsoncells: %d\n", CT->root, CT->nbnodes, CT->nbsoncells); + for (i = 0; i < CT->nbnodes; i++) + { + printf("node: %d ; level %d ; nbsons: %d ; father: %d ; mergeEdge %d", + i, CT->tabnodes[i].data, CT->tabnodes[i].nbsons, CT->tabnodes[i].father, MT->mergeEdge[i]); if (CT->tabnodes[i].nbsons > 0) { - printf("sons: "); - for (s = CT->tabnodes[i].sonlist; s != NULL; s = s->next) - printf("%d ", s->son); + printf("sons: "); + for (s = CT->tabnodes[i].sonlist; s != NULL; s = s->next) + printf("%d ", s->son); } printf("\n"); - } - */ - - + } +*/ /* =============================================================== */ long Partitionner_dec(float *A, uint32_t * I, long p, long r) @@ -330,26 +323,26 @@ long Partitionner_dec(float *A, uint32_t * I, long p, long r) en deux groupes : ceux <= A[p] et les autres. */ { - float t; - int t1; - float x = A[p]; - long i = p - 1; - long j = r + 1; - while (1) + float t; + int t1; + float x = A[p]; + long i = p - 1; + long j = r + 1; + while (1) { - do j--; while (A[j] < x); - do i++; while (A[i] > x); - if (i < j) - { - t = A[i]; - A[i] = A[j]; - A[j] = t; - t1 = I[i]; - I[i] = I[j]; - I[j] = t1; + do j--; while (A[j] < x); + do i++; while (A[i] > x); + if (i < j) + { + t = A[i]; + A[i] = A[j]; + A[j] = t; + t1 = I[i]; + I[i] = I[j]; + I[j] = t1; } - else return j; - } /* while (1) */ + else return j; + } /* while (1) */ } /* Partitionner() */ /* =============================================================== */ @@ -367,30 +360,29 @@ long PartitionStochastique_dec (float *A, uint32_t * I, long p, long r) q = p + (genrand64_int64() % (r - p + 1)); /* rand must be 64-bit safe, should be OK now */ // assert((q >= p) && (q <= r)) ; /* you'd be surprised */ t = A[p]; /* echange A[p] et A[q] */ - A[p] = A[q]; + A[p] = A[q]; A[q] = t; - + t1 = I[p]; /* echange I[p] et I[q] */ - I[p] = I[q]; + I[p] = I[q]; I[q] = t1; return Partitionner_dec(A, I, p, r); } /* PartitionStochastique() */ - /* =============================================================== */ void TriRapideStochastique_dec (float * A, uint32_t *I, long p, long r) /* =============================================================== */ -/* - trie les valeurs du tableau A de l'indice p (compris) a l'indice r (compris) - par ordre decroissant +/* + trie les valeurs du tableau A de l'indice p (compris) a l'indice r (compris) + par ordre decroissant */ { - long q; + long q; if (p < r) - { - q = PartitionStochastique_dec(A, I, p, r); - TriRapideStochastique_dec (A, I, p, q) ; - TriRapideStochastique_dec (A, I, q+1, r) ; - } + { + q = PartitionStochastique_dec(A, I, p, r); + TriRapideStochastique_dec (A, I, p, q) ; + TriRapideStochastique_dec (A, I, q+1, r) ; + } } /* TriRapideStochastique() */