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

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