# Normalizing images between 0 and 1 with ToTensor() doesn't work

**URL:** <https://discuss.pytorch.org/t/normalizing-images-between-0-and-1-with-totensor-doesnt-work/104363>\
**Category:** vision\
**Created:** [November 27, 2020, 4:09pm UTC](https://discuss.pytorch.org/t/normalizing-images-between-0-and-1-with-totensor-doesnt-work/104363 "2020-11-27T16:09:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Codophile1](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/9fc29f/32.png) [@Codophile1](https://discuss.pytorch.org/u/Codophile1)\
**Post date:** [November 27, 2020, 4:09pm UTC](https://discuss.pytorch.org/t/normalizing-images-between-0-and-1-with-totensor-doesnt-work/104363/1 "2020-11-27T16:09:25Z")

</div>

Hi everyone,

I’m trying to train a segmentation network and I would like to normalize images between 0 and 1. However the ToTensor transform I use outputs very low pixel values. Here’s a code sample

```python
img_transforms = transforms.Compose([transforms.Grayscale(num_output_channels=1),
                                     transforms.Resize((100,100), interpolation=2),
                                     transforms.ToTensor()])
img = Image.open("myimg.png")
img = np.array(img).astype(float)
img*= (255.0/img.max())
img = img.astype(np.uint8)
print(img.max())
img = Image.fromarray(img)
img = img_transforms(img)
print(img.max())

```

And the given output:

```auto
255
tensor(0.1137)

```

I don’t understand why I don’t get 1 as a max value since the ToTensor function is supposed to output values between 0 and 1.

Could anyone shed some light on what may be occurring?

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:** [November 28, 2020, 8:29am UTC](https://discuss.pytorch.org/t/normalizing-images-between-0-and-1-with-totensor-doesnt-work/104363/2 "2020-11-28T08:29:46Z")

</div>

The `ToTensor` transformation will normalize the `uint8` input image by dividing by `255` as seen [here](https://github.com/pytorch/vision/blob/4ab46e5f7585b86fb2befdc32d22d13635868c4e/torchvision/transforms/functional.py#L112-L113).  
Based on your code, I guess that the `Resize` operation might lower the max. values of `255` to a smaller one (although I wouldn’t expect to see such a decrease). Could you try to use `PIL.Image.NEAREST` as the `interpolation` method and recheck the code?

---

<div class="post-metadata">

**Author:** ![Codophile1](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/9fc29f/32.png) [@Codophile1](https://discuss.pytorch.org/u/Codophile1)\
**Post date:** [November 28, 2020, 10:27pm UTC](https://discuss.pytorch.org/t/normalizing-images-between-0-and-1-with-totensor-doesnt-work/104363/3 "2020-11-28T22:27:51Z")

</div>

Thank you @ptrblck, the resize transform was not the issue here but I figured out it was actually due to the fact that my images are mainly blue and that the Grayscale transform multiply blue values by 0.114. I solved the problem by manually setting the max value to 1. I still don’t know why the ToTensor() function didn’t normalize the values between 0 and 1 after the Grayscal transform though.

Here is my solution:

```python
img = Image.open("myimg.png")
img = img_transforms(img)
img*= (1.0/img.max())

```
