Skip to content

Commit

Permalink
feat: support yolo-fastestv2
Browse files Browse the repository at this point in the history
  • Loading branch information
susan-mm committed Aug 30, 2022
1 parent 92b9fbd commit 778fe54
Show file tree
Hide file tree
Showing 9 changed files with 833 additions and 33 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ APP_EXTRACT_VERSION()

PROJECT(HelloApp VERSION ${XNN_APP_VERSION} LANGUAGES CXX)

OPTION(BUILD_TESTS "Build with tests" OFF)
OPTION(BUILD_TESTS "Build with tests" ON)
OPTION(BUILD_EXAMPLES "Build with examples" ON)
OPTION(BUILD_BENCHMARKS "Build with benchmarks" OFF)
OPTION(BUILD_NN_MODULES_ONLY "Only build nn modules" ON)
OPTION(BUILD_WITH_MNN "Build with MNN" OFF)
OPTION(BUILD_WITH_TENGINE "Build with Tengine" OFF)
OPTION(BUILD_WITH_TENGINE "Build with Tengine" ON)

MESSAGE(INFO "--------------------------------")
MESSAGE(STATUS "Build XNNApp: ${XNN_APP_VERSION}")
Expand Down
9 changes: 5 additions & 4 deletions examples/eg_ncnn_detect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ void readRawData(const char *filename, unsigned char *data) {
}



int main(int argc, char *argv[])
{
if (argc < 5) {
Expand All @@ -37,8 +38,8 @@ int main(int argc, char *argv[])

// get parameters from config.json
int num_class = 1;
std::string bin_path = "output-detect-nobn.bin";
std::string param_path = "output-detect-nobn.param";
std::string bin_path = "output-detect-gray-nobn.bin";
std::string param_path = "output-detect-gray-nobn.param";
bool is_load_param_bin = false;
int target_size = atoi(argv[4]);
// init
Expand All @@ -54,8 +55,8 @@ int main(int argc, char *argv[])
image.width = atoi(argv[2]);
image.height = atoi(argv[3]);
image.data = new unsigned char[image.width * image.height * img_channel];
image.src_pixel_format = XNN_PIX_BGR2RGB; // XNN_PIX_RGB;
image.dst_pixel_format = XNN_PIX_BGR2RGB;
image.src_pixel_format = XNN_PIX_BGR2GRAY; // XNN_PIX_RGB;
image.dst_pixel_format = XNN_PIX_BGR2GRAY;

readRawData(argv[1], image.data);

Expand Down
91 changes: 91 additions & 0 deletions examples/eg_ncnn_fastv2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include "common/common.hpp"
#include "ncnn/yolo_fastv2.hpp"

#include <iostream>
#include <chrono>

#define TOPK 5
#define LOOP 5


void readRawData(const char *filename, unsigned char *data) {
FILE *fp = NULL;
// open file
fp = fopen(filename,"rb");
if(fp == NULL)
{
printf("failed to read file\n");
exit(-1);
}
// get file size
fseek (fp , 0 , SEEK_END);
long file_size = ftell(fp);
rewind(fp);
// read data
fread(data, sizeof(char), file_size, fp);
fclose(fp);
fp = NULL;
}



int main(int argc, char *argv[])
{
if (argc < 5) {
fprintf(stderr, "Usage: %s [image raw data file] [image_width] [image_height] [target_size]\n", argv[0]);
return -1;
}

// get parameters from config.json
int num_class = 1;

bool is_load_param_bin = false;
int target_size = atoi(argv[4]);
// init
xnn::yoloFastestv2 api;

api.loadModel("./output-detect-fast-opt.param", "./output-detect-fast-opt.bin", target_size);

std::vector<xnn::TargetBox> boxes;

// run
int img_channel = 3; // XNN_PIX_GRAY;
XNNImage image;
image.width = atoi(argv[2]);
image.height = atoi(argv[3]);
image.data = new unsigned char[image.width * image.height * img_channel];
image.src_pixel_format = XNN_PIX_BGR2GRAY; // XNN_PIX_RGB;
image.dst_pixel_format = XNN_PIX_BGR2GRAY;

readRawData(argv[1], image.data);

long long average_time = 0;

for (int i = 0; i < LOOP; i++) {
// start timing
auto start = std::chrono::system_clock::now();
// run classfication
api.detection(&image, boxes);

// print result
for (int i = 0; i < boxes.size(); ++i) {
fprintf(stdout, "(x1=%d, y1=%d)- (x2=%d, y2=%d), score=%.2f, category=%d\n",
boxes[i].x1, boxes[i].y1, boxes[i].x2, boxes[i].y2,
boxes[i].score * 100, boxes[i].cate);
}
fprintf(stdout, "-------------result size=%lu-------------\n", boxes.size());
// abort timer
auto end = std::chrono::system_clock::now();
auto int_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
fprintf(stdout, "%ld ms at a time.\n", int_ms.count());
average_time += int_ms.count();
}

fprintf(stdout, "The average time of %d times is %lld ms\n", LOOP, average_time / LOOP);

// release
delete []image.data;
image.data = nullptr;

return 0;
}
56 changes: 34 additions & 22 deletions src/ncnn/ncnn_detect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,14 @@ namespace xnn
return true;
}

void saveImgRawData(const char *filename, unsigned char *raw, int w, int h, int c) {
char data[128] = "";
sprintf(data, "%s.raw_%d_%d_%d", filename, w, h, c);
FILE *fp = fopen(data, "wb");
fwrite(raw, 1, w * h * c, fp);
fclose(fp);
}

XNNStatus NCNNDetect::run(XNNImage *image, std::vector<DetectObject>& objects, int topk)
{
if (!image || !image->data || image->width <= 0 || image->height <= 0)
Expand Down Expand Up @@ -254,13 +262,16 @@ namespace xnn
h = input_size_;
w = w * scale;
}
// w = input_size_;
// h = input_size_;

ncnn::Mat in;
if (image->width == input_size_ && image->height == input_size_) {
in = ncnn::Mat::from_pixels(image->data, convertXNNPixFormat2NCNN(image->src_pixel_format), input_size_, input_size_);
} else {
fprintf(stdout, "src pix format=%d\n", image->src_pixel_format);
fprintf(stdout, "imgw=%d, imgh=%d, input_size=%d, w = %d, h=%d\n", w, h, input_size_, image->width, image->height);
fprintf(stdout, "w=%d, h=%d, input_size=%d, imgw = %d, imgh=%d\n", w, h, input_size_, image->width, image->height);
//saveImgRawData("detect_input.raw", image->data, image->width, image->height, 3);
in = ncnn::Mat::from_pixels_resize(image->data, convertXNNPixFormat2NCNN(image->src_pixel_format), image->width, image->height, w, h);
}
fprintf(stdout, "xxxxx\n");
Expand Down Expand Up @@ -291,12 +302,12 @@ namespace xnn
ncnn::Mat out;
extractor.extract("output", out);
ncnn::Mat anchors(6);
anchors[0] = 19.f;
anchors[1] = 27.f;
anchors[2] = 44.f;
anchors[3] = 40.f;
anchors[4] = 38.f;
anchors[5] = 94.f;
anchors[0] = 10.f;
anchors[1] = 13.f;
anchors[2] = 16.f;
anchors[3] = 30.f;
anchors[4] = 33.f;
anchors[5] = 23.f;

std::vector<DetectObject> objects8;
generate_proposals(anchors, 8, in_pad, out, prob_threshold_, objects8);
Expand All @@ -307,14 +318,14 @@ namespace xnn
// stride 16
{
ncnn::Mat out;
extractor.extract("468", out);
extractor.extract("365", out);
ncnn::Mat anchors(6);
anchors[0] = 96.f;
anchors[1] = 68.f;
anchors[2] = 86.f;
anchors[3] = 152.f;
anchors[4] = 180.f;
anchors[5] = 137.f;
anchors[0] = 30.f;
anchors[1] = 61.f;
anchors[2] = 62.f;
anchors[3] = 45.f;
anchors[4] = 59.f;
anchors[5] = 119.f;

std::vector<DetectObject> objects16;
generate_proposals(anchors, 16, in_pad, out, prob_threshold_, objects16);
Expand All @@ -325,14 +336,14 @@ namespace xnn
// stride 32
{
ncnn::Mat out;
extractor.extract("508", out);
extractor.extract("385", out);
ncnn::Mat anchors(6);
anchors[0] = 140.f;
anchors[1] = 301.f;
anchors[2] = 303.f;
anchors[3] = 264.f;
anchors[4] = 238.f;
anchors[5] = 542.f;
anchors[0] = 116.f;
anchors[1] = 90.f;
anchors[2] = 156.f;
anchors[3] = 198.f;
anchors[4] = 373.f;
anchors[5] = 326.f;

std::vector<DetectObject> objects32;
generate_proposals(anchors, 32, in_pad, out, prob_threshold_, objects32);
Expand All @@ -350,7 +361,8 @@ namespace xnn
int count = picked.size();

objects.resize(count);
for (int i = 0; i < count; i++)
for (int i = 0; i < count; i++)\

{
objects[i] = proposals[picked[i]];

Expand Down
2 changes: 1 addition & 1 deletion src/ncnn/ncnn_detect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace xnn
int input_size_;
bool load_param_bin_;

float prob_threshold_ = 0.8f;
float prob_threshold_ = 0.5f;
float nms_threshold_ = 0.4f;

};
Expand Down
Loading

0 comments on commit 778fe54

Please sign in to comment.