cpp_postproc_test.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include <pybind11/pybind11.h>
  2. #include <pybind11/stl.h>
  3. #include <memory>
  4. #include <functional>
  5. #include <string>
  6. #include <unordered_map>
  7. #include <vector>
  8. #include "pypostproc.h"
  9. namespace py = pybind11;
  10. bool TestPyPostproc(const std::unordered_map<std::string, std::string> &params) {
  11. auto model = std::make_shared<edk::ModelLoader>("data/test_model.cambricon", "subnet0");
  12. std::vector<float*> outputs;
  13. for (uint32_t i = 0; i < model->OutputNum(); ++i) {
  14. outputs.push_back(new float[model->OutputShape(i).DataCount()]);
  15. }
  16. auto free_outputs = [&outputs] () {
  17. for (auto ptr : outputs) delete[] ptr;
  18. };
  19. cnstream::PyPostproc pypostproc;
  20. if (!pypostproc.Init(params)) {
  21. free_outputs();
  22. return false;
  23. }
  24. bool ret = 0 == pypostproc.Execute(outputs, model, nullptr);
  25. free_outputs();
  26. return ret;
  27. }
  28. bool TestPyObjPostproc(const std::unordered_map<std::string, std::string> &params) {
  29. auto model = std::make_shared<edk::ModelLoader>("data/test_model.cambricon", "subnet0");
  30. std::vector<float*> outputs;
  31. for (uint32_t i = 0; i < model->OutputNum(); ++i) {
  32. outputs.push_back(new float[model->OutputShape(i).DataCount()]);
  33. }
  34. auto free_outputs = [&outputs] () {
  35. for (auto ptr : outputs) delete[] ptr;
  36. };
  37. cnstream::PyObjPostproc pyobjpostproc;
  38. if (!pyobjpostproc.Init(params)) {
  39. free_outputs();
  40. return false;
  41. }
  42. bool ret = 0 == pyobjpostproc.Execute(outputs, model, nullptr, nullptr);
  43. free_outputs();
  44. return ret;
  45. }
  46. void PostprocTestWrapper(py::module &m) { // NOLINT
  47. m.def("cpptest_pypostproc", &TestPyPostproc);
  48. m.def("cpptest_pyobjpostproc", &TestPyObjPostproc);
  49. }