功能:把一个取值范围是[0,255]
的PIL.Image
或者shape
为(H,W,C)
的numpy.ndarray
,转换成形状为(C,H,W)
,取值范围是[0,1.0]
的torch.FloatTensor
。
注意:只有当numpy.ndarray
的dtype=unit8
时才会将像素值scale到[0,1.0]
。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| import torch import numpy as np
from PIL import Image from torchvision import transforms
transform = transforms.Compose([ transforms.ToTensor(), ]) img = Image.open('cat.png')
img1 = np.asarray(img) img1 = transform(img1) img1 = img3.permute([1, 2, 0]) img1 = img1.cpu().data.numpy() print(img1[300: 305, 695: 700, 0]) """ [[0.43529412 0.44705883 0.45490196 0.45882353 0.45490196] [0.41960785 0.43137255 0.4392157 0.44313726 0.4392157 ] [0.40392157 0.41568628 0.42745098 0.43137255 0.43529412] [0.3882353 0.40784314 0.41568628 0.41960785 0.42745098] [0.38431373 0.4 0.4117647 0.41568628 0.42352942]] """
img2 = np.asarray(img, dtype=np.float32) img2 = transform(img2) img2 = img2.permute([1, 2, 0]) img2 = img2.cpu().data.numpy() print(img2[300: 305, 695: 700, 0]) """ [[111. 114. 116. 117. 116.] [107. 110. 112. 113. 112.] [103. 106. 109. 110. 111.] [ 99. 104. 106. 107. 109.] [ 98. 102. 105. 106. 108.]] """
img3 = np.asarray(img, dtype=np.float32) img3 = img3 / 255. print(img3[300: 305, 695: 700, 0]) """ [[0.43529412 0.44705883 0.45490196 0.45882353 0.45490196] [0.41960785 0.43137255 0.4392157 0.44313726 0.4392157 ] [0.40392157 0.41568628 0.42745098 0.43137255 0.43529412] [0.3882353 0.40784314 0.41568628 0.41960785 0.42745098] [0.38431373 0.4 0.4117647 0.41568628 0.42352942]] """
|
结论:使用torchvision.transform.ToTensor()
时避免将图像的数据类型转为float32
。