tree
change_leaves
void change_leaves(tree *t, char *leaf_list)
{
list *llist = get_paths(leaf_list);
char **leaves = (char **)list_to_array(llist);
int n = llist->size;
int i,j;
int found = 0;
for(i = 0; i < t->n; ++i){
t->leaf[i] = 0;
for(j = 0; j < n; ++j){
if (0==strcmp(t->name[i], leaves[j])){
t->leaf[i] = 1;
++found;
break;
}
}
}
fprintf(stderr, "Found %d leaves.\n", found);
}
ํจ์ ์ด๋ฆ: change_leaves
์ ๋ ฅ:
t: ํธ๋ฆฌ๋ฅผ ๋ํ๋ด๋ ๊ตฌ์กฐ์ฒด ํฌ์ธํฐ
leaf_list: ์๋ก์ด leaf ๋ ธ๋์ ์ด๋ฆ๋ค์ ๋ด๊ณ ์๋ ๋ฌธ์์ด
๋์:
leaf_list์ ์๋ leaf ๋ ธ๋๋ค์ t์ leaf ๋ ธ๋๋ก ๋ณ๊ฒฝ
t ๊ตฌ์กฐ์ฒด์ leaf ๋ฐฐ์ด์ ์ ๋ฐ์ดํธํ์ฌ leaf ๋ ธ๋๋ฅผ ํ์
๋ณ๊ฒฝ๋ leaf ๋ ธ๋์ ์๋ฅผ ์ถ๋ ฅ
์ค๋ช :
์ด ํจ์๋ YOLO ๊ฐ์ฒด ๊ฒ์ถ์์ ์ฌ์ฉ๋๋ ํธ๋ฆฌ ๊ตฌ์กฐ์ฒด๋ฅผ ๋ณ๊ฒฝํ๋ ํจ์์ ๋๋ค. ์ด ํจ์๋ leaf_list์ ์๋ leaf ๋ ธ๋๋ค์ t ๊ตฌ์กฐ์ฒด์ leaf ๋ ธ๋๋ก ๋ณ๊ฒฝํฉ๋๋ค.
๋จผ์ , leaf_list์์ leaf ๋ ธ๋์ ์ด๋ฆ์ ๊ฐ์ ธ์์ ๋ฐฐ์ด์ ์ ์ฅํฉ๋๋ค.
๊ทธ๋ฐ ๋ค์, t ๊ตฌ์กฐ์ฒด์ ๋ชจ๋ ๋ ธ๋๋ฅผ ํ์ธํ๋ฉด์, leaf_list์ ์๋ leaf ๋ ธ๋์ ์ด๋ฆ๊ณผ ์ผ์นํ๋ ๋ ธ๋๊ฐ ์๋ ๊ฒฝ์ฐ, ํด๋น ๋ ธ๋๋ฅผ leaf ๋ ธ๋๋ก ํ์ํฉ๋๋ค.
๋ง์ง๋ง์ผ๋ก, ๋ณ๊ฒฝ๋ leaf ๋ ธ๋์ ์๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
get_hierarchy_probability
float get_hierarchy_probability(float *x, tree *hier, int c, int stride)
{
float p = 1;
while(c >= 0){
p = p * x[c*stride];
c = hier->parent[c];
}
return p;
}
ํจ์ ์ด๋ฆ: get_hierarchy_probability
์ ๋ ฅ:
x: ์ ๊ฒฝ๋ง ์ถ๋ ฅ๊ฐ (1์ฐจ์ ์ค์ ๋ฐฐ์ด)
hier: ๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ํํํ๋ ํธ๋ฆฌ
c: ์์ธก ํด๋์ค ์ธ๋ฑ์ค
stride: x ๋ฐฐ์ด์์ ํ ํด๋์ค๋ฅผ ํํํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ ์์ ์
๋์:
๊ณ์ธต ๊ตฌ์กฐ ํธ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ ์์ธก ํด๋์ค์ ๊ณ์ธต ํ๋ฅ ์ ๊ณ์ฐํ๋ค.
์์ธก ํด๋์ค์ ๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ๋ฐ๋ผ ์์ ํด๋์ค์ ํ๋ฅ ์ ํ์ ํด๋์ค ํ๋ฅ ์ ๊ณฑํด ์ต์ข ํ๋ฅ ๊ฐ์ ๊ตฌํ๋ค.
์ค๋ช :
์ด ํจ์๋ ๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํ์ฌ ๋ค๋จ๊ณ ๊ฐ์ฒด ์ธ์์์ ์์ธก๋ ํด๋์ค์ ๋ํ ๊ณ์ธต ํ๋ฅ ์ ๊ณ์ฐํ๋ ๋ฐ ์ฌ์ฉ๋๋ค.
๊ณ์ธต ๊ตฌ์กฐ๋ ํธ๋ฆฌ ํํ๋ก ํํ๋๋ฉฐ, ์ด ํธ๋ฆฌ๋ ๋ถ๋ชจ-์์ ๊ด๊ณ๋ฅผ ๋ํ๋ธ๋ค.
์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง c ์ธ๋ฑ์ค๋ ์์ธก๋ ํด๋์ค์ ์ธ๋ฑ์ค๋ฅผ ๋ํ๋ธ๋ค.
๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ๋ฐ๋ผ์, c ์ธ๋ฑ์ค์ ํด๋์ค์ ํด๋นํ๋ ๋ ธ๋์์ ๋ฃจํธ ๋ ธ๋๊น์ง์ ๊ฒฝ๋ก ์์ ๊ฐ ๋ ธ๋์ ๊ฐ์ ๊ณฑํด ์ต์ข ํ๋ฅ ๊ฐ์ ๊ตฌํ๋ค.
์ด๋, ์ ๋ ฅ ๋ฐฐ์ด x์์ ํ ํด๋์ค๋ฅผ ํํํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ ์์ ์๋ฅผ stride๋ก ๋ํ๋ธ๋ค.
hierarchy_predictions
void hierarchy_predictions(float *predictions, int n, tree *hier, int only_leaves, int stride)
{
int j;
for(j = 0; j < n; ++j){
int parent = hier->parent[j];
if(parent >= 0){
predictions[j*stride] *= predictions[parent*stride];
}
}
if(only_leaves){
for(j = 0; j < n; ++j){
if(!hier->leaf[j]) predictions[j*stride] = 0;
}
}
}
ํจ์ ์ด๋ฆ: hierarchy_predictions
์ ๋ ฅ:
float *predictions: ์์ธก๊ฐ ๋ฐฐ์ด
int n: ์์ธก๊ฐ ๋ฐฐ์ด์ ๊ธธ์ด
tree *hier: ๊ณ์ธต ๊ตฌ์กฐ ์ ๋ณด๋ฅผ ๋ด์ tree ๊ตฌ์กฐ์ฒด
int only_leaves: leaf ๋ ธ๋๋ค๋ง ์ฌ์ฉํ ์ง ์ฌ๋ถ (1: leaf ๋ ธ๋๋ง ์ฌ์ฉ, 0: ์ ์ฒด ๋ ธ๋ ์ฌ์ฉ)
int stride: ์์ธก๊ฐ ๋ฐฐ์ด์์ ๋ ธ๋ ํ๋๋ฅผ ํํํ๋๋ฐ ํ์ํ ์์ ์
๋์:
๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ์ด์ฉํ์ฌ ์์ธก๊ฐ์ ๋ณด์ ํ๋ค.
์์ธก๊ฐ์ด ๋ด๊ธด ๋ฐฐ์ด predictions์ ์ ๋ ฅ์ผ๋ก ๋ฐ์, ๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ๋ฐ๋ผ ์์ธก๊ฐ์ ๋ณด์ ํ๋ค. ์๋ฅผ ๋ค์ด, ๋ง์ฝ j๋ฒ์งธ ๋ ธ๋๊ฐ parent๋ ธ๋์ child ๋ ธ๋๋ผ๋ฉด, j๋ฒ์งธ ๋ ธ๋์ ํด๋นํ๋ ์์ธก๊ฐ์ j๋ฒ์งธ ๋ ธ๋์ ๋ํ ์์ธก๊ฐ๊ณผ parent ๋ ธ๋์ ๋ํ ์์ธก๊ฐ์ ๊ณฑ์ผ๋ก ๊ณ์ฐ๋๋ค.
๋ง์ฝ only_leaves๊ฐ 1๋ก ์ค์ ๋์ด ์์ผ๋ฉด, leaf ๋ ธ๋ ์ด์ธ์ ๋ ธ๋๋ค์ ํด๋นํ๋ ์์ธก๊ฐ์ 0์ผ๋ก ์ค์ ๋๋ค.
์ค๋ช :
์ด ํจ์๋ ๊ณ์ธต ๊ตฌ์กฐ ์ ๋ณด๋ฅผ ์ด์ฉํ์ฌ ์์ธก๊ฐ์ ๋ณด์ ํ๋ ์์ ์ ์ํํ๋ค.
์ด๋ ๊ณ์ธต ๊ตฌ์กฐ ์ ๋ณด๋ tree ๊ตฌ์กฐ์ฒด์ ์ ์ฅ๋์ด ์๋ค.
๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ๊ณ ๋ คํ์ฌ ์์ธก๊ฐ์ ๋ณด์ ํ๋ฉด, ์์ธก ์ฑ๋ฅ์ ๊ฐ์ ํ ์ ์๋ค.
๋ณด์ ๋ ์์ธก๊ฐ์ ์ดํ ํ์ฒ๋ฆฌ ๊ณผ์ ์์ ์ฌ์ฉ๋๋ค.
hierarchy_top_prediction
int hierarchy_top_prediction(float *predictions, tree *hier, float thresh, int stride)
{
float p = 1;
int group = 0;
int i;
while(1){
float max = 0;
int max_i = 0;
for(i = 0; i < hier->group_size[group]; ++i){
int index = i + hier->group_offset[group];
float val = predictions[(i + hier->group_offset[group])*stride];
if(val > max){
max_i = index;
max = val;
}
}
if(p*max > thresh){
p = p*max;
group = hier->child[max_i];
if(hier->child[max_i] < 0) return max_i;
} else if (group == 0){
return max_i;
} else {
return hier->parent[hier->group_offset[group]];
}
}
return 0;
}
ํจ์ ์ด๋ฆ: hierarchy_top_prediction
์ ๋ ฅ:
predictions: ์์ธก๊ฐ์ ๋ด์ ์ค์ํ ๋ฐฐ์ด
hier: tree ํํ์ ๊ณ์ธต ๊ตฌ์กฐ
thresh: ์๊ณ๊ฐ
stride: ๋ฐฐ์ด์ ๊ฐ๊ฒฉ
๋์:
๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ๋ฐ๋ผ ์ต์์ ์์ธก๊ฐ์ ์ฐพ์ ๋ฐํํ๋ ํจ์์ ๋๋ค.
์์ธก๊ฐ ๋ฐฐ์ด๊ณผ ๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ์ ๋ ฅ๋ฐ์ ๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ๋ฐ๋ผ ์ต์์ ์์ธก๊ฐ์ ์ฐพ์ต๋๋ค.
๊ณ์ธต ๊ตฌ์กฐ๋ tree ํํ๋ก ํํ๋๋ฉฐ, ๊ฐ๊ฐ์ ๋ ธ๋๋ ์์ ๋ ธ๋๋ฅผ ๊ฐ์ง ์ ์์ต๋๋ค.
์๊ณ๊ฐ(thresh)๋ณด๋ค ํฐ ๊ฐ ์ค ๊ฐ์ฅ ํฐ ๊ฐ์ ๊ฐ๋ ์์ ๋ ธ๋๋ฅผ ์ฐพ์ต๋๋ค.
์ต์์ ๋ ธ๋๊น์ง ์ฐพ์ ๊ฒฝ์ฐ ํด๋น ์์ ๋ ธ๋์ ์ธ๋ฑ์ค๋ฅผ ๋ฐํํฉ๋๋ค.
์ค๋ช :
predictions ๋ฐฐ์ด์ ๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ๊ณ ๋ คํ ์์ธก๊ฐ์ ํฌํจํฉ๋๋ค.
๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ๋ฐ๋ผ ์ต์์ ์์ธก๊ฐ์ ์ฐพ์ ๋๋ ๊ฐ ๋ ธ๋์ ๊ฐ์ ๊ณฑํด๊ฐ๋ฉด์ ํ์ํฉ๋๋ค.
group_size์ group_offset์ ์ฌ์ฉํ์ฌ ๊ฐ๊ฐ์ ์์ ๋ ธ๋๋ฅผ ๊ทธ๋ฃน์ผ๋ก ๋๋์ด ์ต๋๊ฐ์ ์ฐพ์ต๋๋ค.
p * max > thresh๋ฅผ ๋ง์กฑํ๋ ๊ฒฝ์ฐ ์์ ๋ ธ๋๋ก ์ด๋ํฉ๋๋ค.
group == 0์ธ ๊ฒฝ์ฐ ์ต์์ ๋ ธ๋์ ๋๋ฌํ์์ ์๋ฏธํฉ๋๋ค.
p * max <= thresh๋ฅผ ๋ง์กฑํ๋ ๊ฒฝ์ฐ ํ์ฌ ๊ทธ๋ฃน์ ๋ถ๋ชจ ๋ ธ๋๋ก ์ด๋ํฉ๋๋ค.
๋ง์ง๋ง์ผ๋ก ๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ๋ฐ๋ผ ์ฐพ์ ์ต์์ ๋ ธ๋์ ์ธ๋ฑ์ค๋ฅผ ๋ฐํํฉ๋๋ค.
read_tree
tree *read_tree(char *filename)
{
tree t = {0};
FILE *fp = fopen(filename, "r");
char *line;
int last_parent = -1;
int group_size = 0;
int groups = 0;
int n = 0;
while((line=fgetl(fp)) != 0){
char *id = calloc(256, sizeof(char));
int parent = -1;
sscanf(line, "%s %d", id, &parent);
t.parent = realloc(t.parent, (n+1)*sizeof(int));
t.parent[n] = parent;
t.child = realloc(t.child, (n+1)*sizeof(int));
t.child[n] = -1;
t.name = realloc(t.name, (n+1)*sizeof(char *));
t.name[n] = id;
if(parent != last_parent){
++groups;
t.group_offset = realloc(t.group_offset, groups * sizeof(int));
t.group_offset[groups - 1] = n - group_size;
t.group_size = realloc(t.group_size, groups * sizeof(int));
t.group_size[groups - 1] = group_size;
group_size = 0;
last_parent = parent;
}
t.group = realloc(t.group, (n+1)*sizeof(int));
t.group[n] = groups;
if (parent >= 0) {
t.child[parent] = groups;
}
++n;
++group_size;
}
++groups;
t.group_offset = realloc(t.group_offset, groups * sizeof(int));
t.group_offset[groups - 1] = n - group_size;
t.group_size = realloc(t.group_size, groups * sizeof(int));
t.group_size[groups - 1] = group_size;
t.n = n;
t.groups = groups;
t.leaf = calloc(n, sizeof(int));
int i;
for(i = 0; i < n; ++i) t.leaf[i] = 1;
for(i = 0; i < n; ++i) if(t.parent[i] >= 0) t.leaf[t.parent[i]] = 0;
fclose(fp);
tree *tree_ptr = calloc(1, sizeof(tree));
*tree_ptr = t;
//error(0);
return tree_ptr;
}
ํจ์ ์ด๋ฆ: read_tree
์ ๋ ฅ:
char *filename: ํธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ์ ์ฅํ ํ์ผ ์ด๋ฆ
๋์:
์ ๋ ฅ ํ์ผ์์ ํธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ์ฝ์ด๋ค์ด๊ณ , ํด๋น ํธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ํํํ๋ tree ๊ตฌ์กฐ์ฒด๋ฅผ ์์ฑํ๋ค.
์ค๋ช :
์ ๋ ฅ ํ์ผ์์ ํธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ์ฝ์ด๋ค์ด๋ ๊ณผ์ ์์๋, ๊ฐ ๋ ธ๋์ ์ด๋ฆ๊ณผ ๋ถ๋ชจ ๋ ธ๋์ ์ธ๋ฑ์ค๋ฅผ ์ฝ์ด๋ค์ด๊ณ , ์ด๋ฅผ ์ด์ฉํ์ฌ parent, child, name, group, group_offset, group_size, leaf ๋ฑ์ ํ๋๋ฅผ ์ด๊ธฐํํ๋ค.
์ดํ ์์ฑ๋ tree ๊ตฌ์กฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ ํฌ์ธํฐ๋ฅผ ๋ฐํํ๋ค.
Last updated
Was this helpful?