Skip to content

Commit ae306cc

Browse files
author
zhangdanfeng
committed
tmp
Signed-off-by: zhangdanfeng <[email protected]>
1 parent b2ff0dc commit ae306cc

File tree

4 files changed

+274
-91
lines changed

4 files changed

+274
-91
lines changed

yolov3.cc

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include "yolov3.h"
2+
3+
std::vector<std::vector<float>> YOLOV3::tensorToVector2D() {
4+
std::vector<std::vector<float>> v;
5+
if (_interpreter->outputs().size() != 3) {
6+
std::cout << "yolov3 don't support this model!\n";
7+
std::cout << __FILE__ << ": " << __LINE__ << std::endl;
8+
exit(-1);
9+
}
10+
const size_t num_anchors = 3;
11+
int masks[3][3] = {
12+
{6, 7, 8},
13+
{3, 4, 5},
14+
{0, 1, 2},
15+
};
16+
float anchors[18] = {10, 13, 16, 30, 33, 23, 30, 61, 62,
17+
45, 59, 119, 116, 90, 156, 198, 373, 326};
18+
19+
for (size_t grid = 0; grid < _interpreter->outputs().size(); grid++) {
20+
int out = _interpreter->outputs()[grid];
21+
TfLiteIntArray *out_dims = _interpreter->tensor(out)->dims;
22+
TfLiteTensor *pOutputTensor = _interpreter->tensor(out);
23+
int out_batch = out_dims->data[0];
24+
_out_row = out_dims->data[1];
25+
_out_colum = out_dims->data[2];
26+
_out_channel = out_dims->data[3];
27+
std::cout << "GRID Output Shape:[" << out_batch << "][" << _out_row << "]["
28+
<< _out_colum << "][" << _out_channel << "]\n";
29+
30+
if (pOutputTensor->type == kTfLiteFloat32) {
31+
for (size_t i = 0; i < _out_row; i++) {
32+
for (size_t j = 0; j < _out_colum; j++) {
33+
for (size_t k = 0; k < num_anchors; k++) {
34+
std::vector<float> vtem;
35+
for (int l = 0; l < _out_channel / num_anchors; l++) {
36+
float val_float =
37+
pOutputTensor->data
38+
.f[i * _out_colum * _out_channel + j * _out_channel +
39+
k * _out_channel / num_anchors + l];
40+
if (l != 2 && l != 3) {
41+
val_float = 1. / (1. + exp(-val_float)); // logistic
42+
}
43+
vtem.push_back(val_float);
44+
}
45+
vtem[0] = (j + vtem[0]) / _out_colum;
46+
vtem[1] = (i + vtem[1]) / _out_row;
47+
vtem[2] = exp(vtem[2]) * anchors[2 * masks[grid][k]] / _in_width;
48+
vtem[3] =
49+
exp(vtem[3]) * anchors[2 * masks[grid][k] + 1] / _in_height;
50+
v.push_back(vtem);
51+
}
52+
}
53+
}
54+
} else {
55+
std::cout << "Unsupported output type!\n";
56+
std::cout << __FILE__ << ": " << __LINE__ << std::endl;
57+
exit(-1);
58+
}
59+
}
60+
return v;
61+
}
62+
63+
void YOLOV3::nonMaximumSupprition(std::vector<std::vector<float>> &predV,
64+
std::vector<cv::Rect> &boxes,
65+
std::vector<float> &confidences,
66+
std::vector<int> &classIds,
67+
std::vector<int> &indices)
68+
69+
{
70+
std::vector<cv::Rect> boxesNMS;
71+
std::vector<float> scores;
72+
double confidence;
73+
cv::Point classId;
74+
for (int i = 0; i < predV.size(); i++) {
75+
if (predV[i][4] > _conf_threshold) {
76+
int left = (predV[i][0] - predV[i][2] / 2) * _img_width;
77+
int top = (predV[i][1] - predV[i][3] / 2) * _img_height;
78+
int w = predV[i][2] * _img_width;
79+
int h = predV[i][3] * _img_height;
80+
81+
for (int j = 5; j < 85; j++) {
82+
// # conf = obj_conf * cls_conf
83+
scores.push_back(predV[i][j] * predV[i][4]);
84+
}
85+
86+
cv::minMaxLoc(scores, 0, &confidence, 0, &classId);
87+
88+
scores.clear();
89+
90+
if (confidence > _conf_threshold * _conf_threshold) {
91+
boxes.push_back(cv::Rect(left, top, w, h));
92+
confidences.push_back(confidence);
93+
classIds.push_back(classId.x);
94+
boxesNMS.push_back(cv::Rect(left, top, w, h));
95+
}
96+
}
97+
}
98+
cv::dnn::NMSBoxes(boxesNMS, confidences, _conf_threshold, _nms_threshold,
99+
indices);
100+
}

yolov3.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* zhangdanfeng 2022
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include "yolov5.h"
20+
21+
class YOLOV3 : public YOLOV5 {
22+
public:
23+
// private:
24+
virtual std::vector<std::vector<float>> tensorToVector2D() override;
25+
virtual void nonMaximumSupprition(std::vector<std::vector<float>> &predV,
26+
std::vector<cv::Rect> &boxes,
27+
std::vector<float> &confidences,
28+
std::vector<int> &classIds,
29+
std::vector<int> &indices) override;
30+
};

0 commit comments

Comments
 (0)