How to use RGBA or 4 channel images in a DCGAN model in pytorch

Hello, many thanks to all contributors in advance.

I am trying to apply pytorch’s DCGAN model however when I try to run the dataset it defaults to 3 channel images even though my images are 4 channel RGBA.

How to use RGBA or 4 channel images in a DCGAN model in pytorch?


dataset = dset.ImageFolder(root=dataroot,
                           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)),
                           ]))
# Create the dataloader
dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size,
                                         shuffle=True, num_workers=workers)

This is the code I use to generate the dataset, I would like to modify it to be able to use my images with format [647, 4, 256,256]

You would need to modify the first conv layer of the model which I guess would currently accept tensors with 3 channels.
However, are you sure the alpha channel contains any (useful) information and is not just set to the highest value for all pixels? In the latter case, I would claim you can directly remove this channel as it doesn’t contain any information.

When executing my code without any correction, I get the following error

Given groups=1, weight of size [32, 4, 4, 4], expected input[1, 3, 256, 256] to have 4 channels, but got 3 channels instead 

This is because dataset.dset.ImageFolder is converting the images to 3 channels, how can I make it read all four channels?

The information of the alpha channel is of supreme importance since the images are physical representations of the axes X, Y, Z, Z’ of a physical magnitude.

Thank you very much for taking the time to respond!

You could provide a custom loader to ImageFolder as the default would transform the images to RGB as seen here.