-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImageMixer.cpp
More file actions
181 lines (177 loc) · 7.06 KB
/
ImageMixer.cpp
File metadata and controls
181 lines (177 loc) · 7.06 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
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
#include <SFML/Graphics.hpp>
#include <utility>
#include <memory>
#include <chrono>
#include <thread>
#include <EvoAI/Genome.hpp>
#include <EvoAI/Population.hpp>
#include <EvoAI/Utils.hpp>
sf::Image createImageFromImages(EvoAI::NeuralNetwork* nn, std::vector<sf::Image>& imgs, bool bw) noexcept;
void usage();
int main(int argc, char **argv){
EvoAI::randomGen().setSeed(std::random_device{}());
bool optGenome = false;
bool optGenomeType = false;
bool optMutate = false;
bool optReproduce = false;
std::string genomeType = "0";
int numHidden = 0;
std::string genomeFile1 = "genome1.json";
std::string genomeFile2 = "genome2.json";
bool optBW = false;
bool optSaveNN = false;
std::string saveFileNN = "nn.json";
bool optSaveGenome = false;
std::string saveFileGenome = "g.json";
std::string fileOutput = "image.png";
std::vector<sf::Image> imagesInputs;
for(auto i=0;i<argc;++i){
auto val = std::string(argv[i]);
if(val == "-g" || val == "--genome"){
optGenome = true;
if(std::string(argv[i+1]) == "m"){
optMutate = true;
genomeFile1 = std::string(argv[i+2]);
}else if(std::string(argv[i+1]) == "r"){
optReproduce = true;
genomeFile1 = std::string(argv[i+2]);
genomeFile2 = std::string(argv[i+3]);
}else{
genomeFile1 = std::string(argv[i+1]);
}
}
if(val == "-G" || val == "--genome-type"){
optGenomeType = true;
if(std::string(argv[i+1]) == "0"){
genomeType = "0";
numHidden = 0;
}else if(std::string(argv[i+1]) == "1"){
genomeType = "1";
numHidden = std::stoi(std::string(argv[i+2]));
}
}
if(val == "-bw"){
optBW = true;
}
if(val == "-f" || val == "--file-output"){
fileOutput = std::string(argv[i+1]);
}
if(val == "-sg" || val == "--save-genome"){
optSaveGenome = true;
saveFileGenome = std::string(argv[i+1]);
}
if(val == "-s" || val == "--save"){
optSaveNN = true;
saveFileNN = std::string(argv[i+1]);
}
if(val =="--image"){
auto numImg = std::stoi(argv[i+1]);
for(auto j=0;j<numImg;++j){
imagesInputs.emplace_back(sf::Image());
imagesInputs[j].loadFromFile(std::string(argv[i+(j+2)]));
}
}
if(val == "--help" || val == "-h"){
usage();
return EXIT_FAILURE;
}
}
if(argc < 2){
usage();
return EXIT_FAILURE;
}
std::unique_ptr<EvoAI::Genome> g = nullptr;
std::unique_ptr<EvoAI::NeuralNetwork> nn = nullptr;
if(optGenome){
if(optMutate){
std::cout << "Mutating genome " << genomeFile1 << std::endl;
g = std::make_unique<EvoAI::Genome>(genomeFile1);
g->mutate();
}else if(optReproduce){
auto g1 = std::make_unique<EvoAI::Genome>(genomeFile1);
auto g2 = std::make_unique<EvoAI::Genome>(genomeFile2);
std::cout << "Reproducing genomes " << genomeFile1 << " And " << genomeFile2 << std::endl;
g = std::make_unique<EvoAI::Genome>(EvoAI::Genome::reproduce(*g1,*g2));
g->setCppn(true);
}else{
std::cout << "Loading genome " << genomeFile1 << std::endl;
g = std::make_unique<EvoAI::Genome>(genomeFile1);
}
nn = std::make_unique<EvoAI::NeuralNetwork>(EvoAI::Genome::makePhenotype(*g));
}else if(optGenomeType){
if(genomeType == "0"){
if(optBW){
g = std::make_unique<EvoAI::Genome>(3 * imagesInputs.size(),1,true,true);
}else{
g = std::make_unique<EvoAI::Genome>(3 * imagesInputs.size(),3,true,true);
}
}else if(genomeType == "1"){
if(optBW){
g = std::make_unique<EvoAI::Genome>(3 * 20 * imagesInputs.size(),numHidden,1,true,true);
}else{
g = std::make_unique<EvoAI::Genome>(3 * 20 * imagesInputs.size(),numHidden,3,true,true);
}
}
nn = std::make_unique<EvoAI::NeuralNetwork>(EvoAI::Genome::makePhenotype(*g));
}
if(optSaveNN){
if(nn){
std::cout << "Saving Neural Network to " << saveFileNN << " ..." << std::endl;
nn->writeToFile(saveFileNN);
}
}
if(optSaveGenome){
if(g){
std::cout << "Saving Genome to " << saveFileGenome << "..." << std::endl;
g->writeToFile(saveFileGenome);
}
}
std::cout << "Creating Image..." << std::endl;
auto imgOutput = createImageFromImages(nn.get(),imagesInputs,optBW);
std::cout << "saving Image to " << fileOutput << std::endl;
imgOutput.saveToFile(fileOutput);
}
sf::Image createImageFromImages(EvoAI::NeuralNetwork* nn, std::vector<sf::Image>& imgs, bool bw) noexcept{
sf::Image imgOutput;
if(imgs.empty()){
return imgOutput;
}
auto width = imgs[0].getSize().x;
auto height = imgs[0].getSize().y;
imgOutput.create(width, height);
for(auto x=0u;x<width;++x){
for(auto y=0u;y<height;++y){
std::vector<double> inputs;
for(auto i=0u;i<imgs.size();++i){
if(x <= imgs[i].getSize().x && y <= imgs[i].getSize().y){
auto imgColor = imgs[i].getPixel(x,y);
inputs.emplace_back(imgColor.r);
inputs.emplace_back(imgColor.g);
inputs.emplace_back(imgColor.b);
}
}
nn->setInputs(std::move(inputs));
auto color = nn->run();
nn->reset();
if(bw){
imgOutput.setPixel(x,y,sf::Color(color[0]*128+128,color[0]*128+128,color[0]*128+128));
}else{
imgOutput.setPixel(x,y,sf::Color(color[0]*128+128,color[1]*128+128,color[2]*128+128));
}
}
}
return imgOutput;
}
void usage(){
std::cout << "ImageMixer <options>\n";
std::cout << "-g, --genome [m|r] <filename> [<filename> with r]\tload a genome json file\n";
std::cout << "\t\t\t\t\tWith m will mutate the genome.\n\t\t\t\t\tWith r will combine two genomes, without m or r will load the genome.\n";
std::cout << "-G, --genome-type <type> <numHidden>\twill generate a genome of the type specified\n\t\t\t\t\t\ttypes:\n\t\t\t\t\t\t\t" <<
"0. Without hidden neurons\n\t\t\t\t\t\t\t1. With hidden neurons.\n";
std::cout << "-bw\t\t\t\t\tthe output image is black and white.\n";
std::cout << "-s, --save <filename>\t\t\twill save the neural network generated.\n";
std::cout << "-sg, --save-genome <filename>\t\twill save the genome generated.\n";
std::cout << "-f, --file-output <filename>\t\timage that will output.\n";
std::cout << "--image <n> <filename>...\t\t\tload n images and mix them.\n";
std::cout << "-h, --help\t\t\t\thelp menu (This)\n";
}