synthesisers.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright (c) 2020 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. import os
  15. from utils.config import ArgsParser, load_config, override_config
  16. from utils.logging import get_logger
  17. from engine import style_samplers, corpus_generators, text_drawers, predictors, writers
  18. class ImageSynthesiser(object):
  19. def __init__(self):
  20. self.FLAGS = ArgsParser().parse_args()
  21. self.config = load_config(self.FLAGS.config)
  22. self.config = override_config(self.config, options=self.FLAGS.override)
  23. self.output_dir = self.config["Global"]["output_dir"]
  24. if not os.path.exists(self.output_dir):
  25. os.mkdir(self.output_dir)
  26. self.logger = get_logger(
  27. log_file='{}/predict.log'.format(self.output_dir))
  28. self.text_drawer = text_drawers.StdTextDrawer(self.config)
  29. predictor_method = self.config["Predictor"]["method"]
  30. assert predictor_method is not None
  31. self.predictor = getattr(predictors, predictor_method)(self.config)
  32. def synth_image(self, corpus, style_input, language="en"):
  33. corpus, text_input = self.text_drawer.draw_text(corpus, language)
  34. synth_result = self.predictor.predict(style_input, text_input)
  35. return synth_result
  36. class DatasetSynthesiser(ImageSynthesiser):
  37. def __init__(self):
  38. super(DatasetSynthesiser, self).__init__()
  39. self.tag = self.FLAGS.tag
  40. self.output_num = self.config["Global"]["output_num"]
  41. corpus_generator_method = self.config["CorpusGenerator"]["method"]
  42. self.corpus_generator = getattr(corpus_generators,
  43. corpus_generator_method)(self.config)
  44. style_sampler_method = self.config["StyleSampler"]["method"]
  45. assert style_sampler_method is not None
  46. self.style_sampler = style_samplers.DatasetSampler(self.config)
  47. self.writer = writers.SimpleWriter(self.config, self.tag)
  48. def synth_dataset(self):
  49. for i in range(self.output_num):
  50. style_data = self.style_sampler.sample()
  51. style_input = style_data["image"]
  52. corpus_language, text_input_label = self.corpus_generator.generate(
  53. )
  54. text_input_label, text_input = self.text_drawer.draw_text(
  55. text_input_label, corpus_language)
  56. synth_result = self.predictor.predict(style_input, text_input)
  57. fake_fusion = synth_result["fake_fusion"]
  58. self.writer.save_image(fake_fusion, text_input_label)
  59. self.writer.save_label()
  60. self.writer.merge_label()