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.3 kB

6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. ncnn_net_load_param(squeezenet, "squeezenet_v1.1.param");
  32. ncnn_net_load_model(squeezenet, "squeezenet_v1.1.bin");
  33. 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);
  34. const float mean_vals[3] = {104.f, 117.f, 123.f};
  35. ncnn_mat_substract_mean_normalize(in, mean_vals, 0);
  36. ncnn_extractor_t ex = ncnn_extractor_create(squeezenet);
  37. ncnn_extractor_input(ex, "data", in);
  38. ncnn_mat_t out;
  39. ncnn_extractor_extract(ex, "prob", &out);
  40. const int out_w = ncnn_mat_get_w(out);
  41. const float* out_data = (const float*)ncnn_mat_get_data(out);
  42. cls_scores.resize(out_w);
  43. for (int j = 0; j < out_w; j++)
  44. {
  45. cls_scores[j] = out_data[j];
  46. }
  47. ncnn_mat_destroy(in);
  48. ncnn_mat_destroy(out);
  49. ncnn_extractor_destroy(ex);
  50. ncnn_option_destroy(opt);
  51. ncnn_net_destroy(squeezenet);
  52. return 0;
  53. }
  54. static int print_topk(const std::vector<float>& cls_scores, int topk)
  55. {
  56. // partial sort topk with index
  57. int size = cls_scores.size();
  58. std::vector<std::pair<float, int> > vec;
  59. vec.resize(size);
  60. for (int i = 0; i < size; i++)
  61. {
  62. vec[i] = std::make_pair(cls_scores[i], i);
  63. }
  64. std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(),
  65. std::greater<std::pair<float, int> >());
  66. // print topk and score
  67. for (int i = 0; i < topk; i++)
  68. {
  69. float score = vec[i].first;
  70. int index = vec[i].second;
  71. fprintf(stderr, "%d = %f\n", index, score);
  72. }
  73. return 0;
  74. }
  75. int main(int argc, char** argv)
  76. {
  77. if (argc != 2)
  78. {
  79. fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
  80. return -1;
  81. }
  82. const char* imagepath = argv[1];
  83. cv::Mat m = cv::imread(imagepath, 1);
  84. if (m.empty())
  85. {
  86. fprintf(stderr, "cv::imread %s failed\n", imagepath);
  87. return -1;
  88. }
  89. std::vector<float> cls_scores;
  90. detect_squeezenet(m, cls_scores);
  91. print_topk(cls_scores, 3);
  92. return 0;
  93. }