You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.3 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "tree.h"
  4. #include "utils.h"
  5. #include "data.h"
  6. namespace DN{
  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. float get_hierarchy_probability(float *x, tree *hier, int c, int stride)
  27. {
  28. float p = 1;
  29. while (c >= 0){
  30. p = p * x[c*stride];
  31. c = hier->parent[c];
  32. }
  33. return p;
  34. }
  35. void hierarchy_predictions(float *predictions, int n, tree *hier, int only_leaves, int stride)
  36. {
  37. int j;
  38. for (j = 0; j < n; ++j){
  39. int parent = hier->parent[j];
  40. if (parent >= 0){
  41. predictions[j*stride] *= predictions[parent*stride];
  42. }
  43. }
  44. if (only_leaves){
  45. for (j = 0; j < n; ++j){
  46. if (!hier->leaf[j]) predictions[j*stride] = 0;
  47. }
  48. }
  49. }
  50. int hierarchy_top_prediction(float *predictions, tree *hier, float thresh, int stride)
  51. {
  52. float p = 1;
  53. int group = 0;
  54. int i;
  55. while (1){
  56. float max = 0;
  57. int max_i = 0;
  58. for (i = 0; i < hier->group_size[group]; ++i){
  59. int index = i + hier->group_offset[group];
  60. float val = predictions[(i + hier->group_offset[group])*stride];
  61. if (val > max){
  62. max_i = index;
  63. max = val;
  64. }
  65. }
  66. if (p*max > thresh){
  67. p = p*max;
  68. group = hier->child[max_i];
  69. if (hier->child[max_i] < 0) return max_i;
  70. }
  71. else {
  72. return hier->parent[hier->group_offset[group]];
  73. }
  74. }
  75. return 0;
  76. }
  77. tree *read_tree(char *filename)
  78. {
  79. tree t = { 0 };
  80. FILE *fp = fopen(filename, "r");
  81. char *line;
  82. int last_parent = -1;
  83. int group_size = 0;
  84. int groups = 0;
  85. int n = 0;
  86. while ((line = fgetl(fp)) != 0){
  87. char *id = (char*)calloc(256, sizeof(char));
  88. int parent = -1;
  89. sscanf(line, "%s %d", id, &parent);
  90. t.parent = (int*)realloc(t.parent, (n + 1)*sizeof(int));
  91. t.parent[n] = parent;
  92. t.child = (int*)realloc(t.child, (n + 1)*sizeof(int));
  93. t.child[n] = -1;
  94. t.name = (char**)realloc(t.name, (n + 1)*sizeof(char *));
  95. t.name[n] = id;
  96. if (parent != last_parent){
  97. ++groups;
  98. t.group_offset = (int*)realloc(t.group_offset, groups * sizeof(int));
  99. t.group_offset[groups - 1] = n - group_size;
  100. t.group_size = (int*)realloc(t.group_size, groups * sizeof(int));
  101. t.group_size[groups - 1] = group_size;
  102. group_size = 0;
  103. last_parent = parent;
  104. }
  105. t.group = (int*)realloc(t.group, (n + 1)*sizeof(int));
  106. t.group[n] = groups;
  107. if (parent >= 0) {
  108. t.child[parent] = groups;
  109. }
  110. ++n;
  111. ++group_size;
  112. }
  113. ++groups;
  114. t.group_offset = (int*)realloc(t.group_offset, groups * sizeof(int));
  115. t.group_offset[groups - 1] = n - group_size;
  116. t.group_size = (int*)realloc(t.group_size, groups * sizeof(int));
  117. t.group_size[groups - 1] = group_size;
  118. t.n = n;
  119. t.groups = groups;
  120. t.leaf = (int*)calloc(n, sizeof(int));
  121. int i;
  122. for (i = 0; i < n; ++i) t.leaf[i] = 1;
  123. for (i = 0; i < n; ++i) if (t.parent[i] >= 0) t.leaf[t.parent[i]] = 0;
  124. fclose(fp);
  125. tree *tree_ptr = (tree*)calloc(1, sizeof(tree));
  126. *tree_ptr = t;
  127. //error(0);
  128. return tree_ptr;
  129. }
  130. };