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 4.2 kB

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