resnet.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  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. from __future__ import absolute_import
  15. import logging
  16. from .... import UltraInferModel, ModelFormat
  17. from .... import c_lib_wrap as C
  18. class ResNet(UltraInferModel):
  19. def __init__(
  20. self,
  21. model_file,
  22. params_file="",
  23. runtime_option=None,
  24. model_format=ModelFormat.ONNX,
  25. ):
  26. """Load a image classification model exported by torchvision.ResNet.
  27. :param model_file: (str)Path of model file, e.g resnet/resnet50.onnx
  28. :param params_file: (str)Path of parameters file, if the model_fomat is ModelFormat.ONNX, this param will be ignored, can be set as empty string
  29. :param runtime_option: (ultra_infer.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU
  30. :param model_format: (ultra_infer.ModelForamt)Model format of the loaded model, default is ONNX
  31. """
  32. # call super() to initialize the backend_option
  33. # the result of initialization will be saved in self._runtime_option
  34. super(ResNet, self).__init__(runtime_option)
  35. self._model = C.vision.classification.ResNet(
  36. model_file, params_file, self._runtime_option, model_format
  37. )
  38. # self.initialized shows the initialization of the model is successful or not
  39. assert self.initialized, "ResNet initialize failed."
  40. # Predict and return the inference result of "input_image".
  41. def predict(self, input_image, topk=1):
  42. """Classify an input image
  43. :param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  44. :param topk: (int)The topk result by the classify confidence score, default 1
  45. :return: ClassifyResult
  46. """
  47. return self._model.predict(input_image, topk)
  48. # Implement the setter and getter method for variables
  49. @property
  50. def size(self):
  51. """
  52. Returns the preprocess image size, default size = [224, 224];
  53. """
  54. return self._model.size
  55. @property
  56. def mean_vals(self):
  57. """
  58. Returns the mean value of normlization, default mean_vals = [0.485f, 0.456f, 0.406f];
  59. """
  60. return self._model.mean_vals
  61. @property
  62. def std_vals(self):
  63. """
  64. Returns the std value of normlization, default std_vals = [0.229f, 0.224f, 0.225f];
  65. """
  66. return self._model.std_vals
  67. @size.setter
  68. def size(self, wh):
  69. assert isinstance(
  70. wh, (list, tuple)
  71. ), "The value to set `size` must be type of tuple or list."
  72. assert (
  73. len(wh) == 2
  74. ), "The value to set `size` must contatins 2 elements means [width, height], but now it contains {} elements.".format(
  75. len(wh)
  76. )
  77. self._model.size = wh
  78. @mean_vals.setter
  79. def mean_vals(self, value):
  80. assert isinstance(
  81. value, list
  82. ), "The value to set `mean_vals` must be type of list."
  83. self._model.mean_vals = value
  84. @std_vals.setter
  85. def std_vals(self, value):
  86. assert isinstance(
  87. value, list
  88. ), "The value to set `std_vals` must be type of list."
  89. self._model.std_vals = value