UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tens

文章讨论了在使用PyTorch时遇到的UserWarning,该警告涉及到不可写的NumPy数组和不支持的张量。提供了解决方案,包括将`transforms.PILToTensor()`替换为`transforms.ToTensor()`,以及更新PyTorch和torchvision的版本来消除错误提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误提示:UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tensors. This means you can write to the underlying (supposedly non-writeable) NumPy array using the tensor. You may want to copy the array to protect its data or make it writeable before converting it to a tensor. This type of warning will be suppressed for the rest of this program. (Triggered internally at /pytorch/torch/csrc/utils/tensor_numpy.cpp:143.)
img = torch.as_tensor(np.asarray(pic))
翻译:transforms不需要要numpy转PIL。
出现警告代码片段:

transforms.Compose([
        transforms.Resize(256, interpolation=InterpolationMode.BILINEAR),
        transforms.PILToTensor(),
        transforms.ConvertImageDtype(torch.float),
    ])

解决方法1:
更改为:

transforms.Compose([
        transforms.Resize(256, interpolation=InterpolationMode.BILINEAR),
        transforms.ToTensor(),
        transforms.ConvertImageDtype(torch.float),
    ])

transforms.PILToTensor() ->>>>>transforms.ToTensor()
方法二:直接更新pytorch和torchvision。
我原来是pytorch1.8, torchvision0.9,更新到pytorch1.12.1 torchvision0.13.1,更新后就不会出现这个提示。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值