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.

picture_classification.cpp 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include <thread>
  2. #include "example.h"
  3. #if LITE_BUILD_WITH_MGE
  4. #include <cstdio>
  5. #define STB_IMAGE_STATIC
  6. #define STB_IMAGE_IMPLEMENTATION
  7. #include "stb_image.h"
  8. #define STB_IMAGE_RESIZE_IMPLEMENTATION
  9. #include "stb_image_resize.h"
  10. #define STB_IMAGE_WRITE_IMPLEMENTATION
  11. #include "stb_image_write.h"
  12. using namespace lite;
  13. using namespace example;
  14. namespace {
  15. void preprocess_image(std::string pic_path, std::shared_ptr<Tensor> tensor) {
  16. int width, height, channel;
  17. uint8_t* image = stbi_load(pic_path.c_str(), &width, &height, &channel, 0);
  18. printf("Input image %s with height=%d, width=%d, channel=%d\n", pic_path.c_str(),
  19. width, height, channel);
  20. auto layout = tensor->get_layout();
  21. auto pixels = layout.shapes[2] * layout.shapes[3];
  22. for (size_t i = 0; i < layout.ndim; i++) {
  23. printf("model input shape[%zu]=%zu \n", i, layout.shapes[i]);
  24. }
  25. //! resize to tensor shape
  26. std::shared_ptr<std::vector<uint8_t>> resize_int8 =
  27. std::make_shared<std::vector<uint8_t>>(pixels * channel);
  28. stbir_resize_uint8(
  29. image, width, height, 0, resize_int8->data(), layout.shapes[2],
  30. layout.shapes[3], 0, channel);
  31. stbi_image_free(image);
  32. //! convert form rgba to bgr, relayout from hwc to chw, normalization copy to tensor
  33. float* in_data = static_cast<float*>(tensor->get_memory_ptr());
  34. for (size_t i = 0; i < pixels; i++) {
  35. in_data[i + 2 * pixels] = (resize_int8->at(i * channel + 0) - 123.675) / 58.395;
  36. in_data[i + 1 * pixels] = (resize_int8->at(i * channel + 1) - 116.280) / 57.120;
  37. in_data[i + 0 * pixels] = (resize_int8->at(i * channel + 2) - 103.530) / 57.375;
  38. }
  39. }
  40. void classfication_process(
  41. std::shared_ptr<Tensor> tensor, float& score, size_t& class_id) {
  42. auto layout = tensor->get_layout();
  43. for (size_t i = 0; i < layout.ndim; i++) {
  44. printf("model output shape[%zu]=%zu \n", i, layout.shapes[i]);
  45. }
  46. size_t nr_data = tensor->get_tensor_total_size_in_byte() / layout.get_elem_size();
  47. float* data = static_cast<float*>(tensor->get_memory_ptr());
  48. score = data[0];
  49. class_id = 0;
  50. float sum = data[0];
  51. for (size_t i = 1; i < nr_data; i++) {
  52. if (score < data[i]) {
  53. score = data[i];
  54. class_id = i;
  55. }
  56. sum += data[i];
  57. }
  58. printf("output tensor sum is %f\n", sum);
  59. }
  60. bool picture_classification(const Args& args) {
  61. std::string network_path = args.model_path;
  62. std::string input_path = args.input_path;
  63. //! create and load the network
  64. std::shared_ptr<Network> network = std::make_shared<Network>();
  65. network->load_model(network_path);
  66. //! set input data to input tensor
  67. std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0);
  68. //! copy or forward data to network
  69. preprocess_image(args.input_path, input_tensor);
  70. printf("Begin forward.\n");
  71. network->forward();
  72. network->wait();
  73. printf("End forward.\n");
  74. //! get the output data or read tensor set in network_in
  75. size_t class_id;
  76. float score;
  77. auto output_tensor = network->get_output_tensor(0);
  78. classfication_process(output_tensor, score, class_id);
  79. printf("Picture %s is class_id %zu, with score %f\n", args.input_path.c_str(),
  80. class_id, score);
  81. return 0;
  82. }
  83. } // namespace
  84. REGIST_EXAMPLE("picture_classification", picture_classification);
  85. #endif
  86. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}