numpy和tensor类型更改(增加或减少)参数维度(reshape)

首先需注意的是numpy和tensor的维度排列顺序不一样,这里拿一张图片的shape举例:

to_tensor = torchvision.transforms.ToTensor() img = cv2.imread("sample.jpg") img_tensor = to_tensor(img) print(type(img))  # <class 'numpy.ndarray'> print(img.shape)  # (450, 500, 3) print(type(img_tensor))  # <class 'torch.Tensor'> print(img_tensor.shape)  # torch.Size([3, 450, 500]) 

如果想改变数组内元素的排列顺序,torch和numpy都自带reshape方法:

# 展平操作 print(np.reshape(img, (1, 1, -1)).shape)  # (1, 1, 675000) print(torch.reshape(img_tensor, (1, 1, -1)).shape)  # torch.Size([1, 1, 675000]) # 注意-1的含义是让计算机自己计算这里的值(因为元素总数是不会变的,该例3*450*500 = 675000) # 但“-1”至多出现一次,也很好理解,假若两个位置需要计算机计算(-1),显然是无法做到的 

增加(减少)维度,以numpy为例(tensor同用法):

# 减少维度 img_1 = img[:, :, 1]  # 截取 img_2 = np.reshape(img, (450, -1))  # reshape方法必须保证总量不变 print(img_1.shape)  # (450, 500) print(img_2.shape)  # (450, 1500)  # 增加维度 a = np.array((3, 2, 3)) b = a[:, None, None] c = np.reshape(a, (3, 1, -1)) print(a.shape)  # (3,) print(b.shape)  # (3, 1, 1) print(c.shape)  # (3, 1, 1)

实例应用:

在图像标准化处理中假设我们计算得到RGB的均值(mean)为(127, 127, 127) 标准差std为(8, 8, 8),那么我们接下来要对整个图片各个位置的RGB进行(img - mean) / std 计算,显然是不能直接计算的,因为原图是个三维矩阵,而我们计算得到的均值和标准差都是一维的(包含三个值),所以在计算过程中需要对mean和std进行增加维度操作。

# 举例 img = np.random.randint(0, 255, (2, 2, 3)) mean = np.array((127, 127, 127)) std = np.array((8, 8, 8)) img_result = (img - mean[None, None, :]) / std[None, None, :] print("处理前:", img) # [[[177   1 231] #   [  1 109 203]] #  #  [[177 135 146] #   [ 66  89 122]]] print("处理后", img_result) # [[[  6.25  -15.75   13.   ] #   [-15.75   -2.25    9.5  ]] #  #  [[  6.25    1.      2.375] #   [ -7.625  -4.75   -0.625]]]