resnet_pybind.cc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "ultra_infer/pybind/main.h"
  15. // namespace should be `ultra_infer`
  16. namespace ultra_infer {
  17. // the name of Pybind function should be Bind${model_name}
  18. void BindResNet(pybind11::module &m) {
  19. // the constructor and the predict function are necessary
  20. // the constructor is used to initialize the python model class.
  21. // the necessary public functions and variables like `size`, `mean_vals`
  22. // should also be binded.
  23. pybind11::class_<vision::classification::ResNet, UltraInferModel>(m, "ResNet")
  24. .def(pybind11::init<std::string, std::string, RuntimeOption,
  25. ModelFormat>())
  26. .def("predict",
  27. [](vision::classification::ResNet &self, pybind11::array &data,
  28. int topk = 1) {
  29. auto mat = PyArrayToCvMat(data);
  30. vision::ClassifyResult res;
  31. self.Predict(&mat, &res, topk);
  32. return res;
  33. })
  34. .def_readwrite("size", &vision::classification::ResNet::size)
  35. .def_readwrite("mean_vals", &vision::classification::ResNet::mean_vals)
  36. .def_readwrite("std_vals", &vision::classification::ResNet::std_vals);
  37. }
  38. } // namespace ultra_infer