Hello, I complied the HandWrittenDigitRecognizer from the PacktPub Book “Java Machine Learning for Computer Vision”. I can also train the network with the MNIST data but when I draw I digit and click on “recognize”, I get a DL4JInvalidInputException (Input that is not a matrix) Any Idea what could be wrong?
Lots of things could be wrong, so you need to provide us with some more information.
Most people that frequent this forum don’t have the book you are using but even if we had, you still would need to share your code with us, as we need to know what version of dl4j you are using, and what you are actually feeding to your network.
The program has a GUI with button for training a NN with data from MNIST (which seems to work) and a canvas for drawing digits:
When I click on recognize (no matter whether I use the simple NN or the CNN), I get the org.deeplearning4j.exception.DL4JInvalidInputException: Input that is not a matrix; expected matrix (rank 2), got rank 1 array with shape . Missing preprocessor or wrong input type? (layer name: layer0, layer index: 0, layer type: DenseLayer)
Looks like the code there is about 3 years old. Just be aware that it is probably also using a very old version of dl4j, and if you try to update it you might run into incompatibilities.
The error you are getting tells you that the input data is formatted incorrectly.
In particular, DL4J expects that all prediction requests are batched. The code you are using is passing it a vector of 784 elements. It expects a matrix, in your case it would be a 1x784 matrix, because you’ve got just a single example.
To fix this, you need to turn your vector into a matrix.
You can do this with
matrix = Nd4j.expandDims(vector, 0).
Or in your particular case:
int predict = preTrainedModel.predict(Nd4j.expandDims(Nd4j.create(labeledImage.getNormalizedPixels()), 0));
The same kind of mistake has also been made for the convolutional neural network, where it also looks like it passes the raw pixel vector instead of the appropriately reshaped tensor. The solution there is to reshape it into the proper shape.
problem solved - thanks a lot!