Reputation: 141
I'm trying to get a trained Convolutional Neural Net (CNN) to classify a new image (which comes as a BufferedImage object).
The CNN model was trained using the example provided in DeepLearning4j (https://github.com/eclipse/deeplearning4j-examples/blob/master/dl4j-examples/src/main/java/org/deeplearning4j/examples/convolution/mnist/MnistClassifier.java).
I tried to convert the BufferedImage object into INDArray which is the expected input type for the model.
public static INDArray classify(BufferedImage image) throws IOException {
String modelFile = "e:\\c1\\TrainingData\\mnistplus-model.1.zip";
MultiLayerNetwork classifier = MultiLayerNetwork.load(new File(modelFile), false);
int channels = 1;
ImageLoader loader = new ImageLoader(TARGET_WIDTH, TARGET_HEIGHT, channels);
INDArray input = loader.asMatrix(image);
INDArray output = classifier.output(input);
System.out.println(output);
return output;
}
However I got error when the input is passed to the model. Seems like I didn't properly initialize the INDArray input. Thanks in advance for any help on this.
Exception in thread "main" java.lang.IllegalArgumentException: Invalid input: expect output columns must be equal to rows 28 x columns 28 x channels 1 but was instead [28, 28] at org.deeplearning4j.nn.conf.preprocessor.FeedForwardToCnnPreProcessor.preProcess(FeedForwardToCnnPreProcessor.java:90) at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.outputOfLayerDetached(MultiLayerNetwork.java:1281)
Upvotes: 2
Views: 673
Reputation: 3958
You can change the call of INDArray input = loader.asMatrix(image);
to INDArray input = loader.asMatrix(image).reshape(1, 28, 28, 1);
to get the input into the right shape (which is minibatch x height x width x channels)
Upvotes: 1