Hi All,
I’m new to pytorch and trying to get into it, currently focusing on vision. I’ve been trying to add augmented images to my dataset, but I can’t seem to get contrast correction to work properly. I struggled for a while with having my grayscale images completely changed when applying any sort of contrast or brightness transformation until I figured out I had to perform this transformation before other transformations (why?).
I can now get the brightness adjusted using colorjitter, but when I apply the contrast transformation nothing happens.
I’m also getting a weird result that when I try to apply adjust_contrast directly on the image I get the following error:
Dimension out of range (expected to be in range of [-2, 1], but got -3)
Adjust brightness works (but completely changes the image, as before), so I’m really confused about this.
Here’s some code to showcase the issue:
mean = ([0.5])
std = ([0.2])
image_size = 256
data_transforms = transforms.Compose([transforms.Grayscale(num_output_channels=1),
transforms.Resize([image_size, image_size]),
transforms.ToTensor(),
transforms.Normalize(mean, std)])
ds_no_aug = datasets.ImageFolder(data_path/'val', data_transforms)
dl_no_aug = DataLoader(ds_no_aug, batch_size=1, shuffle = False, num_workers=4)
ds_aug = datasets.ImageFolder(data_path/'val', transforms.Compose([transforms.ColorJitter(contrast=0.7), data_transforms]))
dl_aug = DataLoader(ds_aug, batch_size=1, shuffle = False, num_workers=4)
example_no_aug = iter(dl_no_aug)
sample_no_aug,_ = next(example_no_aug)
example_aug = iter(dl_aug)
sample_aug, _ = next(example_aug)
im_no_aug = sample_no_aug[i][0]
im_aug = sample_aug[i][0]
plt.subplot(1,2,1)
plt.imshow(im_no_aug, cmap = 'gray')
plt.subplot(1,2,2)
plt.imshow(im_aug, cmap = 'gray')
And here’s the output:
Any ideas as to what I’m doing wrong will be very welcome!