Setting transforms attribute of dataset not working after dataset has been initialized

Hello, I want to set the transform attribute of a Dataset object once I have already instantiated the object - I try to do that using the setattr method of python, but it does not work - below is a minimum working example.

import torch
import numpy as np
from import Dataset
import torchvision.transforms as transforms
from import TensorDataset

trans = transforms.Compose([transforms.Resize(size=224), transforms.CenterCrop(size=(224, 224)), transforms.ToTensor(), transforms.Normalize(mean=(0.48145466, 0.4578275, 0.40821073), std=(0.26862954, 0.26130258, 0.27577711))])
images = torch.randn(50000, 3, 32, 32)
labels = torch.randint(9, (50000,1))
dset = TensorDataset(images, labels)
setattr(dset, 'transform', trans)
for idx, (x, y) in enumerate(dset):

The output of this snippet gives me -
torch.Size([3, 32, 32])

but rather, we should be getting
torch.Size([3, 224, 224])

why is setting attribute externally not working?

Please let me know if I am missing anything and thanks for your time!

TensorDataset doesn’t have and use a self.transform attribute and will just directly index the passed tensors as seen here.
So technically you are creating this attribute, which is just never used.

Thank you! But, isnt TensorDataset inherited from Dataset, and hence it must have the transform attribute?

It it inherited from Dataset but this base class also doesn’t define the transform attribute as seen in its definition.

Oh, thank you, that clears it!

With that being said, the easiest approach to apply a transformation on each tensor would be by creating a custom Dataset.

1 Like