我有一个图像的 numpy 数组表示,我想将其转换为张量,这样我就可以通过我的 pytorch 神经网络提供它。
据我了解,神经网络接受变换后的张量,这些张量不是排列在 [100,100,3] 而是排列在 [3,100,100] 中,并且像素被重新缩放,并且图像必须是批量的。
所以我做了以下事情:
import cv2
my_img = cv2.imread('testset/img0.png')
my_img.shape #reuturns [100,100,3] a 3 channel image with 100x100 resolution
my_img = np.transpose(my_img,(2,0,1))
my_img.shape #returns [3,100,100]
#convert the numpy array to tensor
my_img_tensor = torch.from_numpy(my_img)
#rescale to be [0,1] like the data it was trained on by default
my_img_tensor *= (1/255)
#turn the tensor into a batch of size 1
my_img_tensor = my_img_tensor.unsqueeze(0)
#send image to gpu
my_img_tensor.to(device)
#put forward through my neural network. …Run Code Online (Sandbox Code Playgroud)