Element must be a scalar

Hello i’m trying to get a model to predict an handwritten number image, however i am getting this error
java.lang.IllegalArgumentException: Element must be a scalar: element has shape [81600]

However i thought i had already done this by using ImagePreProcessingScaler class

Here is my code

public void TestExampleImage() throws IOException {
    MultiLayerNetwork loadedModel = MultiLayerNetwork.load(locationToSave, saveUpdater);
    BufferedImage myImage = ImageIO.read(new FileInputStream("handwritten4.jpeg"));

    NativeImageLoader loader = new NativeImageLoader(myImage.getHeight(), myImage.getWidth(), 1);
    ImagePreProcessingScaler imageScaler = new ImagePreProcessingScaler();

    INDArray img = loader.asRowVector(myImage);

    INDArray image = img.put(1,img);
    imageScaler.transform(image);


    INDArray output = loadedModel.output(image);
    log.info(output.toString());
}

I guess the error comes from the line

    INDArray image = img.put(1,img);

And you are doing it because you had an error similar to Wrong input size - expected matrix - #3 by will-maclean?

Use asMatrix instead of asRowVector and skip this put thing entirely.

Hey yes, i added that in following that error. But if i change asMatrix instead i get this error org.deeplearning4j.exception.DL4JInvalidInputException: Input that is not a matrix; expected matrix (rank 2), got rank 4 array with shape [1, 1, 340, 240].

It returns in a 4 rank instead

So you are using a model that expects flat input instead of a CNN type input. In this case the vector option is more likely what you want. Then you should just reshape your input using either reshape or expandDims

Hey sorry for slow reply, i used the reshaped as you recommended into a 2d format but now i am getting this error
Input size (81600 columns; shape = [1, 81600]) is invalid: does not match layer input size (layer # inputs = 784)

Here is my code

        MultiLayerNetwork loadedModel = MultiLayerNetwork.load(locationToSave, saveUpdater);
    BufferedImage myImage = ImageIO.read(new FileInputStream("handwritten4.jpeg"));

    NativeImageLoader loader = new NativeImageLoader(myImage.getHeight(), myImage.getWidth(), 1);
    ImagePreProcessingScaler imageScaler = new ImagePreProcessingScaler();

    INDArray img = loader.asMatrix(myImage);
    INDArray reshaped = img.reshape(1,81600);


    imageScaler.transform(reshaped);


    INDArray output = loadedModel.output(reshaped);
    log.info(output.toString());

It looks very much like you are trying to use a model that was trained on MNIST images (28x28) with your 320x240 image. And your model appears to have a dense input, so it can’t handle the different input size.

You will have to either downscale your image to be 28x28 or use a model that can deal with different input picture sizes.