pytorch报错——{ValueError}some of the strides of a given numpy array are negative.

报错

pytorch使用cv2读取图片,并转换成RGB顺序的时候,在用torch.from_numpy转换成tensor的时候,报错:{ValueError}some of the strides of a given numpy array are negative. This is currently not supported, but will be added in future releases.

解决方案

图像颜色通道转换之后,使用copy(),然后在转换成tensor

参考代码

img = cv2.imread(imgpath)
img = img[:,:,::-1].copy()
img_tensor = torch.from_numpy(img)

参考

  1. Pytorch在训练过程中常见的问题

你可能感兴趣的:(pytorch)