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

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright 2021 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 os
  17. import json
  18. import argparse
  19. import numpy as np
  20. from src.config import config2 as config
  21. batch_size = 1
  22. parser = argparse.ArgumentParser(description="resnet inference")
  23. parser.add_argument("--result_path", type=str, required=True, help="result files path.")
  24. parser.add_argument("--label_path", type=str, required=True, help="image file path.")
  25. args = parser.parse_args()
  26. def get_result(result_path, label_path):
  27. files = os.listdir(result_path)
  28. with open(label_path, "r") as label:
  29. labels = json.load(label)
  30. top1 = 0
  31. top5 = 0
  32. total_data = len(files)
  33. for file in files:
  34. img_ids_name = file.split('_0.')[0]
  35. data_path = os.path.join(result_path, img_ids_name + "_0.bin")
  36. result = np.fromfile(data_path, dtype=np.float32).reshape(batch_size, config.class_num)
  37. for batch in range(batch_size):
  38. predict = np.argsort(-result[batch], axis=-1)
  39. if labels[img_ids_name+".JPEG"] == predict[0]:
  40. top1 += 1
  41. if labels[img_ids_name+".JPEG"] in predict[:5]:
  42. top5 += 1
  43. print(f"Total data: {total_data}, top1 accuracy: {top1/total_data}, top5 accuracy: {top5/total_data}.")
  44. if __name__ == '__main__':
  45. get_result(args.result_path, args.label_path)