Skip to content

Instantly share code, notes, and snippets.

@RyanCPeters
Last active May 7, 2019 19:53
Show Gist options
  • Save RyanCPeters/a88609184990ec2d5e487e79fd1a2afa to your computer and use it in GitHub Desktop.
Save RyanCPeters/a88609184990ec2d5e487e79fd1a2afa to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from tensorboard import default
from tensorboard import program
import re
# ToDo: Low Priority
# Investigate if there is a way to derive the default port without having to hard code
# magic numbers like this.
default_port = 6006
class TBoardLauncher:
""" This is a utility class for being able to launch the tensorboard server for monitoring
the model's training without having to mess around with command console.
"""
def __init__(self, dir_path):
global default_port
# monthly_logs = glob.glob(os.path.join(dir_path,"**","logs"),recursive=True)
self.dir_path = dir_path
self.specific_port = default_port
default_port +=1
self.url=""
self.tb = None
def run(self)->program.TensorBoard:
"""Emulates issuing a command terminal call to launch tensorboard.
A typical call would look like this:
tensorboard --logdir=C:/absolute/path/to/summary_file.tfevents.{tag}.{id} --port=600X
where {tag} and {id} are unique to the specific training session and terminal
the training is being run on. A key point to observe is that the target file must have
the `tfevents` extension in it's name. Whether you need to provide the absolute path, as
opposed to a relative path, for logdir is system dependent I believe.
This function will send data to the loop-back socket on your system, `127.0.0.1`, for the user
to view summary results in their browser. They should be able to view the output at the
following url:
`https://localhost:some_port`
Where `some_port` is an integer in the range [6006,inf), based upon the number of
TensorBoard instances you spool up in a given instance of the python interpreter
The initial creation of this function was inspired by looking at:
/tensorboard/main.py
Subsequent modifications were made after reading commends on the following StackOverflow
post:
https://stackoverflow.com/a/52295534/7412747
:return:
A reference to the server object that manages interactions with tfevent files for
producing data to be viewed on the dashboard.
"""
# ToDo: low priority;
# figure out a way to filter graph scalars based upon the presence of NaN values
# See this link for initial inspiriation:
# https://stackoverflow.com/q/48945404/7412747
if self.tb:
return self.tb
program.setup_environment()
self.tb = program.TensorBoard(default.get_plugins(),
program.get_default_assets_zip_provider())
self.tb.configure(argv=[None, '--logdir', self.dir_path, "--port",str(self.specific_port)])
url = str(self.tb.launch())
re_result = re.search("(?<=//).+(?=:)",url)
url = url[:re_result.regs[0][0]]+"localhost"+url[re_result.regs[0][1]:]
url = 'TensorBoard at {} \n'.format(url)
self.url=url
return self.tb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment