pytorch学习

您所在的位置:网站首页 一张测试天才的图片 pytorch学习

pytorch学习

2024-07-11 07:59| 来源: 网络整理| 查看: 265

pytorch学习—图像的加载/读取方式(转)

使用pytorch制作图像数据集时,需要将存储在磁盘、硬盘的图像读取到内存中,涉及到图像I/O问题。

在python中,图像处理主要采用的库:skimage, opencv-python, Pillow (PIL)。 这三个库均提供了图像读取的方法。

三种主流图像处理库的比较:

库函数/方法返回值图像像素格式像素值范围图像矩阵表示skimageio.imread(xxx)numpy.ndarrayRGB[0, 255](H X W X C)cv2cv2.imread(xxx)numpy.ndarrayBGR[0, 255](H X W X C)Pillow(PIL)Image.open(xxx)PIL.Image.Image对象根据图像格式,一般为RGB[0, 255]— 操作系统:ubuntu18.04显卡:GTX1080tipython版本:2.7(3.7)pycharmpytorch1.0, opencv-python 3.4.3,skimage, numpy,PILQQ群:加入深度学习交流群 获取更多环境配置细节和学习资料 实验内容 读取图像

准备一张测试图像,1200*1200 彩色24bit

在这里插入图片描述

显示图像: 从左到右分别为skimage, cv2, PIL 读取之后显示的图像。PIL读取的图像为PIL.Image.Image对象,无法用matplotlib直接显示,需要先转为numpy.ndarray对象。 图1,图3显示正常,图像显示不正常,因为opencv读取的图像为BGR格式,matplotllib使用RGB方式显示,图像通道顺序不一致。

image.png 图像转为torch.Tensor对象

在深度学习中,原始图像需要转换为深度学习框架自定义的数据格式,在pytorch中,需要转为torch.Tensor。 pytorch提供了torch.Tensor 与numpy.ndarray转换为接口:

方法名作用torch.from_numpy(xxx) numpy.ndarray转为torch.Tensor tensor1.numpy()获取tensor1对象的numpy格式数据

torch.Tensor 高维矩阵的表示: (nSample)x C x H x W

numpy.ndarray 高维矩阵的表示: H x W x C 因此在两者转换的时候需要使用numpy.transpose( ) 方法 。

Code

# ------------np.ndarray转为torch.Tensor------------------------------------ # numpy image: H x W x C # torch image: C x H x W # np.transpose( xxx, (2, 0, 1)) # 将 H x W x C 转化为 C x H x W tensor_skimage = torch.from_numpy(np.transpose(img_skimage, (2, 0, 1))) tensor_cv = torch.from_numpy(np.transpose(img_cv, (2, 0, 1))) tensor_pil = torch.from_numpy(np.transpose(img_pil_1, (2, 0, 1)))

torch.Tensor转numpy.ndarray # np.transpose( xxx, (2, 0, 1)) # 将 C x H x W 转化为 H x W x C img_skimage_2 = np.transpose(tensor_skimage.numpy(), (1, 2, 0)) img_cv_2 = np.transpose(tensor_cv.numpy(), (1, 2, 0)) img_pil_2 = np.transpose(tensor_pil.numpy(), (1, 2, 0))

plt.figure() for i, im in enumerate([img_skimage_2, img_cv_2, img_pil_2]): ax = plt.subplot(1, 3, i + 1) ax.imshow(im) plt.pause(0.01)

显示:

image.png opencv图像BGR->RGB操作

opencv默认读取的图像为BGR形式,可以使用opencv提供的方法:cv2.cvtColor( ) 进行图像颜色空间转换

# opencv 读取的图像为BGR # 首先需要转为RGB img_cv = cv2.cvtColor(img_cv, cv2.COLOR_BGR2RGB) # 转torch.Tensor tensor_cv = torch.from_numpy(img_cv) # tensor转numpy img_cv_2 = tensor_cv.numpy() plt.figure() plt.title('cv') plt.imshow(img_cv_2) plt.show()

显示:

image.png


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3