Last active
March 11, 2025 00:33
-
-
Save pszemraj/51fad3623b5ee9171e69af1505966117 to your computer and use it in GitHub Desktop.
CLI utility to quickly inspect the latest scalar values from TensorBoard logs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
CLI utility to quickly inspect the latest scalar values from TensorBoard logs. | |
Dependencies: | |
pip install tbparse pandas fire tqdm | |
Usage: | |
python tensorboard_inspect.py --logdir ./path/to/logs | |
""" | |
import logging | |
from pathlib import Path | |
import pandas as pd | |
from tbparse import SummaryReader | |
from tqdm import tqdm | |
import fire | |
# Configure logging | |
logging.basicConfig(level=logging.INFO, format='%(asctime)s [%(levelname)s]: %(message)s') | |
logger = logging.getLogger(__name__) | |
def infer_latest_logdir(logdir: Path) -> Path: | |
"""Automatically finds the latest modified event file directory.""" | |
logdirs = [d for d in logdir.glob("**/*") if d.is_dir()] | |
latest_dir = max(logdirs, key=lambda d: d.stat().st_mtime, default=logdir) | |
logger.info(f"Inferred latest log directory: {latest_dir}") | |
return latest_dir | |
def extract_latest_scalars(logdir: Path) -> pd.DataFrame: | |
"""Extracts the latest scalar values from TensorBoard logs.""" | |
if not logdir.exists(): | |
raise FileNotFoundError(f"Log directory '{logdir}' does not exist.") | |
reader = SummaryReader(str(logdir)) | |
scalars_df = reader.scalars | |
if scalars_df.empty: | |
raise ValueError(f"No scalar data found in '{logdir}'.") | |
latest_scalars = scalars_df.sort_values("step").groupby("tag").last().reset_index() | |
return latest_scalars | |
def main(logdir: str, infer_latest: bool = False): | |
""" | |
CLI entrypoint to print latest TensorBoard scalar metrics. | |
Args: | |
logdir (str): Path to TensorBoard log directory. | |
infer_latest (bool): If True, auto-selects the most recent log subdirectory. | |
""" | |
logdir_path = Path(logdir).resolve() | |
if infer_latest: | |
logdir_path = infer_latest_logdir(logdir_path) | |
logger.info(f"Loading TensorBoard logs from: {logdir_path}") | |
try: | |
latest_scalars = extract_latest_scalars(logdir_path) | |
except (FileNotFoundError, ValueError) as e: | |
logger.error(e) | |
return | |
logger.info("Latest scalar values:") | |
for _, row in latest_scalars.iterrows(): | |
tag, step, value = row['tag'], row['step'], row['value'] | |
print(f"{tag:30}: step {step:<10} value {value:.6f}") | |
if __name__ == "__main__": | |
fire.Fire(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what it does: