【解决错误】ValueError: some of the strides of a given numpy array are negative.

一、源码

def np2Tensor(l, rgb_range):
    def _np2Tensor(img):
        # if img.shape[2] == 3: # for opencv imread
        #     img = img[:, :, [2, 1, 0]]
        np_transpose = np.ascontiguousarray(img.transpose((2, 0, 1)), dtype=np.float32)
        tensor = torch.from_numpy(np_transpose).float()
        tensor.mul_(rgb_range / 255.)

        return tensor

    return [_np2Tensor(_l) for _l in l]

二、报错

  • 在复现SRFBN的时候遇到了一个报错,这里记录一下解决的过程,以为备忘!!!
  File "D:\source code\SRFBN_CVPR19\data\LRHR_dataset.py", line 41, in __getitem__
    lr_tensor, hr_tensor = common.np2Tensor([lr, hr], self.opt['rgb_range'])
  File "D:\source code\SRFBN_CVPR19\data\common.py", line 119, in np2Tensor
    return [_np2Tensor(_l) for _l in l]
  File "D:\source code\SRFBN_CVPR19\data\common.py", line 119, in <listcomp>
    return [_np2Tensor(_l) for _l in l]
  File "D:\source code\SRFBN_CVPR19\data\common.py", line 114, in _np2Tensor
    tensor = torch.from_numpy(np_transpose).float()
ValueError: some of the strides of a given numpy array are negative. This is currently not supported, but will be added in future releases.

三、原因

  • 这个原因是因为程序中操作的numpy中有使用负索引的情况

四、解决方案

  • **修改之处:**在np_transpose = np.ascontiguousarray(img.transpose((2, 0, 1)), dtype=np.float32)中指定了数据类型。
def np2Tensor(l, rgb_range):
    def _np2Tensor(img):
        # if img.shape[2] == 3: # for opencv imread
        #     img = img[:, :, [2, 1, 0]]
        np_transpose = np.ascontiguousarray(img.transpose((2, 0, 1)), dtype=np.float32)
        tensor = torch.from_numpy(np_transpose).float()
        tensor.mul_(rgb_range / 255.)

        return tensor

    return [_np2Tensor(_l) for _l in l]

五、参考

[1] ValueError: some of the strides of a given numpy array are negative. This is currently not supported
[2] Pytorch在训练过程中常见的问题
[3] pytorch报错——{ValueError}some of the strides of a given numpy array are negative.
[4] ValueError: some of the strides of a given numpy array are negative.的解决方法
[5] pytorch报错----------- ***ValueError: some of the strides of a given numpy array are negative.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值