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