tb_export_sample.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. if only_extract_from_latest_event_file:
  122. max_load_time = max(
  123. list(
  124. AppPath(
  125. "Adversarial Speech", "Christian Heider Nielsen"
  126. ).user_log.iterdir()
  127. ),
  128. key=os.path.getctime,
  129. )
  130. unique_event_files_parents = set(
  131. [ef.parent for ef in max_load_time.rglob("events.out.tfevents.*")]
  132. )
  133. event_files = {max_load_time: unique_event_files_parents}
  134. else:
  135. event_files = {
  136. a: set([ef.parent for ef in a.rglob("events.out.tfevents.*")])
  137. for a in list(
  138. AppPath(
  139. "Adversarial Speech", "Christian Heider Nielsen"
  140. ).user_log.iterdir()
  141. )
  142. }
  143. for k, v in progress_bar(event_files.items()):
  144. for e in progress_bar(v):
  145. relative_path = e.relative_to(k)
  146. mapping_id, *rest = relative_path.parts
  147. mapping_id_test = f"{mapping_id}_Test_{relative_path.name}"
  148. # model_id = relative_path.parent.name can be include but is always the same
  149. relative_path = Path(*(mapping_id_test, *rest))
  150. with TensorboardEventExporter(e, save_to_disk=True) as tee:
  151. if export_test:
  152. out_tags = []
  153. for tag in progress_bar(TestingCurves):
  154. if tag.value in tee.available_tensors:
  155. out_tags.append(tag.value)
  156. if len(out_tags):
  157. tee.pr_curve_export_csv(
  158. *out_tags,
  159. out_dir=ensure_existence(
  160. test_path / k.name / relative_path,
  161. force_overwrite=True,
  162. verbose=verbose,
  163. ),
  164. )
  165. else:
  166. if verbose:
  167. print(
  168. f"{e}, no requested tags found {TestingCurves.__members__.values()}, {tee.available_tensors}"
  169. )
  170. if export_train: # TODO: OUTPUT for all epoch steps, no support yet
  171. out_tags = []
  172. for tag in progress_bar(TrainingCurves):
  173. if tag.value in tee.available_tensors:
  174. out_tags.append(tag.value)
  175. if len(out_tags):
  176. tee.pr_curve_export_csv(
  177. *out_tags,
  178. out_dir=ensure_existence(
  179. # train_path / max_load_time.name / relative_path, # MAX LOAD TIME HERE?
  180. train_path
  181. / k.name
  182. / relative_path, # MAX LOAD TIME HERE?
  183. force_overwrite=True,
  184. verbose=verbose,
  185. ),
  186. )
  187. else:
  188. if verbose:
  189. print(
  190. f"{e}, no requested tags found {TrainingCurves.__members__.values()}, {tee.available_tensors}"
  191. )
  192. def extract_metrics(only_extract_latest=False):
  193. extract_scalars_as_csv(only_extract_from_latest_event_file=only_extract_latest)
  194. extract_tensors_as_csv(only_extract_from_latest_event_file=only_extract_latest)
  195. if __name__ == "__main__":
  196. extract_metrics(only_extract_latest=True)
  197. # extract_scalars_as_csv(verbose=False,export_train=False)