I am playing with DL4J version 1.0.0-beta3 and trying to create a convolutional neural network for recognizing 32x32 images of chess pieces. Here is the code I use to create and train the net:
public class BuildNetwork1 {
public static void main(String[] args) throws Exception {
File rootDir = new File("./CNNinput/chesscom1");
File locationToSave = new File(rootDir, "trained.chesscom1.bin");
int height = 32;
int width = 32;
int channels = 1;
int rngseed = 777;
int numEpochs = 100;
File trainData = new File(rootDir, "training");
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.seed(rngseed)
.updater(new Adam.Builder().learningRate(0.01).build())
.activation(Activation.IDENTITY)
.weightInit(WeightInit.XAVIER)
.list()
//.layer(new ConvolutionLayer.Builder(new int[] {5, 5}, new int[] {1, 1}, new int[]{0, 0}).name("cnn1").nIn(1).nOut(64).biasInit(0).build())
//.layer(new SubsamplingLayer.Builder(new int[] {2, 2}, new int[] {2, 2}).name("maxpool1").build())
//.layer(new ConvolutionLayer.Builder(new int[] {5, 5}, new int[] {1, 1}, new int[]{0, 0}).name("cnn2").nIn(64).nOut(16).biasInit(0).build())
.layer(new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
.nOut(13)
.activation(Activation.SOFTMAX)
.build())
.setInputType(InputType.convolutional(32, 32, 1))
.build();
MultiLayerNetwork network = new MultiLayerNetwork(conf);
network.init();
network.setListeners(new ScoreIterationListener(10));
ImageLoader loader = new ImageLoader(height, width, channels);
DataNormalization scaler = new ImagePreProcessingScaler(0, 1);
for (int e = 0; e < numEpochs; e++) {
File[] labels = trainData.listFiles();
for (int i = 0; i < labels.length; i++) {
File label = labels[i];
File[] images = label.listFiles();
for (int j = 0; j < images.length; j++) {
File imageFile = images[j];
BufferedImage image = ImageIO.read(imageFile);
INDArray input = loader.asMatrix(image).reshape(1, channels, height, width);
scaler.fit(new DataSet(input, null));
scaler.transform(input);
double[][] outputArray = new double[1][13];
outputArray[0][Integer.parseInt(label.getName())] = 1d;
INDArray output = Nd4j.create(outputArray);
network.fit(input, output);
}
}
}
boolean saveUpdater = true;
ModelSerializer.writeModel(network, locationToSave, saveUpdater);
}
}
And the code I am using in order to get the result:
public class CalcNetworkAll {
public static void main(String[] args) throws Exception {
int height = 32;
int width = 32;
int channels = 1;
File rootDir = new File("./CNNinput/chesscom1");
File locationToLoad = new File("./CNNinput/chesscom1/trained.chesscom1.bin");
File testData = new File(rootDir, "testing");
MultiLayerNetwork network = ModelSerializer.restoreMultiLayerNetwork(locationToLoad);
ImageLoader loader = new ImageLoader(height, width, channels);
DataNormalization scaler = new ImagePreProcessingScaler(0, 1);
File[] labels = testData.listFiles();
for (int i = 0; i < labels.length; i++) {
File label = labels[i];
File[] images = label.listFiles();
for (int j = 0; j < images.length; j++) {
File imageFile = images[j];
BufferedImage image = ImageIO.read(imageFile);
INDArray input = loader.asMatrix(image).reshape(1, channels, height, width);
scaler.fit(new DataSet(input, null));
scaler.transform(input);
INDArray output = network.output(input, false);
System.out.println(label.getName() + " => " + output);
}
}
}
}
It works well and provides the expected outcome but my problem is that the output consists of 0s and 1s only instead of probabilities:
0 => [[ 1.0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
0 => [[ 1.0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
0 => [[ 1.0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
0 => [[ 1.0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
0 => [[ 1.0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
0 => [[ 1.0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
0 => [[ 1.0000,8.1707e-37, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
0 => [[ 1.0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
0 => [[ 1.0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
0 => [[ 1.0000, 0, 0, 0, 0, 0, 0, 0, 0, 0,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0, 0, 0, 0, 0, 0, 0, 0, 0, 1.0000, 0, 0, 0]]
Do you have an idea why it happens? Thanks a lot in advance!
Your model is very confident in its output. This might happen when you are showing it data that it might have seen before and when you've trained your model to fit very well on that data (often called overfitting).