How to run Tensorboard from python scipt in virtua

2019-04-07 09:33发布

问题:

Tensorboard should be started from commnad line like that:

tensorboard --logdir=path

I need to run it from code. Until now I use this:

import os
os.system('tensorboard --logdir=' + path)

However tensorboard do not start because is not included in the system path. I use PyCharm with virtualenv on windows. I don't want to change system paths so the only option is to run it from virtualenv. How to do this?

回答1:

Probably a bit late for an answer, but this is what worked for me in Python 3.6.2:

import tensorflow as tf
from tensorboard import main as tb
tf.flags.FLAGS.logdir = "/path/to/graphs/"
tb.main()

That runs tensorboard with the default configuration and looks for graphs and summaries in "/path/to/graphs/". You can of course change the log directory and set as many variables as you like using:

tf.flags.FLAGS.variable = value

Hope it helps.



回答2:

You should launch tensorBoard in the separate thread:

def launchTensorBoard():
    import os
    os.system('tensorboard --logdir=' + tensorBoardPath)
    return

import threading
t = threading.Thread(target=launchTensorBoard, args=([]))
t.start()


回答3:

As I get the same problem, you can use this lines inspired by tensorboard\main.py:

from tensorboard import default
from tensorboard import program

tb = program.TensorBoard(default.PLUGIN_LOADERS, default.get_assets_zip_provider())
tb.configure(argv=['--logdir', my_directory])
tb.main()

With my_directory as the folder you want to check. Don't forget to create a separate Thread if you want to avoid to be block after tb.main(). Best regards

EDIT Tensorboard V1.10:

For some personnal reasons, I write it in a different way:

class TensorBoardTool:

    def __init__(self, dir_path):
        self.dir_path = dir_path

    def run(self):
        # Remove http messages
        log = logging.getLogger('werkzeug')
        log.setLevel(logging.ERROR)
        # Start tensorboard server
        tb = program.TensorBoard(default.PLUGIN_LOADERS, default.get_assets_zip_provider())
        tb.configure(argv=['--logdir', self.dir_path])
        url = tb.launch()
        sys.stdout.write('TensorBoard at %s \n' % url)

EDIT Tensorboard V1.12:

According to Elad Weiss and tsbertalan for the version 1.12 of tensorboard.

    def run(self):
        # Remove http messages
        log = logging.getLogger('werkzeug').setLevel(logging.ERROR)
        # Start tensorboard server
        tb = program.TensorBoard(default.get_plugins(), default.get_assets_zip_provider())
        tb.configure(argv=[None, '--logdir', self.dir_path])
        url = tb.launch()
        sys.stdout.write('TensorBoard at %s \n' % url)

Then to run it just do:

# Tensorboard tool launch
tb_tool = TensorBoardTool(work_dir)
tb_tool.run()

This will allow you to run a Tensorboard server at same time as your main process, without disturbing http request!



回答4:

As of TensorBoard version 1.9.0, the following works to start TensorBoard with default settings in the same Python process:

import tensorboard as tb
import tensorboard.program
import tensorboard.default

tb.program.FLAGS.logdir = 'path/to/logdir'
tb.program.main(tb.default.get_plugins(),
                tb.default.get_assets_zip_provider())


回答5:

The following will open a Chrome tab and launches TensorBoard. Simply provide the desired directory and your system's name .

import os
os.system(
    "cd <directory> \
    && google-chrome http://<your computer name>:6007 \
    && tensorboard --port=6007 --logdir runs"
) 


回答6:

If your python interpreter path is:

/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/bin/python3.6

You can run this command instead of tensorboard

/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorboard/main.py

It's works for me.



回答7:

Had the same problem: As you're working on Windows, you can use batch files to fully-automate opening tensorboard like in the exaple below.

As you probably want to open tensorboard within a visible console window (cmd.exe). Calling one batch-file within your IDE (pycharm) will run it within the IDE, so in the background, which means you can't see the console. Therefore, you can use a workaround: call a batch-file that then calls the batch-file to start tensorboard.

Note: I'm using Anaconda as my virtual-environment for this example

batch_filename = 'start_tb.bat'  # set filename for batch file
tb_command = 'tensorboard --logdir=' + log_dir  # join strings for tensorflow command

# creates batch file that will call seconds batch file in console window (cmd.exe)
with open(os.path.join('invoke.bat'), "w") as f:
    f.writelines('start ' + batch_filename)

# created batch file that activates Anaconda environment and starts tensorboard
with open(os.path.join(batch_filename), "w") as f:
    f.writelines('\nconda activate YOURCondaEnvNAME  && ' + tb_command)  # change to your conda environment, or other virtualenv

# starts tensorboard using the batch files (will open console window)
# calls the 'invoke.bat' that will call 'start_tb.bat'
os.system('invoke.bat')

# starts tensorboard in default browser >> ATTENTION: must be adapted to local host
os.system('start "" http://YOUR-COMPUTER-NAME:6006/')  # just copy the URL that tensorboard runs at on your computer

Sometimes you might have to refresh tensorboard within your browser, as it opened already before it was properly set-up.



回答8:

Try running from python

import os
os.system('python -m tensorflow.tensorboard --logdir=' + path)

works for me in PyCharm (but on linux, so if the shell syntax is different then you have to tweak it)