pytorch报错“some of the strides of a given numpy array are negative“

在运行pytorch程序时,出现了如下的错误:
在这里插入图片描述
经过查找资料,发现是Opencv读取图片时的原因,原始报错代码片段:

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

修改后的代码:

img = cv2.imread(imgpath)
img = img[:,:,::-1].copy()
img_tensor = torch.from_numpy(img)
posted @ 2022-11-13 22:35  dlhl  阅读(22)  评论(0)    收藏  举报