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 2.7 kB

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