# Element must be a scalar

**URL:** <https://community.konduit.ai/t/element-must-be-a-scalar/573>\
**Category:** DL4J\
**Created:** [May 31, 2020, 6:29pm UTC](https://community.konduit.ai/t/element-must-be-a-scalar/573 "2020-05-31T18:29:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbell1702](https://avatars.discourse-cdn.com/v4/letter/t/e19adc/32.png) [@tbell1702](https://community.konduit.ai/u/tbell1702)\
**Post date:** [May 31, 2020, 6:29pm UTC](https://community.konduit.ai/t/element-must-be-a-scalar/573/1 "2020-05-31T18:29:36Z")

</div>

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());
}

```

---

<div class="post-metadata">

**Author:** ![treo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/treo/32/47_2.png) [@treo](https://community.konduit.ai/u/treo)\
**Post date:** [June 1, 2020, 8:27am UTC](https://community.konduit.ai/t/element-must-be-a-scalar/573/2 "2020-06-01T08:27:03Z")

</div>

I guess the error comes from the line

```auto
    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](https://community.konduit.ai/t/wrong-input-size-expected-matrix/566/3)?

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

---

<div class="post-metadata">

**Author:** ![tbell1702](https://avatars.discourse-cdn.com/v4/letter/t/e19adc/32.png) [@tbell1702](https://community.konduit.ai/u/tbell1702)\
**Post date:** [June 1, 2020, 3:21pm UTC](https://community.konduit.ai/t/element-must-be-a-scalar/573/3 "2020-06-01T15:21:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![treo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/treo/32/47_2.png) [@treo](https://community.konduit.ai/u/treo)\
**Post date:** [June 1, 2020, 4:59pm UTC](https://community.konduit.ai/t/element-must-be-a-scalar/573/4 "2020-06-01T16:59:35Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![tbell1702](https://avatars.discourse-cdn.com/v4/letter/t/e19adc/32.png) [@tbell1702](https://community.konduit.ai/u/tbell1702)\
**Post date:** [June 11, 2020, 12:29pm UTC](https://community.konduit.ai/t/element-must-be-a-scalar/573/5 "2020-06-11T12:29:56Z")

</div>

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());

```

---

<div class="post-metadata">

**Author:** ![treo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/treo/32/47_2.png) [@treo](https://community.konduit.ai/u/treo)\
**Post date:** [June 11, 2020, 4:44pm UTC](https://community.konduit.ai/t/element-must-be-a-scalar/573/6 "2020-06-11T16:44:48Z")

</div>

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.
