Concat image datasets with different size and number of channels

Good evening everyone,

I got a problem with loading multiple datasets and did not find a solution so far.
For loading multiple datasets into one dataloader I can use the ConcatDataset class, but how do I concatenate e.g. CIFAR10 and MNIST. For this I would have CIFAR10 to resize and convert to grayscale without applying the transformations to MNIST.

Any ideas on how to do that?

You could specify the transformations for each dataset.
This code should work:

mnist = datasets.MNIST(
    root='/home/ptrblck/python/data',
    download=False,
    transform=transforms.Compose([
        transforms.ToTensor()
    ])
)

cifar = datasets.CIFAR10(
    root='/home/ptrblck/python/data',
    download=False,
    transform=transforms.Compose([
        transforms.Grayscale(),
        transforms.Resize(28),
        transforms.ToTensor()
    ])
)

dataset = ConcatDataset((mnist, cifar))

As you can see transforms.Resize and transforms.Grayscale was only applied for the CIFAR10 dataset.

Oh yes of course, I am really a blockhead. :grin:
Thanks!

Can you concatenate the targets using this approach? If not, how do we go about it?

ConcatDataset will concatenate all passed Datasets keeping their samples and targets.