UserWarning: The given NumPy array is not writable, and PyTorch does not support non-writable tensors.
修改方式:
Instead of using
img = transforms.ToTensor()(img)
Use this:
img = transforms.ToTensor()(np.array(img))
ToTensor()接收 PIL 对象或 numpy 数组。因此,将 pil 数组显式更改为 numpy 数组就可以了。