You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

postprocess.py 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  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. # less 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. # ============================================================================
  15. """post process for 310 inference"""
  16. import argparse
  17. import numpy as np
  18. from PIL import Image
  19. from pycocotools.coco import COCO
  20. from src.config import config
  21. from src.util import coco_eval, bbox2result_1image, results2json, get_seg_masks
  22. dst_width = 1280
  23. dst_height = 768
  24. parser = argparse.ArgumentParser(description="maskrcnn inference")
  25. parser.add_argument("--ann_file", type=str, required=True, help="ann file.")
  26. parser.add_argument("--img_path", type=str, required=True, help="image file path.")
  27. args = parser.parse_args()
  28. def get_imgSize(file_name):
  29. img = Image.open(file_name)
  30. return img.size
  31. def get_resizeRatio(img_size):
  32. org_width, org_height = img_size
  33. resize_ratio = dst_width / org_width
  34. if resize_ratio > dst_height / org_height:
  35. resize_ratio = dst_height / org_height
  36. return resize_ratio
  37. def get_eval_result(ann_file, img_path):
  38. max_num = 128
  39. result_path = "./result_Files/"
  40. outputs = []
  41. dataset_coco = COCO(ann_file)
  42. img_ids = dataset_coco.getImgIds()
  43. for img_id in img_ids:
  44. file_id = str(img_id).zfill(12)
  45. file = img_path + "/" + file_id + ".jpg"
  46. img_size = get_imgSize(file)
  47. resize_ratio = get_resizeRatio(img_size)
  48. img_metas = np.array([img_size[1], img_size[0]] + [resize_ratio, resize_ratio])
  49. bbox_result_file = result_path + file_id + "_0.bin"
  50. label_result_file = result_path + file_id + "_1.bin"
  51. mask_result_file = result_path + file_id + "_2.bin"
  52. mask_fb_result_file = result_path + file_id + "_3.bin"
  53. all_bbox = np.fromfile(bbox_result_file, dtype=np.float16).reshape(80000, 5)
  54. all_label = np.fromfile(label_result_file, dtype=np.int32).reshape(80000, 1)
  55. all_mask = np.fromfile(mask_result_file, dtype=np.bool_).reshape(80000, 1)
  56. all_mask_fb = np.fromfile(mask_fb_result_file, dtype=np.float16).reshape(80000, 28, 28)
  57. all_bbox_squee = np.squeeze(all_bbox)
  58. all_label_squee = np.squeeze(all_label)
  59. all_mask_squee = np.squeeze(all_mask)
  60. all_mask_fb_squee = np.squeeze(all_mask_fb)
  61. all_bboxes_tmp_mask = all_bbox_squee[all_mask_squee, :]
  62. all_labels_tmp_mask = all_label_squee[all_mask_squee]
  63. all_mask_fb_tmp_mask = all_mask_fb_squee[all_mask_squee, :, :]
  64. if all_bboxes_tmp_mask.shape[0] > max_num:
  65. inds = np.argsort(-all_bboxes_tmp_mask[:, -1])
  66. inds = inds[:max_num]
  67. all_bboxes_tmp_mask = all_bboxes_tmp_mask[inds]
  68. all_labels_tmp_mask = all_labels_tmp_mask[inds]
  69. all_mask_fb_tmp_mask = all_mask_fb_tmp_mask[inds]
  70. bbox_results = bbox2result_1image(all_bboxes_tmp_mask, all_labels_tmp_mask, config.num_classes)
  71. segm_results = get_seg_masks(all_mask_fb_tmp_mask, all_bboxes_tmp_mask, all_labels_tmp_mask, img_metas,
  72. True, config.num_classes)
  73. outputs.append((bbox_results, segm_results))
  74. eval_types = ["bbox", "segm"]
  75. result_files = results2json(dataset_coco, outputs, "./results.pkl")
  76. coco_eval(result_files, eval_types, dataset_coco, single_result=False)
  77. if __name__ == '__main__':
  78. get_eval_result(args.ann_file, args.img_path)