极简实用PyTorch记录——如何读取图片、改变大小、转化为numpy数组、转化为tensor

1. 需求

使用torch时,需要读取图片——改变图片的大小——转化为numpy数组——转化为tensor。本文对每一步的方法都做极简的介绍。

2. 极简代码

使用PIL

import numpy as np
from PIL import Image
from torchvision import transforms

transforms1 = transforms.Compose([
    transforms.ToTensor()
])

# 文件名
filename = ''
# 读取文件,注意img不是numpy数组
img = Image.open(filename)
# 改变大小,参数是新的(width, height)
img = img.resize((96, 32))
# 转化为numpy数组
npy_img = np.array(img)
# 输出结果为(32, 96, 3),即顺序为(H, W, C),这个顺序和torch不一致
print(npy_img.shape)
# 转化为tensor
tensor = transforms1(img)
# 输出结果为torch.Size([3, 32, 96]),
# 即顺序在转化为tensor的过程中,自动变为(C, H, W),和torch一致
print(tensor.shape)
# 直接将numpy数组转化为tensor
tensor1 = transforms1(npy_img)
# 输出结果为torch.Size([3, 32, 96]),
# 即直接转化numpy数组,也会自动变为(C, H, W)
print(tensor1.shape)

上述代码运行结果:

(32, 96, 3)
torch.Size([3, 32, 96])
torch.Size([3, 32, 96])

你可能感兴趣的:(AI编程,pytorch,numpy,python,PIL)