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.9 kB

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