anemigan.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright (c) 2024 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. from __future__ import absolute_import
  15. from .... import UltraInferModel, ModelFormat
  16. from .... import c_lib_wrap as C
  17. class AnimeGANPreprocessor:
  18. def __init__(self, config_file):
  19. """Create a preprocessor for AnimeGAN."""
  20. self._preprocessor = C.vision.generation.AnimeGANPreprocessor()
  21. def run(self, input_ims):
  22. """Preprocess input images for AnimeGAN.
  23. :param: input_ims: (list of numpy.ndarray)The input image
  24. :return: list of FDTensor
  25. """
  26. return self._preprocessor.run(input_ims)
  27. class AnimeGANPostprocessor:
  28. def __init__(self):
  29. """Create a postprocessor for AnimeGAN."""
  30. self._postprocessor = C.vision.generation.AnimeGANPostprocessor()
  31. def run(self, runtime_results):
  32. """Postprocess the runtime results for AnimeGAN
  33. :param: runtime_results: (list of FDTensor)The output FDTensor results from runtime
  34. :return: results: (list) Final results
  35. """
  36. return self._postprocessor.run(runtime_results)
  37. class AnimeGAN(UltraInferModel):
  38. def __init__(
  39. self,
  40. model_file,
  41. params_file="",
  42. runtime_option=None,
  43. model_format=ModelFormat.PADDLE,
  44. ):
  45. """Load a AnimeGAN model.
  46. :param model_file: (str)Path of model file, e.g ./model.pdmodel
  47. :param params_file: (str)Path of parameters file, e.g ./model.pdiparams, if the model_fomat is ModelFormat.ONNX, this param will be ignored, can be set as empty string
  48. :param runtime_option: (ultra_infer.RuntimeOption)RuntimeOption for inference this model, if it's None, will use the default backend on CPU
  49. :param model_format: (ultra_infer.ModelForamt)Model format of the loaded model
  50. """
  51. # call super constructor to initialize self._runtime_option
  52. super(AnimeGAN, self).__init__(runtime_option)
  53. self._model = C.vision.generation.AnimeGAN(
  54. model_file, params_file, self._runtime_option, model_format
  55. )
  56. # assert self.initialized to confirm initialization successfully.
  57. assert self.initialized, "AnimeGAN initialize failed."
  58. def predict(self, input_image):
  59. """Predict the style transfer result for an input image
  60. :param input_image: (numpy.ndarray)The input image data, 3-D array with layout HWC, BGR format
  61. :return: style transfer result
  62. """
  63. return self._model.predict(input_image)
  64. def batch_predict(self, input_images):
  65. """Predict the style transfer result for multiple input images
  66. :param input_images: (list of numpy.ndarray)The list of input image data, each image is a 3-D array with layout HWC, BGR format
  67. :return: a list of style transfer results
  68. """
  69. return self._model.batch_predict(input_images)
  70. @property
  71. def preprocessor(self):
  72. """Get AnimeGANPreprocessor object of the loaded model
  73. :return AnimeGANPreprocessor
  74. """
  75. return self._model.preprocessor
  76. @property
  77. def postprocessor(self):
  78. """Get AnimeGANPostprocessor object of the loaded model
  79. :return AnimeGANPostprocessor
  80. """
  81. return self._model.postprocessor