Neighbourhood
Neighbourhood

Reputation: 336

PyTorch augmentation

i'm new to machine learning and pytorch. I'm using imgaug library for images augmentation (https://github.com/aleju/imgaug)

I have this code:

class ImgAugTransform:
    def __init__(self):
        self.aug = seq = iaa.Sequential(
            [
                # Apply the following augmenters to most images
                iaa.Fliplr(0.5), # horizontally flip 50% of all images
                iaa.Flipud(0.2),  # vertically flip 20% of all images
                random_aug_use(iaa.CropAndPad( # crop images by -5% to 10% of their height/width
                    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)}, # scale images to 80-120% of their size, individually per axis
                    translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
                    rotate=(-45, 45), # rotate by -45 to +45 degrees
                    shear=(-16, 16), # shear by -16 to +16 degrees
                    order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
                    cval=(0, 255), # if mode is constant, use a cval between 0 and 255
                    mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
                ))
            ], 
            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)

So now i cant do this:

X_batch, y_batch = next(iter(train_dataloader))

I get error:

ValueError: some of the strides of a given numpy array are negative. This is currently not supported, but will be added in future releases.

Upvotes: 0

Views: 1255

Answers (2)

André Pacheco
André Pacheco

Reputation: 1885

I came across this error as well. The solution that worked to me was:

def __call__(self, img):
        img = np.array(img)
        return self.aug.augment_image(img).copy()

But, if you're composing imgaug with torchvision.transforms you can do something like:

def __call__(self, img):
  img = self.aug.augment_image(np.array(img))
  transforms = torchvision.transforms.Compose([
               torchvision.transforms.ToTensor(),
               torchvision.transforms.Normalize(self.normalization[0], 
               self.normalization[1]),
              ])
  return transforms(img.copy())

Upvotes: 1

Shai
Shai

Reputation: 114786

You should make your augmented numpy arrays contiguous again. try modifying your augmenter code to:

    def __call__(self, img):
        img = np.array(img)
        return np.ascontiguousarray(self.aug.augment_image(img))

Upvotes: 0

Related Questions