21 #include <nn_detect_common.h>
33 inline float logistic_activate(
float x)
34 {
return 1./(1. + exp(-x)); }
36 void flatten(
float const * src,
float * dst,
int size,
int layers,
int batch,
int forward)
38 for (
int b = 0; b < batch; ++b)
39 for (
int c = 0; c < layers; ++c)
40 for (
int i = 0; i < size; ++i)
42 int i1 = b*layers*size + c*size + i;
43 int i2 = b*layers*size + i*layers + c;
44 if (forward) dst[i2] = src[i1];
45 else dst[i1] = src[i2];
53 std::vector<cv::Rect> & boxes,
size_t nclass,
float const * biases,
int const yolonum,
54 float confThreshold, cv::Size
const & bsiz,
int fudge)
56 if (nclass == 0) nclass = 1;
57 if (out.type() != CV_32F)
LFATAL(
"Need FLOAT data");
58 cv::MatSize
const & msiz = out.size;
59 if (msiz.dims() != 4 || msiz[0] != 1)
60 LFATAL(
"Incorrect tensor size: need 1xNxHxW where N=anchors*(4(coords)+1(box score)+nclass(object scores)), got "<<
66 int const w = msiz[3];
67 int const h = msiz[2];
69 int bbsize = coords + 1 + nclass;
70 int const n = msiz[1] / bbsize;
71 if (msiz[1] % bbsize)
LFATAL(
"Incorrect tensor size: need 1xNxHxW where N=anchors*(4(coords)+1(box score)"
73 float const bfac = 1.0F / (8 << yolonum);
74 int const boff = n * 2 * yolonum;
75 int const whn = w *
h * n;
76 int const total = whn * bbsize;
77 if (total !=
int(out.total()))
LFATAL(
"Ooops");
80 float predictions[total];
81 flatten((
float const *)out.data, predictions, w*
h, bbsize*n, 1, 1);
84 for (
int i = 0; i < total; i += bbsize)
86 predictions[i + coords] = logistic_activate(predictions[i + coords]);
91 for (
int i = 0; i < w *
h; ++i)
93 int const row = i / w;
94 int const col = i % w;
97 for (
int nn = 0; nn < n; ++nn)
100 int box_index = index * bbsize;
101 float scale = predictions[box_index + coords];
102 int class_index = box_index + coords + 1;
105 int maxid = -1;
float maxscore = -1.0F;
106 for (
size_t j = 0; j < nclass; ++j)
108 float prob = scale * predictions[class_index + j];
109 if (prob > confThreshold && prob > maxscore) { maxid = j; maxscore = prob; }
116 cv::Rect b( (col + logistic_activate(predictions[box_index + 0])) * bsiz.width / w + 0.499F,
117 (row + logistic_activate(predictions[box_index + 1])) * bsiz.height /
h + 0.499F,
118 exp(predictions[box_index + 2]) * biases[2*nn + boff] * bfac * bsiz.width / w + 0.499F,
119 exp(predictions[box_index + 3]) * biases[2*nn+1 + boff] * bfac * bsiz.height /
h + 0.499F);
123 boxes.emplace_back(b);
124 classIds.emplace_back(maxid + fudge);
125 confidences.emplace_back(maxscore);