一、源码
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.