1 // Tencent is pleased to support the open source community by making ncnn available.
2 //
3 // Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
4 //
5 // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6 // in compliance with the License. You may obtain a copy of the License at
7 //
8 // https://opensource.org/licenses/BSD-3-Clause
9 //
10 // Unless required by applicable law or agreed to in writing, software distributed
11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13 // specific language governing permissions and limitations under the License.
14 
15 #include "net.h"
16 
17 #include <algorithm>
18 #if defined(USE_NCNN_SIMPLEOCV)
19 #include "simpleocv.h"
20 #else
21 #include <opencv2/core/core.hpp>
22 #include <opencv2/highgui/highgui.hpp>
23 #endif
24 #include <stdio.h>
25 #include <vector>
26 
detect_squeezenet(const cv::Mat & bgr,std::vector<float> & cls_scores)27 static int detect_squeezenet(const cv::Mat& bgr, std::vector<float>& cls_scores)
28 {
29     ncnn::Net squeezenet;
30 
31     squeezenet.opt.use_vulkan_compute = true;
32 
33     // the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models
34     squeezenet.load_param("squeezenet_v1.1.param");
35     squeezenet.load_model("squeezenet_v1.1.bin");
36 
37     ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, 227, 227);
38 
39     const float mean_vals[3] = {104.f, 117.f, 123.f};
40     in.substract_mean_normalize(mean_vals, 0);
41 
42     ncnn::Extractor ex = squeezenet.create_extractor();
43 
44     ex.input("data", in);
45 
46     ncnn::Mat out;
47     ex.extract("prob", out);
48 
49     cls_scores.resize(out.w);
50     for (int j = 0; j < out.w; j++)
51     {
52         cls_scores[j] = out[j];
53     }
54 
55     return 0;
56 }
57 
print_topk(const std::vector<float> & cls_scores,int topk)58 static int print_topk(const std::vector<float>& cls_scores, int topk)
59 {
60     // partial sort topk with index
61     int size = cls_scores.size();
62     std::vector<std::pair<float, int> > vec;
63     vec.resize(size);
64     for (int i = 0; i < size; i++)
65     {
66         vec[i] = std::make_pair(cls_scores[i], i);
67     }
68 
69     std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(),
70                       std::greater<std::pair<float, int> >());
71 
72     // print topk and score
73     for (int i = 0; i < topk; i++)
74     {
75         float score = vec[i].first;
76         int index = vec[i].second;
77         fprintf(stderr, "%d = %f\n", index, score);
78     }
79 
80     return 0;
81 }
82 
main(int argc,char ** argv)83 int main(int argc, char** argv)
84 {
85     if (argc != 2)
86     {
87         fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
88         return -1;
89     }
90 
91     const char* imagepath = argv[1];
92 
93     cv::Mat m = cv::imread(imagepath, 1);
94     if (m.empty())
95     {
96         fprintf(stderr, "cv::imread %s failed\n", imagepath);
97         return -1;
98     }
99 
100     std::vector<float> cls_scores;
101     detect_squeezenet(m, cls_scores);
102 
103     print_topk(cls_scores, 3);
104 
105     return 0;
106 }
107