-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathneuralplot.py
193 lines (149 loc) · 5.83 KB
/
neuralplot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
def model2layers(model):
layers = []
for i in model.layers:
name = str(i.with_name_scope).split('.')[-1][:-3]
if name == 'InputLayer':
shape = i.input_shape[0][1:]
elif name == 'MaxPooling2D':
shape = i.input_shape[1:]
else:
shape = i.output_shape[1:]
layers.append((tuple(shape), name))
return layers
class Layer():
def __init__(self, shape, name):
if len(shape) == 1:
self.shape = (shape[0],1,1)
elif len(shape) == 2:
self.shape = (shape[0],shape[1],1)
else:
if name == 'MaxPooling2D' or name == 'AveragePooling2D':
self.shape = (shape[0],shape[1],1)
else:
self.shape = shape
self.x = self.shape[0]
self.y = self.shape[1]
self.z = self.shape[2]
self.name = name
if len(self.shape) == 3 and self.shape[-1] == 3:
self.color = 'rgb'
self.marker = 'o'
else:
if self.name == 'InputLayer':
self.color = 'r'
self.marker = 'o'
elif self.name == 'Conv2D':
self.color = 'y'
self.marker = '^'
elif self.name == 'MaxPooling2D' or self.name == 'AveragePooling2D':
self.color = 'c'
self.marker = '.'
else:
self.color = 'g'
self.marker = '.'
def shape2array(shape, layers_len, xy_max):
x = shape[0]
y = shape[1]
z = shape[2]
single_layer = []
if xy_max[0] < x:
xy_max[0] = x
if xy_max[1] < y:
xy_max[1] = y
for k in range(z):
mx,my,mz = [],[],[]
for i in range(y):
ox = [j for j in range(x)]
mx.append(ox)
for i in range(y):
oy = [j for j in (np.ones(x,dtype=int) * i)]
my.append(oy)
for i in range(x):
oz = [j for j in (np.ones(y,dtype=int) * layers_len)]
mz.append(oz)
layers_len += 2
single_layer.append([mx,my,mz])
layers_len += 4
return single_layer, layers_len, xy_max
# Dense layer
def dense(ax, x1=1, x2=1, y1=1, y2=1, x11=1, x21=1, y11=1, y21=1, z1=1, z2=1, c='r', linewidth=.5):
for i in np.arange(x1,x2+1,1): # 0 1 2 3 X
for j in np.arange(x11,x21+1,1): # 0 1 2 3 X
for k in np.arange(y1,y2+1,1): # 1 2
for l in np.arange(y11,y21+1,1): # 1 2
ax.plot([i, j], [z1, z2], [k, l], c=c,linewidth=linewidth)
def plot_dots(layers_array, layers_name, layers_color, layers_marker, ax, xy_max, linewidth, connection):
temp = True
last_a,last_b,last_c = [0,0], [0,0], [0,0]
for layer, name, color_in, marker in zip(layers_array, layers_name, layers_color, layers_marker):
line_x, line_y, line_z = [],[],[]
color_count = 0
for j in layer:
my_x, my_y, my_z = [],[],[]
for k in j[0]:
k = [a + ((xy_max[0]-len(k))/2) for a in k]
my_x += k
line_x.append([k[0],k[-1]])
temp_list_l=[]
for l in j[1]:
l = [b + ((xy_max[1]-(j[1][-1][-1]+1))/2) for b in l]
my_y += l
temp_list_l.append(l[0])
line_y.append([temp_list_l[0],temp_list_l[-1]])
for k in j[2]:
my_z += k
line_z.append([k[0],k[-1]])
if color_in == 'rgb':
color = color_in[color_count]
color_count+=1
else:
color = color_in
ax.scatter(my_x, my_z, my_y, c=color, marker=marker, s=20)
if connection:
if name == 'Dense' or name == 'Flatten':
for c in line_z:
a,b,c = line_x[0],line_y[0], c
if temp:
temp = False
last_a,last_b,last_c = a,b,c
continue
if color_in == 'rgb':
color = color_in[color_count]
color_count+=1
else:
color = color_in
dense(ax, a[0],a[1],b[0],b[1], last_a[0],last_a[1],last_b[0],last_b[1],c[0],last_c[0], c=color, linewidth=linewidth)
last_a,last_b,last_c = a,b,c
def neuralplot(model, grid=True, connection=True, linewidth=0.1):
# Default mata data
# %matplotlib notebook
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, projection='3d')
layers_len = 0
layers_array = []
layers_name = []
layers_color = []
layers_marker = []
xy_max = [0, 0]
# convert model to layers
layers = model2layers(model)
# create layers array
for lay in layers:
obj = Layer(lay[0], lay[1])
single_layer, layers_len, xy_max = shape2array(obj.shape, layers_len, xy_max)
layers_array.append(single_layer)
layers_name.append(obj.name)
layers_color.append(obj.color)
layers_marker.append(obj.marker)
# plot dots and lines
plot_dots(layers_array, layers_name, layers_color, layers_marker, ax, xy_max, linewidth, connection)
# Hide axes ticks
if grid == False:
ax.grid(False)
plt.axis('off')
# plt.show()
# plt.savefig('fig_new.png', dpi=300)