quant_offline.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # coding: utf8
  2. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import argparse
  16. import paddlex as pdx
  17. from paddlex.seg import transforms
  18. def parse_args():
  19. parser = argparse.ArgumentParser(description='HumanSeg training')
  20. parser.add_argument(
  21. '--model_dir',
  22. dest='model_dir',
  23. help='Model path for quant',
  24. type=str,
  25. default='output/best_model')
  26. parser.add_argument(
  27. '--batch_size',
  28. dest='batch_size',
  29. help='Mini batch size',
  30. type=int,
  31. default=1)
  32. parser.add_argument(
  33. '--batch_nums',
  34. dest='batch_nums',
  35. help='Batch number for quant',
  36. type=int,
  37. default=10)
  38. parser.add_argument(
  39. '--data_dir',
  40. dest='data_dir',
  41. help='the root directory of dataset',
  42. type=str)
  43. parser.add_argument(
  44. '--quant_list',
  45. dest='quant_list',
  46. help='Image file list for model quantization, it can be vat.txt or train.txt',
  47. type=str,
  48. default=None)
  49. parser.add_argument(
  50. '--save_dir',
  51. dest='save_dir',
  52. help='The directory for saving the quant model',
  53. type=str,
  54. default='./output/quant_offline')
  55. parser.add_argument(
  56. "--image_shape",
  57. dest="image_shape",
  58. help="The image shape for net inputs.",
  59. nargs=2,
  60. default=[192, 192],
  61. type=int)
  62. return parser.parse_args()
  63. def evaluate(args):
  64. eval_transforms = transforms.Compose(
  65. [transforms.Resize(args.image_shape), transforms.Normalize()])
  66. eval_dataset = pdx.datasets.SegDataset(
  67. data_dir=args.data_dir,
  68. file_list=args.quant_list,
  69. transforms=eval_transforms)
  70. model = pdx.load_model(args.model_dir)
  71. pdx.slim.export_quant_model(model, eval_dataset, args.batch_size,
  72. args.batch_nums, args.save_dir)
  73. if __name__ == '__main__':
  74. args = parse_args()
  75. evaluate(args)