Increasing the number of channels with transform

I am using the EMNIST dataset which is in grayscale. I would like to include a line using transforms to get 3 channels.

transform=transforms.Compose([
                               transforms.Resize(image_size),
                               transforms.CenterCrop(image_size),
                               transforms.ToTensor(),
                               transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
                              ])

When I am using the dataloader I get this error:
RuntimeError: output with shape [1, 64, 64] doesn’t match the broadcast shape [3, 64, 64]

The code that generates this error is:

dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size,
                                         shuffle=True, num_workers=1)

device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu")

# Plot some training images
real_batch = next(iter(dataloader))
print(real_batch[0].shape)
plt.figure(figsize=(8,8))
plt.axis("off")
plt.title("Training Images")
plt.imshow(np.transpose(vutils.make_grid(real_batch[0].to(device)[:64], padding=2, normalize=True).cpu(),(1,2,0)))

Thanks in advance!

You cannot really autocolorize an image. Is there a way for you to change your first cnn to only have 1 input channel? If not you could look into making a custom transform and just copy the image and stack them. Here is a tutorial where it shows what a custom transform looks like.

1 Like

Thanks for your message. I solved with this (see below), but the link you provided it is very useful since I have to rotate the image and do a flip. Any suggestion to avoid creating a whole custom transform?

transform=transforms.Compose([
                               transforms.Resize(image_size),
                               transforms.CenterCrop(image_size),
                               transforms.ToTensor(),
                               transforms.Lambda(lambda x: x.repeat(3,1,1)),
                               transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
                              
                              ])

Yes you can look at the transforms here but there are some that rotate and flip an image. Examples are random rotation:

torchvision.transforms.RandomRotation(degrees, interpolation=<InterpolationMode.NEAREST: 'nearest'>, expand=False, center=None, fill=0, resample=None)

and random horizontal flip:

torchvision.transforms.RandomHorizontalFlip(p=0.5)
1 Like

Thank you very much @Dwight_Foster for your help. Just one last question. Where can I set the likelihood of the rotation to 100% to perform always a rotation? It hasn’t got a p parameter like the flip.

So basically how it works is that you give it a range of degrees and it picks a random degree in that range and rotates it to that degree. If you just want it to rotate to a specific degree each time you can just set the angle equal to that degree and it should always rotate there.

1 Like

It doesn’t seem to work. I already tried it without success. The original grid is shown below.

I want to display it in normal handwriting position. But after rotating 90 degrees this is what I get.

Can you send your new transforms. You might have to set the angle to a tuple like (90, 90) because if it just says 90 it will interpret that as 0 to 90.

1 Like

Problem solved. Thanks a million. The transform that solved the problem was this:

transform=transforms.Compose([
                               transforms.Resize(image_size),
                               transforms.CenterCrop(image_size),
                               transforms.ToTensor(),
                               transforms.Lambda(lambda x: x.repeat(3,1,1)),
                               transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
                               transforms.RandomHorizontalFlip(p=1),
                               transforms.RandomRotation((90,90),expand=False, center=None, fill=0, resample=None),
                               
                              ])