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 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "platform.h"
  20. #include "net.h"
  21. #if NCNN_VULKAN
  22. #include "gpu.h"
  23. #endif // NCNN_VULKAN
  24. static int detect_squeezenet(const cv::Mat& bgr, std::vector<float>& cls_scores)
  25. {
  26. ncnn::Net squeezenet;
  27. #if NCNN_VULKAN
  28. squeezenet.opt.use_vulkan_compute = true;
  29. #endif // NCNN_VULKAN
  30. squeezenet.load_param("squeezenet_v1.1.param");
  31. squeezenet.load_model("squeezenet_v1.1.bin");
  32. ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, 227, 227);
  33. const float mean_vals[3] = {104.f, 117.f, 123.f};
  34. in.substract_mean_normalize(mean_vals, 0);
  35. ncnn::Extractor ex = squeezenet.create_extractor();
  36. ex.input("data", in);
  37. ncnn::Mat out;
  38. ex.extract("prob", out);
  39. cls_scores.resize(out.w);
  40. for (int j=0; j<out.w; j++)
  41. {
  42. cls_scores[j] = out[j];
  43. }
  44. return 0;
  45. }
  46. static int print_topk(const std::vector<float>& cls_scores, int topk)
  47. {
  48. // partial sort topk with index
  49. int size = cls_scores.size();
  50. std::vector< std::pair<float, int> > vec;
  51. vec.resize(size);
  52. for (int i=0; i<size; i++)
  53. {
  54. vec[i] = std::make_pair(cls_scores[i], i);
  55. }
  56. std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(),
  57. std::greater< std::pair<float, int> >());
  58. // print topk and score
  59. for (int i=0; i<topk; i++)
  60. {
  61. float score = vec[i].first;
  62. int index = vec[i].second;
  63. fprintf(stderr, "%d = %f\n", index, score);
  64. }
  65. return 0;
  66. }
  67. int main(int argc, char** argv)
  68. {
  69. if (argc != 2)
  70. {
  71. fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
  72. return -1;
  73. }
  74. const char* imagepath = argv[1];
  75. cv::Mat m = cv::imread(imagepath, 1);
  76. if (m.empty())
  77. {
  78. fprintf(stderr, "cv::imread %s failed\n", imagepath);
  79. return -1;
  80. }
  81. #if NCNN_VULKAN
  82. ncnn::create_gpu_instance();
  83. #endif // NCNN_VULKAN
  84. std::vector<float> cls_scores;
  85. detect_squeezenet(m, cls_scores);
  86. #if NCNN_VULKAN
  87. ncnn::destroy_gpu_instance();
  88. #endif // NCNN_VULKAN
  89. print_topk(cls_scores, 3);
  90. return 0;
  91. }