tb_export_sample.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. __author__ = "Christian Heider Nielsen"
  4. __doc__ = r"""
  5. Created on 22/06/2020
  6. """
  7. import os
  8. from pathlib import Path
  9. from apppath import AppPath
  10. from draugr.tensorboard_utilities import TensorboardEventExporter
  11. from draugr.visualisation.progress import progress_bar
  12. from draugr.writers import (
  13. StandardTestingCurvesEnum,
  14. StandardTestingScalarsEnum,
  15. StandardTrainingCurvesEnum,
  16. StandardTrainingScalarsEnum,
  17. )
  18. __all__ = ["extract_scalars_as_csv", "extract_tensors_as_csv", "extract_metrics"]
  19. from warg import ensure_existence
  20. EXPORT_RESULTS_PATH = Path.cwd()
  21. def extract_scalars_as_csv(
  22. train_path: Path = EXPORT_RESULTS_PATH / "csv" / "training",
  23. test_path: Path = EXPORT_RESULTS_PATH / "csv" / "testing",
  24. export_train: bool = True,
  25. export_test: bool = True,
  26. verbose: bool = False,
  27. only_extract_from_latest_event_file: bool = False,
  28. ) -> None:
  29. """
  30. :param train_path:
  31. :param test_path:
  32. :param export_train:
  33. :param export_test:
  34. :param verbose:
  35. :param only_extract_from_latest_event_file:
  36. """
  37. if only_extract_from_latest_event_file:
  38. max_load_time = max(
  39. list(
  40. AppPath(
  41. "Adversarial Speech", "Christian Heider Nielsen"
  42. ).user_log.iterdir()
  43. ),
  44. key=os.path.getctime,
  45. )
  46. unique_event_files_parents = set(
  47. [ef.parent for ef in max_load_time.rglob("events.out.tfevents.*")]
  48. )
  49. event_files = {max_load_time: unique_event_files_parents}
  50. else:
  51. event_files = {
  52. a: set([ef.parent for ef in a.rglob("events.out.tfevents.*")])
  53. for a in list(
  54. AppPath(
  55. "Adversarial Speech", "Christian Heider Nielsen"
  56. ).user_log.iterdir()
  57. )
  58. }
  59. for k, v in progress_bar(event_files.items()):
  60. for e in progress_bar(v):
  61. relative_path = e.relative_to(k)
  62. mapping_id, *rest = relative_path.parts
  63. mappind_id_test = f"{mapping_id}_Test_{relative_path.name}"
  64. # model_id = relative_path.parent.name can be include but is always the same
  65. relative_path = Path(*(mappind_id_test, *rest))
  66. with TensorboardEventExporter(e, save_to_disk=True) as tee:
  67. if export_test:
  68. out_tags = []
  69. for tag in progress_bar(StandardTestingScalarsEnum):
  70. if tag.value in tee.available_scalars:
  71. out_tags.append(tag.value)
  72. if len(out_tags):
  73. tee.scalar_export_csv(
  74. *out_tags,
  75. out_dir=ensure_existence(
  76. test_path / k.name / relative_path,
  77. force_overwrite=True,
  78. verbose=verbose,
  79. ),
  80. )
  81. print(e)
  82. else:
  83. if verbose:
  84. print(
  85. f"{e}, no requested tags found {StandardTestingScalarsEnum.__members__.values()}, {tee.available_scalars}"
  86. )
  87. if export_train:
  88. out_tags = []
  89. for tag in progress_bar(StandardTrainingScalarsEnum):
  90. if tag.value in tee.available_scalars:
  91. out_tags.append(tag.value)
  92. if len(out_tags):
  93. tee.scalar_export_csv(
  94. *out_tags,
  95. out_dir=ensure_existence(
  96. train_path / k.name / relative_path,
  97. force_overwrite=True,
  98. verbose=verbose,
  99. ),
  100. )
  101. else:
  102. if verbose:
  103. print(
  104. f"{e}, no requested tags found {StandardTrainingScalarsEnum.__members__.values()}, {tee.available_scalars}"
  105. )
  106. def extract_tensors_as_csv(
  107. train_path: Path = EXPORT_RESULTS_PATH / "csv" / "training",
  108. test_path: Path = EXPORT_RESULTS_PATH / "csv" / "testing",
  109. export_train: bool = False,
  110. export_test: bool = True,
  111. verbose: bool = False,
  112. only_extract_from_latest_event_file: bool = False,
  113. ) -> None:
  114. """
  115. :param train_path:
  116. :param test_path:
  117. :param export_train:
  118. :param export_test:
  119. :param verbose:
  120. :param only_extract_from_latest_event_file:
  121. :return:"""
  122. if only_extract_from_latest_event_file:
  123. max_load_time = max(
  124. list(
  125. AppPath(
  126. "Adversarial Speech", "Christian Heider Nielsen"
  127. ).user_log.iterdir()
  128. ),
  129. key=os.path.getctime,
  130. )
  131. unique_event_files_parents = set(
  132. [ef.parent for ef in max_load_time.rglob("events.out.tfevents.*")]
  133. )
  134. event_files = {max_load_time: unique_event_files_parents}
  135. else:
  136. event_files = {
  137. a: set([ef.parent for ef in a.rglob("events.out.tfevents.*")])
  138. for a in list(
  139. AppPath(
  140. "Adversarial Speech", "Christian Heider Nielsen"
  141. ).user_log.iterdir()
  142. )
  143. }
  144. for k, v in progress_bar(event_files.items()):
  145. for e in progress_bar(v):
  146. relative_path = e.relative_to(k)
  147. mapping_id, *rest = relative_path.parts
  148. mapping_id_test = f"{mapping_id}_Test_{relative_path.name}"
  149. # model_id = relative_path.parent.name can be include but is always the same
  150. relative_path = Path(*(mapping_id_test, *rest))
  151. with TensorboardEventExporter(e, save_to_disk=True) as tee:
  152. if export_test:
  153. out_tags = []
  154. for tag in progress_bar(StandardTestingCurvesEnum):
  155. if tag.value in tee.available_tensors:
  156. out_tags.append(tag.value)
  157. if len(out_tags):
  158. tee.pr_curve_export_csv(
  159. *out_tags,
  160. out_dir=ensure_existence(
  161. test_path / k.name / relative_path,
  162. force_overwrite=True,
  163. verbose=verbose,
  164. ),
  165. )
  166. else:
  167. if verbose:
  168. print(
  169. f"{e}, no requested tags found {StandardTestingCurvesEnum.__members__.values()}, {tee.available_tensors}"
  170. )
  171. if export_train: # TODO: OUTPUT for all epoch steps, no support yet
  172. out_tags = []
  173. for tag in progress_bar(StandardTrainingCurvesEnum):
  174. if tag.value in tee.available_tensors:
  175. out_tags.append(tag.value)
  176. if len(out_tags):
  177. tee.pr_curve_export_csv(
  178. *out_tags,
  179. out_dir=ensure_existence(
  180. # train_path / max_load_time.name / relative_path, # MAX LOAD TIME HERE?
  181. train_path
  182. / k.name
  183. / relative_path, # MAX LOAD TIME HERE?
  184. force_overwrite=True,
  185. verbose=verbose,
  186. ),
  187. )
  188. else:
  189. if verbose:
  190. print(
  191. f"{e}, no requested tags found {StandardTrainingCurvesEnum.__members__.values()}, {tee.available_tensors}"
  192. )
  193. def extract_metrics(only_extract_latest=False):
  194. """
  195. :param only_extract_latest:
  196. """
  197. extract_scalars_as_csv(only_extract_from_latest_event_file=only_extract_latest)
  198. extract_tensors_as_csv(only_extract_from_latest_event_file=only_extract_latest)
  199. if __name__ == "__main__":
  200. extract_metrics(only_extract_latest=True)
  201. # extract_scalars_as_csv(verbose=False,export_train=False)