classify.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  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. import numpy as np
  15. import os
  16. import re
  17. import time
  18. import collections
  19. def topk_accuracy(topk_list, label_list):
  20. match_array = np.logical_or.reduce(topk_list == label_list, axis=1)
  21. topk_acc_score = match_array.sum() / match_array.shape[0]
  22. return topk_acc_score
  23. def eval_classify(model, image_file_path, label_file_path, topk=5):
  24. from tqdm import trange
  25. import cv2
  26. import math
  27. result_list = []
  28. label_list = []
  29. image_label_dict = {}
  30. assert os.path.isdir(
  31. image_file_path
  32. ), "The image_file_path:{} is not a directory.".format(image_file_path)
  33. assert os.path.isfile(
  34. label_file_path
  35. ), "The label_file_path:{} is not a file.".format(label_file_path)
  36. assert isinstance(topk, int), "The tok:{} is not int type".format(topk)
  37. with open(label_file_path, "r") as file:
  38. lines = file.readlines()
  39. for line in lines:
  40. items = line.strip().split()
  41. image_name = items[0]
  42. label = items[1]
  43. image_label_dict[image_name] = int(label)
  44. images_num = len(image_label_dict)
  45. twenty_percent_images_num = math.ceil(images_num * 0.2)
  46. start_time = 0
  47. end_time = 0
  48. average_inference_time = 0
  49. scores = collections.OrderedDict()
  50. for (image, label), i in zip(
  51. image_label_dict.items(), trange(images_num, desc="Inference Progress")
  52. ):
  53. if i == twenty_percent_images_num:
  54. start_time = time.time()
  55. label_list.append([label])
  56. image_path = os.path.join(image_file_path, image)
  57. im = cv2.imread(image_path)
  58. result = model.predict(im, topk)
  59. result_list.append(result.label_ids)
  60. if i == images_num - 1:
  61. end_time = time.time()
  62. average_inference_time = round(
  63. (end_time - start_time) / (images_num - twenty_percent_images_num), 4
  64. )
  65. topk_acc_score = topk_accuracy(np.array(result_list), np.array(label_list))
  66. if topk == 1:
  67. scores.update({"topk1": topk_acc_score})
  68. scores.update({"topk1_average_inference_time(s)": average_inference_time})
  69. elif topk == 5:
  70. scores.update({"topk5": topk_acc_score})
  71. scores.update({"topk5_average_inference_time(s)": average_inference_time})
  72. return scores