| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2020 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 "c_api.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 | |
| 27 | static int detect_squeezenet(const cv::Mat& bgr, std::vector<float>& cls_scores) |
| 28 | { |
| 29 | ncnn_net_t squeezenet = ncnn_net_create(); |
| 30 | |
| 31 | ncnn_option_t opt = ncnn_option_create(); |
| 32 | ncnn_option_set_use_vulkan_compute(opt, 1); |
| 33 | |
| 34 | ncnn_net_set_option(squeezenet, opt); |
| 35 | |
| 36 | // the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models |
| 37 | if (ncnn_net_load_param(squeezenet, "squeezenet_v1.1.param")) |
| 38 | exit(-1); |
| 39 | if (ncnn_net_load_model(squeezenet, "squeezenet_v1.1.bin")) |
| 40 | exit(-1); |
| 41 | |
| 42 | ncnn_mat_t in = ncnn_mat_from_pixels_resize(bgr.data, NCNN_MAT_PIXEL_BGR, bgr.cols, bgr.rows, bgr.cols * 3, 227, 227, NULL); |
| 43 | |
| 44 | const float mean_vals[3] = {104.f, 117.f, 123.f}; |
| 45 | ncnn_mat_substract_mean_normalize(in, mean_vals, 0); |
| 46 | |
| 47 | ncnn_extractor_t ex = ncnn_extractor_create(squeezenet); |
| 48 | |
| 49 | ncnn_extractor_input(ex, "data", in); |
| 50 | |
| 51 | ncnn_mat_t out; |
| 52 | ncnn_extractor_extract(ex, "prob", &out); |
| 53 | |
| 54 | const int out_w = ncnn_mat_get_w(out); |
| 55 | const float* out_data = (const float*)ncnn_mat_get_data(out); |
| 56 | |
| 57 | cls_scores.resize(out_w); |
| 58 | for (int j = 0; j < out_w; j++) |
| 59 | { |
| 60 | cls_scores[j] = out_data[j]; |
| 61 | } |
| 62 | |
| 63 | ncnn_mat_destroy(in); |
| 64 | ncnn_mat_destroy(out); |
| 65 | |
| 66 | ncnn_extractor_destroy(ex); |
| 67 | |
| 68 | ncnn_option_destroy(opt); |
| 69 | |
| 70 | ncnn_net_destroy(squeezenet); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int print_topk(const std::vector<float>& cls_scores, int topk) |
| 76 | { |
| 77 | // partial sort topk with index |
| 78 | int size = cls_scores.size(); |
| 79 | std::vector<std::pair<float, int> > vec; |
| 80 | vec.resize(size); |
| 81 | for (int i = 0; i < size; i++) |
| 82 | { |
| 83 | vec[i] = std::make_pair(cls_scores[i], i); |
| 84 | } |
| 85 | |
| 86 | std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(), |
| 87 | std::greater<std::pair<float, int> >()); |
| 88 | |
| 89 | // print topk and score |
| 90 | for (int i = 0; i < topk; i++) |
| 91 | { |
| 92 | float score = vec[i].first; |
| 93 | int index = vec[i].second; |
| 94 | fprintf(stderr, "%d = %f\n", index, score); |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | int main(int argc, char** argv) |
| 101 | { |
| 102 | if (argc != 2) |
| 103 | { |
| 104 | fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]); |
| 105 | return -1; |
| 106 | } |
| 107 | |
| 108 | const char* imagepath = argv[1]; |
| 109 | |
| 110 | cv::Mat m = cv::imread(imagepath, 1); |
| 111 | if (m.empty()) |
| 112 | { |
| 113 | fprintf(stderr, "cv::imread %s failed\n", imagepath); |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | std::vector<float> cls_scores; |
| 118 | detect_squeezenet(m, cls_scores); |
| 119 | |
| 120 | print_topk(cls_scores, 3); |
| 121 | |
| 122 | return 0; |
| 123 | } |