export_model.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright (c) 2019 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 __future__ import division
  16. from __future__ import print_function
  17. import os
  18. import sys
  19. # add python path of PadleDetection to sys.path
  20. parent_path = os.path.abspath(os.path.join(__file__, *(['..'] * 2)))
  21. if parent_path not in sys.path:
  22. sys.path.append(parent_path)
  23. from paddle import fluid
  24. import logging
  25. FORMAT = '%(asctime)s-%(levelname)s: %(message)s'
  26. logging.basicConfig(level=logging.INFO, format=FORMAT)
  27. logger = logging.getLogger(__name__)
  28. try:
  29. from ppdet.core.workspace import load_config, merge_config, create
  30. from ppdet.utils.cli import ArgsParser
  31. import ppdet.utils.checkpoint as checkpoint
  32. from ppdet.utils.export_utils import save_infer_model, dump_infer_config
  33. from ppdet.utils.check import check_config, check_version, check_py_func, enable_static_mode
  34. except ImportError as e:
  35. if sys.argv[0].find('static') >= 0:
  36. logger.error("Importing ppdet failed when running static model "
  37. "with error: {}\n"
  38. "please try:\n"
  39. "\t1. run static model under PaddleDetection/static "
  40. "directory\n"
  41. "\t2. run 'pip uninstall ppdet' to uninstall ppdet "
  42. "dynamic version firstly.".format(e))
  43. sys.exit(-1)
  44. else:
  45. raise e
  46. def main():
  47. cfg = load_config(FLAGS.config)
  48. merge_config(FLAGS.opt)
  49. check_config(cfg)
  50. check_version()
  51. main_arch = cfg.architecture
  52. # Use CPU for exporting inference model instead of GPU
  53. place = fluid.CPUPlace()
  54. exe = fluid.Executor(place)
  55. model = create(main_arch)
  56. startup_prog = fluid.Program()
  57. infer_prog = fluid.Program()
  58. with fluid.program_guard(infer_prog, startup_prog):
  59. with fluid.unique_name.guard():
  60. inputs_def = cfg['TestReader']['inputs_def']
  61. inputs_def['use_dataloader'] = False
  62. feed_vars, _ = model.build_inputs(**inputs_def)
  63. # postprocess not need in exclude_nms, exclude NMS in exclude_nms mode
  64. test_fetches = model.test(feed_vars, exclude_nms=FLAGS.exclude_nms)
  65. infer_prog = infer_prog.clone(True)
  66. check_py_func(infer_prog)
  67. exe.run(startup_prog)
  68. checkpoint.load_params(exe, infer_prog, cfg.weights)
  69. dump_infer_config(FLAGS, cfg)
  70. save_infer_model(FLAGS, exe, feed_vars, test_fetches, infer_prog)
  71. if __name__ == '__main__':
  72. enable_static_mode()
  73. parser = ArgsParser()
  74. parser.add_argument(
  75. "--output_dir",
  76. type=str,
  77. default="output",
  78. help="Directory for storing the output model files.")
  79. parser.add_argument(
  80. "--exclude_nms",
  81. action='store_true',
  82. default=False,
  83. help="Whether prune NMS for benchmark")
  84. FLAGS = parser.parse_args()
  85. main()