segmentation.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  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 collections
  15. import math
  16. import os
  17. import time
  18. import numpy as np
  19. from tqdm import trange
  20. def eval_segmentation(model, data_dir, batch_size=1):
  21. import cv2
  22. from .utils import Cityscapes, accuracy, calculate_area, f1_score, kappa, mean_iou
  23. assert os.path.isdir(data_dir), "The image_file_path:{} is not a directory.".format(
  24. data_dir
  25. )
  26. eval_dataset = Cityscapes(dataset_root=data_dir, mode="val")
  27. file_list = eval_dataset.file_list
  28. image_num = eval_dataset.num_samples
  29. num_classes = eval_dataset.num_classes
  30. intersect_area_all = 0
  31. pred_area_all = 0
  32. label_area_all = 0
  33. conf_mat_all = []
  34. twenty_percent_image_num = math.ceil(image_num * 0.2)
  35. start_time = 0
  36. end_time = 0
  37. average_inference_time = 0
  38. im_list = []
  39. label_list = []
  40. for image_label_path, i in zip(
  41. file_list, trange(image_num, desc="Inference Progress")
  42. ):
  43. if i == twenty_percent_image_num:
  44. start_time = time.time()
  45. im = cv2.imread(image_label_path[0])
  46. label = cv2.imread(image_label_path[1], cv2.IMREAD_GRAYSCALE)
  47. label_list.append(label)
  48. if batch_size == 1:
  49. result = model.predict(im)
  50. results = [result]
  51. else:
  52. im_list.append(im)
  53. # If the batch_size is not satisfied, the remaining pictures are formed into a batch
  54. if (i + 1) % batch_size != 0 and i != image_num - 1:
  55. continue
  56. results = model.batch_predict(im_list)
  57. if i == image_num - 1:
  58. end_time = time.time()
  59. average_inference_time = round(
  60. (end_time - start_time) / (image_num - twenty_percent_image_num), 4
  61. )
  62. for result, label in zip(results, label_list):
  63. pred = np.array(result.label_map).reshape(result.shape[0], result.shape[1])
  64. intersect_area, pred_area, label_area = calculate_area(
  65. pred, label, num_classes
  66. )
  67. intersect_area_all = intersect_area_all + intersect_area
  68. pred_area_all = pred_area_all + pred_area
  69. label_area_all = label_area_all + label_area
  70. im_list.clear()
  71. label_list.clear()
  72. class_iou, miou = mean_iou(intersect_area_all, pred_area_all, label_area_all)
  73. class_acc, oacc = accuracy(intersect_area_all, pred_area_all)
  74. kappa_res = kappa(intersect_area_all, pred_area_all, label_area_all)
  75. category_f1score = f1_score(intersect_area_all, pred_area_all, label_area_all)
  76. eval_metrics = collections.OrderedDict(
  77. zip(
  78. [
  79. "miou",
  80. "category_iou",
  81. "oacc",
  82. "category_acc",
  83. "kappa",
  84. "category_F1-score",
  85. "average_inference_time(s)",
  86. ],
  87. [
  88. miou,
  89. class_iou,
  90. oacc,
  91. class_acc,
  92. kappa_res,
  93. category_f1score,
  94. average_inference_time,
  95. ],
  96. )
  97. )
  98. return eval_metrics