numpy - openCv 和 PyTorch 逆变器转换不起作用
问题描述
我有一个转换类,它只做:
if transform is None:
transform = transforms.Compose([
transforms.Resize((256, 256)),
transforms.ToTensor()
])
root = os.path.join(PROJECT_ROOT_DIR, "data")
super(AttributesDataset, self).__init__()
self.data = torchvision.datasets.CelebA(
root=root,
split=split,
target_type='attr',
download=True,
transform=transform
)
从文档中,我了解到这仅意味着 0,1 范围内的值的按比例缩小,即所有像素值应介于 [0,1] 之间(我也已对此进行了验证)。我想可视化来自模型的一些输出。因此,我创建了一个简单的方法:-
for img, label in dataloader:
img.squeeze_(0)
# permute the channels. cv2 expects image in format (h, w, c)
unscaled_img = img.permute(1, 2, 0)
# move images to cpu and convert to numpy as required by cv2 library
unscaled_img = torch.round(unscaled_img * 255)
unscaled_img = unscaled_img.to(torch.uint8)
# unscaled_img = np.rint(unscaled_img * 255).astype(np.uint8)
unscaled_img = cv2.cvtColor(unscaled_img, cv2.COLOR_RGB2BGR)
cv2.imshow(unscaled_img.numpy())
但是,创建的所有图像都具有异常的蓝色阴影。例如,
有人可以告诉我我到底在做什么错吗?您的帮助将不胜感激
解决方案
由@LajosArpad 评论解决。罪魁祸首是
unscaled_img = cv2.cvtColor(unscaled_img, cv2.COLOR_RGB2BGR)
删除它会产生正确的值。
推荐阅读
- php - 如何包含 mPDF 文件?
- arrays - 如何使用嵌套的 for 循环在范围内创建一维整数对数组?
- java - 硒选择不起作用
- python - 警告:未找到隐藏的导入“pygame._view”
- rest - 为什么我的 REST API 在 AWS 上返回错误数据,而不是在本地计算机上?
- python - 如何将列表输出全部保留在一行上而不包含下一行代码
- django - 如何使用 djoser 向 curl 注册新用户?
- python - sklearn 的线性回归模型如何在以下代码中进行非线性预测?
- scala - 无法使用 Kafka-Streams 反序列化自定义 Serde
- python - Turtle Filling Colors - 我在调用函数时尝试选择颜色,以便将来可以轻松更改