Can I use Tensorboard with Google Colab?

2019-01-08 07:22发布

问题:

Is there any way to use Tensorboard when training a Tensorflow model on Google Colab ?

回答1:

I currently use ngrok to tunnel traffic to localhost.
A colab example can be found here.

These are the steps (the code snippets represent cells of type "code" in colab):

  1. Get TensorBoard running in the background.
    Inspired by this answer.

    LOG_DIR = '/tmp/log'
    get_ipython().system_raw(
        'tensorboard --logdir {} --host 0.0.0.0 --port 6006 &'
        .format(LOG_DIR)
    )
    
  2. Download and unzip ngrok.
    Replace the link passed to wget with the correct download link for your OS.

    ! wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
    ! unzip ngrok-stable-linux-amd64.zip
    
  3. Launch ngrok background process...

    get_ipython().system_raw('./ngrok http 6006 &')
    

    ...and retrieve public url. Source

    ! curl -s http://localhost:4040/api/tunnels | python3 -c \
        "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"
    


回答2:

TensorBoard for TensorFlow running on Google Colab using tensorboardcolab. This uses ngrok internally for tunnelling.

  1. Install TensorBoardColab

!pip install tensorboardcolab

  1. Create a tensorboardcolab object

tbc = TensorBoardColab()

This automatically creates a TensorBoard link that can be used. This Tensorboard is reading the data at './Graph'

  1. Create a FileWriter pointing to this location

summary_writer = tbc.get_writer()

tensorboardcolab library has the method that returns FileWriter object pointing to above './Graph' location.

  1. Start adding summary information to Event files at './Graph' location using summary_writer object

You can add scalar info or graph or histogram data.

Reference: https://github.com/taomanwai/tensorboardcolab



回答3:

Here is how you can display your models inline on Google Colab. Below is a very simple example that displays a placeholder:

from IPython.display import clear_output, Image, display, HTML
import tensorflow as tf
import numpy as np
from google.colab import files

def strip_consts(graph_def, max_const_size=32):
    """Strip large constant values from graph_def."""
    strip_def = tf.GraphDef()
    for n0 in graph_def.node:
        n = strip_def.node.add() 
        n.MergeFrom(n0)
        if n.op == 'Const':
            tensor = n.attr['value'].tensor
            size = len(tensor.tensor_content)
            if size > max_const_size:
                tensor.tensor_content = "<stripped %d bytes>"%size
    return strip_def

def show_graph(graph_def, max_const_size=32):
    """Visualize TensorFlow graph."""
    if hasattr(graph_def, 'as_graph_def'):
        graph_def = graph_def.as_graph_def()
    strip_def = strip_consts(graph_def, max_const_size=max_const_size)
    code = """
        <script>
          function load() {{
            document.getElementById("{id}").pbtxt = {data};
          }}
        </script>
        <link rel="import" href="https://tensorboard.appspot.com/tf-graph-basic.build.html" onload=load()>
        <div style="height:600px">
          <tf-graph-basic id="{id}"></tf-graph-basic>
        </div>
    """.format(data=repr(str(strip_def)), id='graph'+str(np.random.rand()))

    iframe = """
        <iframe seamless style="width:1200px;height:620px;border:0" srcdoc="{}"></iframe>
    """.format(code.replace('"', '&quot;'))
    display(HTML(iframe))


"""Create a sample tensor"""
sample_placeholder= tf.placeholder(dtype=tf.float32) 
"""Show it"""
graph_def = tf.get_default_graph().as_graph_def()
show_graph(graph_def)

Currently, you cannot run a Tensorboard service on Google Colab the way you run it locally. Also, you cannot export your entire log to your Drive via something like summary_writer = tf.summary.FileWriter('./logs', graph_def=sess.graph_def) so that you could then download it and look at it locally.



回答4:

Here's an easier way to do the same ngrok tunneling method on Google Colab.

!pip install tensorboardcolab

then,

from tensorboardcolab import TensorBoardColab, TensorBoardColabCallback

tbc=TensorBoardColab()

Assuming you are using Keras:

model.fit(......,callbacks=[TensorBoardColabCallback(tbc)])

You can read the original post here.



回答5:

I make use of google drive's back-up and sync https://www.google.com/drive/download/backup-and-sync/. The event files, which are prediodically saved in my google drive during training, are automatically synchronised to a folder on my own computer. Let's call this folder sync_folder. To access the visualizations in tensorboard I open the command prompt and type: tensorboard --logdir=sync_folder.

So, by automatically syncing my drive with my computer (using back-up and sync), I can use tensorboard as if I was training on my own computer.