# Comparing With Custom Input Data

## Introduction

In some cases, we may want to run comparisons using custom input data.
Polygraphy provides multiple ways to do so, which are detailed [here](../../../../how-to/use_custom_input_data.md).

In this example, we'll demonstrate 2 different approaches:

1. Using a data loader script by defining a `load_data()` function in a Python script (`data_loader.py`).
    Polygraphy will use `load_data()` to generate inputs at runtime.

2. Using a JSON file containing pre-generated inputs.
    For convenience, we'll use our script from above (`data_loader.py`) to save the inputs
    generated by `load_data()` to a file called `custom_inputs.json`.

*TIP: Generally, a data loader script is preferrable when working with large amounts of input data*
    *as it avoids the need to write to the disk.*
    *On the other hand, JSON files may be more portable and can help ensure reproducibility.*

Finally, we'll supply our custom input data to `polygraphy run` and compare outputs between
ONNX-Runtime and TensorRT.

Since our model has dynamic shapes, we'll need to set up a TensorRT Optimization Profile.
For details on how we can do this via the command-line,
see [`convert` example 03](../../convert/03_dynamic_shapes_in_tensorrt).
For simplicitly, we'll create a profile where `min` == `opt` == `max`.

*NOTE: It is important that our optimization profile works with the shapes provided by our*
    *custom data loader. In our very simple case, the data loader always generates inputs of*
    *shape (1, 2, 28, 28), so we just need to ensure this falls within [`min`, `max`].*

## Running The Example

1. Run the script to save input data to the disk.
    *NOTE: This is only necessary for option 2.*
    ```bash
    python3 data_loader.py
    ```

2. Run the model with TensorRT and ONNX-Runtime using custom input data:
    - Option 1: Using the data loader script:

    ```bash
    polygraphy run dynamic_identity.onnx --trt --onnxrt \
        --trt-min-shapes X:[1,2,28,28] --trt-opt-shapes X:[1,2,28,28] --trt-max-shapes X:[1,2,28,28] \
        --data-loader-script data_loader.py
    ```

    - Option 2: Using the JSON file containing the saved inputs:

    ```bash
    polygraphy run dynamic_identity.onnx --trt --onnxrt \
        --trt-min-shapes X:[1,2,28,28] --trt-opt-shapes X:[1,2,28,28] --trt-max-shapes X:[1,2,28,28] \
        --load-inputs custom_inputs.json
    ```
