-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualNetworkTest.java
More file actions
116 lines (87 loc) · 3.39 KB
/
VisualNetworkTest.java
File metadata and controls
116 lines (87 loc) · 3.39 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
package io.bhagat.projects.handwrittendigits;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import io.bhagat.ai.supervised.NeuralNetwork;
import io.bhagat.util.SerializableUtil;
public class VisualNetworkTest {
public static final String root = "C:\\Users\\Bhagat\\Downloads\\mnist_png\\mnist_png\\mnist_png\\all\\";
private static SimplePanel simplePanel;
private static NeuralNetwork neuralNetwork;
private static int correct;
private static int total;
public static void main(String[] args) throws IOException {
ArrayList<double[]> images = new ArrayList<>();
ArrayList<Integer> labels = new ArrayList<>();
Scanner imageScanner = new Scanner(new File("files/csv/test/images.csv"));
Scanner labelScanner = new Scanner(new File("files/csv/test/labels.csv"));
correct = 0;
total = 0;
while(imageScanner.hasNext()){
String[] stringImageArr = imageScanner.next().split(",");
double[] imageArr = new double[784];
for(int i = 0; i < 784; i++)
{
imageArr[i] = Integer.parseInt(stringImageArr[i]) / 255.0;
}
images.add(imageArr);
labels.add(Integer.parseInt(labelScanner.next()));
}
imageScanner.close();
labelScanner.close();
try {
neuralNetwork = SerializableUtil.deserialize("mnist/network.ser");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
int index = (int)(Math.random()*images.size());
double[] outputs = neuralNetwork.feedForward(images.get(index));
int guess = 0;
for(int i = 1; i < outputs.length; i++)
if(outputs[i] > outputs[guess])
guess = i;
if(guess == (int) labels.get(index))
correct++;
total++;
simplePanel = new SimplePanel(root + index + ".png", "Guess: "+guess+"\nAnswer: "+labels.get(index)+"\nCorrect: "+correct+"\nTotal: "+total+"\nAccuracy: " + (Math.round(10000.0 * correct / total) / 100.0) + "%");
JFrame frame = new JFrame("Test");
frame.setPreferredSize(new Dimension(300, 300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea area=new JTextArea("Welcome to javatpoint");
frame.getContentPane().add(area);
frame.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
int index = (int)(Math.random()*images.size());
double[] outputs = neuralNetwork.feedForward(images.get(index));
int guess = 0;
for(int i = 1; i < outputs.length; i++)
if(outputs[i] > outputs[guess])
guess = i;
if(guess == (int) labels.get(index))
correct++;
total++;
simplePanel.setImage(root + index + ".png");
simplePanel.setStr("Guess: "+guess+"\nAnswer: "+labels.get(index)+"\nCorrect: "+correct+"\nTotal: "+total+"\nAccuracy: " + (Math.round(10000.0 * correct / total) / 100.0) + "%");
simplePanel.repaint();
}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
});
frame.getContentPane().add(simplePanel);
frame.pack();
frame.setVisible(true);
}
}