将 PIL 图像转换为张量时,为什么像素会发生变化? [英] While converting a PIL image into a tensor why the pixels are changing?

查看:36
本文介绍了将 PIL 图像转换为张量时,为什么像素会发生变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

transform = transforms.Compose([transforms.ToPILImage(), transforms.ToTensor()])

应用转换之前

应用转换后

Q.1 为什么要改变像素值?
Q.2 如何更正?

Q.1 Why the pixel values are changed?
Q.2 How to correct this?

推荐答案

我能够通过在转换之前对输入数据进行规范化来解决这个问题.
问题是 ToPILImage() 丢弃了所有大于 1 的值,因此亮像素变暗.

I was able to solve this problem by normalizing the input data before transforming it.
The problem was that ToPILImage() was discarding all the values which were greater than 1 hence the bright pixels became dark.

这里是工作代码的链接.

这篇关于将 PIL 图像转换为张量时,为什么像素会发生变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆