更多pytorch源码实现细节可以转到:
pytorch源码实现
import numpy as np
from PIL import Image
import torch
##PIL.Image
pil_img = Image.open('./dog.jpg').convert('RGB')
print(type(pil_img))
##
## ToNumpy
np_img = np.array(pil_img, dtype=np.uint8)
np_img = np.rollaxis(np_img,2)
print(np_img.shape,type(np_img))
##(3, 151, 149)
## ToTensor
tensor_img = torch.from_numpy(np_img).to(dtype=torch.float32)
print(tensor_img.shape, type(tensor_img))
##torch.Size([3, 151, 149])