18
#if defined(USE_NCNN_SIMPLEOCV)
21
#include <opencv2/core/core.hpp>
22
#include <opencv2/highgui/highgui.hpp>
27
static int detect_shufflenetv2(const cv::Mat& bgr, std::vector<float>& cls_scores)
29
ncnn::Net shufflenetv2;
31
shufflenetv2.opt.use_vulkan_compute = true;
35
if (shufflenetv2.load_param("shufflenet_v2_x0.5.param"))
37
if (shufflenetv2.load_model("shufflenet_v2_x0.5.bin"))
40
ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, 224, 224);
42
const float norm_vals[3] = {1 / 255.f, 1 / 255.f, 1 / 255.f};
43
in.substract_mean_normalize(0, norm_vals);
45
ncnn::Extractor ex = shufflenetv2.create_extractor();
50
ex.extract("fc", out);
56
ncnn::Layer* softmax = ncnn::create_layer("Softmax");
59
softmax->load_param(pd);
61
softmax->forward_inplace(out, shufflenetv2.opt);
66
out = out.reshape(out.w * out.h * out.c);
68
cls_scores.resize(out.w);
69
for (int j = 0; j < out.w; j++)
71
cls_scores[j] = out[j];
77
static int print_topk(const std::vector<float>& cls_scores, int topk)
80
int size = cls_scores.size();
81
std::vector<std::pair<float, int> > vec;
83
for (int i = 0; i < size; i++)
85
vec[i] = std::make_pair(cls_scores[i], i);
88
std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(),
89
std::greater<std::pair<float, int> >());
92
for (int i = 0; i < topk; i++)
94
float score = vec[i].first;
95
int index = vec[i].second;
96
fprintf(stderr, "%d = %f\n", index, score);
102
int main(int argc, char** argv)
106
fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
110
const char* imagepath = argv[1];
112
cv::Mat m = cv::imread(imagepath, 1);
115
fprintf(stderr, "cv::imread %s failed\n", imagepath);
119
std::vector<float> cls_scores;
120
detect_shufflenetv2(m, cls_scores);
122
print_topk(cls_scores, 3);