Can you set a dataset attribute using python formatting?

You can set the transforms of a dataset programatically like so:

for a_idx in range(128):
    train_loader.dataset.transforms=albumentations_transform_0

Is it possible to do this with Python formats? I tried this:

for a_idx in range(128):
    train_loader.dataset.transforms=f"albumentations_transform_{a_idx}"
TypeError: Caught TypeError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/torch/utils/data/_utils/worker.py", line 178, in _worker_loop
    data = fetcher.fetch(index)
  File "/opt/conda/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/opt/conda/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "<ipython-input-18-4a491769a098>", line 30, in __getitem__
    augmented = self.transforms(image=image)
TypeError: 'str' object is not callable

Python stores all the local objects in locals(), so you can do something as

def func():
    pass

locals().get('func')() # To run the function by string name

There is also globals(), which you can use instead as

my_globals = globals().copy()
my_globals.update(locals())

# then same as above

I may try that. I ended up doing this for now, which appears to work, but is crude:

for a_idx in range(128):
    exec(f"train_loader.dataset.transforms=albumentations_transform_{a_idx}")
1 Like