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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 <stdio.h>
  15. #include <algorithm>
  16. #include <vector>
  17. #include <opencv2/core/core.hpp>
  18. #include <opencv2/highgui/highgui.hpp>
  19. #include "net.h"
  20. static int detect_squeezenet(const cv::Mat& bgr, std::vector<float>& cls_scores)
  21. {
  22. ncnn::Net squeezenet;
  23. squeezenet.load_param("squeezenet_v1.1.param");
  24. squeezenet.load_model("squeezenet_v1.1.bin");
  25. ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, 227, 227);
  26. const float mean_vals[3] = {104.f, 117.f, 123.f};
  27. in.substract_mean_normalize(mean_vals, 0);
  28. ncnn::Extractor ex = squeezenet.create_extractor();
  29. ex.set_light_mode(true);
  30. ex.input("data", in);
  31. ncnn::Mat out;
  32. ex.extract("prob", out);
  33. cls_scores.resize(out.w);
  34. for (int j=0; j<out.w; j++)
  35. {
  36. cls_scores[j] = out[j];
  37. }
  38. return 0;
  39. }
  40. static int print_topk(const std::vector<float>& cls_scores, int topk)
  41. {
  42. // partial sort topk with index
  43. int size = cls_scores.size();
  44. std::vector< std::pair<float, int> > vec;
  45. vec.resize(size);
  46. for (int i=0; i<size; i++)
  47. {
  48. vec[i] = std::make_pair(cls_scores[i], i);
  49. }
  50. std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(),
  51. std::greater< std::pair<float, int> >());
  52. // print topk and score
  53. for (int i=0; i<topk; i++)
  54. {
  55. float score = vec[i].first;
  56. int index = vec[i].second;
  57. fprintf(stderr, "%d = %f\n", index, score);
  58. }
  59. return 0;
  60. }
  61. int main(int argc, char** argv)
  62. {
  63. const char* imagepath = argv[1];
  64. cv::Mat m = cv::imread(imagepath, CV_LOAD_IMAGE_COLOR);
  65. if (m.empty())
  66. {
  67. fprintf(stderr, "cv::imread %s failed\n", imagepath);
  68. return -1;
  69. }
  70. std::vector<float> cls_scores;
  71. detect_squeezenet(m, cls_scores);
  72. print_topk(cls_scores, 3);
  73. return 0;
  74. }