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?