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.

349 lines
7.5 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "box.h"
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. namespace DN{
  6. box float_to_box(float *f, int stride)
  7. {
  8. box b;
  9. b.x = f[0];
  10. b.y = f[1 * stride];
  11. b.w = f[2 * stride];
  12. b.h = f[3 * stride];
  13. return b;
  14. }
  15. dbox derivative(box a, box b)
  16. {
  17. dbox d;
  18. d.dx = 0;
  19. d.dw = 0;
  20. float l1 = a.x - a.w / 2;
  21. float l2 = b.x - b.w / 2;
  22. if (l1 > l2){
  23. d.dx -= 1;
  24. d.dw += .5;
  25. }
  26. float r1 = a.x + a.w / 2;
  27. float r2 = b.x + b.w / 2;
  28. if (r1 < r2){
  29. d.dx += 1;
  30. d.dw += .5;
  31. }
  32. if (l1 > r2) {
  33. d.dx = -1;
  34. d.dw = 0;
  35. }
  36. if (r1 < l2){
  37. d.dx = 1;
  38. d.dw = 0;
  39. }
  40. d.dy = 0;
  41. d.dh = 0;
  42. float t1 = a.y - a.h / 2;
  43. float t2 = b.y - b.h / 2;
  44. if (t1 > t2){
  45. d.dy -= 1;
  46. d.dh += .5;
  47. }
  48. float b1 = a.y + a.h / 2;
  49. float b2 = b.y + b.h / 2;
  50. if (b1 < b2){
  51. d.dy += 1;
  52. d.dh += .5;
  53. }
  54. if (t1 > b2) {
  55. d.dy = -1;
  56. d.dh = 0;
  57. }
  58. if (b1 < t2){
  59. d.dy = 1;
  60. d.dh = 0;
  61. }
  62. return d;
  63. }
  64. float overlap(float x1, float w1, float x2, float w2)
  65. {
  66. float l1 = x1 - w1 / 2;
  67. float l2 = x2 - w2 / 2;
  68. float left = l1 > l2 ? l1 : l2;
  69. float r1 = x1 + w1 / 2;
  70. float r2 = x2 + w2 / 2;
  71. float right = r1 < r2 ? r1 : r2;
  72. return right - left;
  73. }
  74. float box_intersection(box a, box b)
  75. {
  76. float w = overlap(a.x, a.w, b.x, b.w);
  77. float h = overlap(a.y, a.h, b.y, b.h);
  78. if (w < 0 || h < 0) return 0;
  79. float area = w*h;
  80. return area;
  81. }
  82. float box_union(box a, box b)
  83. {
  84. float i = box_intersection(a, b);
  85. float u = a.w*a.h + b.w*b.h - i;
  86. return u;
  87. }
  88. float box_iou(box a, box b)
  89. {
  90. return box_intersection(a, b) / box_union(a, b);
  91. }
  92. float box_rmse(box a, box b)
  93. {
  94. return sqrt(pow(a.x - b.x, 2) +
  95. pow(a.y - b.y, 2) +
  96. pow(a.w - b.w, 2) +
  97. pow(a.h - b.h, 2));
  98. }
  99. dbox dintersect(box a, box b)
  100. {
  101. float w = overlap(a.x, a.w, b.x, b.w);
  102. float h = overlap(a.y, a.h, b.y, b.h);
  103. dbox dover = derivative(a, b);
  104. dbox di;
  105. di.dw = dover.dw*h;
  106. di.dx = dover.dx*h;
  107. di.dh = dover.dh*w;
  108. di.dy = dover.dy*w;
  109. return di;
  110. }
  111. dbox dunion(box a, box b)
  112. {
  113. dbox du;
  114. dbox di = dintersect(a, b);
  115. du.dw = a.h - di.dw;
  116. du.dh = a.w - di.dh;
  117. du.dx = -di.dx;
  118. du.dy = -di.dy;
  119. return du;
  120. }
  121. void test_dunion()
  122. {
  123. box a = { 0, 0, 1, 1 };
  124. box dxa = { 0 + .0001, 0, 1, 1 };
  125. box dya = { 0, 0 + .0001, 1, 1 };
  126. box dwa = { 0, 0, 1 + .0001, 1 };
  127. box dha = { 0, 0, 1, 1 + .0001 };
  128. box b = { .5, .5, .2, .2 };
  129. dbox di = dunion(a, b);
  130. printf("Union: %f %f %f %f\n", di.dx, di.dy, di.dw, di.dh);
  131. float inter = box_union(a, b);
  132. float xinter = box_union(dxa, b);
  133. float yinter = box_union(dya, b);
  134. float winter = box_union(dwa, b);
  135. float hinter = box_union(dha, b);
  136. xinter = (xinter - inter) / (.0001);
  137. yinter = (yinter - inter) / (.0001);
  138. winter = (winter - inter) / (.0001);
  139. hinter = (hinter - inter) / (.0001);
  140. printf("Union Manual %f %f %f %f\n", xinter, yinter, winter, hinter);
  141. }
  142. void test_dintersect()
  143. {
  144. box a = { 0, 0, 1, 1 };
  145. box dxa = { 0 + .0001, 0, 1, 1 };
  146. box dya = { 0, 0 + .0001, 1, 1 };
  147. box dwa = { 0, 0, 1 + .0001, 1 };
  148. box dha = { 0, 0, 1, 1 + .0001 };
  149. box b = { .5, .5, .2, .2 };
  150. dbox di = dintersect(a, b);
  151. printf("Inter: %f %f %f %f\n", di.dx, di.dy, di.dw, di.dh);
  152. float inter = box_intersection(a, b);
  153. float xinter = box_intersection(dxa, b);
  154. float yinter = box_intersection(dya, b);
  155. float winter = box_intersection(dwa, b);
  156. float hinter = box_intersection(dha, b);
  157. xinter = (xinter - inter) / (.0001);
  158. yinter = (yinter - inter) / (.0001);
  159. winter = (winter - inter) / (.0001);
  160. hinter = (hinter - inter) / (.0001);
  161. printf("Inter Manual %f %f %f %f\n", xinter, yinter, winter, hinter);
  162. }
  163. void test_box()
  164. {
  165. test_dintersect();
  166. test_dunion();
  167. box a = { 0, 0, 1, 1 };
  168. box dxa = { 0 + .00001, 0, 1, 1 };
  169. box dya = { 0, 0 + .00001, 1, 1 };
  170. box dwa = { 0, 0, 1 + .00001, 1 };
  171. box dha = { 0, 0, 1, 1 + .00001 };
  172. box b = { .5, 0, .2, .2 };
  173. float iou = box_iou(a, b);
  174. iou = (1 - iou)*(1 - iou);
  175. printf("%f\n", iou);
  176. dbox d = diou(a, b);
  177. printf("%f %f %f %f\n", d.dx, d.dy, d.dw, d.dh);
  178. float xiou = box_iou(dxa, b);
  179. float yiou = box_iou(dya, b);
  180. float wiou = box_iou(dwa, b);
  181. float hiou = box_iou(dha, b);
  182. xiou = ((1 - xiou)*(1 - xiou) - iou) / (.00001);
  183. yiou = ((1 - yiou)*(1 - yiou) - iou) / (.00001);
  184. wiou = ((1 - wiou)*(1 - wiou) - iou) / (.00001);
  185. hiou = ((1 - hiou)*(1 - hiou) - iou) / (.00001);
  186. printf("manual %f %f %f %f\n", xiou, yiou, wiou, hiou);
  187. }
  188. dbox diou(box a, box b)
  189. {
  190. float u = box_union(a, b);
  191. float i = box_intersection(a, b);
  192. dbox di = dintersect(a, b);
  193. dbox du = dunion(a, b);
  194. dbox dd = { 0, 0, 0, 0 };
  195. if (i <= 0 || 1) {
  196. dd.dx = b.x - a.x;
  197. dd.dy = b.y - a.y;
  198. dd.dw = b.w - a.w;
  199. dd.dh = b.h - a.h;
  200. return dd;
  201. }
  202. dd.dx = 2 * pow((1 - (i / u)), 1)*(di.dx*u - du.dx*i) / (u*u);
  203. dd.dy = 2 * pow((1 - (i / u)), 1)*(di.dy*u - du.dy*i) / (u*u);
  204. dd.dw = 2 * pow((1 - (i / u)), 1)*(di.dw*u - du.dw*i) / (u*u);
  205. dd.dh = 2 * pow((1 - (i / u)), 1)*(di.dh*u - du.dh*i) / (u*u);
  206. return dd;
  207. }
  208. typedef struct{
  209. int index;
  210. int cls;
  211. float **probs;
  212. } sortable_bbox;
  213. int nms_comparator(const void *pa, const void *pb)
  214. {
  215. sortable_bbox a = *(sortable_bbox *)pa;
  216. sortable_bbox b = *(sortable_bbox *)pb;
  217. float diff = a.probs[a.index][b.cls] - b.probs[b.index][b.cls];
  218. if (diff < 0) return 1;
  219. else if (diff > 0) return -1;
  220. return 0;
  221. }
  222. void do_nms_obj(box *boxes, float **probs, int total, int classes, float thresh)
  223. {
  224. int i, j, k;
  225. sortable_bbox *s = (sortable_bbox *)calloc(total, sizeof(sortable_bbox));
  226. for (i = 0; i < total; ++i){
  227. s[i].index = i;
  228. s[i].cls = classes;
  229. s[i].probs = probs;
  230. }
  231. qsort(s, total, sizeof(sortable_bbox), nms_comparator);
  232. for (i = 0; i < total; ++i){
  233. if (probs[s[i].index][classes] == 0) continue;
  234. box a = boxes[s[i].index];
  235. for (j = i + 1; j < total; ++j){
  236. box b = boxes[s[j].index];
  237. if (box_iou(a, b) > thresh){
  238. for (k = 0; k < classes + 1; ++k){
  239. probs[s[j].index][k] = 0;
  240. }
  241. }
  242. }
  243. }
  244. free(s);
  245. }
  246. void do_nms_sort(box *boxes, float **probs, int total, int classes, float thresh)
  247. {
  248. int i, j, k;
  249. sortable_bbox *s = (sortable_bbox *)calloc(total, sizeof(sortable_bbox));
  250. for (i = 0; i < total; ++i){
  251. s[i].index = i;
  252. s[i].cls = 0;
  253. s[i].probs = probs;
  254. }
  255. for (k = 0; k < classes; ++k){
  256. for (i = 0; i < total; ++i){
  257. s[i].cls = k;
  258. }
  259. qsort(s, total, sizeof(sortable_bbox), nms_comparator);
  260. for (i = 0; i < total; ++i){
  261. if (probs[s[i].index][k] == 0) continue;
  262. box a = boxes[s[i].index];
  263. for (j = i + 1; j < total; ++j){
  264. box b = boxes[s[j].index];
  265. if (box_iou(a, b) > thresh){
  266. probs[s[j].index][k] = 0;
  267. }
  268. }
  269. }
  270. }
  271. free(s);
  272. }
  273. void do_nms(box *boxes, float **probs, int total, int classes, float thresh)
  274. {
  275. int i, j, k;
  276. for (i = 0; i < total; ++i){
  277. int any = 0;
  278. for (k = 0; k < classes; ++k) any = any || (probs[i][k] > 0);
  279. if (!any) {
  280. continue;
  281. }
  282. for (j = i + 1; j < total; ++j){
  283. if (box_iou(boxes[i], boxes[j]) > thresh){
  284. for (k = 0; k < classes; ++k){
  285. if (probs[i][k] < probs[j][k]) probs[i][k] = 0;
  286. else probs[j][k] = 0;
  287. }
  288. }
  289. }
  290. }
  291. }
  292. box encode_box(box b, box anchor)
  293. {
  294. box encode;
  295. encode.x = (b.x - anchor.x) / anchor.w;
  296. encode.y = (b.y - anchor.y) / anchor.h;
  297. encode.w = log2(b.w / anchor.w);
  298. encode.h = log2(b.h / anchor.h);
  299. return encode;
  300. }
  301. box decode_box(box b, box anchor)
  302. {
  303. box decode;
  304. decode.x = b.x * anchor.w + anchor.x;
  305. decode.y = b.y * anchor.h + anchor.y;
  306. decode.w = pow(2.f, b.w) * anchor.w;
  307. decode.h = pow(2.f, b.h) * anchor.h;
  308. return decode;
  309. }
  310. };