我有影像和總和火炬張量,而 numpy 陣列不同,為什么?如何torch_img.sum() = numpy_float_img.sum()?
from PIL import Image
from torchvision import transforms as T
# Read image with PIL
img = Image.open(img_path).resize((224,224))
torch_img = T.ToTensor()(img)
numpy_img = np.asarray(img)
numpy_img_float = np.asarray(img).astype(np.float32)
print(torch_img.sum(), numpy_img.sum(), numpy_img_float.sum())
->56914.496, 14513196, 14513196.0
有誰知道為什么?
uj5u.com熱心網友回復:
注意 how torch_img
is in the [0,1]
range while numpy_img
and numpy_img_float
are both in the [0, 255]
range. 查看 的檔案torchvision.transforms.ToTensor
,如果提供的輸入是PIL
影像,則這些值將映射到[0, 1]
。相反,numpy.array
將有值保持在[0, 255]
范圍內。
除此之外,結果的微小變化是由不同的浮點精度引起的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/341063.html
上一篇:如何只獲取不到兩年的日期?