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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 pycocotools.coco import COCO
  19. from src.config import config
  20. from src.util import coco_eval, bbox2result_1image, results2json
  21. dst_width = 1280
  22. dst_height = 768
  23. parser = argparse.ArgumentParser(description="FasterRcnn inference")
  24. parser.add_argument("--ann_file", type=str, required=True, help="ann file.")
  25. parser.add_argument("--img_path", type=str, required=True, help="image file path.")
  26. args = parser.parse_args()
  27. def get_eval_result(ann_file, img_path):
  28. max_num = 128
  29. result_path = "./result_Files/"
  30. outputs = []
  31. dataset_coco = COCO(ann_file)
  32. img_ids = dataset_coco.getImgIds()
  33. for img_id in img_ids:
  34. file_id = str(img_id).zfill(12)
  35. bbox_result_file = result_path + file_id + "_0.bin"
  36. label_result_file = result_path + file_id + "_1.bin"
  37. mask_result_file = result_path + file_id + "_2.bin"
  38. all_bbox = np.fromfile(bbox_result_file, dtype=np.float16).reshape(80000, 5)
  39. all_label = np.fromfile(label_result_file, dtype=np.int32).reshape(80000, 1)
  40. all_mask = np.fromfile(mask_result_file, dtype=np.bool_).reshape(80000, 1)
  41. all_bbox_squee = np.squeeze(all_bbox)
  42. all_label_squee = np.squeeze(all_label)
  43. all_mask_squee = np.squeeze(all_mask)
  44. all_bboxes_tmp_mask = all_bbox_squee[all_mask_squee, :]
  45. all_labels_tmp_mask = all_label_squee[all_mask_squee]
  46. if all_bboxes_tmp_mask.shape[0] > max_num:
  47. inds = np.argsort(-all_bboxes_tmp_mask[:, -1])
  48. inds = inds[:max_num]
  49. all_bboxes_tmp_mask = all_bboxes_tmp_mask[inds]
  50. all_labels_tmp_mask = all_labels_tmp_mask[inds]
  51. outputs_tmp = bbox2result_1image(all_bboxes_tmp_mask, all_labels_tmp_mask, config.num_classes)
  52. outputs.append(outputs_tmp)
  53. eval_types = ["bbox"]
  54. result_files = results2json(dataset_coco, outputs, "./results.pkl")
  55. coco_eval(result_files, eval_types, dataset_coco, single_result=False)
  56. if __name__ == '__main__':
  57. get_eval_result(args.ann_file, args.img_path)