Skip to content

Instantly share code, notes, and snippets.

@j-min
Created May 16, 2017 04:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j-min/b37a4c664b2496d284dad989c2c6fb6f to your computer and use it in GitHub Desktop.
Save j-min/b37a4c664b2496d284dad989c2c6fb6f to your computer and use it in GitHub Desktop.
tensorboard inline
from IPython.display import clear_output, Image, display, HTML
import numpy as np
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))
@j-min
Copy link
Author

j-min commented May 16, 2017

Sample usage

Build Graph

import tensorflow as tf
from tensorflow.contrib import slim
from tensorflow.contrib.slim.nets import resnet_utils, resnet_v1

num_gpus = 1

tf.reset_default_graph()

for i in range(num_gpus):
    with tf.name_scope('tower_%d' % i):
        image = tf.ones(shape=[1,1,1,3], name='input_image')
        
        with slim.arg_scope(resnet_v1.resnet_arg_scope()):

            blocks = [
                resnet_v1.resnet_v1_block('block1', base_depth=64, num_units=3, stride=2),
                resnet_v1.resnet_v1_block('block2', base_depth=128, num_units=4, stride=2),
                resnet_v1.resnet_v1_block('block3', base_depth=256, num_units=23, stride=2),
            ]

            net, end_points = resnet_v1.resnet_v1(
                inputs=image,
                blocks=blocks,
                scope='resnet_demo')
            
        tf.get_variable_scope().reuse_variables()

Vizualization

show_graph(tf.get_default_graph().as_graph_def())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment