-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectProblem.java
512 lines (433 loc) · 19.2 KB
/
ProjectProblem.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
package ec.app;
import ec.app.ProjectData;
import ec.util.*;
import ec.*;
import ec.gp.*;
import ec.gp.koza.*;
import ec.simple.*;
import java.lang.*;
import java.io.File;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.util.Random;
import java.util.ArrayList;
import java.awt.Color;
public class ProjectProblem extends GPProblem implements SimpleProblemForm
{
private static final long serialVersionUID = 1;
//image data
public ArrayList<int[][]> images = new ArrayList<int[][]>();
public int numOfImages = 1;
public int goodImage = -1;
public int sampleSize = -1;
public int testLog;
public int[] outputFiles = new int[4];
//spatial stats for corrisponding pixels in image
public ArrayList<double[][]> avg3 = new ArrayList<double[][]>(), avg5= new ArrayList<double[][]>(), avg7= new ArrayList<double[][]>(); //average
public ArrayList<double[][]> std3= new ArrayList<double[][]>(), std5= new ArrayList<double[][]>(), std7= new ArrayList<double[][]>(); //standard deviation
public ArrayList<double[][]> dev3= new ArrayList<double[][]>(),dev5= new ArrayList<double[][]>(),dev7= new ArrayList<double[][]>(); //deviation from mean
public ArrayList<ArrayList<double[][]>> devList = new ArrayList<ArrayList<double[][]>>();
public ArrayList<double[][]> edge= new ArrayList<double[][]>(), sharpen= new ArrayList<double[][]>();
//terminals for computation
public int currentPixel;
public double currentAvg3, currentAvg5, currentAvg7; //average
public double currentStd3, currentStd5, currentStd7; //standard deviation
public double currentDev3, currentDev5, currentDev7; //deviation
public double currentEdge, currentSharpen; //edge/sharpen
//locations of positive testing pixels
public ArrayList<ArrayList<int[]>> testingCoords = new ArrayList<ArrayList<int[]>>();
public boolean[][][] sampleReference = new boolean[4][256][256];
public void setup(final EvolutionState state, final Parameter base){
super.setup(state,base);
// verify our input is the right class (or subclasses from it)
if (!(input instanceof ProjectData))
state.output.fatal("GPData class must subclass from " + ProjectData.class,
base.push(P_DATA), null);
//section of image that testing data is to be taken from
goodImage = state.parameters.getInt(base.push("goodimage"),null) - 1;
System.out.println("Good Image: " + goodImage);
//number of pixels to be used in each testing array
sampleSize = state.parameters.getInt(base.push("samplesize"), null);
numOfImages = state.parameters.getInt(base.push("numberofimages"),null);
for(int x = 0; x < numOfImages; x++){
//image to use
String name = "imagefile" + (x+1);
File file = state.parameters.getFile(base.push(name),null);
addImageToList(file, x);
File outFile = state.parameters.getFile(base.push("confusionimagefile" + (x+1)),null);
if(outFile!=null)try{
outputFiles[x] = state.output.addLog(outFile,true);
}
catch (IOException i){
state.output.fatal("An IOException occurred while trying to create the image " +
x );
}
}
//compute spatial stats
computeAverage();
computeStdDev();
computeEdge();
computeSharpen();
//choose random pixel for testing
Random rand = new Random();
int samp;
for(int i = 0; i < 4; i++){
ArrayList<int[]> temp = new ArrayList<int[]>();
if(i==goodImage) samp = 999;
else samp = 333;
for(int j = 0; j < samp; j++){
int y = rand.nextInt(256);
int x = rand.nextInt(256);
temp.add(new int[]{y,x});
sampleReference[i][y][x] = true;
}
testingCoords.add(temp);
}
File infoFile = state.parameters.getFile(
base.push("testingfile"),null);
if (infoFile!=null) try
{
testLog = state.output.addLog(infoFile,true);
}
catch (IOException i)
{
state.output.fatal("An IOException occurred while trying to create the log " +
infoFile + ":\n" + i);
}
System.out.println("Begining Training");
}
public void addImageToList(File file, int imageNumber){
int imageHeight = 0;
int imageWidth = 0;
byte[] imageBytes = null;
try {
BufferedImage bufferedImage = ImageIO.read(file);
//get image dimensions
imageHeight = bufferedImage.getHeight();
imageWidth = bufferedImage.getWidth();
imageBytes = ((DataBufferByte) bufferedImage.getData().getDataBuffer()).getData();
System.out.println("Image " + imageNumber + " upload success.");
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
System.exit(0);
}
int[][] tempImage = new int[imageHeight][imageWidth];
initArrays(imageHeight, imageWidth);
//convert to 2D array
int c = 0; //counter
for(int y = 0; y < imageHeight; y++){
for(int x = 0; x < imageWidth; x++){
tempImage[y][x] = imageBytes[c] & 0xFF;
//System.out.print(tempImage[y][x] + " ");
c++;
}
//System.out.println();
}
images.add(tempImage);
}
public void initArrays(int imgHeight, int imgWidth){
//average arrays
avg3.add(new double[imgHeight][imgWidth]);
avg5.add(new double[imgHeight][imgWidth]);
avg7.add(new double[imgHeight][imgWidth]);
//std dev arrays
std3.add(new double[imgHeight][imgWidth]);
std5.add(new double[imgHeight][imgWidth]);
std7.add(new double[imgHeight][imgWidth]);
//deviation arrays
dev3.add(new double[imgHeight][imgWidth]);
dev5.add(new double[imgHeight][imgWidth]);
dev7.add(new double[imgHeight][imgWidth]);
//convolution
edge.add(new double[imgHeight][imgWidth]);
//sharpen
sharpen.add(new double[imgHeight][imgWidth]);
}
public void computeAverage(){
int sum3 = 0, sum2 = 0, sum1 = 0;
for(int i = 0; i < numOfImages; i++){
for(int y = 0; y < images.get(i).length; y++){
for(int x = 0; x < images.get(i)[0].length; x++){
//copmute average for pixel[x,y]
avg3.get(i)[y][x] = computeAverage(i,y,x,1);
avg5.get(i)[y][x] = computeAverage(i,y,x,2);
avg7.get(i)[y][x] = computeAverage(i,y,x,3);
//compute deviation for pixels[x,y]
dev3.get(i)[y][x] = Math.pow(images.get(i)[y][x] - avg3.get(i)[y][x],2);
dev5.get(i)[y][x] = Math.pow(images.get(i)[y][x] - avg5.get(i)[y][x],2);
dev7.get(i)[y][x] = Math.pow(images.get(i)[y][x] - avg7.get(i)[y][x],2);
}
}
}
devList.add(dev3);
devList.add(dev5);
devList.add(dev7);
System.out.println("Averages and Deviations computed.");
}
public double computeAverage(int img, int y, int x, int ring){
int sum = 0;
int count = 0;
int boxSize = (2*ring)+1;
int startY = y - ring;
int startX = x - ring;
int imgHeight = images.get(img).length;
int imgWidth = images.get(img)[0].length;
for(int i = startY; i < startY + boxSize; i++){
for(int j = startX; j < startX + boxSize;j++){
if(i >= 0 && i < imgHeight && j >= 0 && j < imgWidth){
sum += images.get(img)[i][j];
count++;
}
}
}
return (double)(sum/count);
}
public void computeStdDev(){
int sum3 = 0, sum2 = 0, sum1 = 0;
for(int i = 0; i < numOfImages; i++){
for(int y = 0; y < images.get(i).length; y++){
for(int x = 0; x < images.get(i)[0].length; x++){
std3.get(i)[y][x] = computeStdDev(i,y,x,1);
std5.get(i)[y][x] = computeStdDev(i,y,x,2);
std7.get(i)[y][x] = computeStdDev(i,y,x,3);
}
}
}
System.out.println("StdDev computed.");
}
public double computeStdDev(int img, int y, int x, int ring){
int sum = 0;
int count = 0;
int boxSize = 2*ring+1;
int startY = y - ring;
int startX = x - ring;
int imgHeight = images.get(img).length;
int imgWidth = images.get(img)[0].length;
for(int i = startY; i < startY + boxSize; i++){
for(int j = startX; j < startX + boxSize;j++){
if(i >= 0 && i < imgHeight && j >= 0 && j < imgWidth){
sum += devList.get(ring-1).get(img)[i][j];
count++;
}
}
}
return Math.sqrt((double)(sum/count));
}
public void computeEdge(){
int[][] mask = new int[][] {
{-1,-1,-1},
{-1,8,-1},
{-1,-1,-1}};
edge = computeMask(mask);
System.out.println("Edge computed.");
}
public void computeSharpen(){
int[][] mask = new int[][] {
{0,-1,0},
{-1,5,-1},
{0,-1,0}};
sharpen = computeMask(mask);
System.out.println("Sharpen computed.");
}
public ArrayList<double[][]> computeMask(int[][] mask){
ArrayList<double[][]> tempList = new ArrayList<double[][]>();
for(int i = 0; i < numOfImages; i++){
//get image dimensions
int imgHeight = images.get(i).length;
int imgWidth = images.get(i)[0].length;
//make array to hold calculated values
//same size as image
double[][] tempArray = new double[imgHeight][imgWidth];
//iterate through pixels
for(int y = 0; y<imgHeight;y++){
for(int x= 0; x < imgWidth; x++){
tempArray[y][x] = computeMask(i, y, x, mask);
}
}
tempList.add(tempArray);
}
return tempList;
}
public double computeMask(int img, int y, int x, int[][] mask){
double sum = 0;
int count = 0;
int startY = y - 1;
int startX = x - 1;
int imgHeight = images.get(img).length;
int imgWidth = images.get(img)[0].length;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3;j++){
int pY = i + startY;
int pX = j + startX;
if(pY >= 0 && pY < imgHeight && pX >= 0 && pX < imgWidth){
sum += mask[i][j] * images.get(img)[pY][pX];
}
}
}
return sum;
}
public boolean isNaN(double x){return x != x;}
public void evaluate(final EvolutionState state,
final Individual ind,
final int subpopulation,
final int threadnum)
{
if (!ind.evaluated) // don't bother reevaluating
{
ProjectData input = (ProjectData)(this.input);
int hits = 0;
int count = 0;
double sum = 0.0;
double result = 0;
final double PROBABLY_ZERO = 1.11E-15;
final double BIG_NUMBER = 1.0e15; // the same as lilgp uses
for(int[] coords: testingCoords.get(goodImage)){
int y = coords[0];
int x = coords[1];
currentPixel = images.get(goodImage)[y][x];
currentAvg3 = avg3.get(goodImage)[y][x];
currentAvg5 = avg5.get(goodImage)[y][x];
currentAvg7 = avg7.get(goodImage)[y][x];
currentStd3 = std3.get(goodImage)[y][x];
currentStd5 = std5.get(goodImage)[y][x];
currentStd7 = std7.get(goodImage)[y][x];
currentEdge = edge.get(goodImage)[y][x];
currentSharpen = sharpen.get(goodImage)[y][x];
((GPIndividual)ind).trees[0].child.eval(
state,threadnum,input,stack,((GPIndividual)ind),this);
if(input.x <= 0.0 || isNaN(input.x)){ //if input is negative
result = Math.abs(input.x);
if (! (result < BIG_NUMBER ) )
result = BIG_NUMBER;
else if (result<1.0 || isNaN(input.x)) //if result is less that 0 increase the penalty
result = 3.0;
sum += result;
}else{
hits++;
}
}
for(int i = 0; i < 4; i++){
if(i!=goodImage){
for(int[] coords: testingCoords.get(i)){
int y = coords[0];
int x = coords[1];
currentPixel = images.get(i)[y][x];
currentAvg3 = avg3.get(i)[y][x];
currentAvg5 = avg5.get(i)[y][x];
currentAvg7 = avg7.get(i)[y][x];
currentStd3 = std3.get(i)[y][x];
currentStd5 = std5.get(i)[y][x];
currentStd7 = std7.get(i)[y][x];
currentEdge = edge.get(i)[y][x];
currentSharpen = sharpen.get(i)[y][x];
((GPIndividual)ind).trees[0].child.eval(
state,threadnum,input,stack,((GPIndividual)ind),this);
if(input.x > 0.0 || isNaN(input.x)){ //if result is positive when it should be negative
result = input.x;
if (! (result < BIG_NUMBER ) )
result = BIG_NUMBER;
else if (result< 1.0 || isNaN(input.x))
result = 1.0;
sum += result;
}else{ //its been labelled correctly
hits++;
}
}
}
}
KozaFitness f = ((KozaFitness)ind.fitness);
f.setStandardizedFitness(state, sum);
f.hits = hits;
ind.evaluated = true;
}
}
public void describe(EvolutionState state, Individual ind, int subpopulation, int threadnum, int log)
{
System.out.println("Beginning Test");
ProjectData input = (ProjectData)(this.input);
int hits = 0;
int count = 0;
double sum = 0.0;
double result = 0;
final double PROBABLY_ZERO = 1.11E-15;
final double BIG_NUMBER = 1.0e15; // the same as 'lilgp uses
for(int i = 0; i < 4; i++){
boolean[][] confResult = new boolean[256][256];
for(int y = 0; y< 256;y++){
for(int x=0; x<256;x++){
if(!sampleReference[goodImage][y][x]){
currentPixel = images.get(i)[y][x];
currentAvg3 = avg3.get(i)[y][x];
currentAvg5 = avg5.get(i)[y][x];
currentAvg7 = avg7.get(i)[y][x];
currentStd3 = std3.get(i)[y][x];
currentStd5 = std5.get(i)[y][x];
currentStd7 = std7.get(i)[y][x];
currentEdge = edge.get(i)[y][x];
currentSharpen = sharpen.get(i)[y][x];
((GPIndividual)ind).trees[0].child.eval(
state,threadnum,input,stack,((GPIndividual)ind),this);
if(i==goodImage){
if(input.x <= 0.0 || isNaN(input.x)){ //if input is negative when it should be positive
confResult[y][x] = false;
result = Math.abs(input.x);
if (! (result < BIG_NUMBER ) )
result = BIG_NUMBER;
else if (result<1.0 || isNaN(input.x)) //if result is less that 0 increase the penis
result = 3.0;
sum += result;
}else{
confResult[y][x] = true;
hits++;
}
}
else{
if(input.x > 0.0 || isNaN(input.x)){ //if result is positive when it should be negative
confResult[y][x] = false;
result = input.x;
if (! (result < BIG_NUMBER ) )
result = BIG_NUMBER;
else if (result< 1.0 || isNaN(input.x))
result = 1.0;
sum += result;
}else{ //its been labelled correctly
confResult[y][x] = true;
hits++;
}
}
}
}
}
BufferedImage bufferImage = new BufferedImage(256,256,BufferedImage.TYPE_INT_RGB);
int right = 0, wrong = 0;
for(int y = 0; y < 256; y++){
for(int x = 0; x< 256; x++){
if(sampleReference[i][y][x])
bufferImage.setRGB(x, y,Color.BLACK.getRGB());
else if(confResult[y][x]){
bufferImage.setRGB(x, y,Color.GREEN.getRGB());
right++;
}
else{
bufferImage.setRGB(x, y,Color.RED.getRGB());
wrong ++;
}
}
}
state.output.println(right + " " + wrong,testLog);
try{
boolean boo = ImageIO.write(bufferImage, "png", state.output.getLog(outputFiles[i]).filename);
if(boo)
System.out.println("printed image");
}catch(IOException exp){
System.out.println("Could not print confusion image " + i);
}
}
KozaFitness f = ((KozaFitness)ind.fitness);
f.setStandardizedFitness(state, sum);
f.hits = hits;
state.output.print(f.fitnessToStringForHumans(),testLog);
}
}