KohyaSS/library/tensorboard_gui.py

54 lines
1.4 KiB
Python
Raw Normal View History

2023-03-01 18:14:47 +00:00
import os
import gradio as gr
from easygui import msgbox
import subprocess
import time
2023-03-02 00:24:11 +00:00
tensorboard_proc = None # I know... bad but heh
2023-03-04 23:56:22 +00:00
TENSORBOARD = 'tensorboard' if os.name == 'posix' else 'tensorboard.exe'
2023-03-01 18:14:47 +00:00
def start_tensorboard(logging_dir):
global tensorboard_proc
2023-03-02 00:24:11 +00:00
2023-03-01 18:14:47 +00:00
if not os.listdir(logging_dir):
2023-03-02 00:24:11 +00:00
print('Error: log folder is empty')
msgbox(msg='Error: log folder is empty')
2023-03-01 18:14:47 +00:00
return
2023-03-02 00:24:11 +00:00
run_cmd = [f'{TENSORBOARD}', '--logdir', f'{logging_dir}']
2023-03-02 00:24:11 +00:00
2023-03-01 18:14:47 +00:00
print(run_cmd)
if tensorboard_proc is not None:
2023-03-02 00:24:11 +00:00
print(
'Tensorboard is already running. Terminating existing process before starting new one...'
)
2023-03-01 18:14:47 +00:00
stop_tensorboard()
2023-03-02 00:24:11 +00:00
2023-03-01 18:14:47 +00:00
# Start background process
2023-03-02 00:24:11 +00:00
print('Starting tensorboard...')
2023-03-01 18:14:47 +00:00
tensorboard_proc = subprocess.Popen(run_cmd)
2023-03-02 00:24:11 +00:00
2023-03-01 18:14:47 +00:00
# Wait for some time to allow TensorBoard to start up
time.sleep(5)
2023-03-02 00:24:11 +00:00
2023-03-01 18:14:47 +00:00
# Open the TensorBoard URL in the default browser
print('Opening tensorboard url in browser...')
import webbrowser
2023-03-02 00:24:11 +00:00
2023-03-01 18:14:47 +00:00
webbrowser.open('http://localhost:6006')
2023-03-02 00:24:11 +00:00
2023-03-01 18:14:47 +00:00
def stop_tensorboard():
print('Stopping tensorboard process...')
tensorboard_proc.kill()
print('...process stopped')
2023-03-02 00:24:11 +00:00
2023-03-01 18:14:47 +00:00
def gradio_tensorboard():
with gr.Row():
button_start_tensorboard = gr.Button('Start tensorboard')
button_stop_tensorboard = gr.Button('Stop tensorboard')
2023-03-02 00:24:11 +00:00
return (button_start_tensorboard, button_stop_tensorboard)