# Input format for pretrained torchvision models

**URL:** <https://discuss.pytorch.org/t/input-format-for-pretrained-torchvision-models/48759>\
**Category:** vision\
**Created:** [June 23, 2019, 3:35pm UTC](https://discuss.pytorch.org/t/input-format-for-pretrained-torchvision-models/48759 "2019-06-23T15:35:18Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![M\_S](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/m_s/32/6108_2.png) [@M\_S](https://discuss.pytorch.org/u/M_S)\
**Post date:** [June 23, 2019, 3:35pm UTC](https://discuss.pytorch.org/t/input-format-for-pretrained-torchvision-models/48759/1 "2019-06-23T15:35:18Z")

</div>

Hi,  
Many torchvision models are also available in a fully trained mode.  
As it is not obvious, I’d like to know what’s the input format that the models are trained on.  
I’m assuming it’s color images so 3 channel tensors, but is it RGB or BGR (like in OpenCV \ cv2)?  
Also what’s the range values? Is it uint8 ([0:255]) or a float type?  
If it’s float, is it normalized to [0:1] or to [-1:1]?  
Please specify these details so it would be easy to reproduce the results.

Thanks

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [June 23, 2019, 3:58pm UTC](https://discuss.pytorch.org/t/input-format-for-pretrained-torchvision-models/48759/2 "2019-06-23T15:58:48Z")

</div>

From the [docs](https://pytorch.org/docs/stable/torchvision/models.html):

> All pre-trained models expect input images normalized in the same way, i.e. mini-batches of 3-channel RGB images of shape (3 x H x W), where H and W are expected to be at least 224. The images have to be loaded in to a range of [0, 1] and then normalized using `mean = [0.485, 0.456,0.406]` and `std = [0.229, 0.224, 0.225]` .

---

<div class="post-metadata">

**Author:** ![M\_S](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/m_s/32/6108_2.png) [@M\_S](https://discuss.pytorch.org/u/M_S)\
**Post date:** [June 23, 2019, 4:09pm UTC](https://discuss.pytorch.org/t/input-format-for-pretrained-torchvision-models/48759/3 "2019-06-23T16:09:48Z")

</div>

Thank you for the detailed answer!

---

<div class="post-metadata">

**Author:** ![M\_S](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/m_s/32/6108_2.png) [@M\_S](https://discuss.pytorch.org/u/M_S)\
**Post date:** [June 26, 2019, 7:11am UTC](https://discuss.pytorch.org/t/input-format-for-pretrained-torchvision-models/48759/4 "2019-06-26T07:11:03Z")

</div>

Hi @ptrblck, I was wondering, isn’t it preferred to normalize the data to the [-1:1] range instead of the [0:1] range?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [June 26, 2019, 7:25am UTC](https://discuss.pytorch.org/t/input-format-for-pretrained-torchvision-models/48759/5 "2019-06-26T07:25:48Z")

</div>

Not sure, if `[-1:1]` is preferred over `[0:1]`, but the implemented normalization standardizes the data, such that it has a zero mean and a unit variance (sometimes known as z-score).

---

<div class="post-metadata">

**Author:** ![tazz](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@tazz](https://discuss.pytorch.org/u/tazz)\
**Post date:** [May 26, 2020, 3:45pm UTC](https://discuss.pytorch.org/t/input-format-for-pretrained-torchvision-models/48759/6 "2020-05-26T15:45:26Z")

</div>

@ptrblck What does one do if you have to export the model to onnx ? Can one just assume the batch size for a model? and specify the batch size later? or is it possible to not mention the batch size while transferring it to ONNX?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [May 27, 2020, 3:52am UTC](https://discuss.pytorch.org/t/input-format-for-pretrained-torchvision-models/48759/7 "2020-05-27T03:52:31Z")

</div>

You could use the `dynamic_axes` argument to specify that the batch size is not fixes as used in [this tutorial](https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html).

---

<div class="post-metadata">

**Author:** ![tazz](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@tazz](https://discuss.pytorch.org/u/tazz)\
**Post date:** [June 5, 2020, 12:09am UTC](https://discuss.pytorch.org/t/input-format-for-pretrained-torchvision-models/48759/8 "2020-06-05T00:09:20Z")

</div>

@ptrblck thanks a lot. That helped!

---

<div class="post-metadata">

**Author:** ![M\_S](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/m_s/32/6108_2.png) [@M\_S](https://discuss.pytorch.org/u/M_S)\
**Post date:** [July 5, 2020, 9:46pm UTC](https://discuss.pytorch.org/t/input-format-for-pretrained-torchvision-models/48759/9 "2020-07-05T21:46:29Z")

</div>

@ptrblck,

In the docs, mean and STD values are specified for classification, segmentation, and video classification.  
They are not specified for object detection models: [https://pytorch.org/docs/stable/torchvision/models.html#object-detection-instance-segmentation-and-person-keypoint-detection](https://pytorch.org/docs/stable/torchvision/models.html#object-detection-instance-segmentation-and-person-keypoint-detection)

Can we get their values as well?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [July 6, 2020, 5:29am UTC](https://discuss.pytorch.org/t/input-format-for-pretrained-torchvision-models/48759/10 "2020-07-06T05:29:49Z")

</div>

You could manually pass the `image_mean` and `image_std` to the model creation based, if you’ve calculated them manually on your datase.  
If you don’t pass them, [these values](https://github.com/pytorch/vision/blob/75f5b57e680549d012b3fc01b356b2fb92658ea7/torchvision/models/detection/faster_rcnn.py#L227-L230) will be used, which apparently are the `ImageNet` stats.

CC @pmeier for more information.

---

<div class="post-metadata">

**Author:** ![pmeier](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pmeier/32/20601_2.png) [@pmeier](https://discuss.pytorch.org/u/pmeier)\
**Post date:** [July 6, 2020, 9:24am UTC](https://discuss.pytorch.org/t/input-format-for-pretrained-torchvision-models/48759/11 "2020-07-06T09:24:15Z")

</div>

@M_S

For detection models we do [**not** use any normalization](https://github.com/pytorch/vision/blob/75f5b57e680549d012b3fc01b356b2fb92658ea7/references/detection/train.py#L51-L56). I don’t know why not and can’t comment if this would be beneficial or not. You’ll have to ask @fmassa about that 😉

---

<div class="post-metadata">

**Author:** ![M\_S](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/m_s/32/6108_2.png) [@M\_S](https://discuss.pytorch.org/u/M_S)\
**Post date:** [July 6, 2020, 10:42am UTC](https://discuss.pytorch.org/t/input-format-for-pretrained-torchvision-models/48759/12 "2020-07-06T10:42:07Z")

</div>

Thank you. Indeed I tried inference on a pretrained model with no normalization and it worked. Some notes though:

1. Consider explaining why the pretrained detection models are the only ones that don’t require image normalization (I understand that the training set was not normalized. But again, why?)
2. Worth mentioning that no normalization is needed. The classification, segmentation and detection pretrained models are trained on ImageNet, so one may think all of them require ImageNet normalization, when in fact only the classification and segmentation models require normalization.  
Perhaps it’s best to put this info in a table, since the pretrained video models also have a normalization, but different.

---

<div class="post-metadata">

**Author:** ![pmeier](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pmeier/32/20601_2.png) [@pmeier](https://discuss.pytorch.org/u/pmeier)\
**Post date:** [July 6, 2020, 11:56am UTC](https://discuss.pytorch.org/t/input-format-for-pretrained-torchvision-models/48759/13 "2020-07-06T11:56:29Z")

</div>

Those are valid points. Do you want to open an [issue](https://github.com/pytorch/vision/issues/new?assignees=&labels=&template=---documentation.md&title=) for that or should I do it in your name?

---

<div class="post-metadata">

**Author:** ![M\_S](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/m_s/32/6108_2.png) [@M\_S](https://discuss.pytorch.org/u/M_S)\
**Post date:** [July 6, 2020, 1:39pm UTC](https://discuss.pytorch.org/t/input-format-for-pretrained-torchvision-models/48759/14 "2020-07-06T13:39:35Z")

</div>

I would appreciate if you could do it. I’m mattans on GitHub. Thanks!

---

<div class="post-metadata">

**Author:** ![pmeier](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pmeier/32/20601_2.png) [@pmeier](https://discuss.pytorch.org/u/pmeier)\
**Post date:** [July 6, 2020, 2:40pm UTC](https://discuss.pytorch.org/t/input-format-for-pretrained-torchvision-models/48759/15 "2020-07-06T14:40:39Z")

</div>

For reference: [https://github.com/pytorch/vision/issues/2397](https://github.com/pytorch/vision/issues/2397)

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [July 7, 2020, 8:35am UTC](https://discuss.pytorch.org/t/input-format-for-pretrained-torchvision-models/48759/16 "2020-07-07T08:35:26Z")

</div>

The the sake of completeness:  
Based on [Francisco’s answer](https://github.com/pytorch/vision/issues/2397#issuecomment-654286088), the normalization is used internally in [these lines of code](https://github.com/pytorch/vision/blob/e212cc86b80baf1a46681442db1312ebce5a21bb/torchvision/models/detection/transform.py#L104-L105), which is using the `GeneralizedRCNNTransform` from my previous link.
