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.cpp 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2017 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 "net.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 squeezenet;
  27. squeezenet.opt.use_vulkan_compute = true;
  28. // the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models
  29. squeezenet.load_param("squeezenet_v1.1.param");
  30. squeezenet.load_model("squeezenet_v1.1.bin");
  31. ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, 227, 227);
  32. const float mean_vals[3] = {104.f, 117.f, 123.f};
  33. in.substract_mean_normalize(mean_vals, 0);
  34. ncnn::Extractor ex = squeezenet.create_extractor();
  35. ex.input("data", in);
  36. ncnn::Mat out;
  37. ex.extract("prob", out);
  38. cls_scores.resize(out.w);
  39. for (int j = 0; j < out.w; j++)
  40. {
  41. cls_scores[j] = out[j];
  42. }
  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. }