This repository has been archived by the owner on Oct 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 216
This change uses tensorflow directly #31
Open
aliakbarhamzeh1378
wants to merge
1
commit into
RedaOps:master
Choose a base branch
from
aliakbarhamzeh1378:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,180 +27,186 @@ def ann_viz(model, view=True, filename="network.gv", title="My Neural Network"): | |
|
||
title: A title for the graph | ||
""" | ||
from graphviz import Digraph; | ||
import keras; | ||
from keras.models import Sequential; | ||
from keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten; | ||
import json; | ||
input_layer = 0; | ||
hidden_layers_nr = 0; | ||
layer_types = []; | ||
hidden_layers = []; | ||
output_layer = 0; | ||
from graphviz import Digraph | ||
from tensorflow.python.keras.layers import Conv2D, MaxPooling2D, Activation, Dense | ||
from tensorflow.python.keras.layers.core import Dropout, Flatten | ||
# import keras | ||
input_layer = 0 | ||
hidden_layers_nr = 0 | ||
layer_types = [] | ||
hidden_layers = [] | ||
output_layer = 0 | ||
for layer in model.layers: | ||
if(layer == model.layers[0]): | ||
input_layer = int(str(layer.input_shape).split(",")[1][1:-1]); | ||
hidden_layers_nr += 1; | ||
if (type(layer) == keras.layers.core.Dense): | ||
hidden_layers.append(int(str(layer.output_shape).split(",")[1][1:-1])); | ||
layer_types.append("Dense"); | ||
if layer == model.layers[0]: | ||
input_layer = int(str(layer.input_shape).split(",")[1][1:-1]) | ||
hidden_layers_nr += 1 | ||
if type(layer) == Dense: | ||
hidden_layers.append(int(str(layer.output_shape).split(",")[1][1:-1])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit complex for noobs to read, would be nice to simplify it. |
||
layer_types.append("Dense") | ||
else: | ||
hidden_layers.append(1); | ||
if (type(layer) == keras.layers.convolutional.Conv2D): | ||
layer_types.append("Conv2D"); | ||
elif (type(layer) == keras.layers.pooling.MaxPooling2D): | ||
layer_types.append("MaxPooling2D"); | ||
elif (type(layer) == keras.layers.core.Dropout): | ||
layer_types.append("Dropout"); | ||
elif (type(layer) == keras.layers.core.Flatten): | ||
layer_types.append("Flatten"); | ||
elif (type(layer) == keras.layers.core.Activation): | ||
layer_types.append("Activation"); | ||
hidden_layers.append(1) | ||
if type(layer) == Conv2D: | ||
layer_types.append("Conv2D") | ||
elif type(layer) == MaxPooling2D: | ||
layer_types.append("MaxPooling2D") | ||
elif type(layer) == Dropout: | ||
layer_types.append("Dropout") | ||
elif type(layer) == Flatten: | ||
layer_types.append("Flatten") | ||
elif type(layer) == Activation: | ||
Comment on lines
+48
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto! |
||
layer_types.append("Activation") | ||
else: | ||
if(layer == model.layers[-1]): | ||
output_layer = int(str(layer.output_shape).split(",")[1][1:-1]); | ||
if (layer == model.layers[-1]): | ||
output_layer = int(str(layer.output_shape).split(",")[1][1:-1]) | ||
else: | ||
hidden_layers_nr += 1; | ||
if (type(layer) == keras.layers.core.Dense): | ||
hidden_layers.append(int(str(layer.output_shape).split(",")[1][1:-1])); | ||
layer_types.append("Dense"); | ||
hidden_layers_nr += 1 | ||
if (type(layer) == Dense): | ||
hidden_layers.append(int(str(layer.output_shape).split(",")[1][1:-1])) | ||
layer_types.append("Dense") | ||
else: | ||
hidden_layers.append(1); | ||
if (type(layer) == keras.layers.convolutional.Conv2D): | ||
layer_types.append("Conv2D"); | ||
elif (type(layer) == keras.layers.pooling.MaxPooling2D): | ||
layer_types.append("MaxPooling2D"); | ||
elif (type(layer) == keras.layers.core.Dropout): | ||
layer_types.append("Dropout"); | ||
elif (type(layer) == keras.layers.core.Flatten): | ||
layer_types.append("Flatten"); | ||
elif (type(layer) == keras.layers.core.Activation): | ||
layer_types.append("Activation"); | ||
last_layer_nodes = input_layer; | ||
nodes_up = input_layer; | ||
if(type(model.layers[0]) != keras.layers.core.Dense): | ||
last_layer_nodes = 1; | ||
nodes_up = 1; | ||
input_layer = 1; | ||
hidden_layers.append(1) | ||
if (type(layer) == Conv2D): | ||
layer_types.append("Conv2D") | ||
elif (type(layer) == MaxPooling2D): | ||
layer_types.append("MaxPooling2D") | ||
elif (type(layer) == Dropout): | ||
layer_types.append("Dropout") | ||
elif (type(layer) == Flatten): | ||
layer_types.append("Flatten") | ||
elif (type(layer) == Activation): | ||
layer_types.append("Activation") | ||
Comment on lines
+68
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
last_layer_nodes = input_layer | ||
nodes_up = input_layer | ||
if type(model.layers[0]) != Dense: | ||
last_layer_nodes = 1 | ||
nodes_up = 1 | ||
input_layer = 1 | ||
|
||
g = Digraph('g', filename=filename); | ||
n = 0; | ||
g.graph_attr.update(splines="false", nodesep='1', ranksep='2'); | ||
#Input Layer | ||
g = Digraph('g', filename=filename) | ||
n = 0 | ||
g.graph_attr.update(splines="false", nodesep='1', ranksep='2') | ||
# Input Layer | ||
with g.subgraph(name='cluster_input') as c: | ||
if(type(model.layers[0]) == keras.layers.core.Dense): | ||
the_label = title+'\n\n\n\nInput Layer'; | ||
if (int(str(model.layers[0].input_shape).split(",")[1][1:-1]) > 10): | ||
the_label += " (+"+str(int(str(model.layers[0].input_shape).split(",")[1][1:-1]) - 10)+")"; | ||
input_layer = 10; | ||
if type(model.layers[0]) == Dense: | ||
the_label = title + '\n\n\n\nInput Layer' | ||
if int(str(model.layers[0].input_shape).split(",")[1][1:-1]) > 10: | ||
the_label += " (+" + str(int(str(model.layers[0].input_shape).split(",")[1][1:-1]) - 10) + ")" | ||
input_layer = 10 | ||
c.attr(color='white') | ||
for i in range(0, input_layer): | ||
n += 1; | ||
c.node(str(n)); | ||
n += 1 | ||
c.node(str(n)) | ||
c.attr(label=the_label) | ||
c.attr(rank='same'); | ||
c.node_attr.update(color="#2ecc71", style="filled", fontcolor="#2ecc71", shape="circle"); | ||
c.attr(rank='same') | ||
c.node_attr.update(color="#2ecc71", style="filled", fontcolor="#2ecc71", shape="circle") | ||
|
||
elif(type(model.layers[0]) == keras.layers.convolutional.Conv2D): | ||
#Conv2D Input visualizing | ||
the_label = title+'\n\n\n\nInput Layer'; | ||
c.attr(color="white", label=the_label); | ||
c.node_attr.update(shape="square"); | ||
pxls = str(model.layers[0].input_shape).split(','); | ||
clr = int(pxls[3][1:-1]); | ||
elif (type(model.layers[0]) == Conv2D): | ||
# Conv2D Input visualizing | ||
the_label = title + '\n\n\n\nInput Layer' | ||
c.attr(color="white", label=the_label) | ||
c.node_attr.update(shape="square") | ||
pxls = str(model.layers[0].input_shape).split(',') | ||
clr = int(pxls[3][1:-1]) | ||
if (clr == 1): | ||
clrmap = "Grayscale"; | ||
the_color = "black:white"; | ||
clrmap = "Grayscale" | ||
the_color = "black:white" | ||
elif (clr == 3): | ||
clrmap = "RGB"; | ||
the_color = "#e74c3c:#3498db"; | ||
clrmap = "RGB" | ||
the_color = "#e74c3c:#3498db" | ||
else: | ||
clrmap = ""; | ||
c.node_attr.update(fontcolor="white", fillcolor=the_color, style="filled"); | ||
n += 1; | ||
c.node(str(n), label="Image\n"+pxls[1]+" x"+pxls[2]+" pixels\n"+clrmap, fontcolor="white"); | ||
clrmap = "" | ||
c.node_attr.update(fontcolor="white", fillcolor=the_color, style="filled") | ||
n += 1 | ||
c.node(str(n), label="Image\n" + pxls[1] + " x" + pxls[2] + " pixels\n" + clrmap, fontcolor="white") | ||
else: | ||
raise ValueError("ANN Visualizer: Layer not supported for visualizing"); | ||
raise ValueError("ANN Visualizer: Layer not supported for visualizing") | ||
for i in range(0, hidden_layers_nr): | ||
with g.subgraph(name="cluster_"+str(i+1)) as c: | ||
with g.subgraph(name="cluster_" + str(i + 1)) as c: | ||
if (layer_types[i] == "Dense"): | ||
c.attr(color='white'); | ||
c.attr(rank='same'); | ||
#If hidden_layers[i] > 10, dont include all | ||
the_label = ""; | ||
c.attr(color='white') | ||
c.attr(rank='same') | ||
# If hidden_layers[i] > 10, dont include all | ||
the_label = "" | ||
if (int(str(model.layers[i].output_shape).split(",")[1][1:-1]) > 10): | ||
the_label += " (+"+str(int(str(model.layers[i].output_shape).split(",")[1][1:-1]) - 10)+")"; | ||
hidden_layers[i] = 10; | ||
c.attr(labeljust="right", labelloc="b", label=the_label); | ||
the_label += " (+" + str(int(str(model.layers[i].output_shape).split(",")[1][1:-1]) - 10) + ")" | ||
hidden_layers[i] = 10 | ||
c.attr(labeljust="right", labelloc="b", label=the_label) | ||
for j in range(0, hidden_layers[i]): | ||
n += 1; | ||
c.node(str(n), shape="circle", style="filled", color="#3498db", fontcolor="#3498db"); | ||
for h in range(nodes_up - last_layer_nodes + 1 , nodes_up + 1): | ||
g.edge(str(h), str(n)); | ||
last_layer_nodes = hidden_layers[i]; | ||
nodes_up += hidden_layers[i]; | ||
n += 1 | ||
c.node(str(n), shape="circle", style="filled", color="#3498db", fontcolor="#3498db") | ||
for h in range(nodes_up - last_layer_nodes + 1, nodes_up + 1): | ||
g.edge(str(h), str(n)) | ||
last_layer_nodes = hidden_layers[i] | ||
nodes_up += hidden_layers[i] | ||
elif (layer_types[i] == "Conv2D"): | ||
c.attr(style='filled', color='#5faad0'); | ||
n += 1; | ||
kernel_size = str(model.layers[i].get_config()['kernel_size']).split(',')[0][1] + "x" + str(model.layers[i].get_config()['kernel_size']).split(',')[1][1 : -1]; | ||
filters = str(model.layers[i].get_config()['filters']); | ||
c.node("conv_"+str(n), label="Convolutional Layer\nKernel Size: "+kernel_size+"\nFilters: "+filters, shape="square"); | ||
c.node(str(n), label=filters+"\nFeature Maps", shape="square"); | ||
g.edge("conv_"+str(n), str(n)); | ||
for h in range(nodes_up - last_layer_nodes + 1 , nodes_up + 1): | ||
g.edge(str(h), "conv_"+str(n)); | ||
last_layer_nodes = 1; | ||
nodes_up += 1; | ||
c.attr(style='filled', color='#5faad0') | ||
n += 1 | ||
kernel_size = str(model.layers[i].get_config()['kernel_size']).split(',')[0][1] + "x" + \ | ||
str(model.layers[i].get_config()['kernel_size']).split(',')[1][1: -1] | ||
filters = str(model.layers[i].get_config()['filters']) | ||
c.node("conv_" + str(n), | ||
label="Convolutional Layer\nKernel Size: " + kernel_size + "\nFilters: " + filters, | ||
shape="square") | ||
c.node(str(n), label=filters + "\nFeature Maps", shape="square") | ||
g.edge("conv_" + str(n), str(n)) | ||
for h in range(nodes_up - last_layer_nodes + 1, nodes_up + 1): | ||
g.edge(str(h), "conv_" + str(n)) | ||
last_layer_nodes = 1 | ||
nodes_up += 1 | ||
elif (layer_types[i] == "MaxPooling2D"): | ||
c.attr(color="white"); | ||
n += 1; | ||
pool_size = str(model.layers[i].get_config()['pool_size']).split(',')[0][1] + "x" + str(model.layers[i].get_config()['pool_size']).split(',')[1][1 : -1]; | ||
c.node(str(n), label="Max Pooling\nPool Size: "+pool_size, style="filled", fillcolor="#8e44ad", fontcolor="white"); | ||
for h in range(nodes_up - last_layer_nodes + 1 , nodes_up + 1): | ||
g.edge(str(h), str(n)); | ||
last_layer_nodes = 1; | ||
nodes_up += 1; | ||
c.attr(color="white") | ||
n += 1 | ||
pool_size = str(model.layers[i].get_config()['pool_size']).split(',')[0][1] + "x" + \ | ||
str(model.layers[i].get_config()['pool_size']).split(',')[1][1: -1] | ||
c.node(str(n), label="Max Pooling\nPool Size: " + pool_size, style="filled", fillcolor="#8e44ad", | ||
fontcolor="white") | ||
for h in range(nodes_up - last_layer_nodes + 1, nodes_up + 1): | ||
g.edge(str(h), str(n)) | ||
last_layer_nodes = 1 | ||
nodes_up += 1 | ||
elif (layer_types[i] == "Flatten"): | ||
n += 1; | ||
c.attr(color="white"); | ||
c.node(str(n), label="Flattening", shape="invtriangle", style="filled", fillcolor="#2c3e50", fontcolor="white"); | ||
for h in range(nodes_up - last_layer_nodes + 1 , nodes_up + 1): | ||
g.edge(str(h), str(n)); | ||
last_layer_nodes = 1; | ||
nodes_up += 1; | ||
n += 1 | ||
c.attr(color="white") | ||
c.node(str(n), label="Flattening", shape="invtriangle", style="filled", fillcolor="#2c3e50", | ||
fontcolor="white") | ||
for h in range(nodes_up - last_layer_nodes + 1, nodes_up + 1): | ||
g.edge(str(h), str(n)) | ||
last_layer_nodes = 1 | ||
nodes_up += 1 | ||
elif (layer_types[i] == "Dropout"): | ||
n += 1; | ||
c.attr(color="white"); | ||
c.node(str(n), label="Dropout Layer", style="filled", fontcolor="white", fillcolor="#f39c12"); | ||
for h in range(nodes_up - last_layer_nodes + 1 , nodes_up + 1): | ||
g.edge(str(h), str(n)); | ||
last_layer_nodes = 1; | ||
nodes_up += 1; | ||
n += 1 | ||
c.attr(color="white") | ||
c.node(str(n), label="Dropout Layer", style="filled", fontcolor="white", fillcolor="#f39c12") | ||
for h in range(nodes_up - last_layer_nodes + 1, nodes_up + 1): | ||
g.edge(str(h), str(n)) | ||
last_layer_nodes = 1 | ||
nodes_up += 1 | ||
elif (layer_types[i] == "Activation"): | ||
n += 1; | ||
c.attr(color="white"); | ||
fnc = model.layers[i].get_config()['activation']; | ||
c.node(str(n), shape="octagon", label="Activation Layer\nFunction: "+fnc, style="filled", fontcolor="white", fillcolor="#00b894"); | ||
for h in range(nodes_up - last_layer_nodes + 1 , nodes_up + 1): | ||
g.edge(str(h), str(n)); | ||
last_layer_nodes = 1; | ||
nodes_up += 1; | ||
|
||
n += 1 | ||
c.attr(color="white") | ||
fnc = model.layers[i].get_config()['activation'] | ||
c.node(str(n), shape="octagon", label="Activation Layer\nFunction: " + fnc, style="filled", | ||
fontcolor="white", fillcolor="#00b894") | ||
for h in range(nodes_up - last_layer_nodes + 1, nodes_up + 1): | ||
g.edge(str(h), str(n)) | ||
last_layer_nodes = 1 | ||
nodes_up += 1 | ||
|
||
with g.subgraph(name='cluster_output') as c: | ||
if (type(model.layers[-1]) == keras.layers.core.Dense): | ||
if (type(model.layers[-1]) == Dense): | ||
c.attr(color='white') | ||
c.attr(rank='same'); | ||
c.attr(labeljust="1"); | ||
for i in range(1, output_layer+1): | ||
n += 1; | ||
c.node(str(n), shape="circle", style="filled", color="#e74c3c", fontcolor="#e74c3c"); | ||
for h in range(nodes_up - last_layer_nodes + 1 , nodes_up + 1): | ||
g.edge(str(h), str(n)); | ||
c.attr(rank='same') | ||
c.attr(labeljust="1") | ||
for i in range(1, output_layer + 1): | ||
n += 1 | ||
c.node(str(n), shape="circle", style="filled", color="#e74c3c", fontcolor="#e74c3c") | ||
for h in range(nodes_up - last_layer_nodes + 1, nodes_up + 1): | ||
g.edge(str(h), str(n)) | ||
c.attr(label='Output Layer', labelloc="bottom") | ||
c.node_attr.update(color="#2ecc71", style="filled", fontcolor="#2ecc71", shape="circle"); | ||
c.node_attr.update(color="#2ecc71", style="filled", fontcolor="#2ecc71", shape="circle") | ||
|
||
g.attr(arrowShape="none"); | ||
g.edge_attr.update(arrowhead="none", color="#707070"); | ||
g.attr(arrowShape="none") | ||
g.edge_attr.update(arrowhead="none", color="#707070") | ||
if view == True: | ||
g.view(); | ||
g.view() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps, rewrite this as: