-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeuralTest.java
380 lines (329 loc) · 8.77 KB
/
NeuralTest.java
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import java.io.*;
import p79068.bmpio.*;
interface NeuronInterface
{
String serializeWightsToString();
void initWithString(String stringOfWeights);
void initWithMathRandom();
void print();
double evaluate(double[] vector);
void tuneToVector(double[] vector);
void dumpNeuronAsBmp(String filename, int width, int height) throws IOException;
}
interface NeuralNetworkInterface
{
void print();
void saveToFile() throws IOException;
void loadFromFile() throws IOException;
void initWithMathRandom();
void tuneNeuronIdxToBufferedImage(int targetNueronIdx);
int evaluateNeuronsWithBufferImage();
void bufferImage(String imageName) throws IOException;
void dumpNeuronAsBmp(int nIdx, String fileName, int width, int height) throws IOException;
void dumpAllNeurons(int width, int height) throws IOException;
}
class Utilities
{
public static double[] sumVectors(double[] vector1, double[] vector2)
{
if (vector1.length != vector2.length)
{
System.out.println("summing vectors have different number of components");
return null;
}
double[] result = new double[vector1.length];
System.arraycopy(vector1, 0, result, 0, vector1.length);
for (int idx = 0; idx < vector1.length; idx++)
{
result[idx] += vector2[idx];
}
return result;
}
public static double[] substructVectors(double[] vector1, double[] vector2)
{
if (vector1.length != vector2.length)
{
System.out.printf("substructing vectors have different number of components (%d vs %d)", vector1.length, vector2.length);
return null;
}
double[] result = new double[vector1.length];
System.arraycopy(vector1, 0, result, 0, vector1.length);
for (int idx = 0; idx < vector1.length; idx++)
{
result[idx] -= vector2[idx];
}
return result;
}
public static double[] multiplyVectorsOnScalar(double[] vector, double k)
{
double[] result = new double[vector.length];
System.arraycopy(vector, 0, result, 0, vector.length);
for (int idx = 0; idx < vector.length; idx++)
{
result[idx] *= k;
}
return result;
}
public static void printVector(double[] vector)
{
System.out.printf("Printing vector:\n");
for (int idx = 0; idx < vector.length; idx++)
{
System.out.printf("%f, ", vector[idx]);
}
System.out.printf("\n");
}
public static int getRgbPixelWithIntensity(double intensity)
{
int intensityValueFrom0to255 = (int)intensity >> 16;
int intensityValue = intensityValueFrom0to255 | intensityValueFrom0to255 << 8 | intensityValueFrom0to255 << 16;
return 0xffffff - intensityValue;
}
}
class Neuron implements NeuronInterface
{
private double weights[];
private double beta = 0.7;
public String serializeWightsToString()
{
String result = new String();
for (int wIdx = 0; wIdx < weights.length; wIdx++)
{
result = result.concat(String.valueOf(weights[wIdx]) + " ");
}
return result;
}
public void initWithString(String stringOfWeights)
{
String[] stringsSplittedbySpace = stringOfWeights.split(" ");
for(int idx = 0; idx < stringsSplittedbySpace.length; idx++)
{
double theWeight = Double.parseDouble(stringsSplittedbySpace[idx]);
weights[idx] = theWeight;
}
}
public Neuron(int n)
{
weights = new double[n];
}
public void initWithMathRandom()
{
for (int idx = 0; idx < weights.length; idx++)
{
weights[idx] = Math.random() * 16777215;
}
}
public void print()
{
for (int idx = 0; idx < weights.length; idx++)
{
System.out.printf("%f ", weights[idx]);
}
System.out.printf("\n");
}
public double evaluate(double[] vector)
{
if (vector.length != weights.length)
{
System.out.printf("input vector dimension differs from neuron dimension (weights num)\n%d against %d\n", vector.length, weights.length);
assert(false);
}
double sum = 0;
for (int idx = 0; idx < vector.length; idx ++)
{
sum += vector[idx] * weights[idx];
}
return sum;
}
public void tuneToVector(double[] vector)
{
double[] diffVector = Utilities.substructVectors(vector, weights);
double[] nVector = Utilities.multiplyVectorsOnScalar(diffVector, beta);
weights = Utilities.sumVectors(weights, nVector);
}
public void dumpNeuronAsBmp(String filename, int width, int height) throws IOException
{
File file = new File(filename);
if (!file.exists())
{
file.createNewFile();
}
FileOutputStream fileOut = new FileOutputStream(file);
BufferedRgb888Image bufferedRgb888Image = new BufferedRgb888Image(width, height);
for (int wIdx = 0; wIdx < weights.length; wIdx++)
{
int x = wIdx % width;
int y = wIdx / width;
int intensityPixel = Utilities.getRgbPixelWithIntensity(weights[wIdx]);
bufferedRgb888Image.setRgb888Pixel(x, y, intensityPixel);
}
BmpImage bmpImage = new BmpImage();
bmpImage.image = bufferedRgb888Image;
BmpWriter.write(fileOut, bmpImage);
}
}
class NeuralNetwork implements NeuralNetworkInterface
{
private Neuron neurons[];
private double[] vector;
public NeuralNetwork(int m, int n)
{
neurons = new Neuron[m];
for (int idx = 0; idx < neurons.length; idx++)
{
neurons[idx] = new Neuron(n);
}
}
public void initWithMathRandom()
{
for (int idx = 0; idx < neurons.length; idx++)
{
neurons[idx].initWithMathRandom();
}
}
public void print()
{
Neuron theNeuron = neurons[0];
if (theNeuron == null)
{
System.out.printf("theNeuron == null\n");
}
for (int nIdx = 0; nIdx < neurons.length; nIdx++)
{
Neuron theN = neurons[nIdx];
theN.print();
}
}
public void saveToFile() throws IOException
{
File file = new File("nn.txt");
if (!file.exists())
{
file.createNewFile();
}
FileOutputStream fileOut = new FileOutputStream(file);
PrintStream printStream = new PrintStream(fileOut);
for (int nIdx = 0; nIdx < neurons.length; nIdx++)
{
Neuron theNeuron = neurons[nIdx];
printStream.printf("%s\n", theNeuron.serializeWightsToString());
}
fileOut.close();
}
public void loadFromFile() throws IOException
{
File file = new File("nn.txt");
if (!file.exists())
{
System.out.printf("file doesn't exists\n");
return;
}
FileInputStream fileIn = new FileInputStream(file);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileIn));
String stringOfWeights;
int nIdx = 0;
while((stringOfWeights = bufferedReader.readLine()) != null)
{
if (stringOfWeights.length() == 0) { break; }
neurons[nIdx++].initWithString(stringOfWeights);
}
}
public void tuneNeuronIdxToBufferedImage(int targetNueronIdx)
{
neurons[targetNueronIdx].tuneToVector(vector);
System.out.printf("now %d neuron is (%f)", targetNueronIdx, neurons[targetNueronIdx].evaluate(vector));
}
public void bufferImage(String imageName) throws IOException
{
InputStream in = new FileInputStream(imageName);
BmpImage bmp;
try
{
bmp = BmpReader.read(in);
}
finally
{
in.close();
}
int bmpWidth = bmp.image.getWidth();
int bmpHeight = bmp.image.getHeight();
//System.out.printf("width: %d\nheight: %d\n", bmpWidth, bmpHeight);
vector = new double[bmpWidth * bmpHeight];
for (int y = 0; y < bmpHeight; y++)
{
for (int x = 0; x < bmpWidth; x++)
{
int idx = y * bmpWidth + x;
int pixel = 0xffffff - bmp.image.getRgb888Pixel(x, y);
if (pixel < 0xffffff)
{
pixel = 0;
}
vector[idx] = pixel;
//System.out.printf("%6x ", pixel);
}
//System.out.printf("\n");
}
}
public int evaluateNeuronsWithBufferImage()
{
double max = 0;
int winnerIdx = -1;
for (int nIdx = 0; nIdx < neurons.length; nIdx++)
{
double eval = neurons[nIdx].evaluate(vector);
if (eval > max)
{
max = eval;
winnerIdx = nIdx;
}
//System.out.printf("%d neuron evaluate vector as %f\n", nIdx, eval);
}
System.out.printf("stronger neuron: %d (%f)\n", winnerIdx, max);
return winnerIdx;
}
public void dumpNeuronAsBmp(int nIdx, String fileName, int width, int height) throws IOException
{
Neuron theNeuron = neurons[nIdx];
theNeuron.dumpNeuronAsBmp(fileName, width, height);
}
public void dumpAllNeurons(int width, int height) throws IOException
{
for (int nIdx = 0; nIdx < neurons.length; nIdx++)
{
neurons[nIdx].dumpNeuronAsBmp("n" + nIdx + ".bmp", width, height);
}
}
}
class NeuralTest
{
public static void main(String[] args) throws IOException
{
String fileName;
if (args.length > 0)
{
fileName = args[0];
}
else
{
fileName = "test.bmp";
}
NeuralNetwork nn = new NeuralNetwork(4, 300);
//nn.initWithMathRandom();
//nn.saveToFile();
nn.loadFromFile();
nn.bufferImage(fileName);
if (args.length == 2)
{
int targetNeuron = Integer.parseInt(args[1]);
nn.tuneNeuronIdxToBufferedImage(targetNeuron);
nn.saveToFile();
System.out.printf("%d neuron tuned to image\n", targetNeuron);
nn.dumpAllNeurons(15, 20);
}
else if (args.length == 1)
{
nn.evaluateNeuronsWithBufferImage();
}
}
}