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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.c);
  34. for (int j=0; j<out.c; j++)
  35. {
  36. const float* prob = out.data + out.cstep * j;
  37. cls_scores[j] = prob[0];
  38. }
  39. return 0;
  40. }
  41. static int print_topk(const std::vector<float>& cls_scores, int topk)
  42. {
  43. // partial sort topk with index
  44. int size = cls_scores.size();
  45. std::vector< std::pair<float, int> > vec;
  46. vec.resize(size);
  47. for (int i=0; i<size; i++)
  48. {
  49. vec[i] = std::make_pair(cls_scores[i], i);
  50. }
  51. std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(),
  52. std::greater< std::pair<float, int> >());
  53. // print topk and score
  54. for (int i=0; i<topk; i++)
  55. {
  56. float score = vec[i].first;
  57. int index = vec[i].second;
  58. fprintf(stderr, "%d = %f\n", index, score);
  59. }
  60. return 0;
  61. }
  62. int main(int argc, char** argv)
  63. {
  64. const char* imagepath = argv[1];
  65. cv::Mat m = cv::imread(imagepath, CV_LOAD_IMAGE_COLOR);
  66. if (m.empty())
  67. {
  68. fprintf(stderr, "cv::imread %s failed\n", imagepath);
  69. return -1;
  70. }
  71. std::vector<float> cls_scores;
  72. detect_squeezenet(m, cls_scores);
  73. print_topk(cls_scores, 3);
  74. return 0;
  75. }