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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 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):
  28. """ get evaluation result of faster rcnn"""
  29. max_num = 128
  30. result_path = "./result_Files/"
  31. outputs = []
  32. dataset_coco = COCO(ann_file)
  33. img_ids = dataset_coco.getImgIds()
  34. for img_id in img_ids:
  35. file_id = str(img_id).zfill(12)
  36. bbox_result_file = result_path + file_id + "_0.bin"
  37. label_result_file = result_path + file_id + "_1.bin"
  38. mask_result_file = result_path + file_id + "_2.bin"
  39. all_bbox = np.fromfile(bbox_result_file, dtype=np.float16).reshape(80000, 5)
  40. all_label = np.fromfile(label_result_file, dtype=np.int32).reshape(80000, 1)
  41. all_mask = np.fromfile(mask_result_file, dtype=np.bool_).reshape(80000, 1)
  42. all_bbox_squee = np.squeeze(all_bbox)
  43. all_label_squee = np.squeeze(all_label)
  44. all_mask_squee = np.squeeze(all_mask)
  45. all_bboxes_tmp_mask = all_bbox_squee[all_mask_squee, :]
  46. all_labels_tmp_mask = all_label_squee[all_mask_squee]
  47. if all_bboxes_tmp_mask.shape[0] > max_num:
  48. inds = np.argsort(-all_bboxes_tmp_mask[:, -1])
  49. inds = inds[:max_num]
  50. all_bboxes_tmp_mask = all_bboxes_tmp_mask[inds]
  51. all_labels_tmp_mask = all_labels_tmp_mask[inds]
  52. outputs_tmp = bbox2result_1image(all_bboxes_tmp_mask, all_labels_tmp_mask, config.num_classes)
  53. outputs.append(outputs_tmp)
  54. eval_types = ["bbox"]
  55. result_files = results2json(dataset_coco, outputs, "./results.pkl")
  56. coco_eval(result_files, eval_types, dataset_coco, single_result=False)
  57. if __name__ == '__main__':
  58. get_eval_result(args.ann_file)