# IMAGE\_TO\_NDARRAY

`ImageToNDArrayStep` is a `PipelineStep` for converting images to n-dimensional arrays. The exact way that images are converted is highly configurable (formats, channels, output sizes, normalization, etc).

| Configs         | Descriptions                                                                                                                                      |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| config          | Configuration for how conversion should be performed.                                                                                             |
| keys            | May be null. If non-null, these are the names of images in the Data instance to convert.                                                          |
| outputNames     | May be null. If non-null, the input images are renamed to this in the output Data instance after conversion to n-dimensional array.               |
| keepOtherValues | True by default. If true, copy all the other (non-converted/non-image) entries in the input data to the output data.                              |
| metadata        | False by default. If true, include metadata about the images in the output data. For example, if/how it was cropped, and the original input size. |
| metadataKey     | Sets the key that the metadata will be stored under. Not relevant if metadata == `false`. Default is `@ImageToNDArrayStepMetadata`                |

`ImageToNDArrayConfig` is configuration for converting an image into n-dimensional array. This configuration is used in `config` from `ImageToNDArrayStep`, for example:

```
inferenceConfiguration.pipeline(SequencePipeline.builder()
                .add(new ImageToNDArrayStep() //add ImageToNDArrayStep() into pipeline to set image to NDArray for input
                        .config(new ImageToNDArrayConfig() //image configuration
                                .width(28)
                                .height(28)
                                .dataType(NDArrayType.FLOAT)
                                .aspectRatioHandling(AspectRatioHandling.CENTER_CROP)
                                .includeMinibatchDim(true)
                                .channelLayout(NDChannelLayout.GRAYSCALE)
                                .format(NDFormat.CHANNELS_FIRST)
                                .normalization(ImageNormalization.builder().type(ImageNormalization.Type.SCALE).build())
                        )
                        .keys("image")
                        .outputNames("input_layer")
                        .keepOtherValues(true)
                        .metadata(false)
                        .metadataKey(ImageToNDArrayStep.DEFAULT_METADATA_KEY))
                .build()
```

| Configs             | Descriptions                                                                                                                                                                                                                                                                                                                                                             |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| height              | Output array image height. Leave null to convert to the same size as the image height.                                                                                                                                                                                                                                                                                   |
| width               | Output array image width. Leave null to convert to the same size as the image width.                                                                                                                                                                                                                                                                                     |
| dataType            | Data type of the n-dimensional array. Default value is `FLOAT`.                                                                                                                                                                                                                                                                                                          |
| includeMinibatchDim | If true, the output array will contain an extra dimension for the minibatch number. This will look like (1, Channels, Height, Width) instead of (Channels, Height, Width) for format == `CHANNELS_FIRST` or (1, Height, Width, Channels) instead of (Height, Width, Channels) for format == `CHANNELS_LAST`. Default is `true`.                                          |
| aspectRatioHandling | <p>An enum to Handle the situation where the input image and output NDArray have different aspect ratios. </p><p><code>CENTER\_CROP</code> (crop larger dimension then resize if necessary), <code>PAD</code> (pad smaller dimension then resize if necessary), <code>STRETCH</code> (simply resize, distorting if necessary). Default is <code>CENTER\_CROP</code>.</p> |
| format              | The format to be used when converting an Image to an NDArray. Default is `CHANNEL_FIRST`. Another option is `CHANNEL_LAST`.                                                                                                                                                                                                                                              |
| channelLayout       | An enum that represents the type (and order) of the color channels for an image after it has been converted to an NDArray. For example, `RGB` vs. `BGR` etc, default value is `RGB`.                                                                                                                                                                                     |
| normalization       | Configuration that specifies the normalization type of an image array values.                                                                                                                                                                                                                                                                                            |
| listHandling        | An enum to specify how to handle a list of input images. Default is `NONE`.                                                                                                                                                                                                                                                                                              |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://serving.konduit.ai/references/pipeline-steps/image_to_ndarray.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
