export_model.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright (c) 2020 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. import os
  15. import sys
  16. __dir__ = os.path.dirname(os.path.abspath(__file__))
  17. sys.path.append(__dir__)
  18. sys.path.append(os.path.abspath(os.path.join(__dir__, '..')))
  19. import argparse
  20. import paddle
  21. from paddle.jit import to_static
  22. from ppocr.modeling.architectures import build_model
  23. from ppocr.postprocess import build_post_process
  24. from ppocr.utils.save_load import init_model
  25. from ppocr.utils.logging import get_logger
  26. from tools.program import load_config, merge_config, ArgsParser
  27. def parse_args():
  28. parser = argparse.ArgumentParser()
  29. parser.add_argument("-c", "--config", help="configuration file to use")
  30. parser.add_argument(
  31. "-o", "--output_path", type=str, default='./output/infer/')
  32. return parser.parse_args()
  33. def main():
  34. FLAGS = ArgsParser().parse_args()
  35. config = load_config(FLAGS.config)
  36. merge_config(FLAGS.opt)
  37. logger = get_logger()
  38. # build post process
  39. post_process_class = build_post_process(config['PostProcess'],
  40. config['Global'])
  41. # build model
  42. # for rec algorithm
  43. if hasattr(post_process_class, 'character'):
  44. char_num = len(getattr(post_process_class, 'character'))
  45. config['Architecture']["Head"]['out_channels'] = char_num
  46. model = build_model(config['Architecture'])
  47. init_model(config, model, logger)
  48. model.eval()
  49. save_path = '{}/inference'.format(config['Global']['save_inference_dir'])
  50. if config['Architecture']['algorithm'] == "SRN":
  51. other_shape = [
  52. paddle.static.InputSpec(
  53. shape=[None, 1, 64, 256], dtype='float32'), [
  54. paddle.static.InputSpec(
  55. shape=[None, 256, 1],
  56. dtype="int64"), paddle.static.InputSpec(
  57. shape=[None, 25, 1],
  58. dtype="int64"), paddle.static.InputSpec(
  59. shape=[None, 8, 25, 25], dtype="int64"),
  60. paddle.static.InputSpec(
  61. shape=[None, 8, 25, 25], dtype="int64")
  62. ]
  63. ]
  64. model = to_static(model, input_spec=other_shape)
  65. else:
  66. infer_shape = [3, -1, -1]
  67. if config['Architecture']['model_type'] == "rec":
  68. infer_shape = [3, 32, -1] # for rec model, H must be 32
  69. if 'Transform' in config['Architecture'] and config['Architecture'][
  70. 'Transform'] is not None and config['Architecture'][
  71. 'Transform']['name'] == 'TPS':
  72. logger.info(
  73. 'When there is tps in the network, variable length input is not supported, and the input size needs to be the same as during training'
  74. )
  75. infer_shape[-1] = 100
  76. model = to_static(
  77. model,
  78. input_spec=[
  79. paddle.static.InputSpec(
  80. shape=[None] + infer_shape, dtype='float32')
  81. ])
  82. paddle.jit.save(model, save_path)
  83. logger.info('inference model is saved to {}'.format(save_path))
  84. if __name__ == "__main__":
  85. main()