analyse_dataset.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 os
  15. import platform
  16. import numpy as np
  17. from .....utils.deps import function_requires_deps, is_dep_available
  18. from .....utils.file_interface import custom_open
  19. from .....utils.fonts import PINGFANG_FONT
  20. if is_dep_available("matplotlib"):
  21. import matplotlib.pyplot as plt
  22. from matplotlib import font_manager
  23. @function_requires_deps("matplotlib")
  24. def deep_analyse(dataset_path, output, dataset_type="ShiTuRec"):
  25. """class analysis for dataset"""
  26. tags = ["train", "gallery", "query"]
  27. tags_info = dict()
  28. for tag in tags:
  29. anno_path = os.path.join(dataset_path, f"{tag}.txt")
  30. with custom_open(anno_path, "r") as f:
  31. lines = f.readlines()
  32. lines = [line.strip("\n").split(" ") for line in lines]
  33. num_images = len(lines)
  34. num_labels = len(set([int(line[1]) for line in lines]))
  35. tags_info[tag] = {
  36. "num_images": num_images,
  37. "num_labels": num_labels,
  38. }
  39. categories = list(tags_info.keys())
  40. num_images = [tags_info[category]["num_images"] for category in categories]
  41. num_labels = [tags_info[category]["num_labels"] for category in categories]
  42. # bar
  43. os_system = platform.system().lower()
  44. if os_system == "windows":
  45. plt.rcParams["font.sans-serif"] = "FangSong"
  46. else:
  47. font = font_manager.FontProperties(fname=PINGFANG_FONT.path, size=10)
  48. x = np.arange(len(categories)) # 标签位置
  49. width = 0.35 # 每个条形的宽度
  50. fig, ax = plt.subplots()
  51. rects1 = ax.bar(x - width / 2, num_images, width, label="Num Images")
  52. rects2 = ax.bar(x + width / 2, num_labels, width, label="Num Classes")
  53. # 添加一些文本标签
  54. ax.set_xlabel("集合", fontproperties=None if os_system == "windows" else font)
  55. ax.set_ylabel("数量", fontproperties=None if os_system == "windows" else font)
  56. ax.set_title(
  57. "不同集合的图片和类别数量",
  58. fontproperties=None if os_system == "windows" else font,
  59. )
  60. ax.set_xticks(x, fontproperties=None if os_system == "windows" else font)
  61. ax.set_xticklabels(categories)
  62. ax.legend()
  63. # 在条形图上添加数值标签
  64. def autolabel(rects):
  65. """Attach a text label above each bar in *rects*, displaying its height."""
  66. for rect in rects:
  67. height = rect.get_height()
  68. ax.annotate(
  69. "{}".format(height),
  70. xy=(rect.get_x() + rect.get_width() / 2, height),
  71. xytext=(0, 3), # 3 points vertical offset
  72. textcoords="offset points",
  73. ha="center",
  74. va="bottom",
  75. )
  76. autolabel(rects1)
  77. autolabel(rects2)
  78. fig.tight_layout()
  79. file_path = os.path.join(output, "histogram.png")
  80. fig.savefig(file_path, dpi=300)
  81. return {"histogram": os.path.join("check_dataset", "histogram.png")}