87

Is there any way to use TensorBoard when training a TensorFlow model on Google Colab?

korakot
  • 24,489
  • 13
  • 84
  • 114
ociule
  • 981
  • 1
  • 7
  • 10
  • 2
    Official documentation: https://colab.research.google.com/github/tensorflow/tensorboard/blob/master/docs/tensorboard_in_notebooks.ipynb – EFreak Apr 27 '20 at 21:23

19 Answers19

87

EDIT: You probably want to give the official %tensorboard magic a go, available from TensorFlow 1.13 onward.


Prior to the existence of the %tensorboard magic, the standard way to achieve this was to proxy network traffic to the Colab VM using ngrok. 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'])"
    
wchargin
  • 14,264
  • 11
  • 61
  • 106
Joppe Geluykens
  • 1,714
  • 13
  • 16
  • I am trying to run the tensorbaord in my colabVM, & also need the events & checkpoints to be stored in my google drive? Does your solution help in that regards?. Moreover, it would be really helpful if you could elaborate more a bit how your method written in your colab could achieve that. Did bring the events from colabVM to local desktop using this method? – Anu Jan 20 '19 at 17:20
  • 1
    @anu There are example notebooks for those use cases: [Drive IO](https://colab.research.google.com/notebooks/io.ipynb), [system aliases](https://colab.research.google.com/notebooks/basic_features_overview.ipynb#scrollTo=Wej_mEyXQSHc). – Joppe Geluykens Jan 21 '19 at 13:20
50

Many of the answers here are now obsolete. So will be mine I'm sure in a few weeks. But at the time of this writing all I had to do is run these lines of code from colab. And tensorboard opened up just fine.

%load_ext tensorboard
%tensorboard --logdir logs
RajV
  • 5,925
  • 6
  • 39
  • 54
  • 2
    Hi, thanks for the comment. Did you run these two lines in colab notebook? I did and I get a TensorBoard window in my notebook with "No dashboards are active for the current data set." message. Can you help me? – desmond13 Apr 22 '20 at 11:19
25

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.

Keshan
  • 12,589
  • 10
  • 44
  • 71
  • 1
    Hi, this seems really cool, but I'm getting this error in Colab: `FailedPreconditionError: Error while reading resource variable conv_dw_8/depthwise_kernel from Container: localhost. This could mean that the variable was uninitialized.` – Austin Sep 02 '18 at 16:51
  • @Austin The author of tensorboardcolab has found a work-around for that: "I find that it happens occasionally and can be solved by: 1.Waiting 2.Restart Colab server 3.Change Colab server types (CPU/GPU/TPU), and then change it back". See his comment [here](https://medium.com/@tommytao_54597/use-tensorboard-in-google-colab-16b4bb9812a6) – NeStack Mar 08 '19 at 14:20
  • Also, make sure that your model is constructed directly our of Keras and not embeded tensorflow. For example, instead of `model.add(tf.keras.layers.LSTM(....))` do `model.add(keras.layers.LSTM(...))`. Else there might be error messages. – NeStack Mar 08 '19 at 14:25
12

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

solver149
  • 385
  • 3
  • 11
6

I tried but did not get the result but when used as below, got the results

import tensorboardcolab as tb
tbc = tb.TensorBoardColab()

after this open the link from the output.

import tensorflow as tf
import numpy as np

Explicitly create a Graph object

graph = tf.Graph()
with graph.as_default()

Complete example :

with tf.name_scope("variables"):
    # Variable to keep track of how many times the graph has been run
    global_step = tf.Variable(0, dtype=tf.int32, name="global_step")
    
    # Increments the above `global_step` Variable, should be run whenever the graph is run
    increment_step = global_step.assign_add(1)
    
    # Variable that keeps track of previous output value:
    previous_value = tf.Variable(0.0, dtype=tf.float32, name="previous_value")

# Primary transformation Operations
with tf.name_scope("exercise_transformation"):
    
    # Separate input layer
    with tf.name_scope("input"):
        # Create input placeholder- takes in a Vector 
        a = tf.placeholder(tf.float32, shape=[None], name="input_placeholder_a")

    # Separate middle layer
    with tf.name_scope("intermediate_layer"):
        b = tf.reduce_prod(a, name="product_b")
        c = tf.reduce_sum(a, name="sum_c")
    
    # Separate output layer
    with tf.name_scope("output"):
        d = tf.add(b, c, name="add_d")
        output = tf.subtract(d, previous_value, name="output")
        update_prev = previous_value.assign(output)

# Summary Operations
with tf.name_scope("summaries"):
    tf.summary.scalar('output', output)  # Creates summary for output node
    tf.summary.scalar('product of inputs', b, )
    tf.summary.scalar('sum of inputs', c)

# Global Variables and Operations
with tf.name_scope("global_ops"):
    # Initialization Op
    init = tf.initialize_all_variables()
    # Collect all summary Ops in graph
    merged_summaries = tf.summary.merge_all()

# Start a Session, using the explicitly created Graph
sess = tf.Session(graph=graph)

# Open a SummaryWriter to save summaries
writer = tf.summary.FileWriter('./Graph', sess.graph)

# Initialize Variables
sess.run(init)

def run_graph(input_tensor):
    """
    Helper function; runs the graph with given input tensor and saves summaries
    """
    feed_dict = {a: input_tensor}
    output, summary, step = sess.run([update_prev, merged_summaries, increment_step], feed_dict=feed_dict)
    writer.add_summary(summary, global_step=step)


# Run the graph with various inputs
run_graph([2,8])
run_graph([3,1,3,3])
run_graph([8])
run_graph([1,2,3])
run_graph([11,4])
run_graph([4,1])
run_graph([7,3,1])
run_graph([6,3])
run_graph([0,2])
run_graph([4,5,6])

# Writes the summaries to disk
writer.flush()

# Flushes the summaries to disk and closes the SummaryWriter
writer.close()

# Close the session
sess.close()

# To start TensorBoard after running this file, execute the following command:
# $ tensorboard --logdir='./improved_graph'
desertnaut
  • 46,107
  • 19
  • 109
  • 140
DJ6968
  • 89
  • 1
  • 2
5

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.

JMA
  • 1,673
  • 9
  • 16
4

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 logs. To access the visualizations in tensorboard I open the command prompt, navigate to the synchronized google drive folder, and type: tensorboard --logdir=logs.

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

Edit: Here is a notebook that might be helpful. https://colab.research.google.com/gist/MartijnCa/961c5f4c774930f4bdd32d51829da6f6/tensorboard-with-google-drive-backup-and-sync.ipynb

  • Could you please write your method in a sharable colab and share here in your post. That would be really helpful and quick ! – Anu Jan 20 '19 at 17:21
  • 1
    By following your advice I can only access through colab the files in "My Drive", but not in "Computers", where my synced computer is located. And your notebook doesn't log into your synced computer neither, but into the "My Drive" folder - see the line `os.chdir('/content/drive/My Drive')`. Can you explain further how you access your synced computer? – NeStack Mar 16 '19 at 20:20
  • 1
    @NeStack You are right my solution uses only the "My Drive" folder. Sorry for the confusion, I do not use "computers". You can use the backup and sync program to synchronize the "My Drive" folder to your own computer. This way you can access the event files in the file explorer on your own computer. – Martijn Cazemier Mar 18 '19 at 10:01
  • @MartijnCazemier Alright, this makes sense. It is an option that would work for me, too – NeStack Mar 18 '19 at 14:20
  • @MartijnCazemier it does not allow me because My drive has a space, and nor does it allow to escape it. – technazi Sep 21 '19 at 18:37
4

2.0 Compatible Answer: Yes, you can use Tensorboard in Google Colab. Please find the below code which shows the complete example.

!pip install tensorflow==2.0

import tensorflow as tf
# The function to be traced.
@tf.function
def my_func(x, y):
  # A simple hand-rolled layer.
  return tf.nn.relu(tf.matmul(x, y))

# Set up logging.
logdir = './logs/func'
writer = tf.summary.create_file_writer(logdir)

# Sample data for your function.
x = tf.random.uniform((3, 3))
y = tf.random.uniform((3, 3))

# Bracket the function call with
# tf.summary.trace_on() and tf.summary.trace_export().
tf.summary.trace_on(graph=True, profiler=True)
# Call only one tf.function when tracing.
z = my_func(x, y)
with writer.as_default():
  tf.summary.trace_export(
      name="my_func_trace",
      step=0,
      profiler_outdir=logdir)

%load_ext tensorboard
%tensorboard --logdir ./logs/func

For the working copy of Google Colab, please refer this link. For more information, please go through this link.

Tensorflow Support
  • 5,091
  • 1
  • 21
  • 52
2

There is an alternative solution but we have to use TFv2.0 preview. So if you don't have problems with the migration try this:

install tfv2.0 for GPU or CPU (TPU no available yet)

CPU
tf-nightly-2.0-preview
GPU
tf-nightly-gpu-2.0-preview

%%capture
!pip install -q tf-nightly-gpu-2.0-preview
# Load the TensorBoard notebook extension
# %load_ext tensorboard.notebook # For older versions
%load_ext tensorboard

import TensorBoard as usual:

from tensorflow.keras.callbacks import TensorBoard

Clean or Create folder where to save the logs (run this lines before run the training fit())

# Clear any logs from previous runs
import time

!rm -R ./logs/ # rf
log_dir="logs/fit/{}".format(time.strftime("%Y%m%d-%H%M%S", time.gmtime()))
tensorboard = TensorBoard(log_dir=log_dir, histogram_freq=1)

Have fun with TensorBoard! :)

%tensorboard --logdir logs/fit

Here the official colab notebook and the repo on github

New TFv2.0 alpha release:

CPU
!pip install -q tensorflow==2.0.0-alpha0 GPU
!pip install -q tensorflow-gpu==2.0.0-alpha0

M. Erfan Mowlaei
  • 1,106
  • 1
  • 13
  • 24
virtualdvid
  • 1,966
  • 3
  • 8
  • 27
2

I tried to show TensorBoard on google colab today,

# in case of CPU, you can this line
# !pip install -q tf-nightly-2.0-preview
# in case of GPU, you can use this line
!pip install -q tf-nightly-gpu-2.0-preview

# %load_ext tensorboard.notebook  # not working on 22 Apr
%load_ext tensorboard # you need to use this line instead

import tensorflow as tf

'################
do training
'################

# show tensorboard
%tensorboard --logdir logs/fit

here is actual example made by google. https://colab.research.google.com/github/tensorflow/tensorboard/blob/master/docs/r2/get_started.ipynb

Naga
  • 8,886
  • 2
  • 17
  • 32
2

Yes definitely, using tensorboard in google colab is quite easy. Follow the following steps-

1) Load the tensorboard extension

%load_ext tensorboard.notebook

2) Add it to keras callback

tensorboard_callback = tf.keras.callbacks.TensorBoard(logdir, histogram_freq=1)

3) Start tensorboard

%tensorboard — logdir logs

Hope it helps.

2

Using summary_writer to write log at every epoch in a folder then running the following magic worked for me.

%load_ext tensorboard 
%tensorboard --logdir=./logs 
Mridul Pandey
  • 162
  • 2
  • 5
2

You can directly connect to tensorboard in google colab using the recent upgrade from google colab.

https://medium.com/@today.rafi/tensorboard-in-google-colab-bd49fa554f9b

rafi
  • 346
  • 3
  • 9
2

According to the documentation all you need to do is this:

%load_ext tensorboard
!rm -rf ./logs/ #to delete previous runs
%tensorboard --logdir logs/
tensorboard = TensorBoard(log_dir="./logs")

And just call it in the fit method:

model.fit(X_train, y_train, epochs = 1000,
         callbacks=[tensorboard], validation_data=(X_test, y_test))

And that should give you something like this:

I can't post a picture yet so use the link.

hhh
  • 44,388
  • 56
  • 154
  • 251
1

To join @solver149 answer, here is a simple example how to use TensorBoard in google colab

1.Create the Graph,ex:

a = tf.constant(3.0, dtype=tf.float32)
b = tf.constant(4.0) 
total = a + b

2. Install Tensorboard

!pip install tensorboardcolab # to install tensorboeadcolab if it does not it not exist

==> Result in my case :

Requirement already satisfied: tensorboardcolab in /usr/local/lib/python3.6/dist-packages (0.0.22)

3. Use it :)

Fist of all import TensorBoard from tensorboaedcolab (you can use import* to import everything at once), then create your tensorboeardcolab after that attach a writer to it like this :

from tensorboardcolab import * 
tbc = TensorBoardColab() # To create a tensorboardcolab object it will automatically creat a link
writer = tbc.get_writer() # To create a FileWriter
writer.add_graph(tf.get_default_graph()) # add the graph 
writer.flush()

==> Result

Using TensorFlow backend.

Wait for 8 seconds...
TensorBoard link:
http://cf426c39.ngrok.io

4.Check the given link :D

Tensorboard_Result_Graph_Image

This example was token from TF guide : TensorBoard.

DINA TAKLIT
  • 4,946
  • 7
  • 42
  • 50
  • 1
    The only solution which worked for me perfectly on Colab. Thank you @Dina Taklit! – JChat Jul 28 '19 at 19:49
  • I receive an error, `AttributeError: module 'tensorflow_core.summary' has no attribute 'FileWriter'` – sarannns Jan 09 '20 at 21:45
  • @sarannns I hope this will help https://stackoverflow.com/questions/43304270/attributeerror-module-tensorflow-python-summary-summary-has-no-attribute-fil – DINA TAKLIT Jan 14 '20 at 19:30
1

TensorBoard works with Google Colab and TensorFlow 2.0

!pip install tensorflow==2.0.0-alpha0 
%load_ext tensorboard.notebook
chb
  • 1,397
  • 7
  • 23
  • 39
mmulibra
  • 109
  • 1
  • 2
  • 10
0

Simple and easiest way I have found so far:

Get setup_google_colab.py file using wget

!wget https://raw.githubusercontent.com/hse-aml/intro-to- dl/master/setup_google_colab.py -O setup_google_colab.py
import setup_google_colab

To run tensorboard in background, expose port and click on the link.
I am assuming that you have proper added value to visualize in your summary and then merge all summaries.

import os
os.system("tensorboard --logdir=./logs --host 0.0.0.0 --port 6006 &")
setup_google_colab.expose_port_on_colab(6006)

After running above statements you will prompted with a link like:

Open https://a1b2c34d5.ngrok.io to access your 6006 port

Refer following git for further help:

https://github.com/MUmarAmanat/MLWithTensorflow/blob/master/colab_tensorboard.ipynb
  • Have a look at the other answers, the options above look better as they don't have a manual step required. – lucid_dreamer Jun 20 '19 at 22:39
  • @lucid_dreamer, no doubt above answer have better explanation, steps and also easy, but if we know other answers do let other people know all techniques, may be some techniques helpful for some people. Its all depends on your choice. – Muhammad Umar Amanat Jun 24 '19 at 06:21
  • I am getting ```AttributeError: module 'setup_google_colab' has no attribute 'expose_port_on_colab'``` – Biranchi Aug 13 '20 at 08:50
  • @Biranchi expose_port_on_colab is function is in setup_google_colab.py, you can visit this link for reference https://github.com/hse-aml/intro-to-dl/blob/master/setup_google_colab.py#L105 – Muhammad Umar Amanat Aug 14 '20 at 14:11
0

I am using tensorflow==1.15.

%load_ext tensorboard
%tensorboard --logdir /content/logs

works for me.

/content/logs

is the path of my logs in google drive.

ziyi liu
  • 31
  • 1
  • 5
-1

Try this, it's working for me

%load_ext tensorboard
import datetime
logdir = os.path.join("logs", datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
tensorboard_callback = tf.keras.callbacks.TensorBoard(logdir, histogram_freq=1)

 model.fit(x=x_train, 
        y=y_train, 
        epochs=5, 
        validation_data=(x_test, y_test), 
        callbacks=[tensorboard_callback])
Myra
  • 11
  • 1
  • 3