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.

177 lines
4.6 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "cost_layer.h"
  2. #include "utils.h"
  3. #include "dn_cuda.h"
  4. #include "blas.h"
  5. #include <math.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. namespace DN{
  10. COST_TYPE get_cost_type(char *s)
  11. {
  12. if (strcmp(s, "seg") == 0) return SEG;
  13. if (strcmp(s, "sse") == 0) return SSE;
  14. if (strcmp(s, "masked") == 0) return MASKED;
  15. if (strcmp(s, "smooth") == 0) return SMOOTH;
  16. if (strcmp(s, "L1") == 0) return L1;
  17. fprintf(stderr, "Couldn't find cost type %s, going with SSE\n", s);
  18. return SSE;
  19. }
  20. char *get_cost_string(COST_TYPE a)
  21. {
  22. switch (a){
  23. case SEG:
  24. return "seg";
  25. case SSE:
  26. return "sse";
  27. case MASKED:
  28. return "masked";
  29. case SMOOTH:
  30. return "smooth";
  31. case L1:
  32. return "L1";
  33. }
  34. return "sse";
  35. }
  36. cost_layer make_cost_layer(int batch, int inputs, COST_TYPE cost_type, float scale)
  37. {
  38. fprintf(stderr, "cost %4d\n", inputs);
  39. cost_layer l;
  40. memset(&l, 0, sizeof(cost_layer));
  41. l.type = COST;
  42. l.scale = scale;
  43. l.batch = batch;
  44. l.inputs = inputs;
  45. l.outputs = inputs;
  46. l.cost_type = cost_type;
  47. l.delta = (float*)calloc(inputs*batch, sizeof(float));
  48. l.output = (float*)calloc(inputs*batch, sizeof(float));
  49. l.cost = (float*)calloc(1, sizeof(float));
  50. l.forward = forward_cost_layer;
  51. l.backward = backward_cost_layer;
  52. #ifdef GPU
  53. l.forward_gpu = forward_cost_layer_gpu;
  54. l.backward_gpu = backward_cost_layer_gpu;
  55. l.delta_gpu = cuda_make_array(l.output, inputs*batch);
  56. l.output_gpu = cuda_make_array(l.delta, inputs*batch);
  57. #endif
  58. return l;
  59. }
  60. void resize_cost_layer(cost_layer *l, int inputs)
  61. {
  62. l->inputs = inputs;
  63. l->outputs = inputs;
  64. l->delta = (float*)realloc(l->delta, inputs*l->batch*sizeof(float));
  65. l->output = (float*)realloc(l->output, inputs*l->batch*sizeof(float));
  66. #ifdef GPU
  67. cuda_free(l->delta_gpu);
  68. cuda_free(l->output_gpu);
  69. l->delta_gpu = cuda_make_array(l->delta, inputs*l->batch);
  70. l->output_gpu = cuda_make_array(l->output, inputs*l->batch);
  71. #endif
  72. }
  73. void forward_cost_layer(cost_layer l, network net)
  74. {
  75. if (!net.truth) return;
  76. if (l.cost_type == MASKED){
  77. int i;
  78. for (i = 0; i < l.batch*l.inputs; ++i){
  79. if (net.truth[i] == SECRET_NUM) net.input[i] = SECRET_NUM;
  80. }
  81. }
  82. if (l.cost_type == SMOOTH){
  83. smooth_l1_cpu(l.batch*l.inputs, net.input, net.truth, l.delta, l.output);
  84. }
  85. else if (l.cost_type == L1){
  86. l1_cpu(l.batch*l.inputs, net.input, net.truth, l.delta, l.output);
  87. }
  88. else {
  89. l2_cpu(l.batch*l.inputs, net.input, net.truth, l.delta, l.output);
  90. }
  91. l.cost[0] = sum_array(l.output, l.batch*l.inputs);
  92. }
  93. void backward_cost_layer(const cost_layer l, network net)
  94. {
  95. axpy_cpu(l.batch*l.inputs, l.scale, l.delta, 1, net.delta, 1);
  96. }
  97. #ifdef GPU
  98. void pull_cost_layer(cost_layer l)
  99. {
  100. cuda_pull_array(l.delta_gpu, l.delta, l.batch*l.inputs);
  101. }
  102. void push_cost_layer(cost_layer l)
  103. {
  104. cuda_push_array(l.delta_gpu, l.delta, l.batch*l.inputs);
  105. }
  106. int float_abs_compare (const void * a, const void * b)
  107. {
  108. float fa = *(const float*) a;
  109. if(fa < 0) fa = -fa;
  110. float fb = *(const float*) b;
  111. if(fb < 0) fb = -fb;
  112. return (fa > fb) - (fa < fb);
  113. }
  114. void forward_cost_layer_gpu(cost_layer l, network net)
  115. {
  116. if (!net.truth_gpu) return;
  117. if(l.smooth){
  118. scal_gpu(l.batch*l.inputs, (1-l.smooth), net.truth_gpu, 1);
  119. add_gpu(l.batch*l.inputs, l.smooth * 1./l.inputs, net.truth_gpu, 1);
  120. }
  121. if (l.cost_type == MASKED) {
  122. mask_gpu(l.batch*l.inputs, net.input_gpu, SECRET_NUM, net.truth_gpu);
  123. }
  124. if(l.cost_type == SMOOTH){
  125. smooth_l1_gpu(l.batch*l.inputs, net.input_gpu, net.truth_gpu, l.delta_gpu, l.output_gpu);
  126. } else if (l.cost_type == L1){
  127. l1_gpu(l.batch*l.inputs, net.input_gpu, net.truth_gpu, l.delta_gpu, l.output_gpu);
  128. } else {
  129. l2_gpu(l.batch*l.inputs, net.input_gpu, net.truth_gpu, l.delta_gpu, l.output_gpu);
  130. }
  131. if (l.cost_type == SEG && l.noobject_scale != 1) {
  132. scale_mask_gpu(l.batch*l.inputs, l.delta_gpu, 0, net.truth_gpu, l.noobject_scale);
  133. scale_mask_gpu(l.batch*l.inputs, l.output_gpu, 0, net.truth_gpu, l.noobject_scale);
  134. }
  135. if(l.ratio){
  136. cuda_pull_array(l.delta_gpu, l.delta, l.batch*l.inputs);
  137. qsort(l.delta, l.batch*l.inputs, sizeof(float), float_abs_compare);
  138. int n = (1-l.ratio) * l.batch*l.inputs;
  139. float thresh = l.delta[n];
  140. thresh = 0;
  141. printf("%f\n", thresh);
  142. supp_gpu(l.batch*l.inputs, thresh, l.delta_gpu, 1);
  143. }
  144. if(l.thresh){
  145. supp_gpu(l.batch*l.inputs, l.thresh*1./l.inputs, l.delta_gpu, 1);
  146. }
  147. cuda_pull_array(l.output_gpu, l.output, l.batch*l.inputs);
  148. l.cost[0] = sum_array(l.output, l.batch*l.inputs);
  149. }
  150. void backward_cost_layer_gpu(const cost_layer l, network net)
  151. {
  152. axpy_gpu(l.batch*l.inputs, l.scale, l.delta_gpu, 1, net.delta_gpu, 1);
  153. }
  154. #endif
  155. };