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.

283 lines
8.5 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "detection_layer.h"
  2. #include "activations.h"
  3. #include "softmax_layer.h"
  4. #include "blas.h"
  5. #include "box.h"
  6. #include "dn_cuda.h"
  7. #include "utils.h"
  8. #include <stdio.h>
  9. #include <assert.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. namespace DN{
  13. detection_layer make_detection_layer(int batch, int inputs, int n, int side, int classes, int coords, int rescore)
  14. {
  15. detection_layer l;
  16. memset(&l, 0, sizeof(detection_layer));
  17. l.type = DETECTION;
  18. l.n = n;
  19. l.batch = batch;
  20. l.inputs = inputs;
  21. l.classes = classes;
  22. l.coords = coords;
  23. l.rescore = rescore;
  24. l.side = side;
  25. l.w = side;
  26. l.h = side;
  27. assert(side*side*((1 + l.coords)*l.n + l.classes) == inputs);
  28. l.cost = (float*)calloc(1, sizeof(float));
  29. l.outputs = l.inputs;
  30. l.truths = l.side*l.side*(1 + l.coords + l.classes);
  31. l.output = (float*)calloc(batch*l.outputs, sizeof(float));
  32. l.delta = (float*)calloc(batch*l.outputs, sizeof(float));
  33. l.forward = forward_detection_layer;
  34. l.backward = backward_detection_layer;
  35. #ifdef GPU
  36. l.forward_gpu = forward_detection_layer_gpu;
  37. l.backward_gpu = backward_detection_layer_gpu;
  38. l.output_gpu = cuda_make_array(l.output, batch*l.outputs);
  39. l.delta_gpu = cuda_make_array(l.delta, batch*l.outputs);
  40. #endif
  41. fprintf(stderr, "Detection Layer\n");
  42. srand(0);
  43. return l;
  44. }
  45. void forward_detection_layer(const detection_layer l, network net)
  46. {
  47. int locations = l.side*l.side;
  48. int i, j;
  49. memcpy(l.output, net.input, l.outputs*l.batch*sizeof(float));
  50. //if(l.reorg) reorg(l.output, l.w*l.h, size*l.n, l.batch, 1);
  51. int b;
  52. if (l.softmax){
  53. for (b = 0; b < l.batch; ++b){
  54. int index = b*l.inputs;
  55. for (i = 0; i < locations; ++i) {
  56. int offset = i*l.classes;
  57. softmax(l.output + index + offset, l.classes, 1, 1,
  58. l.output + index + offset);
  59. }
  60. }
  61. }
  62. if (net.train){
  63. float avg_iou = 0;
  64. float avg_cat = 0;
  65. float avg_allcat = 0;
  66. float avg_obj = 0;
  67. float avg_anyobj = 0;
  68. int count = 0;
  69. *(l.cost) = 0;
  70. int size = l.inputs * l.batch;
  71. memset(l.delta, 0, size * sizeof(float));
  72. for (b = 0; b < l.batch; ++b){
  73. int index = b*l.inputs;
  74. for (i = 0; i < locations; ++i) {
  75. int truth_index = (b*locations + i)*(1 + l.coords + l.classes);
  76. int is_obj = (int)net.truth[truth_index];
  77. for (j = 0; j < l.n; ++j) {
  78. int p_index = index + locations*l.classes + i*l.n + j;
  79. l.delta[p_index] = l.noobject_scale*(0 - l.output[p_index]);
  80. *(l.cost) += l.noobject_scale*pow(l.output[p_index], 2);
  81. avg_anyobj += l.output[p_index];
  82. }
  83. int best_index = -1;
  84. float best_iou = 0;
  85. float best_rmse = 20;
  86. if (!is_obj){
  87. continue;
  88. }
  89. int class_index = index + i*l.classes;
  90. for (j = 0; j < l.classes; ++j) {
  91. l.delta[class_index + j] = l.class_scale * (net.truth[truth_index + 1 + j] - l.output[class_index + j]);
  92. *(l.cost) += l.class_scale * pow(net.truth[truth_index + 1 + j] - l.output[class_index + j], 2);
  93. if (net.truth[truth_index + 1 + j]) avg_cat += l.output[class_index + j];
  94. avg_allcat += l.output[class_index + j];
  95. }
  96. box truth = float_to_box(net.truth + truth_index + 1 + l.classes, 1);
  97. truth.x /= l.side;
  98. truth.y /= l.side;
  99. for (j = 0; j < l.n; ++j){
  100. int box_index = index + locations*(l.classes + l.n) + (i*l.n + j) * l.coords;
  101. box out = float_to_box(l.output + box_index, 1);
  102. out.x /= l.side;
  103. out.y /= l.side;
  104. if (l.sqrt){
  105. out.w = out.w*out.w;
  106. out.h = out.h*out.h;
  107. }
  108. float iou = box_iou(out, truth);
  109. //iou = 0;
  110. float rmse = box_rmse(out, truth);
  111. if (best_iou > 0 || iou > 0){
  112. if (iou > best_iou){
  113. best_iou = iou;
  114. best_index = j;
  115. }
  116. }
  117. else{
  118. if (rmse < best_rmse){
  119. best_rmse = rmse;
  120. best_index = j;
  121. }
  122. }
  123. }
  124. if (l.forced){
  125. if (truth.w*truth.h < .1){
  126. best_index = 1;
  127. }
  128. else{
  129. best_index = 0;
  130. }
  131. }
  132. if (l.random && *(net.seen) < 64000){
  133. best_index = rand() % l.n;
  134. }
  135. int box_index = index + locations*(l.classes + l.n) + (i*l.n + best_index) * l.coords;
  136. int tbox_index = truth_index + 1 + l.classes;
  137. box out = float_to_box(l.output + box_index, 1);
  138. out.x /= l.side;
  139. out.y /= l.side;
  140. if (l.sqrt) {
  141. out.w = out.w*out.w;
  142. out.h = out.h*out.h;
  143. }
  144. float iou = box_iou(out, truth);
  145. //printf("%d,", best_index);
  146. int p_index = index + locations*l.classes + i*l.n + best_index;
  147. *(l.cost) -= l.noobject_scale * pow(l.output[p_index], 2);
  148. *(l.cost) += l.object_scale * pow(1 - l.output[p_index], 2);
  149. avg_obj += l.output[p_index];
  150. l.delta[p_index] = (float)(l.object_scale * (1. - l.output[p_index]));
  151. if (l.rescore){
  152. l.delta[p_index] = l.object_scale * (iou - l.output[p_index]);
  153. }
  154. l.delta[box_index + 0] = l.coord_scale*(net.truth[tbox_index + 0] - l.output[box_index + 0]);
  155. l.delta[box_index + 1] = l.coord_scale*(net.truth[tbox_index + 1] - l.output[box_index + 1]);
  156. l.delta[box_index + 2] = l.coord_scale*(net.truth[tbox_index + 2] - l.output[box_index + 2]);
  157. l.delta[box_index + 3] = l.coord_scale*(net.truth[tbox_index + 3] - l.output[box_index + 3]);
  158. if (l.sqrt){
  159. l.delta[box_index + 2] = l.coord_scale*(sqrt(net.truth[tbox_index + 2]) - l.output[box_index + 2]);
  160. l.delta[box_index + 3] = l.coord_scale*(sqrt(net.truth[tbox_index + 3]) - l.output[box_index + 3]);
  161. }
  162. *(l.cost) += pow(1 - iou, 2);
  163. avg_iou += iou;
  164. ++count;
  165. }
  166. }
  167. if (0){
  168. float *costs = (float*)calloc(l.batch*locations*l.n, sizeof(float));
  169. for (b = 0; b < l.batch; ++b) {
  170. int index = b*l.inputs;
  171. for (i = 0; i < locations; ++i) {
  172. for (j = 0; j < l.n; ++j) {
  173. int p_index = index + locations*l.classes + i*l.n + j;
  174. costs[b*locations*l.n + i*l.n + j] = l.delta[p_index] * l.delta[p_index];
  175. }
  176. }
  177. }
  178. int indexes[100];
  179. top_k(costs, l.batch*locations*l.n, 100, indexes);
  180. float cutoff = costs[indexes[99]];
  181. for (b = 0; b < l.batch; ++b) {
  182. int index = b*l.inputs;
  183. for (i = 0; i < locations; ++i) {
  184. for (j = 0; j < l.n; ++j) {
  185. int p_index = index + locations*l.classes + i*l.n + j;
  186. if (l.delta[p_index] * l.delta[p_index] < cutoff) l.delta[p_index] = 0;
  187. }
  188. }
  189. }
  190. free(costs);
  191. }
  192. *(l.cost) = pow(mag_array(l.delta, l.outputs * l.batch), 2);
  193. printf("Detection Avg IOU: %f, Pos Cat: %f, All Cat: %f, Pos Obj: %f, Any Obj: %f, count: %d\n", avg_iou / count, avg_cat / count, avg_allcat / (count*l.classes), avg_obj / count, avg_anyobj / (l.batch*locations*l.n), count);
  194. //if(l.reorg) reorg(l.delta, l.w*l.h, size*l.n, l.batch, 0);
  195. }
  196. }
  197. void backward_detection_layer(const detection_layer l, network net)
  198. {
  199. axpy_cpu(l.batch*l.inputs, 1, l.delta, 1, net.delta, 1);
  200. }
  201. void get_detection_boxes(layer l, int w, int h, float thresh, float **probs, box *boxes, int only_objectness)
  202. {
  203. int i, j, n;
  204. float *predictions = l.output;
  205. //int per_cell = 5*num+classes;
  206. for (i = 0; i < l.side*l.side; ++i){
  207. int row = i / l.side;
  208. int col = i % l.side;
  209. for (n = 0; n < l.n; ++n){
  210. int index = i*l.n + n;
  211. int p_index = l.side*l.side*l.classes + i*l.n + n;
  212. float scale = predictions[p_index];
  213. int box_index = l.side*l.side*(l.classes + l.n) + (i*l.n + n) * 4;
  214. boxes[index].x = (predictions[box_index + 0] + col) / l.side * w;
  215. boxes[index].y = (predictions[box_index + 1] + row) / l.side * h;
  216. boxes[index].w = pow(predictions[box_index + 2], (l.sqrt ? 2 : 1)) * w;
  217. boxes[index].h = pow(predictions[box_index + 3], (l.sqrt ? 2 : 1)) * h;
  218. for (j = 0; j < l.classes; ++j){
  219. int class_index = i*l.classes;
  220. float prob = scale*predictions[class_index + j];
  221. probs[index][j] = (prob > thresh) ? prob : 0;
  222. }
  223. if (only_objectness){
  224. probs[index][0] = scale;
  225. }
  226. }
  227. }
  228. }
  229. #ifdef GPU
  230. void forward_detection_layer_gpu(const detection_layer l, network net)
  231. {
  232. if(!net.train){
  233. copy_gpu(l.batch*l.inputs, net.input_gpu, 1, l.output_gpu, 1);
  234. return;
  235. }
  236. //float *in_cpu = calloc(l.batch*l.inputs, sizeof(float));
  237. //float *truth_cpu = 0;
  238. forward_detection_layer(l, net);
  239. cuda_push_array(l.output_gpu, l.output, l.batch*l.outputs);
  240. cuda_push_array(l.delta_gpu, l.delta, l.batch*l.inputs);
  241. }
  242. void backward_detection_layer_gpu(detection_layer l, network net)
  243. {
  244. axpy_gpu(l.batch*l.inputs, 1, l.delta_gpu, 1, net.delta_gpu, 1);
  245. //copy_gpu(l.batch*l.inputs, l.delta_gpu, 1, net.delta_gpu, 1);
  246. }
  247. #endif
  248. };