我是机器学习和PyTorch的新手。我使用imgaug库进行图像增强(https://github.com/aleju/imgaug)
我有以下代码:
class ImgAugTransform: def __init__(self): self.aug = seq = iaa.Sequential( [ # 对大多数图像应用以下增强器 iaa.Fliplr(0.5), # 水平翻转50%的图像 iaa.Flipud(0.2), # 垂直翻转20%的图像 random_aug_use(iaa.CropAndPad( # 裁剪图像高度/宽度的-5%到10% percent=(-0.1, 0.2), pad_mode=ia.ALL, pad_cval=(0.,255) )), random_aug_use(iaa.Affine( scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # 缩放图像到其尺寸的80-120%,每个轴独立 translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # 平移-20%到+20%(每个轴) rotate=(-45, 45), # 旋转-45到+45度 shear=(-16, 16), # 剪切-16到+16度 order=[0, 1], # 使用最近邻或双线性插值(快速) cval=(0, 255), # 如果模式为常量,使用0到255之间的cval mode=ia.ALL # 使用scikit-image的任何变形模式(参见顶部第二张图片的示例) )) ], random_order=True) def __call__(self, img): img = np.array(img) return self.aug.augment_image(img)train_transforms = ImgAugTransform()train_dataset = torchvision.datasets.ImageFolder(train_dir, train_transforms)train_dataloader = torch.utils.data.DataLoader( train_dataset, batch_size=batch_size, shuffle=True, num_workers=batch_size)
所以现在我不能这样做:
X_batch, y_batch = next(iter(train_dataloader))
我得到错误:
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数组再次变得连续。尝试修改你的增强器代码为:
def __call__(self, img): img = np.array(img) return np.ascontiguousarray(self.aug.augment_image(img))