darknet  v3
tree.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "tree.h"
4 #include "utils.h"
5 #include "data.h"
6 
7 void change_leaves(tree *t, char *leaf_list)
8 {
9  list *llist = get_paths(leaf_list);
10  char **leaves = (char **)list_to_array(llist);
11  int n = llist->size;
12  int i,j;
13  int found = 0;
14  for(i = 0; i < t->n; ++i){
15  t->leaf[i] = 0;
16  for(j = 0; j < n; ++j){
17  if (0==strcmp(t->name[i], leaves[j])){
18  t->leaf[i] = 1;
19  ++found;
20  break;
21  }
22  }
23  }
24  fprintf(stderr, "Found %d leaves.\n", found);
25 }
26 
27 float get_hierarchy_probability(float *x, tree *hier, int c, int stride)
28 {
29  float p = 1;
30  while(c >= 0){
31  p = p * x[c*stride];
32  c = hier->parent[c];
33  }
34  return p;
35 }
36 
37 void hierarchy_predictions(float *predictions, int n, tree *hier, int only_leaves, int stride)
38 {
39  int j;
40  for(j = 0; j < n; ++j){
41  int parent = hier->parent[j];
42  if(parent >= 0){
43  predictions[j*stride] *= predictions[parent*stride];
44  }
45  }
46  if(only_leaves){
47  for(j = 0; j < n; ++j){
48  if(!hier->leaf[j]) predictions[j*stride] = 0;
49  }
50  }
51 }
52 
53 int hierarchy_top_prediction(float *predictions, tree *hier, float thresh, int stride)
54 {
55  float p = 1;
56  int group = 0;
57  int i;
58  while(1){
59  float max = 0;
60  int max_i = 0;
61 
62  for(i = 0; i < hier->group_size[group]; ++i){
63  int index = i + hier->group_offset[group];
64  float val = predictions[(i + hier->group_offset[group])*stride];
65  if(val > max){
66  max_i = index;
67  max = val;
68  }
69  }
70  if(p*max > thresh){
71  p = p*max;
72  group = hier->child[max_i];
73  if(hier->child[max_i] < 0) return max_i;
74  } else if (group == 0){
75  return max_i;
76  } else {
77  return hier->parent[hier->group_offset[group]];
78  }
79  }
80  return 0;
81 }
82 
83 tree *read_tree(char *filename)
84 {
85  tree t = {0};
86  FILE *fp = fopen(filename, "r");
87 
88  char *line;
89  int last_parent = -1;
90  int group_size = 0;
91  int groups = 0;
92  int n = 0;
93  while((line=fgetl(fp)) != 0){
94  char *id = calloc(256, sizeof(char));
95  int parent = -1;
96  sscanf(line, "%s %d", id, &parent);
97  t.parent = realloc(t.parent, (n+1)*sizeof(int));
98  t.parent[n] = parent;
99 
100  t.child = realloc(t.child, (n+1)*sizeof(int));
101  t.child[n] = -1;
102 
103  t.name = realloc(t.name, (n+1)*sizeof(char *));
104  t.name[n] = id;
105  if(parent != last_parent){
106  ++groups;
107  t.group_offset = realloc(t.group_offset, groups * sizeof(int));
108  t.group_offset[groups - 1] = n - group_size;
109  t.group_size = realloc(t.group_size, groups * sizeof(int));
110  t.group_size[groups - 1] = group_size;
111  group_size = 0;
112  last_parent = parent;
113  }
114  t.group = realloc(t.group, (n+1)*sizeof(int));
115  t.group[n] = groups;
116  if (parent >= 0) {
117  t.child[parent] = groups;
118  }
119  ++n;
120  ++group_size;
121  }
122  ++groups;
123  t.group_offset = realloc(t.group_offset, groups * sizeof(int));
124  t.group_offset[groups - 1] = n - group_size;
125  t.group_size = realloc(t.group_size, groups * sizeof(int));
126  t.group_size[groups - 1] = group_size;
127  t.n = n;
128  t.groups = groups;
129  t.leaf = calloc(n, sizeof(int));
130  int i;
131  for(i = 0; i < n; ++i) t.leaf[i] = 1;
132  for(i = 0; i < n; ++i) if(t.parent[i] >= 0) t.leaf[t.parent[i]] = 0;
133 
134  fclose(fp);
135  tree *tree_ptr = calloc(1, sizeof(tree));
136  *tree_ptr = t;
137  //error(0);
138  return tree_ptr;
139 }
int hierarchy_top_prediction(float *predictions, tree *hier, float thresh, int stride)
Definition: tree.c:53
int * parent
Definition: darknet.h:45
void ** list_to_array(list *l)
Definition: list.c:82
int size
Definition: darknet.h:603
int * child
Definition: darknet.h:46
char ** name
Definition: darknet.h:48
int * group
Definition: darknet.h:47
int * group_offset
Definition: darknet.h:52
void hierarchy_predictions(float *predictions, int n, tree *hier, int only_leaves, int stride)
Definition: tree.c:37
Definition: darknet.h:42
int * leaf
Definition: darknet.h:43
int groups
Definition: darknet.h:50
char * fgetl(FILE *fp)
Definition: utils.c:335
void change_leaves(tree *t, char *leaf_list)
Definition: tree.c:7
int * group_size
Definition: darknet.h:51
Definition: darknet.h:602
tree * read_tree(char *filename)
Definition: tree.c:83
list * get_paths(char *filename)
Definition: data.c:12
int n
Definition: darknet.h:44
float get_hierarchy_probability(float *x, tree *hier, int c, int stride)
Definition: tree.c:27