Pytorch图像数据格式之间的转换

使用Pytorch进行模型训练时,经常会遇到数据格式的错误,不同的数据类型之间的相互转换十分有用,总结如下。

一、Tensor与PILImage

使用PIL包读图片并将其转化为tensor类型,再转化为PIL类型

import torch
from PIL import Image
import torchvision

img = Image.open('datasets\\input_test\\3063.jpg')
print(type(img))
trans_totensor = torchvision.transforms.ToTensor()
img_tensor = trans_totensor(img)
print(type(img_tensor))
trans_toPIL = torchvision.transforms.ToPILImage()
img_PIL = trans_toPIL(img_tensor)
print(type(img_PIL))

输出:

<class 'PIL.JpegImagePlugin.JpegImageFile'>
<class 'torch.Tensor'>
<class 'PIL.Image.Image'>

二、Tensor与Numpy

使用cv2读入图片得到的数据类型是Numpy类型,将其转化为tensor

import cv2
import torch
import torchvision
img = cv2.imread('datasets\\input_test\\3063.jpg')
print(type(img), img.shape)
trans_totensor = torchvision.transforms.ToTensor()
img_tensor = trans_totensor(img)
print(type(img_tensor), img_tensor.shape)
img_tensor = img_tensor.reshape((img_tensor.shape[1], img_tensor.shape[2],-1))
print(type(img_tensor), img_tensor.shape)
img_numpy=img_tensor.numpy()
print(type(img_numpy), img_numpy.shape)
print(img_numpy == img)

输出:

<class 'numpy.ndarray'> (159, 239, 3)
<class 'torch.Tensor'> torch.Size([3, 159, 239])
<class 'torch.Tensor'> torch.Size([159, 239, 3])
<class 'numpy.ndarray'> (159, 239, 3)
[[[False False False]
  [False False False]
  [False False False]
    ...

注意,totensor操作会将numpy和PIL格式的图片转换为[0,1]之间的小数,并且改变张量的形状,因此直接从tensor转为numpy得到的是小数,与读入的numpy类型图片内容完全不同。

官方文档解释如下:

Converts a PIL Image or numpy.ndarray (H x W x C) in the range [0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0] if the PIL Image belongs to one of the modes (L, LA, P, I, F, RGB, YCbCr, RGBA, CMYK, 1) or if the numpy.ndarray has dtype = np.uint8

In the other cases, tensors are returned without scaling.

三、PIL与Numpy

from PIL import Image
import numpy as np
img = Image.open('datasets\\input_test\\3063.jpg')
print(type(img),img.size)
img_np=np.array(img)
print(type(img_np),img_np.shape)
img_PIL=Image.fromarray(img_np)
print(type(img_PIL),img_PIL.size)

输出:

<class 'PIL.JpegImagePlugin.JpegImageFile'> (239, 159)
<class 'numpy.ndarray'> (159, 239, 3)
<class 'PIL.Image.Image'> (239, 159)

注意注意注意,cv2和PIL.Image读图后转化为Numpy类型,二者通道不同,同一通道数值也不同!!!,cv2读入通道数为‘BGR’,PIL为‘RGB’

四、图像颜色表示方式

RGB HSL HSV

三分钟带你快速学习RGB、HSV和HSL颜色空间 - 知乎

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值