convert_image_dtype¶
- torchvision.transforms.functional.convert_image_dtype(image: Tensor, dtype: dtype = torch.float32) Tensor [源代码]¶
将张量图像转换为给定的
dtype
并相应地缩放值。此函数不支持 PIL 图像。- 参数:
image (torch.Tensor) – 要转换的图像
dtype (torch.dpython:type) – 输出的期望数据类型
- 返回:
转换后的图像
- 返回类型:
Tensor
注意
当从较小的整数
dtype
转换为较大的整数dtype
时,最大值不会精确映射。如果来回转换,这种不匹配没有影响。- 引发:
RuntimeError – 当尝试将
torch.float32
转换为torch.int32
或torch.int64
,以及尝试将torch.float64
转换为torch.int64
时。这些转换可能会导致溢出错误,因为浮点dtype
无法在整数dtype
的整个范围内存储连续的整数。
使用
convert_image_dtype
的示例