当前位置: 首页 > 工具软件 > TENS > 使用案例 >

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

庞修贤
2023-12-01

错误提示: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,更新后就不会出现这个提示。

 类似资料:

相关阅读

相关文章

相关问答