infer.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 os
  17. import os.path as osp
  18. import cv2
  19. import numpy as np
  20. import tqdm
  21. import paddlex as pdx
  22. from paddlex.seg import transforms
  23. def parse_args():
  24. parser = argparse.ArgumentParser(
  25. description='HumanSeg prediction and visualization')
  26. parser.add_argument(
  27. '--model_dir',
  28. dest='model_dir',
  29. help='Model path for prediction',
  30. type=str)
  31. parser.add_argument(
  32. '--data_dir',
  33. dest='data_dir',
  34. help='The root directory of dataset',
  35. type=str)
  36. parser.add_argument(
  37. '--test_list',
  38. dest='test_list',
  39. help='Test list file of dataset',
  40. type=str)
  41. parser.add_argument(
  42. '--save_dir',
  43. dest='save_dir',
  44. help='The directory for saving the inference results',
  45. type=str,
  46. default='./output/result')
  47. parser.add_argument(
  48. "--image_shape",
  49. dest="image_shape",
  50. help="The image shape for net inputs.",
  51. nargs=2,
  52. default=[192, 192],
  53. type=int)
  54. return parser.parse_args()
  55. def infer(args):
  56. def makedir(path):
  57. sub_dir = osp.dirname(path)
  58. if not osp.exists(sub_dir):
  59. os.makedirs(sub_dir)
  60. test_transforms = transforms.Compose(
  61. [transforms.Resize(args.image_shape), transforms.Normalize()])
  62. model = pdx.load_model(args.model_dir)
  63. added_saved_path = osp.join(args.save_dir, 'added')
  64. mat_saved_path = osp.join(args.save_dir, 'mat')
  65. scoremap_saved_path = osp.join(args.save_dir, 'scoremap')
  66. with open(args.test_list, 'r') as f:
  67. files = f.readlines()
  68. for file in tqdm.tqdm(files):
  69. file = file.strip()
  70. im_file = osp.join(args.data_dir, file)
  71. im = cv2.imread(im_file)
  72. result = model.predict(im_file, transforms=test_transforms)
  73. # save added image
  74. added_image = pdx.seg.visualize(
  75. im_file, result, weight=0.6, save_dir=None)
  76. added_image_file = osp.join(added_saved_path, file)
  77. makedir(added_image_file)
  78. cv2.imwrite(added_image_file, added_image)
  79. # save score map
  80. score_map = result['score_map'][:, :, 1]
  81. score_map = (score_map * 255).astype(np.uint8)
  82. score_map_file = osp.join(scoremap_saved_path, file)
  83. makedir(score_map_file)
  84. cv2.imwrite(score_map_file, score_map)
  85. # save mat image
  86. score_map = np.expand_dims(score_map, axis=-1)
  87. mat_image = np.concatenate([im, score_map], axis=2)
  88. mat_file = osp.join(mat_saved_path, file)
  89. ext = osp.splitext(mat_file)[-1]
  90. mat_file = mat_file.replace(ext, '.png')
  91. makedir(mat_file)
  92. cv2.imwrite(mat_file, mat_image)
  93. if __name__ == '__main__':
  94. args = parse_args()
  95. infer(args)