-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvisualize.php
292 lines (229 loc) · 10.6 KB
/
visualize.php
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
ini_set("max_execution_time", "-1");
ini_set('memory_limit','-1');
set_time_limit(0);
$neuron_size = 50; // height & width in px
$tiles_between_neurons = 2;
$draw_connection_weights = true;
// In densely connected networks it can be difficult
// to see all the connections so randomizing the
// connection color can improve the visibility some,
// See the Pathfinder examples.
// true = random color
// false = colors: red negitive, green positive
$random_connection_color = true;
$tiles_between_layers = 3;
$draw_grid = false;
$output_stats_image = true; // Output a second image with the ANN stats?
// Rotate view?
// default is "waterfall" inputs on top outputs on bottom
// rotate set to true is inputs on the left and outputs on the right
$rotate = false;
// Path to ANN's - change to your bot
$ann_name = "xor_float.net";
//$ann_name = "pathfinder_float.net";
//$ann_name = "dui.net";
//$ann_name = "ocr_float.net";
////////////////////////////////////////////////////////////////////////
// Don't Change Below this line
////////////////////////////////////////////////////////////////////////
$ann = fann_create_from_file(dirname(__FILE__) . DIRECTORY_SEPARATOR . $ann_name); //Load ANN
$num_inputs = fann_get_num_input($ann); // int
$num_layers = fann_get_num_layers($ann); // int
$num_outputs = fann_get_num_output($ann); // int
$total_neurons = fann_get_total_neurons($ann); // int
$total_connections = fann_get_total_connections($ann); // int
$layers_array = fann_get_layer_array($ann); // array
$bias_array = fann_get_bias_array($ann); // array
$connections_array = fann_get_connection_array($ann); // array of FANN connection objects
fann_destroy($ann);
// Array of FANN CONN OBJ's to Array of Keyed Neurons
// Use the connections to model the neurons.
// The $my_neurons array holds the neurons in a list with the key
// being the from_neuron. Additionally, this
// also creates a "connections" array on the neuron
// with a list of neurons that this neuron is connected
// to with the value being the connection weight.
$my_neurons = array_fill(0, $total_neurons, array());
foreach($connections_array as $connection){
$my_neurons[$connection->from_neuron]['connections'][$connection->to_neuron] = $connection->weight;
}
// Figure out which neuron belongs on which layer
// Assign it a type: ['input', 'output', 'hidden', 'bias']
$current_neuron = 0;
foreach($layers_array as $layer=>$layer_neuron_count){
// Detect Input, Output & Hidden Neurons
for($i = $current_neuron; $i < ($current_neuron + $layer_neuron_count); $i++){
$my_neurons[$i]['layer'] = $layer;
if($layer == 0){
$my_neurons[$i]['type'] = 'input';
}
elseif($layer == count($layers_array )-1){
$my_neurons[$i]['type'] = 'output';
}
else{
$my_neurons[$i]['type'] = 'hidden';
}
}
$current_neuron = $i;
// Detect Bias Neurons
for($i = $current_neuron; $i < ($current_neuron + $bias_array[$layer]); $i++){
$my_neurons[$i]['layer'] = $layer;
$my_neurons[$i]['type'] = 'bias';
}
$current_neuron = $i;
}
$current_neuron = NULL;
unset($current_neuron);
// Determine Neuron X,Y Position
$row = 1; // y
$col = 1; // x
foreach($my_neurons as $index=>&$neuron){
if($neuron['layer'] > @$my_neurons[$index-1]['layer']){
$row += $tiles_between_layers;
$col = 1;
}else{
$col += $tiles_between_neurons;
}
$neuron['x'] = ($neuron_size * $col) - ($neuron_size / 2);
$neuron['y'] = ($neuron_size * $row) - ($neuron_size / 2);
}
// Get complete_layer count = neurons + bias neurons
foreach($layers_array as $layer=>$layer_neuron_count){
$complete_layers[$layer] = $layer_neuron_count + $bias_array[$layer];
}
$largest_layer = max($complete_layers); // Find the largest layer width
// Create a Blank Image
$image_width = ($neuron_size * ($largest_layer + ($tiles_between_neurons / 2))) * $tiles_between_neurons - ($neuron_size) + 1;
$image_height = ($neuron_size * $num_layers) * $tiles_between_layers - ($neuron_size * ($tiles_between_layers - 1)) + 1;
$neural_network_image = imagecreatetruecolor($image_width, $image_height);
// Create Colors Array
$colors = array(
'background'=>imagecolorallocate($neural_network_image, 153, 153, 153),
'grid'=>imagecolorallocate($neural_network_image, 128, 128, 128),
'neuron_stroke_color'=>imagecolorallocate($neural_network_image, 92, 92, 92),
'input_neuron_color'=>imagecolorallocate($neural_network_image, 170, 255, 170),
'hidden_neuron_color'=>imagecolorallocate($neural_network_image, 233, 175, 174),
'output_neuron_color'=>imagecolorallocate($neural_network_image, 171, 204, 255),
'bias_neuron_color'=>imagecolorallocate($neural_network_image, 255, 255, 0),
'positive_connection_weight_color'=>imagecolorallocate($neural_network_image, 0, 255, 0),
'negitive_connection_weight_color'=>imagecolorallocate($neural_network_image, 255, 0, 0),
'dead_connection_weight_color'=>imagecolorallocate($neural_network_image, 0, 0, 0)
);
// Paint Background
imagefill($neural_network_image, 0, 0, $colors['background']);
// Draw Grid if $draw_grid is set to true
if($draw_grid == true){
$row = 0;
$col = 0;
foreach(range(0, $image_height - 1, 1) as $y){
foreach(range(0, $image_width - 1, 1) as $x){
// paint grid
if($row == $neuron_size || (($y % $neuron_size) == 0)){
imagesetpixel($neural_network_image, $x, $y, $colors['grid']);
$row = 0;
}
if($col == $neuron_size || (($x % $neuron_size) == 0)){
imagesetpixel($neural_network_image, $x, $y, $colors['grid']);
$col = 0;
}
$col++;
}
$row++;
}
}
// Draw Connections
foreach($my_neurons as $key=>&$neuron){
if(array_key_exists('connections', $neuron)){
foreach($neuron['connections'] as $connection=>$weight){
// What color is the connection
if($random_connection_color == false){
if($weight > 0.0){
$color = $colors['positive_connection_weight_color'];
}
elseif($weight < 0.0){
$color = $colors['negitive_connection_weight_color'];
}
else{
$color = $colors['dead_connection_weight_color'];
}
}
else{
$color = imagecolorallocate($neural_network_image, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
}
// Set connection thickness
if($draw_connection_weights == true){
$thickness = 1 + (abs($weight) * 2);
if($thickness > 32){$thickness = 32;}
imagesetthickness ($neural_network_image , $thickness);
}
else{
imagesetthickness ($neural_network_image , 1);
}
// Draw connection
imageline ($neural_network_image , $neuron['x'], $neuron['y'], $my_neurons[$connection]['x'], $my_neurons[$connection]['y'], $color);
}
}
}
imagesetthickness ($neural_network_image , 2); // Reset line brush thickness
// Draw Neurons
foreach($my_neurons as $key=>&$neuron){
if($neuron['type'] == 'input'){
$color = $colors['input_neuron_color'];
}
elseif($neuron['type'] == 'hidden'){
$color = $colors['hidden_neuron_color'];
}
elseif($neuron['type'] == 'output'){
$color = $colors['output_neuron_color'];
}
elseif($neuron['type'] == 'bias'){
$color = $colors['bias_neuron_color'];
}
imagefilledellipse($neural_network_image, $neuron['x'], $neuron['y'], $neuron_size, $neuron_size, $color);
imagearc ($neural_network_image, $neuron['x'], $neuron['y'], $neuron_size+1, $neuron_size+1, 0, 360, $colors['neuron_stroke_color']);
}
// Rotate if you insist on looking at the network wrong! :-P
if($rotate == true){
$neural_network_image = imagerotate($neural_network_image, 90, 0);
}
// Output the image.
imagepng($neural_network_image, "$ann_name.png");
imagedestroy($neural_network_image);
if($output_stats_image == true){
// Create the image
//$neural_network_stats_image = imagecreatetruecolor(250, 400);
$neural_network_stats_image = imagecreate(250, 400);
// Create Colors Array
$colors = array(
'background'=>imagecolorallocate($neural_network_stats_image, 153, 153, 153),
'inputs_text_color'=>imagecolorallocate($neural_network_stats_image, 170, 255, 170),
'hidden_text_color'=>imagecolorallocate($neural_network_stats_image, 233, 175, 174),
'outputs_text_color'=>imagecolorallocate($neural_network_stats_image, 171, 204, 255),
'bias_text_color'=>imagecolorallocate($neural_network_stats_image, 255, 255, 0),
'layers_text_color'=>imagecolorallocate($neural_network_stats_image, 0, 128, 128),
'connections_text_color'=>imagecolorallocate($neural_network_stats_image, 128, 64, 0),
);
// Paint Background
imagefill($neural_network_stats_image, 0, 0, $colors['background']);
$font = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Pacifico'. DIRECTORY_SEPARATOR . 'Pacifico-Regular.ttf';
$size = 25;
$angle = 0.00;
$x = 25;
$y = 50;
$increment = $y + 10;
imagettftext($neural_network_stats_image, $size, $angle, $x, $y, $colors['inputs_text_color'], $font, $num_inputs . ' Inputs');
$y += $increment;
imagettftext($neural_network_stats_image, $size, $angle, $x, $y, $colors['hidden_text_color'], $font, $total_neurons - ($num_inputs + $num_outputs + array_sum($bias_array)) . ' Hidden');
$y += $increment;
imagettftext($neural_network_stats_image, $size, $angle, $x, $y, $colors['outputs_text_color'], $font, $num_outputs . ' Outputs');
$y += $increment;
imagettftext($neural_network_stats_image, $size, $angle, $x, $y, $colors['bias_text_color'], $font, array_sum($bias_array) . ' Bias');
$y += $increment;
imagettftext($neural_network_stats_image, $size, $angle, $x, $y, $colors['layers_text_color'], $font, count($layers_array) . ' Layers');
$y += $increment;
imagettftext($neural_network_stats_image, $size, $angle, $x, $y, $colors['connections_text_color'], $font, count($connections_array) . ' Connections');
$y += $increment;
imagepng($neural_network_stats_image, "$ann_name.stats.png");
imagedestroy($neural_network_stats_image);
}