# Bugs with torchvision.transforms.ToPILImage()?

**URL:** <https://discuss.pytorch.org/t/bugs-with-torchvision-transforms-topilimage/26109>\
**Category:** vision\
**Created:** [September 28, 2018, 8:31am UTC](https://discuss.pytorch.org/t/bugs-with-torchvision-transforms-topilimage/26109 "2018-09-28T08:31:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Boyu\_Zhang](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/boyu_zhang/32/10084_2.png) [@Boyu\_Zhang](https://discuss.pytorch.org/u/Boyu_Zhang)\
**Post date:** [September 28, 2018, 8:31am UTC](https://discuss.pytorch.org/t/bugs-with-torchvision-transforms-topilimage/26109/1 "2018-09-28T08:31:34Z")

</div>

Hi, I was trying to convert an image of tensor into PILImage and then convert it back around by using the following code. But finally, I found the image’s values were modified after the operation `tfs.Compose([tfs.ToPILImage(), tfs.ToTensor()])`. Is this a bug in `torchvision.transforms.ToPILImage()` ? Thank you.

```auto
from skimage import io, color
from torchvision import transforms as tfs
import numpy as np
import torch

file = 'path/to/image.png'
img = color.rgb2ycbcr(io.imread(file)) / 255
(rows, cols, channel) = img.shape
img_y, img_cb, img_cr = np.split(img, indices_or_sections=channel, axis=2)
tensor_y = torch.from_numpy(img_y).float().view(1, rows, cols)

trans = tfs.Compose([tfs.ToPILImage(), tfs.ToTensor()])
preds = trans(tensor_y)

print((tensor_y.data.numpy()==preds.data.numpy()).all()) # return False

```

---

<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:** [September 28, 2018, 1:46pm UTC](https://discuss.pytorch.org/t/bugs-with-torchvision-transforms-topilimage/26109/2 "2018-09-28T13:46:47Z")

</div>

I think you are just losing the accuracy due to quantization.  
If you transform your `FloatTensor` to a `PIL.Image`, it will be scaled to `[0, 255]` in `uint8` type.  
This will already quantize your values, as you cannot map all floating values in `[0, 1]` to a `ByteTensor`.  
The reverse (`ToTensor()`) thus yields a `FloatTensor` with these already quantized values.

---

<div class="post-metadata">

**Author:** ![lrningml](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/l/7cd45c/32.png) [@lrningml](https://discuss.pytorch.org/u/lrningml)\
**Post date:** [September 28, 2018, 7:21pm UTC](https://discuss.pytorch.org/t/bugs-with-torchvision-transforms-topilimage/26109/3 "2018-09-28T19:21:27Z")

</div>

Just ran into the same thing, what’s a good way to view a tensor as an image without the quantization?

---

<div class="post-metadata">

**Author:** ![Boyu\_Zhang](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/boyu_zhang/32/10084_2.png) [@Boyu\_Zhang](https://discuss.pytorch.org/u/Boyu_Zhang)\
**Post date:** [September 29, 2018, 4:08am UTC](https://discuss.pytorch.org/t/bugs-with-torchvision-transforms-topilimage/26109/4 "2018-09-29T04:08:54Z")

</div>

Thanks for your answer. I’ve looked into the function `to_pil_image(pic, mode=None)` and found it explicitly converts a FloatTensor to [0, 255] in uint8 type. I’m just wondering can we keep the type as float so that there won’t be a precision loss?

---

<div class="post-metadata">

**Author:** ![Boyu\_Zhang](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/boyu_zhang/32/10084_2.png) [@Boyu\_Zhang](https://discuss.pytorch.org/u/Boyu_Zhang)\
**Post date:** [September 29, 2018, 4:12am UTC](https://discuss.pytorch.org/t/bugs-with-torchvision-transforms-topilimage/26109/5 "2018-09-29T04:12:07Z")

</div>

Currently, I just convert the tensor into `numpy.ndarray` and visualize it through `matplotlib`, or implement some other image transformations to ensure the precision. After that, I convert it back to `torch.Tenosr`.

---

<div class="post-metadata">

**Author:** ![monster](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/monster/32/24299_2.png) [@monster](https://discuss.pytorch.org/u/monster)\
**Post date:** [November 18, 2020, 12:16pm UTC](https://discuss.pytorch.org/t/bugs-with-torchvision-transforms-topilimage/26109/6 "2020-11-18T12:16:33Z")

</div>

My problem is similar, currently I am using this for augmentation.

```auto
augmentations = transforms.Compose([
    transforms.ToPILImage(),
    transforms.RandomHorizontalFlip(0.5),
    transforms.ColorJitter(saturation=0.5),
    transforms.ToTensor(),
    utils.normalize_transform(),
])

```

so should I remove " transforms.ToTensor()" ?

---

<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 19, 2020, 7:40am UTC](https://discuss.pytorch.org/t/bugs-with-torchvision-transforms-topilimage/26109/7 "2020-11-19T07:40:46Z")

</div>

I don’t think the `ToTensor` transformation is problematic, but the transformation to a `uint8` image in `ToPILImage()` if your use case fits my previous description.  
How did you create these floating point images and what do the values represent?
