Skip to content

Commit 0f44496

Browse files
author
zhangdanfeng
committed
support YOLOV3 & YOLOV5 model
Signed-off-by: zhangdanfeng <[email protected]>
1 parent ae306cc commit 0f44496

File tree

8 files changed

+83
-21
lines changed

8 files changed

+83
-21
lines changed

Makefile-rv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ segmentation/tflite_segmentation: segmentation.cc $(COMMON_SRC)
112112
mkdir -p segmentation
113113
$(CXX) segmentation.cc $(COMMON_SRC) -o segmentation/tflite_segmentation $(LDFLAGS) $(LIBS) $(CXXFLAGS) $(CCFLAGS) $(INCLUDES)
114114

115-
detection/tflite_detection: detection.cc yolov5.cc $(COMMON_SRC)
115+
detection/tflite_detection: detection.cc yolov5.cc yolov3.cc $(COMMON_SRC)
116116
mkdir -p detection
117-
$(CXX) detection.cc yolov5.cc $(COMMON_SRC) -o detection/tflite_detection $(LDFLAGS) $(LIBS) $(CXXFLAGS) $(CCFLAGS) $(INCLUDES)
117+
$(CXX) detection.cc yolov5.cc yolov3.cc $(COMMON_SRC) -o detection/tflite_detection $(LDFLAGS) $(LIBS) $(CXXFLAGS) $(CCFLAGS) $(INCLUDES)
118118

119119
clean:
120120
rm -rf classification/tflite_classification segmentation/tflite_segmentation detection/tflite_detection

detection.cc

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,22 @@
3939
#include "tensorflow/lite/interpreter.h"
4040
#include "tensorflow/lite/model.h"
4141
#include "utils.h"
42+
#include "yolov3.h"
4243
#include "yolov5.h"
4344

4445
using namespace cv;
4546
using namespace std;
4647

48+
/*
49+
qemu-riscv64 detection/tflite_detection -m \
50+
detection/yolov5s_ultralytics_640_quantized.tflite -i detection/bus.jpg -l \
51+
detection/labels.txt -c 1 -b 0 -s 255 -t 1 -v 5
52+
53+
qemu-riscv64 detection/tflite_detection -m \
54+
detection/yolov3_keras_416_quantized.tflite -i detection/bus.jpg -l \
55+
detection/labels.txt -c 1 -b 0 -s 255 -t 1 -v 3
56+
*/
57+
4758
/*
4859
* Display command line usage
4960
*/
@@ -60,6 +71,7 @@ void display_usage() {
6071
<< "--input_std, -s: input standard deviation\n"
6172
<< "--profiling, -p: [0|1], profiling or not\n"
6273
<< "--threads, -t: number of threads\n"
74+
<< "--model-version, -v: yolo version\n"
6375
<< "\n";
6476
}
6577

@@ -72,6 +84,7 @@ int main(int argc, char **argv) {
7284
std::string label_path = "labels.txt";
7385
std::string input_path = "grace_hopper.bmp";
7486
eInputType input_source = INPUT_Image;
87+
int yolo_version = 5;
7588
int frame_cnt = 1;
7689
int num_threads = 1;
7790
float input_mean = 0.f;
@@ -90,12 +103,13 @@ int main(int argc, char **argv) {
90103
{"threads", required_argument, nullptr, 't'},
91104
{"input_mean", required_argument, nullptr, 'b'},
92105
{"input_std", required_argument, nullptr, 's'},
106+
{"--model-version", required_argument, nullptr, 'v'},
93107
{nullptr, 0, nullptr, 0}};
94108

95109
/* getopt_long stores the option index here. */
96110
int option_index = 0;
97111

98-
c = getopt_long(argc, argv, "b:c:i:m:p:r:s:t:h", long_options,
112+
c = getopt_long(argc, argv, "b:c:i:m:l:p:r:s:t:v:h", long_options,
99113
&option_index);
100114

101115
/* Detect the end of the options. */
@@ -130,6 +144,9 @@ int main(int argc, char **argv) {
130144
case 't':
131145
num_threads = strtol(optarg, nullptr, 10);
132146
break;
147+
case 'v':
148+
yolo_version = strtol(optarg, nullptr, 10);
149+
break;
133150
case 'h':
134151
display_usage();
135152
exit(-1);
@@ -138,16 +155,25 @@ int main(int argc, char **argv) {
138155
}
139156
}
140157

141-
YOLOV5 model;
158+
YOLOV5 *model = NULL;
159+
if (yolo_version == 3) {
160+
model = new YOLOV3;
161+
} else {
162+
model = new YOLOV5;
163+
}
164+
if (!model) {
165+
exit(-1);
166+
}
167+
142168
Prediction out_pred;
143169
std::vector<std::string> labelNames;
144170

145171
std::cout << "Loading model... " << std::endl;
146172

147173
// Read model.
148-
model.loadModel(model_path);
174+
model->loadModel(model_path);
149175

150-
model.getLabelsName(label_path, labelNames);
176+
model->getLabelsName(label_path, labelNames);
151177
std::cout << "\nLabel Count: " << labelNames.size() << "\n" << std::endl;
152178

153179
// Setup input
@@ -163,7 +189,7 @@ int main(int argc, char **argv) {
163189
// Predict on the input image
164190
cv::Mat show_image;
165191
input_image.copyTo(show_image);
166-
model.run(input_image, out_pred);
192+
model->run(input_image, out_pred);
167193
auto stop = std::chrono::high_resolution_clock::now();
168194
auto duration =
169195
std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
@@ -188,6 +214,6 @@ int main(int argc, char **argv) {
188214
cv::imwrite("out.png", show_image);
189215

190216
std::cout << "detection completes! " << std::endl;
191-
217+
delete model;
192218
return 0;
193219
}

scripts/tf/build-linux-riscv64.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
#
3+
# This source code is licensed under the BSD-style license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
# tf v2.11.1 tested
6+
7+
set -e
8+
rm -rf build_tflite_2_11_x
9+
mkdir -p build_tflite_2_11_x
10+
11+
CMAKE_ARGS=()
12+
13+
# CMake-level configuration
14+
CMAKE_ARGS+=("-DCMAKE_MAKE_PROGRAM=make")
15+
CMAKE_ARGS+=("-DCMAKE_TOOLCHAIN_FILE=$(pwd)/scripts/tf/cmake/riscv64.JDSK.toolchain.cmake")
16+
CMAKE_ARGS+=("-DCMAKE_BUILD_TYPE=RelWithDebInfo")
17+
18+
# Cross-compilation options for Google Benchmark
19+
CMAKE_ARGS+=("-DHAVE_POSIX_REGEX=0")
20+
CMAKE_ARGS+=("-DHAVE_STEADY_CLOCK=0")
21+
CMAKE_ARGS+=("-DHAVE_STD_REGEX=0")
22+
23+
# xnnpack
24+
CMAKE_ARGS+=("-DTFLITE_ENABLE_XNNPACK=OFF")
25+
26+
# flatbuffer
27+
# 防止交叉编译出的elf直接运行报错
28+
CMAKE_ARGS+=("-DFLATBUFFERS_BUILD_FLATC=OFF")
29+
# 最好有预装
30+
CMAKE_ARGS+=("-DFLATBUFFERS_FLATC_EXECUTABLE=flatc")
31+
32+
# Use-specified CMake arguments go last to allow overridding defaults
33+
CMAKE_ARGS+=($@)
34+
35+
pushd build_tflite_2_11_x
36+
37+
# 需要手动修改tensorflow的地址
38+
cmake ../../tensorflow/tensorflow/lite "${CMAKE_ARGS[@]}"
39+
make -j4
40+
41+
popd

scripts/tf/cmake/riscv64.JDSK.toolchain.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
set(CMAKE_SYSTEM_NAME Linux)
1212
set(CMAKE_SYSTEM_PROCESSOR riscv64)
13+
set(CMAKE_CROSSCOMPILING TRUE)
1314

1415
if(DEFINED ENV{RISCV_ROOT_PATH})
1516
file(TO_CMAKE_PATH $ENV{RISCV_ROOT_PATH} RISCV_ROOT_PATH)

yolov3.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ std::vector<std::vector<float>> YOLOV3::tensorToVector2D() {
77
std::cout << __FILE__ << ": " << __LINE__ << std::endl;
88
exit(-1);
99
}
10-
const size_t num_anchors = 3;
1110
int masks[3][3] = {
1211
{6, 7, 8},
1312
{3, 4, 5},
@@ -30,15 +29,16 @@ std::vector<std::vector<float>> YOLOV3::tensorToVector2D() {
3029
if (pOutputTensor->type == kTfLiteFloat32) {
3130
for (size_t i = 0; i < _out_row; i++) {
3231
for (size_t j = 0; j < _out_colum; j++) {
33-
for (size_t k = 0; k < num_anchors; k++) {
32+
for (size_t k = 0; k < _num_anchors; k++) {
3433
std::vector<float> vtem;
35-
for (int l = 0; l < _out_channel / num_anchors; l++) {
34+
for (int l = 0; l < _out_channel / _num_anchors; l++) {
3635
float val_float =
3736
pOutputTensor->data
3837
.f[i * _out_colum * _out_channel + j * _out_channel +
39-
k * _out_channel / num_anchors + l];
38+
k * _out_channel / _num_anchors + l];
4039
if (l != 2 && l != 3) {
41-
val_float = 1. / (1. + exp(-val_float)); // logistic
40+
val_float =
41+
1. / (1. + exp(-val_float)); // logistic (redundancy)
4242
}
4343
vtem.push_back(val_float);
4444
}
@@ -78,7 +78,7 @@ void YOLOV3::nonMaximumSupprition(std::vector<std::vector<float>> &predV,
7878
int w = predV[i][2] * _img_width;
7979
int h = predV[i][3] * _img_height;
8080

81-
for (int j = 5; j < 85; j++) {
81+
for (int j = 5; j < _out_channel / _num_anchors; j++) {
8282
// # conf = obj_conf * cls_conf
8383
scores.push_back(predV[i][j] * predV[i][4]);
8484
}

yolov3.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
class YOLOV3 : public YOLOV5 {
2222
public:
23-
// private:
23+
const size_t _num_anchors{3};
2424
virtual std::vector<std::vector<float>> tensorToVector2D() override;
2525
virtual void nonMaximumSupprition(std::vector<std::vector<float>> &predV,
2626
std::vector<cv::Rect> &boxes,

yolov5.cc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,7 @@ void YOLOV5::nonMaximumSupprition(std::vector<std::vector<float>> &predV,
163163
cv::Point classId;
164164

165165
for (int i = 0; i < _out_row; i++) {
166-
if (predV[i][4] > 0.5) {
167-
printf("rowid:%d score:%f\n", i, predV[i][4]);
168-
}
169-
170166
if (predV[i][4] > _conf_threshold) {
171-
// height--> image.rows, width--> image.cols;
172167
int left = (predV[i][0] - predV[i][2] / 2) * _img_width;
173168
int top = (predV[i][1] - predV[i][3] / 2) * _img_height;
174169
int w = predV[i][2] * _img_width;

yolov5.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ class YOLOV5 {
5757
// number of threads
5858
const int _n_threads = 1;
5959

60-
// private:
6160
// model's
6261
std::unique_ptr<tflite::FlatBufferModel> _model;
6362
std::unique_ptr<tflite::Interpreter> _interpreter;

0 commit comments

Comments
 (0)