I saw this question about how to visualise a tensorflow graph in Jupyter notebook. I found that this answer comes from this example with just one modification in (tensor.tensor_content = bytes("<stripped %d bytes>"%size, 'utf-8')
is replaced by tensor.tensor_content = "<stripped %d bytes>"%size
). However if I try to rerun it on tensorflow_inception_graph.pb
the visualisation doesn't work: the iframe is white and there is no nodes displayed.
I would highly appreciate if you explain me what I am doing wrong. Here there is a simple example to reproduce the problem.
Import:
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
import tensorflow as tf
import numpy as np
from IPython.display import clear_output, Image, display, HTML
Create graph:
graph = tf.Graph()
sess = tf.InteractiveSession(graph=graph)
x = tf.placeholder(tf.float32, shape=[None, 25, 25, 3], name='x')
y_true = tf.placeholder(tf.float32, shape=[None, 10], name='y_true')
y_true_cls = tf.argmax(y_true, dimension=1, name='y_true_cls')
print graph.get_operations()
The output:
[<tensorflow.python.framework.ops.Operation at 0x115902850>,
<tensorflow.python.framework.ops.Operation at 0x115902690>,
<tensorflow.python.framework.ops.Operation at 0x115902b10>,
<tensorflow.python.framework.ops.Operation at 0x1159029d0>]
The visualisation functions:
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 = bytes("<stripped %d bytes>"%size, "utf-8")
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('"', '"'))
display(HTML(iframe))
The result:
UPD I tried a simpler example:
tf.reset_default_graph()
x = tf.ones((), name="x")
y = tf.ones((), name="y")
z = tf.add(x, y, name="z")
show_graph()
But it still doesn't work. I suspect the problem is related to Javascript/HTML code that is generated:
<script>
function load() {
document.getElementById("graph0.746875762596").pbtxt = 'node {\n name: "x"\n op: "Const"\n attr {\n key: "dtype"\n value {\n type: DT_FLOAT\n }\n }\n attr {\n key: "value"\n value {\n tensor {\n dtype: DT_FLOAT\n tensor_shape {\n }\n float_val: 1.0\n }\n }\n }\n}\nnode {\n name: "y"\n op: "Const"\n attr {\n key: "dtype"\n value {\n type: DT_FLOAT\n }\n }\n attr {\n key: "value"\n value {\n tensor {\n dtype: DT_FLOAT\n tensor_shape {\n }\n float_val: 1.0\n }\n }\n }\n}\nnode {\n name: "z"\n op: "Add"\n input: "x"\n input: "y"\n attr {\n key: "T"\n value {\n type: DT_FLOAT\n }\n }\n}\n';
}
</script>
<link rel="import" href="https://tensorboard.appspot.com/tf-graph-basic.build.html" onload=load()>
<div style="height:600px">
<tf-graph-basic id="graph0.746875762596"></tf-graph-basic>
</div>
Maybe something with "
and '
?