56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
import os
|
|
import argparse
|
|
import subprocess
|
|
import webbrowser
|
|
import time
|
|
from pathlib import Path
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description='Visualize TensorBoard logs')
|
|
parser.add_argument('--logdir', type=str, default='./logs', help='Directory containing TensorBoard logs')
|
|
parser.add_argument('--port', type=int, default=6006, help='Port for TensorBoard server')
|
|
args = parser.parse_args()
|
|
|
|
log_dir = Path(args.logdir)
|
|
|
|
if not log_dir.exists():
|
|
print(f"Log directory {log_dir} does not exist. Creating it...")
|
|
log_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
# Check if TensorBoard is installed
|
|
try:
|
|
subprocess.run(['tensorboard', '--version'], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
except (subprocess.CalledProcessError, FileNotFoundError):
|
|
print("TensorBoard not found. Installing...")
|
|
subprocess.run(['pip', 'install', 'tensorboard'], check=True)
|
|
|
|
# Start TensorBoard server
|
|
print(f"Starting TensorBoard server on port {args.port}...")
|
|
tensorboard_process = subprocess.Popen(
|
|
['tensorboard', '--logdir', str(log_dir), '--port', str(args.port)],
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE
|
|
)
|
|
|
|
# Wait for TensorBoard to start
|
|
time.sleep(3)
|
|
|
|
# Open browser
|
|
url = f"http://localhost:{args.port}"
|
|
print(f"Opening TensorBoard in browser: {url}")
|
|
webbrowser.open(url)
|
|
|
|
print("TensorBoard is running. Press Ctrl+C to stop.")
|
|
|
|
try:
|
|
# Keep the script running until interrupted
|
|
while True:
|
|
time.sleep(1)
|
|
except KeyboardInterrupt:
|
|
print("Stopping TensorBoard server...")
|
|
tensorboard_process.terminate()
|
|
tensorboard_process.wait()
|
|
print("TensorBoard server stopped.")
|
|
|
|
if __name__ == "__main__":
|
|
main() |