tb_export_sample.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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, ensure_existence
  10. from draugr.tensorboard_utilities import TensorboardEventExporter
  11. from draugr.tqdm_utilities import progress_bar
  12. from draugr.writers import (
  13. TestingCurves,
  14. TestingScalars,
  15. TrainingCurves,
  16. TrainingScalars,
  17. )
  18. __all__ = ["extract_scalars_as_csv", "extract_tensors_as_csv", "extract_metrics"]
  19. EXPORT_RESULTS_PATH = Path.cwd()
  20. def extract_scalars_as_csv(
  21. train_path: Path = EXPORT_RESULTS_PATH / "csv" / "training",
  22. test_path: Path = EXPORT_RESULTS_PATH / "csv" / "testing",
  23. export_train: bool = True,
  24. export_test: bool = True,
  25. verbose: bool = False,
  26. only_extract_from_latest_event_file: bool = False,
  27. ) -> None:
  28. """
  29. :param train_path:
  30. :param test_path:
  31. :param export_train:
  32. :param export_test:
  33. :param verbose:
  34. :param only_extract_from_latest_event_file:
  35. """
  36. if only_extract_from_latest_event_file:
  37. max_load_time = max(
  38. list(
  39. AppPath(
  40. "Adversarial Speech", "Christian Heider Nielsen"
  41. ).user_log.iterdir()
  42. ),
  43. key=os.path.getctime,
  44. )
  45. unique_event_files_parents = set(
  46. [ef.parent for ef in max_load_time.rglob("events.out.tfevents.*")]
  47. )
  48. event_files = {max_load_time: unique_event_files_parents}
  49. else:
  50. event_files = {
  51. a: set([ef.parent for ef in a.rglob("events.out.tfevents.*")])
  52. for a in list(
  53. AppPath(
  54. "Adversarial Speech", "Christian Heider Nielsen"
  55. ).user_log.iterdir()
  56. )
  57. }
  58. for k, v in progress_bar(event_files.items()):
  59. for e in progress_bar(v):
  60. relative_path = e.relative_to(k)
  61. mapping_id, *rest = relative_path.parts
  62. mappind_id_test = f"{mapping_id}_Test_{relative_path.name}"
  63. # model_id = relative_path.parent.name can be include but is always the same
  64. relative_path = Path(*(mappind_id_test, *rest))
  65. with TensorboardEventExporter(e, save_to_disk=True) as tee:
  66. if export_test:
  67. out_tags = []
  68. for tag in progress_bar(TestingScalars):
  69. if tag.value in tee.available_scalars:
  70. out_tags.append(tag.value)
  71. if len(out_tags):
  72. tee.scalar_export_csv(
  73. *out_tags,
  74. out_dir=ensure_existence(
  75. test_path / k.name / relative_path,
  76. force_overwrite=True,
  77. verbose=verbose,
  78. ),
  79. )
  80. print(e)
  81. else:
  82. if verbose:
  83. print(
  84. f"{e}, no requested tags found {TestingScalars.__members__.values()}, {tee.available_scalars}"
  85. )
  86. if export_train:
  87. out_tags = []
  88. for tag in progress_bar(TrainingScalars):
  89. if tag.value in tee.available_scalars:
  90. out_tags.append(tag.value)
  91. if len(out_tags):
  92. tee.scalar_export_csv(
  93. *out_tags,
  94. out_dir=ensure_existence(
  95. train_path / k.name / relative_path,
  96. force_overwrite=True,
  97. verbose=verbose,
  98. ),
  99. )
  100. else:
  101. if verbose:
  102. print(
  103. f"{e}, no requested tags found {TrainingScalars.__members__.values()}, {tee.available_scalars}"
  104. )
  105. def extract_tensors_as_csv(
  106. train_path: Path = EXPORT_RESULTS_PATH / "csv" / "training",
  107. test_path: Path = EXPORT_RESULTS_PATH / "csv" / "testing",
  108. export_train: bool = False,
  109. export_test: bool = True,
  110. verbose: bool = False,
  111. only_extract_from_latest_event_file: bool = False,
  112. ) -> None:
  113. """
  114. :param train_path:
  115. :param test_path:
  116. :param export_train:
  117. :param export_test:
  118. :param verbose:
  119. :param only_extract_from_latest_event_file:
  120. :return:
  121. """
  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(TestingCurves):
  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 {TestingCurves.__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(TrainingCurves):
  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 {TrainingCurves.__members__.values()}, {tee.available_tensors}"
  192. )
  193. def extract_metrics(only_extract_latest=False):
  194. extract_scalars_as_csv(only_extract_from_latest_event_file=only_extract_latest)
  195. extract_tensors_as_csv(only_extract_from_latest_event_file=only_extract_latest)
  196. if __name__ == "__main__":
  197. extract_metrics(only_extract_latest=True)
  198. # extract_scalars_as_csv(verbose=False,export_train=False)