How to transform data in a flexible way?

Currently, it seems only one way to transforms the data, in ImageFolder.
But it is so inconvenient for different purposes.
For example, I want to import the data first, and then transform it, so I want to perform data transforms on tensor.
Could it be realized in the next release? I wish a flexible method to transform data.

Iā€™m not sure I understand what you ask. Transforms can be use flexibly, like so:

    from torchvision.transforms import transforms
    from yourdataset import yourdatasetclass

    tf = transforms.ToTensor()
    dset = yourdatasetclass(root, transforms=tf)
    dloader =, batch_size=2,shuffle=True,num_workers=2)

You can use transforms.Compose to chain your transforms sequentially.

What is the yourdatasetclass?
Can I define it by myself?
It seems like importing dataset from root. Is it ImageFolder?
I want to import dataset first, and then transform it in the tensor.

you can define ImageFolder by yourself, it is very easy. This is the source code, you only need to define init, get_item and len, it is subclass of data.Dataset

1 Like

Thanks for your kind answer~
It is indeed an effective solution.
Besides, I conceive that a more flexible transform operation should be added to the new release for convenience.

I see!
I can define mydatasetclass reference to dset.CIFAR10(root, train=True, transform=None, target_transform=None, download=False).
Thank you very much!

Datasets are really simple.
Transforms are also very flexible.

See for tutorial on both datasets and transforms.