resnet.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #pragma once
  15. #include "ultra_infer/ultra_infer_model.h"
  16. #include "ultra_infer/vision/common/processors/transform.h"
  17. #include "ultra_infer/vision/common/result.h"
  18. // The namespace should be
  19. // ultra_infer::vision::classification (ultra_infer::vision::${task})
  20. namespace ultra_infer {
  21. namespace vision {
  22. /** \brief All object classification model APIs are defined inside this
  23. * namespace
  24. *
  25. */
  26. namespace classification {
  27. /*! @brief Torchvision ResNet series model
  28. */
  29. class ULTRAINFER_DECL ResNet : public UltraInferModel {
  30. public:
  31. /** \brief Set path of model file and the configuration of runtime.
  32. *
  33. * \param[in] model_file Path of model file, e.g ./resnet50.onnx
  34. * \param[in] params_file Path of parameter file, e.g ppyoloe/model.pdiparams,
  35. * if the model format is ONNX, this parameter will be ignored \param[in]
  36. * custom_option RuntimeOption for inference, the default will use cpu, and
  37. * choose the backend defined in "valid_cpu_backends" \param[in] model_format
  38. * Model format of the loaded model, default is ONNX format
  39. */
  40. ResNet(const std::string &model_file, const std::string &params_file = "",
  41. const RuntimeOption &custom_option = RuntimeOption(),
  42. const ModelFormat &model_format = ModelFormat::ONNX);
  43. virtual std::string ModelName() const { return "ResNet"; }
  44. /** \brief Predict for the input "im", the result will be saved in "result".
  45. *
  46. * \param[in] im The input image data, comes from cv::imread(), is a 3-D array
  47. * with layout HWC, BGR format \param[in] result Saving the inference result.
  48. * \param[in] topk The length of return values, e.g., if topk==2, the result
  49. * will include the 2 most possible class label for input image.
  50. */
  51. virtual bool Predict(cv::Mat *im, ClassifyResult *result, int topk = 1);
  52. /*! @brief
  53. Argument for image preprocessing step, tuple of (width, height), decide the
  54. target size after resize, default size = {224, 224}
  55. */
  56. std::vector<int> size;
  57. /*! @brief
  58. Mean parameters for normalize, size should be the the same as channels,
  59. default mean_vals = {0.485f, 0.456f, 0.406f}
  60. */
  61. std::vector<float> mean_vals;
  62. /*! @brief
  63. Std parameters for normalize, size should be the the same as channels, default
  64. std_vals = {0.229f, 0.224f, 0.225f}
  65. */
  66. std::vector<float> std_vals;
  67. private:
  68. /*! @brief Initialize for ResNet model, assign values to the global variables
  69. * and call InitRuntime()
  70. */
  71. bool Initialize();
  72. /// PreProcessing for the input "mat", the result will be saved in "outputs".
  73. bool Preprocess(Mat *mat, FDTensor *outputs);
  74. /*! @brief PostProcessing for the input "infer_result", the result will be
  75. * saved in "result".
  76. */
  77. bool Postprocess(FDTensor &infer_result, ClassifyResult *result,
  78. int topk = 1);
  79. };
  80. } // namespace classification
  81. } // namespace vision
  82. } // namespace ultra_infer