如何将图像转换为 PyTorch 张量?
PyTorch 张量是一个包含单个数据类型元素的 n 维数组(矩阵)。张量类似于 NumPy 数组。NumPy 数组和 PyTorch 张量之间的区别在于,张量利用 GPU 加速数值计算。为了加速计算,图像被转换为张量。
要将图像转换为 PyTorch 张量,我们可以采取以下步骤:
步骤
导入所需的库。所需的库是 **torch、torchvision、Pillow**。
读取图像。图像必须是 PIL 图像或范围在 [0, 255] 内的 **numpy.ndarray (HxWxC)**。这里 **H、W** 和 **C** 分别是图像的高度、宽度和通道数。
定义一个将图像转换为张量的变换。我们使用 **transforms.ToTensor()** 来定义变换。
使用上面定义的变换将图像转换为张量。
输入图像

示例 1
# Import the required libraries
import torch
from PIL import Image
import torchvision.transforms as transforms
# Read the image
image = Image.open('Penguins.jpg')
# Define a transform to convert the image to tensor
transform = transforms.ToTensor()
# Convert the image to PyTorch tensor
tensor = transform(image)
# print the converted image tensor
print(tensor)输出
tensor([[[0.4510, 0.4549, 0.4667, ..., 0.3333, 0.3333, 0.3333], [0.4549, 0.4510, 0.4627, ..., 0.3373, 0.3373, 0.3373], [0.4667, 0.4588, 0.4667, ..., 0.3451, 0.3451, 0.3412], ..., [0.6706, 0.5020, 0.5490, ..., 0.4627, 0.4275, 0.3333], [0.4196, 0.5922, 0.6784, ..., 0.4627, 0.4549, 0.3569], [0.3569, 0.3529, 0.4784, ..., 0.3922, 0.4314, 0.3490]], [[0.6824, 0.6863, 0.7020, ..., 0.6392, 0.6392, 0.6392], [0.6863, 0.6824, 0.6980, ..., 0.6314, 0.6314, 0.6314], [0.6980, 0.6902, 0.6980, ..., 0.6392, 0.6392, 0.6353], ..., [0.7255, 0.5412, 0.5765, ..., 0.5255, 0.5020, 0.4157], [0.4706, 0.6314, 0.7098, ..., 0.5255, 0.5294, 0.4392], [0.4196, 0.3961, 0.5020, ..., 0.4510, 0.5059, 0.4314]], [[0.8157, 0.8196, 0.8353, ..., 0.7922, 0.7922, 0.7922], [0.8196, 0.8157, 0.8314, ..., 0.7882, 0.7882, 0.7882], [0.8314, 0.8235, 0.8314, ..., 0.7961, 0.7961, 0.7922], ..., [0.6235, 0.5059, 0.6157, ..., 0.4863, 0.4941, 0.4196], [0.3922, 0.6000, 0.7176, ..., 0.4863, 0.5216, 0.4431], [0.3686, 0.3647, 0.4863, ..., 0.4235, 0.4980, 0.4353]]])
在上面的 Python 程序中,我们已将 PIL 图像转换为张量。
示例 2
我们也可以使用 **OpenCV** 读取图像。使用 OpenCV 读取的图像是 **numpy.ndarray** 类型。我们可以使用 **transforms.ToTensor()** 将 **numpy.ndarray** 转换为张量。请看下面的例子。
# Import the required libraries
import torch
import cv2
import torchvision.transforms as transforms
# Read the image
image = cv2.imread('Penguins.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Define a transform to convert the image to tensor
transform = transforms.ToTensor()
# Convert the image to PyTorch tensor
tensor = transform(image)
# Print the converted image tensor
print(tensor)输出
tensor([[[0.4510, 0.4549, 0.4667, ..., 0.3333, 0.3333, 0.3333], [0.4549, 0.4510, 0.4627, ..., 0.3373, 0.3373, 0.3373], [0.4667, 0.4588, 0.4667, ..., 0.3451, 0.3451, 0.3412], ..., [0.6706, 0.5020, 0.5490, ..., 0.4627, 0.4275, 0.3333], [0.4196, 0.5922, 0.6784, ..., 0.4627, 0.4549, 0.3569], [0.3569, 0.3529, 0.4784, ..., 0.3922, 0.4314, 0.3490]], [[0.6824, 0.6863, 0.7020, ..., 0.6392, 0.6392, 0.6392], [0.6863, 0.6824, 0.6980, ..., 0.6314, 0.6314, 0.6314], [0.6980, 0.6902, 0.6980, ..., 0.6392, 0.6392, 0.6353], ..., [0.7255, 0.5412, 0.5765, ..., 0.5255, 0.5020, 0.4157], [0.4706, 0.6314, 0.7098, ..., 0.5255, 0.5294, 0.4392], [0.4196, 0.3961, 0.5020, ..., 0.4510, 0.5059, 0.4314]], [[0.8157, 0.8196, 0.8353, ..., 0.7922, 0.7922, 0.7922], [0.8196, 0.8157, 0.8314, ..., 0.7882, 0.7882, 0.7882], [0.8314, 0.8235, 0.8314, ..., 0.7961, 0.7961, 0.7922], ..., [0.6235, 0.5059, 0.6157, ..., 0.4863, 0.4941, 0.4196], [0.3922, 0.6000, 0.7176, ..., 0.4863, 0.5216, 0.4431], [0.3686, 0.3647, 0.4863, ..., 0.4235, 0.4980, 0.4353]]])
广告
数据结构
网络
关系型数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP