Created
March 27, 2017 13:31
-
-
Save solaris33/8578fdffc9b0a509d76684ed83b1bdf6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
layers = [op.name for op in graph.get_operations() if op.type=='Conv2D' and 'import/' in op.name] | |
feature_nums = [int(graph.get_tensor_by_name(name+':0').get_shape()[-1]) for name in layers] | |
print('Number of layers', len(layers)) | |
print('Total number of feature channels:', sum(feature_nums)) | |
# Helper functions for TF Graph visualization | |
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) | |
return strip_def | |
def rename_nodes(graph_def, rename_func): | |
res_def = tf.GraphDef() | |
for n0 in graph_def.node: | |
n = res_def.node.add() | |
n.MergeFrom(n0) | |
n.name = rename_func(n.name) | |
for i, s in enumerate(n.input): | |
n.input[i] = rename_func(s) if s[0]!='^' else '^'+rename_func(s[1:]) | |
return res_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:800px;height:620px;border:0" srcdoc="{}"></iframe> | |
""".format(code.replace('"', '"')) | |
display(HTML(iframe)) | |
# Visualizing the network graph. Be sure expand the "mixed" nodes to see their | |
# internal structure. We are going to visualize "Conv2D" nodes. | |
tmp_def = rename_nodes(graph_def, lambda s:"/".join(s.split('_',1))) | |
show_graph(tmp_def) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment