# Dynamic Input for ONNX.js using a Pytorch trained model

**URL:** <https://discuss.pytorch.org/t/dynamic-input-for-onnx-js-using-a-pytorch-trained-model/93981>\
**Category:** deployment\
**Created:** [August 25, 2020, 12:38am UTC](https://discuss.pytorch.org/t/dynamic-input-for-onnx-js-using-a-pytorch-trained-model/93981 "2020-08-25T00:38:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashraf\_Ali](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ashraf_ali/32/11824_2.png) [@Ashraf\_Ali](https://discuss.pytorch.org/u/Ashraf_Ali)\
**Post date:** [August 25, 2020, 12:38am UTC](https://discuss.pytorch.org/t/dynamic-input-for-onnx-js-using-a-pytorch-trained-model/93981/1 "2020-08-25T00:38:41Z")

</div>

So I’ve got this autoencoder that I’ve trained and now I wanna deploy it to a website.  
I came across ONNX.js and liked it because it can run natively on the browser(Saving me my much needed Student cloud credits 🙂)

However I noticed that onnx requires a dummy input so that it can trace the graph and this requires a fixed input size.

```auto
dummy = torch.randn(1, 3, 1920, 1080)
torch.onnx.export(transformer, dummy, save_path, opset_version=11)

```

I’m almost certain that on the ONNX.js side I need the input to be the same shape.  
Is there anyway around this, because I need dynamic shape inputs.  
Or should I just used Flask and host the model?

Thanks!

---

<div class="post-metadata">

**Author:** ![afg1](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/afg1/32/28826_2.png) [@afg1](https://discuss.pytorch.org/u/afg1)\
**Post date:** [September 15, 2020, 3:49pm UTC](https://discuss.pytorch.org/t/dynamic-input-for-onnx-js-using-a-pytorch-trained-model/93981/2 "2020-09-15T15:49:41Z")

</div>

I’m by no means an expert, but I think you can use the `dynamic_axes` optional argument to `onnx.export`

In the tutorial [here](https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html#running-the-model-on-an-image-using-onnx-runtime) (about a quarter of the way down) the example uses the dynamic\_axes argument to have a dynamic batch size:

```auto
                  dynamic_axes={'input' : {0 : 'batch_size'}, # variable lenght axes
                                'output' : {0 : 'batch_size'}})

```

I _assume_ that can also be done for the other axes, but I haven’t tried it - my use case has images which are guaranteed to be the same size, so I don’t need it.

I have seen some discussion that would suggest this is not the case though, so maybe someone with more experience can weigh in?
