# Replicating PyTorch image pre-processing into TRT DeepStream

**URL:** <https://forums.developer.nvidia.com/t/replicating-pytorch-image-pre-processing-into-trt-deepstream/184423>\
**Category:** DeepStream SDK\
**Tags:** tensorrt\
**Created:** [July 22, 2021, 10:54am UTC](https://forums.developer.nvidia.com/t/replicating-pytorch-image-pre-processing-into-trt-deepstream/184423 "2021-07-22T10:54:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![LwsChlds](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@LwsChlds](https://forums.developer.nvidia.com/u/LwsChlds)\
**Post date:** [July 22, 2021, 10:54am UTC](https://forums.developer.nvidia.com/t/replicating-pytorch-image-pre-processing-into-trt-deepstream/184423/1 "2021-07-22T10:54:29Z")

</div>

I recently posted [this](https://forums.developer.nvidia.com/t/pytorch-model-loosing-accuracy-when-converting-to-tensorrt/183642) post on the TensorRT forums and was referred here.

Currently my I think that the problem is due to the different image pre-processing from the PyTorch model and when running on TRT.  
How do I find the values I use in my spec file for net-scale-factor, offsets or other variables to best replicate the image preprocessing used in the PyTorch program?

**• Hardware Platform (Jetson / GPU) = Jetson**  
**• DeepStream Version = 5.1**  
**• JetPack Version (valid for Jetson only) = jetson-nano-jp451-sd-card-image**  
**• TensorRT Version = 7.1.3-1**  
**• Issue Type( questions, new requirements, bugs) = questions**

Thanks

---

<div class="post-metadata">

**Author:** ![AastaLLL](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/aastalll/32/14043_2.png) [@AastaLLL](https://forums.developer.nvidia.com/u/AastaLLL)\
**Post date:** [July 23, 2021, 2:56am UTC](https://forums.developer.nvidia.com/t/replicating-pytorch-image-pre-processing-into-trt-deepstream/184423/3 "2021-07-23T02:56:22Z")

</div>

Hi,

Please find below’s topic for the information:

> [@How to set the correct config for a pytorch model in nvinfer?](https://forums.developer.nvidia.com/t/how-to-set-the-correct-config-for-a-pytorch-model-in-nvinfer/174633/):
>
> Hello, I have trained a Resnet classifier in PyTorch and exported to an ONNX model. The FC layer of Resnet18 is set to: model.fc = nn.Sequential( nn.Linear(model.fc.in\_features, args.n\_classes, bias=False), nn.Softmax(1), ) These are the normalization values I have used. normalize = transforms.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) The model accuracy goes up to 80%, but when I infer (using nvinfer) on the exact same images used…

Thanks.

---

<div class="post-metadata">

**Author:** ![LwsChlds](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@LwsChlds](https://forums.developer.nvidia.com/u/LwsChlds)\
**Post date:** [July 23, 2021, 1:13pm UTC](https://forums.developer.nvidia.com/t/replicating-pytorch-image-pre-processing-into-trt-deepstream/184423/4 "2021-07-23T13:13:00Z")

</div>

Hi,

Unfortunately, when I try to use those values I get outputs that are much larger than expected and are not normalised as they should be.

I am trying to replicate this from torchvision:

```auto
torchvision.transforms.Compose([torchvision.transforms.ToTensor(),
                                           torchvision.transforms.Normalize(mean=[0.43476477, 0.44504763, 0.43252817],
                                                                            std=[0.20490805, 0.19712372, 0.20312176]),
                                           torchvision.transforms.Resize(540, 960)
                                           ]) # normalize to (-1, 1)

```

Thanks

---

<div class="post-metadata">

**Author:** ![AastaLLL](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/aastalll/32/14043_2.png) [@AastaLLL](https://forums.developer.nvidia.com/u/AastaLLL)\
**Post date:** [July 26, 2021, 7:56am UTC](https://forums.developer.nvidia.com/t/replicating-pytorch-image-pre-processing-into-trt-deepstream/184423/5 "2021-07-26T07:56:32Z")

</div>

**There is no update from you for a period, assuming this is not an issue any more.  
Hence we are closing this topic. If need further support, please open a new one.  
Thanks**

Hi,

Could you share the details about your preprocessing so we can check?  
Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/system/32/68080_2.png) [@system](https://forums.developer.nvidia.com/u/system)\
**Post date:** [October 9, 2021, 12:57am UTC](https://forums.developer.nvidia.com/t/replicating-pytorch-image-pre-processing-into-trt-deepstream/184423/7 "2021-10-09T00:57:58Z")

</div>

Closed on October 9, 2021, 12:57am UTC

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
