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.

517 lines
12 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "basicBox.h"
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6. float overlap(float x1, float w1, float x2, float w2){
  7. float l1 = x1 - w1 / 2;
  8. float l2 = x2 - w2 / 2;
  9. float left = l1 > l2 ? l1 : l2;
  10. float r1 = x1 + w1 / 2;
  11. float r2 = x2 + w2 / 2;
  12. float right = r1 < r2 ? r1 : r2;
  13. return right - left;
  14. };
  15. void S_BOXLABEL::_float(float *val){
  16. x = val[0];
  17. y = val[1];
  18. w = val[2];
  19. h = val[3];
  20. };
  21. void S_BOXLABEL::_encode(S_BOXLABEL anchor){
  22. x = (x - anchor.x) / anchor.w;
  23. y = (y - anchor.y) / anchor.h;
  24. w = (float)log2(w / anchor.w);
  25. h = (float)log2(h / anchor.h);
  26. };
  27. void S_BOXLABEL::_decode(S_BOXLABEL anchor){
  28. x = x * anchor.w + anchor.x;
  29. y = y * anchor.h + anchor.y;
  30. w = (float)(pow(2.f, w) * anchor.w);
  31. h = (float)(pow(2.f, h) * anchor.h);
  32. };
  33. float S_BOXLABEL::_intersection(S_BOXLABEL b){
  34. float _w = overlap(x, w, b.x, b.w);
  35. float _h = overlap(y, h, b.y, b.h);
  36. if (_w < 0 || _h < 0) return 0;
  37. float area = _w*_h;
  38. return area;
  39. };
  40. float S_BOXLABEL::_union(S_BOXLABEL b){
  41. float i = _intersection(b);
  42. float u = w*h + b.w*b.h - i;
  43. return u;
  44. };
  45. float S_BOXLABEL::_iou(S_BOXLABEL b){
  46. return _intersection(b) / _union(b);
  47. };
  48. float S_BOXLABEL::_rmse(S_BOXLABEL b){
  49. return (float)sqrt(pow(x - b.x, 2) + pow(y - b.y, 2) + pow(w - b.w, 2) + pow(h - b.h, 2));
  50. };
  51. void S_DBOX::_intersect(S_BOXLABEL a, S_BOXLABEL b){
  52. float _w = overlap(a.x, a.w, b.x, b.w);
  53. float _h = overlap(a.y, a.h, b.y, b.h);
  54. _derivative(a, b);
  55. dw *= _h;
  56. dx *= _h;
  57. dh *= _w;
  58. dy *= _w;
  59. };
  60. void S_DBOX::_derivative(S_BOXLABEL a, S_BOXLABEL b){
  61. dx = 0;
  62. dw = 0;
  63. float l1 = a.x - a.w / 2;
  64. float l2 = b.x - b.w / 2;
  65. if (l1 > l2){
  66. dx -= 1;
  67. dw += .5;
  68. };
  69. float r1 = a.x + a.w / 2;
  70. float r2 = b.x + b.w / 2;
  71. if (r1 < r2){
  72. dx += 1;
  73. dw += .5;
  74. };
  75. if (l1 > r2) {
  76. dx = -1;
  77. dw = 0;
  78. };
  79. if (r1 < l2){
  80. dx = 1;
  81. dw = 0;
  82. };
  83. dy = 0;
  84. dh = 0;
  85. float t1 = a.y - a.h / 2;
  86. float t2 = b.y - b.h / 2;
  87. if (t1 > t2){
  88. dy -= 1;
  89. dh += .5;
  90. };
  91. float b1 = a.y + a.h / 2;
  92. float b2 = b.y + b.h / 2;
  93. if (b1 < b2){
  94. dy += 1;
  95. dh += .5;
  96. };
  97. if (t1 > b2) {
  98. dy = -1;
  99. dh = 0;
  100. };
  101. if (b1 < t2){
  102. dy = 1;
  103. dh = 0;
  104. };
  105. };
  106. void S_DBOX::_union(S_BOXLABEL a, S_BOXLABEL b){
  107. _intersect(a, b);
  108. dw = a.h - dw;
  109. dh = a.w - dh;
  110. dx *= -1.f;
  111. dy *= -1.f;
  112. };
  113. void S_DBOX::_iou(S_BOXLABEL a, S_BOXLABEL b){
  114. float i = a._intersection(b);
  115. if (i <= 0 || 1) {
  116. dx = b.x - a.x;
  117. dy = b.y - a.y;
  118. dw = b.w - a.w;
  119. dh = b.h - a.h;
  120. return;
  121. }
  122. else {
  123. S_DBOX di;
  124. S_DBOX du;
  125. float u = a._union(b);
  126. di._intersect(a, b);
  127. du._union(a, b);
  128. dx = (float)(2 * pow((1 - (i / u)), 1)*(di.dx*u - du.dx*i) / (u*u));
  129. dy = (float)(2 * pow((1 - (i / u)), 1)*(di.dy*u - du.dy*i) / (u*u));
  130. dw = (float)(2 * pow((1 - (i / u)), 1)*(di.dw*u - du.dw*i) / (u*u));
  131. dh = (float)(2 * pow((1 - (i / u)), 1)*(di.dh*u - du.dh*i) / (u*u));
  132. };
  133. };
  134. int nmsComparator(const void *pa, const void *pb){
  135. S_SORTABLE_DBOX a = *(S_SORTABLE_DBOX *)pa;
  136. S_SORTABLE_DBOX b = *(S_SORTABLE_DBOX *)pb;
  137. float diff = a.probs[a.index][b.cls] - b.probs[b.index][b.cls];
  138. if (diff < 0) return 1;
  139. else if (diff > 0) return -1;
  140. return 0;
  141. }
  142. void nmsSort(S_BOXLABEL *boxes, float **probs, uint32_t total, uint32_t classes, float thresh){
  143. uint32_t i, j, k;
  144. S_SORTABLE_DBOX *s = (S_SORTABLE_DBOX *)calloc(total, sizeof(S_SORTABLE_DBOX));
  145. for (i = 0; i < total; ++i){
  146. s[i].index = i;
  147. s[i].cls = 0;
  148. s[i].probs = probs;
  149. };
  150. for (k = 0; k < classes; ++k){
  151. for (i = 0; i < total; ++i){
  152. s[i].cls = k;
  153. };
  154. qsort(s, total, sizeof(S_SORTABLE_DBOX), nmsComparator);
  155. for (i = 0; i < total; ++i){
  156. if (probs[s[i].index][k] == 0) continue;
  157. S_BOXLABEL a = boxes[s[i].index];
  158. for (j = i + 1; j < total; ++j){
  159. S_BOXLABEL b = boxes[s[j].index];
  160. if (a._iou(b) > thresh){
  161. probs[s[j].index][k] = 0;
  162. };
  163. };
  164. };
  165. };
  166. free(s);
  167. };
  168. void doNMS(S_BOXLABEL *boxes, float **probs, uint32_t total, uint32_t classes, float thresh){
  169. uint32_t i, j, k;
  170. for (i = 0; i < total; ++i){
  171. int any = 0;
  172. for (k = 0; k < classes; ++k) any = any || (probs[i][k] > 0);
  173. if (!any) {
  174. continue;
  175. };
  176. for (j = i + 1; j < total; ++j){
  177. if (boxes[i]._iou(boxes[j]) > thresh){
  178. for (k = 0; k < classes; ++k){
  179. if (probs[i][k] < probs[j][k]) probs[i][k] = 0;
  180. else probs[j][k] = 0;
  181. };
  182. };
  183. };
  184. };
  185. };
  186. /*
  187. box float_to_box(float *f)
  188. {
  189. box b;
  190. b.x = f[0];
  191. b.y = f[1];
  192. b.w = f[2];
  193. b.h = f[3];
  194. return b;
  195. }
  196. dbox derivative(box a, box b)
  197. {
  198. dbox d;
  199. d.dx = 0;
  200. d.dw = 0;
  201. float l1 = a.x - a.w/2;
  202. float l2 = b.x - b.w/2;
  203. if (l1 > l2){
  204. d.dx -= 1;
  205. d.dw += .5;
  206. }
  207. float r1 = a.x + a.w/2;
  208. float r2 = b.x + b.w/2;
  209. if(r1 < r2){
  210. d.dx += 1;
  211. d.dw += .5;
  212. }
  213. if (l1 > r2) {
  214. d.dx = -1;
  215. d.dw = 0;
  216. }
  217. if (r1 < l2){
  218. d.dx = 1;
  219. d.dw = 0;
  220. }
  221. d.dy = 0;
  222. d.dh = 0;
  223. float t1 = a.y - a.h/2;
  224. float t2 = b.y - b.h/2;
  225. if (t1 > t2){
  226. d.dy -= 1;
  227. d.dh += .5;
  228. }
  229. float b1 = a.y + a.h/2;
  230. float b2 = b.y + b.h/2;
  231. if(b1 < b2){
  232. d.dy += 1;
  233. d.dh += .5;
  234. }
  235. if (t1 > b2) {
  236. d.dy = -1;
  237. d.dh = 0;
  238. }
  239. if (b1 < t2){
  240. d.dy = 1;
  241. d.dh = 0;
  242. }
  243. return d;
  244. }
  245. float overlap(float x1, float w1, float x2, float w2)
  246. {
  247. float l1 = x1 - w1/2;
  248. float l2 = x2 - w2/2;
  249. float left = l1 > l2 ? l1 : l2;
  250. float r1 = x1 + w1/2;
  251. float r2 = x2 + w2/2;
  252. float right = r1 < r2 ? r1 : r2;
  253. return right - left;
  254. }
  255. float box_intersection(box a, box b)
  256. {
  257. float w = overlap(a.x, a.w, b.x, b.w);
  258. float h = overlap(a.y, a.h, b.y, b.h);
  259. if(w < 0 || h < 0) return 0;
  260. float area = w*h;
  261. return area;
  262. }
  263. float box_union(box a, box b)
  264. {
  265. float i = box_intersection(a, b);
  266. float u = a.w*a.h + b.w*b.h - i;
  267. return u;
  268. }
  269. float box_iou(box a, box b)
  270. {
  271. return box_intersection(a, b)/box_union(a, b);
  272. }
  273. float box_rmse(box a, box b)
  274. {
  275. return (float)sqrt(pow(a.x-b.x, 2) +
  276. pow(a.y-b.y, 2) +
  277. pow(a.w-b.w, 2) +
  278. pow(a.h-b.h, 2));
  279. }
  280. dbox dintersect(box a, box b)
  281. {
  282. float w = overlap(a.x, a.w, b.x, b.w);
  283. float h = overlap(a.y, a.h, b.y, b.h);
  284. dbox dover = derivative(a, b);
  285. dbox di;
  286. di.dw = dover.dw*h;
  287. di.dx = dover.dx*h;
  288. di.dh = dover.dh*w;
  289. di.dy = dover.dy*w;
  290. return di;
  291. }
  292. dbox dunion(box a, box b)
  293. {
  294. dbox du;
  295. dbox di = dintersect(a, b);
  296. du.dw = a.h - di.dw;
  297. du.dh = a.w - di.dh;
  298. du.dx = -di.dx;
  299. du.dy = -di.dy;
  300. return du;
  301. }
  302. void test_dunion()
  303. {
  304. box a = {0.f, 0.f, 1.f, 1.f};
  305. box dxa= {0.f+.0001f, 0.f, 1.f, 1.f};
  306. box dya= {0.f, 0.f+.0001f, 1.f, 1.f};
  307. box dwa= {0.f, 0.f, 1.f+.0001f, 1.f};
  308. box dha= {0.f, 0.f, 1.f, 1.f+.0001f};
  309. box b = {.5f, .5f, .2f, .2f};
  310. dbox di = dunion(a,b);
  311. printf("Union: %f %f %f %f\n", di.dx, di.dy, di.dw, di.dh);
  312. float inter = box_union(a, b);
  313. float xinter = box_union(dxa, b);
  314. float yinter = box_union(dya, b);
  315. float winter = box_union(dwa, b);
  316. float hinter = box_union(dha, b);
  317. xinter = (xinter - inter)/(.0001f);
  318. yinter = (yinter - inter)/(.0001f);
  319. winter = (winter - inter)/(.0001f);
  320. hinter = (hinter - inter)/(.0001f);
  321. printf("Union Manual %f %f %f %f\n", xinter, yinter, winter, hinter);
  322. }
  323. void test_dintersect()
  324. {
  325. box a = {0.f, 0.f, 1.f, 1.f};
  326. box dxa= {0.f+.0001f, 0.f, 1.f, 1.f};
  327. box dya= {0.f, 0.f+.0001f, 1.f, 1.f};
  328. box dwa= {0.f, 0.f, 1.f+.0001f, 1.f};
  329. box dha= {0.f, 0.f, 1.f, 1.f+.0001f};
  330. box b = {.5f, .5f, .2f, .2f};
  331. dbox di = dintersect(a,b);
  332. printf("Inter: %f %f %f %f\n", di.dx, di.dy, di.dw, di.dh);
  333. float inter = box_intersection(a, b);
  334. float xinter = box_intersection(dxa, b);
  335. float yinter = box_intersection(dya, b);
  336. float winter = box_intersection(dwa, b);
  337. float hinter = box_intersection(dha, b);
  338. xinter = (xinter - inter)/(.0001f);
  339. yinter = (yinter - inter)/(.0001f);
  340. winter = (winter - inter)/(.0001f);
  341. hinter = (hinter - inter)/(.0001f);
  342. printf("Inter Manual %f %f %f %f\n", xinter, yinter, winter, hinter);
  343. }
  344. void test_box()
  345. {
  346. test_dintersect();
  347. test_dunion();
  348. box a = {0.f, 0.f, 1.f, 1.f};
  349. box dxa= {0.f+.00001f, 0.f, 1.f, 1.f};
  350. box dya= {0.f, 0.f+.00001f, 1.f, 1.f};
  351. box dwa= {0.f, 0.f, 1.f+.00001f, 1.f};
  352. box dha= {0.f, 0.f, 1.f, 1.f+.00001f};
  353. box b = {.5f, 0.f, .2f, .2f};
  354. float iou = box_iou(a,b);
  355. iou = (1-iou)*(1-iou);
  356. printf("%f\n", iou);
  357. dbox d = diou(a, b);
  358. printf("%f %f %f %f\n", d.dx, d.dy, d.dw, d.dh);
  359. float xiou = box_iou(dxa, b);
  360. float yiou = box_iou(dya, b);
  361. float wiou = box_iou(dwa, b);
  362. float hiou = box_iou(dha, b);
  363. xiou = ((1-xiou)*(1-xiou) - iou)/(.00001f);
  364. yiou = ((1-yiou)*(1-yiou) - iou)/(.00001f);
  365. wiou = ((1-wiou)*(1-wiou) - iou)/(.00001f);
  366. hiou = ((1-hiou)*(1-hiou) - iou)/(.00001f);
  367. printf("manual %f %f %f %f\n", xiou, yiou, wiou, hiou);
  368. }
  369. dbox diou(box a, box b)
  370. {
  371. float u = box_union(a,b);
  372. float i = box_intersection(a,b);
  373. dbox di = dintersect(a,b);
  374. dbox du = dunion(a,b);
  375. dbox dd = {0,0,0,0};
  376. if(i <= 0 || 1) {
  377. dd.dx = b.x - a.x;
  378. dd.dy = b.y - a.y;
  379. dd.dw = b.w - a.w;
  380. dd.dh = b.h - a.h;
  381. return dd;
  382. }
  383. dd.dx = (float)( 2*pow((1-(i/u)),1)*(di.dx*u - du.dx*i)/(u*u));
  384. dd.dy = (float)(2 * pow((1 - (i / u)), 1)*(di.dy*u - du.dy*i) / (u*u));
  385. dd.dw = (float)(2 * pow((1 - (i / u)), 1)*(di.dw*u - du.dw*i) / (u*u));
  386. dd.dh = (float)(2 * pow((1 - (i / u)), 1)*(di.dh*u - du.dh*i) / (u*u));
  387. return dd;
  388. }
  389. typedef struct{
  390. int index;
  391. int cls;
  392. float **probs;
  393. } sortable_bbox;
  394. int nms_comparator(const void *pa, const void *pb)
  395. {
  396. sortable_bbox a = *(sortable_bbox *)pa;
  397. sortable_bbox b = *(sortable_bbox *)pb;
  398. float diff = a.probs[a.index][b.cls] - b.probs[b.index][b.cls];
  399. if(diff < 0) return 1;
  400. else if(diff > 0) return -1;
  401. return 0;
  402. }
  403. void do_nms_sort(box *boxes, float **probs, int total, int classes, float thresh)
  404. {
  405. int i, j, k;
  406. sortable_bbox *s = (sortable_bbox *)calloc(total, sizeof(sortable_bbox));
  407. for(i = 0; i < total; ++i){
  408. s[i].index = i;
  409. s[i].cls = 0;
  410. s[i].probs = probs;
  411. }
  412. for(k = 0; k < classes; ++k){
  413. for(i = 0; i < total; ++i){
  414. s[i].cls = k;
  415. }
  416. qsort(s, total, sizeof(sortable_bbox), nms_comparator);
  417. for(i = 0; i < total; ++i){
  418. if(probs[s[i].index][k] == 0) continue;
  419. box a = boxes[s[i].index];
  420. for(j = i+1; j < total; ++j){
  421. box b = boxes[s[j].index];
  422. if (box_iou(a, b) > thresh){
  423. probs[s[j].index][k] = 0;
  424. }
  425. }
  426. }
  427. }
  428. free(s);
  429. }
  430. void do_nms(box *boxes, float **probs, int total, int classes, float thresh)
  431. {
  432. int i, j, k;
  433. for(i = 0; i < total; ++i){
  434. int any = 0;
  435. for(k = 0; k < classes; ++k) any = any || (probs[i][k] > 0);
  436. if(!any) {
  437. continue;
  438. }
  439. for(j = i+1; j < total; ++j){
  440. if (box_iou(boxes[i], boxes[j]) > thresh){
  441. for(k = 0; k < classes; ++k){
  442. if (probs[i][k] < probs[j][k]) probs[i][k] = 0;
  443. else probs[j][k] = 0;
  444. }
  445. }
  446. }
  447. }
  448. }
  449. box encode_box(box b, box anchor)
  450. {
  451. box encode;
  452. encode.x = (b.x - anchor.x) / anchor.w;
  453. encode.y = (b.y - anchor.y) / anchor.h;
  454. encode.w = (float)log2(b.w / anchor.w);
  455. encode.h = (float)log2(b.h / anchor.h);
  456. return encode;
  457. }
  458. box decode_box(box b, box anchor)
  459. {
  460. box decode;
  461. decode.x = b.x * anchor.w + anchor.x;
  462. decode.y = b.y * anchor.h + anchor.y;
  463. decode.w = (float)(pow(2.f, b.w) * anchor.w);
  464. decode.h = (float)(pow(2.f, b.h) * anchor.h);
  465. return decode;
  466. }
  467. */