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):

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

for a_idx in range(128):
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/", line 178, in _worker_loop
    data = fetcher.fetch(index)
  File "/opt/conda/lib/python3.6/site-packages/torch/utils/data/_utils/", 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/", 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():

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

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

my_globals = globals().copy()

# 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):
1 Like