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.

600 lines
19 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "region_layer.h"
  2. #include "activations.h"
  3. #include "blas.h"
  4. #include "box.h"
  5. #include "dn_cuda.h"
  6. #include "utils.h"
  7. #include <stdio.h>
  8. #include <assert.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. namespace DN{
  12. layer make_region_layer(int batch, int w, int h, int n, int classes, int coords)
  13. {
  14. layer l;
  15. memset(&l, 0, sizeof(layer));
  16. l.type = REGION;
  17. l.n = n;
  18. l.batch = batch;
  19. l.h = h;
  20. l.w = w;
  21. l.c = n*(classes + coords + 1);
  22. l.out_w = l.w;
  23. l.out_h = l.h;
  24. l.out_c = l.c;
  25. l.classes = classes;
  26. l.coords = coords;
  27. l.cost = (float*)calloc(1, sizeof(float));
  28. l.biases = (float*)calloc(n * 2, sizeof(float));
  29. l.bias_updates = (float*)calloc(n * 2, sizeof(float));
  30. l.outputs = h*w*n*(classes + coords + 1);
  31. l.inputs = l.outputs;
  32. l.truths = 30 * (l.coords + 1);
  33. l.delta = (float*)calloc(batch*l.outputs, sizeof(float));
  34. l.output = (float*)calloc(batch*l.outputs, sizeof(float));
  35. int i;
  36. for (i = 0; i < n * 2; ++i){
  37. l.biases[i] = .5;
  38. }
  39. l.forward = forward_region_layer;
  40. l.backward = backward_region_layer;
  41. #ifdef GPU
  42. l.forward_gpu = forward_region_layer_gpu;
  43. l.backward_gpu = backward_region_layer_gpu;
  44. l.output_gpu = cuda_make_array(l.output, batch*l.outputs);
  45. l.delta_gpu = cuda_make_array(l.delta, batch*l.outputs);
  46. #endif
  47. fprintf(stderr, "detection\n");
  48. srand(0);
  49. return l;
  50. }
  51. void resize_region_layer(layer *l, int w, int h)
  52. {
  53. l->w = w;
  54. l->h = h;
  55. l->outputs = h*w*l->n*(l->classes + l->coords + 1);
  56. l->inputs = l->outputs;
  57. l->output = (float*)realloc(l->output, l->batch*l->outputs*sizeof(float));
  58. l->delta = (float*)realloc(l->delta, l->batch*l->outputs*sizeof(float));
  59. #ifdef GPU
  60. cuda_free(l->delta_gpu);
  61. cuda_free(l->output_gpu);
  62. l->delta_gpu = cuda_make_array(l->delta, l->batch*l->outputs);
  63. l->output_gpu = cuda_make_array(l->output, l->batch*l->outputs);
  64. #endif
  65. }
  66. box get_region_box(float *x, float *biases, int n, int index, int i, int j, int w, int h, int stride)
  67. {
  68. box b;
  69. b.x = (i + x[index + 0 * stride]) / w;
  70. b.y = (j + x[index + 1 * stride]) / h;
  71. b.w = (float)exp(x[index + 2 * stride]) * biases[2 * n] / w;
  72. b.h = (float)exp(x[index + 3 * stride]) * biases[2 * n + 1] / h;
  73. return b;
  74. }
  75. float delta_region_box(box truth, float *x, float *biases, int n, int index, int i, int j, int w, int h, float *delta, float scale, int stride)
  76. {
  77. box pred = get_region_box(x, biases, n, index, i, j, w, h, stride);
  78. float iou = box_iou(pred, truth);
  79. float tx = (truth.x*w - i);
  80. float ty = (truth.y*h - j);
  81. float tw = (float)log(truth.w*w / biases[2 * n]);
  82. float th = (float)log(truth.h*h / biases[2 * n + 1]);
  83. delta[index + 0 * stride] = scale * (tx - x[index + 0 * stride]);
  84. delta[index + 1 * stride] = scale * (ty - x[index + 1 * stride]);
  85. delta[index + 2 * stride] = scale * (tw - x[index + 2 * stride]);
  86. delta[index + 3 * stride] = scale * (th - x[index + 3 * stride]);
  87. return iou;
  88. }
  89. void delta_region_mask(float *truth, float *x, int n, int index, float *delta, int stride, int scale)
  90. {
  91. int i;
  92. for (i = 0; i < n; ++i){
  93. delta[index + i*stride] = scale*(truth[i] - x[index + i*stride]);
  94. }
  95. }
  96. void delta_region_class(float *output, float *delta, int index, int cls, int classes, tree *hier, float scale, int stride, float *avg_cat)
  97. {
  98. int i, n;
  99. if (hier){
  100. float pred = 1;
  101. while (cls >= 0){
  102. pred *= output[index + stride*cls];
  103. int g = hier->group[cls];
  104. int offset = hier->group_offset[g];
  105. for (i = 0; i < hier->group_size[g]; ++i){
  106. delta[index + stride*(offset + i)] = scale * (0 - output[index + stride*(offset + i)]);
  107. }
  108. delta[index + stride*cls] = scale * (1 - output[index + stride*cls]);
  109. cls = hier->parent[cls];
  110. }
  111. *avg_cat += pred;
  112. }
  113. else {
  114. for (n = 0; n < classes; ++n){
  115. delta[index + stride*n] = scale * (((n == cls) ? 1 : 0) - output[index + stride*n]);
  116. if (n == cls) *avg_cat += output[index + stride*n];
  117. }
  118. }
  119. }
  120. float logit(float x)
  121. {
  122. return (float)log(x / (1. - x));
  123. }
  124. float tisnan(float x)
  125. {
  126. return (float)(x != x);
  127. }
  128. int entry_index(layer l, int batch, int location, int entry)
  129. {
  130. int n = location / (l.w*l.h);
  131. int loc = location % (l.w*l.h);
  132. return batch*l.outputs + n*l.w*l.h*(l.coords + l.classes + 1) + entry*l.w*l.h + loc;
  133. }
  134. void forward_region_layer(const layer l, network net)
  135. {
  136. int i, j, b, t, n;
  137. memcpy(l.output, net.input, l.outputs*l.batch*sizeof(float));
  138. #ifndef GPU
  139. for (b = 0; b < l.batch; ++b){
  140. for (n = 0; n < l.n; ++n){
  141. int index = entry_index(l, b, n*l.w*l.h, 0);
  142. activate_array(l.output + index, 2 * l.w*l.h, LOGISTIC);
  143. index = entry_index(l, b, n*l.w*l.h, l.coords);
  144. if (!l.background) activate_array(l.output + index, l.w*l.h, LOGISTIC);
  145. }
  146. }
  147. if (l.softmax_tree){
  148. int i;
  149. int count = l.coords + 1;
  150. for (i = 0; i < l.softmax_tree->groups; ++i) {
  151. int group_size = l.softmax_tree->group_size[i];
  152. softmax_cpu(net.input + count, group_size, l.batch, l.inputs, l.n*l.w*l.h, 1, l.n*l.w*l.h, l.temperature, l.output + count);
  153. count += group_size;
  154. }
  155. }
  156. else if (l.softmax){
  157. int index = entry_index(l, 0, 0, l.coords + !l.background);
  158. softmax_cpu(net.input + index, l.classes + l.background, l.batch*l.n, l.inputs / l.n, l.w*l.h, 1, l.w*l.h, 1, l.output + index);
  159. }
  160. #endif
  161. memset(l.delta, 0, l.outputs * l.batch * sizeof(float));
  162. if (!net.train) return;
  163. float avg_iou = 0;
  164. float recall = 0;
  165. float avg_cat = 0;
  166. float avg_obj = 0;
  167. float avg_anyobj = 0;
  168. int count = 0;
  169. int class_count = 0;
  170. *(l.cost) = 0;
  171. for (b = 0; b < l.batch; ++b) {
  172. if (l.softmax_tree){
  173. int onlyclass = 0;
  174. for (t = 0; t < 30; ++t){
  175. box truth = float_to_box(net.truth + t*(l.coords + 1) + b*l.truths, 1);
  176. if (!truth.x) break;
  177. int cls = (int)net.truth[t*(l.coords + 1) + b*l.truths + l.coords];
  178. float maxp = 0;
  179. int maxi = 0;
  180. if (truth.x > 100000 && truth.y > 100000){
  181. for (n = 0; n < l.n*l.w*l.h; ++n){
  182. int class_index = entry_index(l, b, n, l.coords + 1);
  183. int obj_index = entry_index(l, b, n, l.coords);
  184. float scale = l.output[obj_index];
  185. l.delta[obj_index] = l.noobject_scale * (0 - l.output[obj_index]);
  186. float p = scale*get_hierarchy_probability(l.output + class_index, l.softmax_tree, cls, l.w*l.h);
  187. if (p > maxp){
  188. maxp = p;
  189. maxi = n;
  190. }
  191. }
  192. int class_index = entry_index(l, b, maxi, l.coords + 1);
  193. int obj_index = entry_index(l, b, maxi, l.coords);
  194. delta_region_class(l.output, l.delta, class_index, cls, l.classes, l.softmax_tree, l.class_scale, l.w*l.h, &avg_cat);
  195. if (l.output[obj_index] < (float).3) l.delta[obj_index] = (float)(l.object_scale * (.3 - l.output[obj_index]));
  196. else l.delta[obj_index] = (float)0;
  197. l.delta[obj_index] = 0;
  198. ++class_count;
  199. onlyclass = 1;
  200. break;
  201. }
  202. }
  203. if (onlyclass) continue;
  204. }
  205. for (j = 0; j < l.h; ++j) {
  206. for (i = 0; i < l.w; ++i) {
  207. for (n = 0; n < l.n; ++n) {
  208. int box_index = entry_index(l, b, n*l.w*l.h + j*l.w + i, 0);
  209. box pred = get_region_box(l.output, l.biases, n, box_index, i, j, l.w, l.h, l.w*l.h);
  210. float best_iou = 0;
  211. for (t = 0; t < 30; ++t){
  212. box truth = float_to_box(net.truth + t*(l.coords + 1) + b*l.truths, 1);
  213. if (!truth.x) break;
  214. float iou = box_iou(pred, truth);
  215. if (iou > best_iou) {
  216. best_iou = iou;
  217. }
  218. }
  219. int obj_index = entry_index(l, b, n*l.w*l.h + j*l.w + i, l.coords);
  220. avg_anyobj += l.output[obj_index];
  221. l.delta[obj_index] = l.noobject_scale * (0 - l.output[obj_index]);
  222. if (l.background) l.delta[obj_index] = l.noobject_scale * (1 - l.output[obj_index]);
  223. if (best_iou > l.thresh) {
  224. l.delta[obj_index] = 0;
  225. }
  226. if (*(net.seen) < 12800){
  227. box truth = { 0 };
  228. truth.x = (float)(i + .5) / l.w;
  229. truth.y = (float)(j + .5) / l.h;
  230. truth.w = l.biases[2 * n] / l.w;
  231. truth.h = l.biases[2 * n + 1] / l.h;
  232. delta_region_box(truth, l.output, l.biases, n, box_index, i, j, l.w, l.h, l.delta, (float).01, (int)(l.w*l.h));
  233. }
  234. }
  235. }
  236. }
  237. for (t = 0; t < 30; ++t){
  238. box truth = float_to_box(net.truth + t*(l.coords + 1) + b*l.truths, 1);
  239. if (!truth.x) break;
  240. float best_iou = 0;
  241. int best_n = 0;
  242. i = (int)(truth.x * l.w);
  243. j = (int)(truth.y * l.h);
  244. //printf("%d %f %d %f\n", i, truth.x*l.w, j, truth.y*l.h);
  245. box truth_shift = truth;
  246. truth_shift.x = 0;
  247. truth_shift.y = 0;
  248. //printf("index %d %d\n",i, j);
  249. for (n = 0; n < l.n; ++n){
  250. int box_index = entry_index(l, b, n*l.w*l.h + j*l.w + i, 0);
  251. box pred = get_region_box(l.output, l.biases, n, box_index, i, j, l.w, l.h, l.w*l.h);
  252. if (l.bias_match){
  253. pred.w = l.biases[2 * n] / l.w;
  254. pred.h = l.biases[2 * n + 1] / l.h;
  255. }
  256. //printf("pred: (%f, %f) %f x %f\n", pred.x, pred.y, pred.w, pred.h);
  257. pred.x = 0;
  258. pred.y = 0;
  259. float iou = box_iou(pred, truth_shift);
  260. if (iou > best_iou){
  261. best_iou = iou;
  262. best_n = n;
  263. }
  264. }
  265. //printf("%d %f (%f, %f) %f x %f\n", best_n, best_iou, truth.x, truth.y, truth.w, truth.h);
  266. int box_index = entry_index(l, b, best_n*l.w*l.h + j*l.w + i, 0);
  267. float iou = delta_region_box(truth, l.output, l.biases, best_n, box_index, i, j, l.w, l.h, l.delta, l.coord_scale * (2 - truth.w*truth.h), l.w*l.h);
  268. if (l.coords > 4){
  269. int mask_index = entry_index(l, b, best_n*l.w*l.h + j*l.w + i, 4);
  270. delta_region_mask(net.truth + t*(l.coords + 1) + b*l.truths + 5, l.output, l.coords - 4, mask_index, l.delta, l.w*l.h, (int)l.mask_scale);
  271. }
  272. if (iou > .5) recall += 1;
  273. avg_iou += iou;
  274. //l.delta[best_index + 4] = iou - l.output[best_index + 4];
  275. int obj_index = entry_index(l, b, best_n*l.w*l.h + j*l.w + i, l.coords);
  276. avg_obj += l.output[obj_index];
  277. l.delta[obj_index] = l.object_scale * (1 - l.output[obj_index]);
  278. if (l.rescore) {
  279. l.delta[obj_index] = l.object_scale * (iou - l.output[obj_index]);
  280. }
  281. if (l.background){
  282. l.delta[obj_index] = l.object_scale * (0 - l.output[obj_index]);
  283. }
  284. int cls = (int)net.truth[t*(l.coords + 1) + b*l.truths + l.coords];
  285. if (l.map) cls = l.map[cls];
  286. int class_index = entry_index(l, b, best_n*l.w*l.h + j*l.w + i, l.coords + 1);
  287. delta_region_class(l.output, l.delta, class_index, cls, l.classes, l.softmax_tree, l.class_scale, l.w*l.h, &avg_cat);
  288. ++count;
  289. ++class_count;
  290. }
  291. }
  292. //printf("\n");
  293. *(l.cost) = (float)pow(mag_array(l.delta, l.outputs * l.batch), 2);
  294. printf("Region Avg IOU: %f, Class: %f, Obj: %f, No Obj: %f, Avg Recall: %f, count: %d\n", avg_iou / count, avg_cat / class_count, avg_obj / count, avg_anyobj / (l.w*l.h*l.n*l.batch), recall / count, count);
  295. }
  296. void backward_region_layer(const layer l, network net)
  297. {
  298. /*
  299. int b;
  300. int size = l.coords + l.classes + 1;
  301. for (b = 0; b < l.batch*l.n; ++b){
  302. int index = (b*size + 4)*l.w*l.h;
  303. gradient_array(l.output + index, l.w*l.h, LOGISTIC, l.delta + index);
  304. }
  305. axpy_cpu(l.batch*l.inputs, 1, l.delta, 1, net.delta, 1);
  306. */
  307. }
  308. void correct_region_boxes(box *boxes, int n, int w, int h, int netw, int neth, int relative)
  309. {
  310. int i;
  311. int new_w = 0;
  312. int new_h = 0;
  313. if (((float)netw / w) < ((float)neth / h)) {
  314. new_w = netw;
  315. new_h = (h * netw) / w;
  316. }
  317. else {
  318. new_h = neth;
  319. new_w = (w * neth) / h;
  320. }
  321. for (i = 0; i < n; ++i){
  322. box b = boxes[i];
  323. b.x = (float)(b.x - (netw - new_w) / 2. / netw) / ((float)new_w / netw);
  324. b.y = (float)(b.y - (neth - new_h) / 2. / neth) / ((float)new_h / neth);
  325. b.w *= (float)netw / new_w;
  326. b.h *= (float)neth / new_h;
  327. if (!relative){
  328. b.x *= w;
  329. b.w *= w;
  330. b.y *= h;
  331. b.h *= h;
  332. }
  333. boxes[i] = b;
  334. }
  335. }
  336. void get_region_boxes(layer l, int w, int h, int netw, int neth, float thresh, float **probs, box *boxes, float **masks, int only_objectness, int *map, float tree_thresh, int relative)
  337. {
  338. int i, j, n, z;
  339. float *predictions = l.output;
  340. if (l.batch == 2) {
  341. float *flip = l.output + l.outputs;
  342. for (j = 0; j < l.h; ++j) {
  343. for (i = 0; i < l.w / 2; ++i) {
  344. for (n = 0; n < l.n; ++n) {
  345. for (z = 0; z < l.classes + l.coords + 1; ++z){
  346. int i1 = z*l.w*l.h*l.n + n*l.w*l.h + j*l.w + i;
  347. int i2 = z*l.w*l.h*l.n + n*l.w*l.h + j*l.w + (l.w - i - 1);
  348. float swap = flip[i1];
  349. flip[i1] = flip[i2];
  350. flip[i2] = swap;
  351. if (z == 0){
  352. flip[i1] = -flip[i1];
  353. flip[i2] = -flip[i2];
  354. }
  355. }
  356. }
  357. }
  358. }
  359. for (i = 0; i < l.outputs; ++i){
  360. l.output[i] = (float)((l.output[i] + flip[i]) / 2.);
  361. }
  362. }
  363. for (i = 0; i < l.w*l.h; ++i){
  364. int row = i / l.w;
  365. int col = i % l.w;
  366. for (n = 0; n < l.n; ++n){
  367. int index = n*l.w*l.h + i;
  368. for (j = 0; j < l.classes; ++j){
  369. probs[index][j] = 0;
  370. }
  371. int obj_index = entry_index(l, 0, n*l.w*l.h + i, l.coords);
  372. int box_index = entry_index(l, 0, n*l.w*l.h + i, 0);
  373. int mask_index = entry_index(l, 0, n*l.w*l.h + i, 4);
  374. float scale = l.background ? 1 : predictions[obj_index];
  375. boxes[index] = get_region_box(predictions, l.biases, n, box_index, col, row, l.w, l.h, l.w*l.h);
  376. if (masks){
  377. for (j = 0; j < l.coords - 4; ++j){
  378. masks[index][j] = l.output[mask_index + j*l.w*l.h];
  379. }
  380. }
  381. int class_index = entry_index(l, 0, n*l.w*l.h + i, l.coords + !l.background);
  382. if (l.softmax_tree){
  383. hierarchy_predictions(predictions + class_index, l.classes, l.softmax_tree, 0, l.w*l.h);
  384. if (map){
  385. for (j = 0; j < 200; ++j){
  386. int class_index = entry_index(l, 0, n*l.w*l.h + i, l.coords + 1 + map[j]);
  387. float prob = scale*predictions[class_index];
  388. probs[index][j] = (prob > thresh) ? prob : 0;
  389. }
  390. }
  391. else {
  392. int j = hierarchy_top_prediction(predictions + class_index, l.softmax_tree, tree_thresh, l.w*l.h);
  393. probs[index][j] = (scale > thresh) ? scale : 0;
  394. probs[index][l.classes] = scale;
  395. }
  396. }
  397. else {
  398. float max = 0;
  399. for (j = 0; j < l.classes; ++j){
  400. int class_index = entry_index(l, 0, n*l.w*l.h + i, l.coords + 1 + j);
  401. float prob = scale*predictions[class_index];
  402. probs[index][j] = (prob > thresh) ? prob : 0;
  403. if (prob > max) max = prob;
  404. // TODO REMOVE
  405. // if (j == 56 ) probs[index][j] = 0;
  406. /*
  407. if (j != 0) probs[index][j] = 0;
  408. int blacklist[] = {121, 497, 482, 504, 122, 518,481, 418, 542, 491, 914, 478, 120, 510,500};
  409. int bb;
  410. for (bb = 0; bb < sizeof(blacklist)/sizeof(int); ++bb){
  411. if(index == blacklist[bb]) probs[index][j] = 0;
  412. }
  413. */
  414. }
  415. probs[index][l.classes] = max;
  416. }
  417. if (only_objectness){
  418. probs[index][0] = scale;
  419. }
  420. }
  421. }
  422. correct_region_boxes(boxes, l.w*l.h*l.n, w, h, netw, neth, relative);
  423. }
  424. #ifdef GPU
  425. void forward_region_layer_gpu(const layer l, network net)
  426. {
  427. copy_gpu(l.batch*l.inputs, net.input_gpu, 1, l.output_gpu, 1);
  428. int b, n;
  429. for (b = 0; b < l.batch; ++b){
  430. for(n = 0; n < l.n; ++n){
  431. int index = entry_index(l, b, n*l.w*l.h, 0);
  432. activate_array_gpu(l.output_gpu + index, 2*l.w*l.h, LOGISTIC);
  433. if(l.coords > 4){
  434. index = entry_index(l, b, n*l.w*l.h, 4);
  435. activate_array_gpu(l.output_gpu + index, (l.coords - 4)*l.w*l.h, LOGISTIC);
  436. }
  437. index = entry_index(l, b, n*l.w*l.h, l.coords);
  438. if(!l.background) activate_array_gpu(l.output_gpu + index, l.w*l.h, LOGISTIC);
  439. }
  440. }
  441. if (l.softmax_tree){
  442. int index = entry_index(l, 0, 0, l.coords + 1);
  443. softmax_tree(net.input_gpu + index, l.w*l.h, l.batch*l.n, l.inputs/l.n, 1, l.output_gpu + index, *l.softmax_tree);
  444. /*
  445. int mmin = 9000;
  446. int mmax = 0;
  447. int i;
  448. for(i = 0; i < l.softmax_tree->groups; ++i){
  449. int group_size = l.softmax_tree->group_size[i];
  450. if (group_size < mmin) mmin = group_size;
  451. if (group_size > mmax) mmax = group_size;
  452. }
  453. //printf("%d %d %d \n", l.softmax_tree->groups, mmin, mmax);
  454. */
  455. /*
  456. // TIMING CODE
  457. int zz;
  458. int number = 1000;
  459. int count = 0;
  460. int i;
  461. for (i = 0; i < l.softmax_tree->groups; ++i) {
  462. int group_size = l.softmax_tree->group_size[i];
  463. count += group_size;
  464. }
  465. printf("%d %d\n", l.softmax_tree->groups, count);
  466. {
  467. double then = what_time_is_it_now();
  468. for(zz = 0; zz < number; ++zz){
  469. int index = entry_index(l, 0, 0, 5);
  470. softmax_tree(net.input_gpu + index, l.w*l.h, l.batch*l.n, l.inputs/l.n, 1, l.output_gpu + index, *l.softmax_tree);
  471. }
  472. cudaDeviceSynchronize();
  473. printf("Good GPU Timing: %f\n", what_time_is_it_now() - then);
  474. }
  475. {
  476. double then = what_time_is_it_now();
  477. for(zz = 0; zz < number; ++zz){
  478. int i;
  479. int count = 5;
  480. for (i = 0; i < l.softmax_tree->groups; ++i) {
  481. int group_size = l.softmax_tree->group_size[i];
  482. int index = entry_index(l, 0, 0, count);
  483. softmax_gpu(net.input_gpu + index, group_size, l.batch*l.n, l.inputs/l.n, l.w*l.h, 1, l.w*l.h, 1, l.output_gpu + index);
  484. count += group_size;
  485. }
  486. }
  487. cudaDeviceSynchronize();
  488. printf("Bad GPU Timing: %f\n", what_time_is_it_now() - then);
  489. }
  490. {
  491. double then = what_time_is_it_now();
  492. for(zz = 0; zz < number; ++zz){
  493. int i;
  494. int count = 5;
  495. for (i = 0; i < l.softmax_tree->groups; ++i) {
  496. int group_size = l.softmax_tree->group_size[i];
  497. softmax_cpu(net.input + count, group_size, l.batch, l.inputs, l.n*l.w*l.h, 1, l.n*l.w*l.h, l.temperature, l.output + count);
  498. count += group_size;
  499. }
  500. }
  501. cudaDeviceSynchronize();
  502. printf("CPU Timing: %f\n", what_time_is_it_now() - then);
  503. }
  504. */
  505. /*
  506. int i;
  507. int count = 5;
  508. for (i = 0; i < l.softmax_tree->groups; ++i) {
  509. int group_size = l.softmax_tree->group_size[i];
  510. int index = entry_index(l, 0, 0, count);
  511. softmax_gpu(net.input_gpu + index, group_size, l.batch*l.n, l.inputs/l.n, l.w*l.h, 1, l.w*l.h, 1, l.output_gpu + index);
  512. count += group_size;
  513. }
  514. */
  515. } else if (l.softmax) {
  516. int index = entry_index(l, 0, 0, l.coords + !l.background);
  517. //printf("%d\n", index);
  518. softmax_gpu(net.input_gpu + index, l.classes + l.background, l.batch*l.n, l.inputs/l.n, l.w*l.h, 1, l.w*l.h, 1, l.output_gpu + index);
  519. }
  520. if(!net.train || l.onlyforward){
  521. cuda_pull_array(l.output_gpu, l.output, l.batch*l.outputs);
  522. return;
  523. }
  524. cuda_pull_array(l.output_gpu, net.input, l.batch*l.inputs);
  525. forward_region_layer(l, net);
  526. //cuda_push_array(l.output_gpu, l.output, l.batch*l.outputs);
  527. if(!net.train) return;
  528. cuda_push_array(l.delta_gpu, l.delta, l.batch*l.outputs);
  529. }
  530. void backward_region_layer_gpu(const layer l, network net)
  531. {
  532. int b, n;
  533. for (b = 0; b < l.batch; ++b){
  534. for(n = 0; n < l.n; ++n){
  535. int index = entry_index(l, b, n*l.w*l.h, 0);
  536. gradient_array_gpu(l.output_gpu + index, 2*l.w*l.h, LOGISTIC, l.delta_gpu + index);
  537. if(l.coords > 4){
  538. index = entry_index(l, b, n*l.w*l.h, 4);
  539. gradient_array_gpu(l.output_gpu + index, (l.coords - 4)*l.w*l.h, LOGISTIC, l.delta_gpu + index);
  540. }
  541. index = entry_index(l, b, n*l.w*l.h, l.coords);
  542. if(!l.background) gradient_array_gpu(l.output_gpu + index, l.w*l.h, LOGISTIC, l.delta_gpu + index);
  543. }
  544. }
  545. axpy_gpu(l.batch*l.inputs, 1, l.delta_gpu, 1, net.delta_gpu, 1);
  546. }
  547. #endif
  548. void zero_objectness(layer l)
  549. {
  550. int i, n;
  551. for (i = 0; i < l.w*l.h; ++i){
  552. for (n = 0; n < l.n; ++n){
  553. int obj_index = entry_index(l, 0, n*l.w*l.h + i, l.coords);
  554. l.output[obj_index] = 0;
  555. }
  556. }
  557. }
  558. };