-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNeuralNetworkVisualizer.cpp
More file actions
114 lines (112 loc) · 3.65 KB
/
NeuralNetworkVisualizer.cpp
File metadata and controls
114 lines (112 loc) · 3.65 KB
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
#include <iostream>
#include <SFML/Graphics.hpp>
#include <utility>
#include <memory>
#include <chrono>
#include <EvoAI/NeuralNetwork.hpp>
#include <EvoAI/Utils.hpp>
#include "NNRenderer.hpp"
void usage(){
std::cout << "usage:\n";
std::cout << "NeuralNetworkVisualizer" << " <filename> --dot" << std::endl;
}
int main(int argc, char **argv){
using EvoAI::NeuralNetwork;
using EvoAI::Connection;
using EvoAI::NNRenderer;
bool running = true;
bool showText = false;
std::string filename = "";
bool saveDotFile = false;
if(argc < 2){
usage();
return EXIT_FAILURE;
}
for(auto i=0;i<argc;++i){
std::string opt(argv[i]);
if(opt == "--dot"){
saveDotFile = true;
}else if(!opt.empty()){
filename = opt;
}
}
if(filename.empty()){
usage();
std::cout << "filename is empty." << std::endl;
return 1;
}
// build network
std::unique_ptr<NeuralNetwork> nn = std::make_unique<NeuralNetwork>(filename);
if(saveDotFile){
std::cout << "writing dot file" << std::endl;
nn->writeDotFile(filename.substr(0,filename.find_first_of('.')) + ".dot");
}
// build renderer for the nn.
std::unique_ptr<NNRenderer> nr = std::make_unique<NNRenderer>(nn.get());
sf::RenderWindow App(sf::VideoMode(1270, 720), "Neural Network Visualizer - " + filename);
auto counter = 2u;
while(running){
sf::Event event;
while(App.pollEvent(event)){
switch(event.type){
case sf::Event::Closed:
running = false;
break;
case sf::Event::KeyReleased:
if(event.key.code == sf::Keyboard::Add){
sf::View v = App.getView();
v.zoom(0.5);
App.setView(v);
++counter;
}
if(event.key.code == sf::Keyboard::Subtract){
sf::View v = App.getView();
v.zoom(1.5);
App.setView(v);
++counter;
}
break;
case sf::Event::KeyPressed:
if(event.key.code == sf::Keyboard::Down){
auto v = App.getView();
v.move(0,100);
App.setView(v);
++counter;
}
if(event.key.code == sf::Keyboard::Up){
auto v = App.getView();
v.move(0,-100);
App.setView(v);
++counter;
}
if(event.key.code == sf::Keyboard::Right){
auto v = App.getView();
v.move(100,0);
App.setView(v);
++counter;
}
if(event.key.code == sf::Keyboard::Left){
auto v = App.getView();
v.move(-100,0);
App.setView(v);
++counter;
}
if(event.key.code == sf::Keyboard::T){
showText = !showText;
++counter;
}
break;
default:
break;
}
}
if(counter > 0u){
App.clear(sf::Color::Black);
nr->Render(App,showText);
App.display();
--counter;
}
}
App.close();
return 0;
}