You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

squeezenet_c_api.cpp 3.4 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include "c_api.h"
  15. #include <algorithm>
  16. #if defined(USE_NCNN_SIMPLEOCV)
  17. #include "simpleocv.h"
  18. #else
  19. #include <opencv2/core/core.hpp>
  20. #include <opencv2/highgui/highgui.hpp>
  21. #endif
  22. #include <stdio.h>
  23. #include <vector>
  24. static int detect_squeezenet(const cv::Mat& bgr, std::vector<float>& cls_scores)
  25. {
  26. ncnn_net_t squeezenet = ncnn_net_create();
  27. ncnn_option_t opt = ncnn_option_create();
  28. ncnn_option_set_use_vulkan_compute(opt, 1);
  29. ncnn_net_set_option(squeezenet, opt);
  30. // the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models
  31. if (ncnn_net_load_param(squeezenet, "squeezenet_v1.1.param"))
  32. exit(-1);
  33. if (ncnn_net_load_model(squeezenet, "squeezenet_v1.1.bin"))
  34. exit(-1);
  35. 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);
  36. const float mean_vals[3] = {104.f, 117.f, 123.f};
  37. ncnn_mat_substract_mean_normalize(in, mean_vals, 0);
  38. ncnn_extractor_t ex = ncnn_extractor_create(squeezenet);
  39. ncnn_extractor_input(ex, "data", in);
  40. ncnn_mat_t out;
  41. ncnn_extractor_extract(ex, "prob", &out);
  42. const int out_w = ncnn_mat_get_w(out);
  43. const float* out_data = (const float*)ncnn_mat_get_data(out);
  44. cls_scores.resize(out_w);
  45. for (int j = 0; j < out_w; j++)
  46. {
  47. cls_scores[j] = out_data[j];
  48. }
  49. ncnn_mat_destroy(in);
  50. ncnn_mat_destroy(out);
  51. ncnn_extractor_destroy(ex);
  52. ncnn_option_destroy(opt);
  53. ncnn_net_destroy(squeezenet);
  54. return 0;
  55. }
  56. static int print_topk(const std::vector<float>& cls_scores, int topk)
  57. {
  58. // partial sort topk with index
  59. int size = cls_scores.size();
  60. std::vector<std::pair<float, int> > vec;
  61. vec.resize(size);
  62. for (int i = 0; i < size; i++)
  63. {
  64. vec[i] = std::make_pair(cls_scores[i], i);
  65. }
  66. std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(),
  67. std::greater<std::pair<float, int> >());
  68. // print topk and score
  69. for (int i = 0; i < topk; i++)
  70. {
  71. float score = vec[i].first;
  72. int index = vec[i].second;
  73. fprintf(stderr, "%d = %f\n", index, score);
  74. }
  75. return 0;
  76. }
  77. int main(int argc, char** argv)
  78. {
  79. if (argc != 2)
  80. {
  81. fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
  82. return -1;
  83. }
  84. const char* imagepath = argv[1];
  85. cv::Mat m = cv::imread(imagepath, 1);
  86. if (m.empty())
  87. {
  88. fprintf(stderr, "cv::imread %s failed\n", imagepath);
  89. return -1;
  90. }
  91. std::vector<float> cls_scores;
  92. detect_squeezenet(m, cls_scores);
  93. print_topk(cls_scores, 3);
  94. return 0;
  95. }