ValueError: some of the strides of a given numpy array are negative. This is currently not……

一、错误描述

  训练数据集时,写完数据增强的代码后,训练时出现了如下的报错:


ValueError: some of the strides of a given numpy array are negative. This is currently not……_第1张图片

  数据增强的代码如下:

pilimg_list = [Image.open(path) for path in path_list]   # path_list是数据存储路径
if self.contrast:
    p = np.random.uniform(0.5, 2)
    pilimg_list = [ImageEnhance.Contrast(pil_img).enhance(p)
                   for pil_img in pilimg_list]
npimg_list = [np.array(pil_img, np.float32) / 255 for pil_img in pilimg_list]


# Random flip
if self.flip and np.random.randint(2) == 0:
    npimg_list = [np.flip(npimg, axis=1) for npimg in npimg_list]


# Random gamma augmentation
if self.gamma:
    p = np.random.uniform(0.5, 2)
    npimg_list[0] = npimg_list[0] ** p

# Transpose to C x H x W
npimg_list = [
    np.expand_dims(npimg, axis=0) if npimg.ndim == 2 else npimg.transpose([2, 0, 1])
    for npimg in npimg_list]


if self.return_filenames:
    return tuple(torch.FloatTensor(npimg) for npimg in npimg_list) + \
        (self.fnames[idx], )
return tuple(torch.FloatTensor(npimg) for npimg in npimg_list)

二、错因分析

  代码报错的原因就在于Dataloader将numpy数组转为tensor时,要求内存是连续的;而上述代码中在进行flip后numpy数组的内存不再连续。
  解决办法是使用copy()对flip后的numpy进行拷贝,拷贝之后得到的数组内存是连续的,代码如下:

#  原代码
# Random flip
if self.flip and np.random.randint(2) == 0:
    npimg_list = [np.flip(npimg, axis=1) for npimg in npimg_list]

# 修改后的代码
# Random flip
if self.flip and np.random.randint(2) == 0:
    npimg_list = [np.flip(npimg, axis=1).copy() for npimg in npimg_list]

你可能感兴趣的:(程序BUG,python,深度学习)