Can't pickle local object 'generate_dataset.<locals>.<lambda>'

I got the following after I change my code to use multiprocessing and distributed dataparallel module.
Additionally I also started to use apex package for mixed precision computation.

-- Process 0 terminated with the following error:

Traceback (most recent call last):

File "/usr/local/lib/python3.5/dist-packages/torch/multiprocessing/spawn.py", line 19, in _wrap

fn(i, *args)

File "/app/train_action_model_apex.py", line 385, in main_worker

evaluate_model(args, root_dir)

File "/app/train_action_model_apex.py", line 343, in evaluate_model

dict_results = evaluator.inference()

File "/app/evaluators/action_model_evaluator.py", line 224, in inference

for data in self.dataloader:

File "/usr/local/lib/python3.5/dist-packages/torch/utils/data/dataloader.py", line 193, in __iter__

return _DataLoaderIter(self)

File "/usr/local/lib/python3.5/dist-packages/torch/utils/data/dataloader.py", line 469, in __init__

w.start()

File "/usr/lib/python3.5/multiprocessing/process.py", line 105, in start
self._popen = self._Popen(self)

File "/usr/lib/python3.5/multiprocessing/context.py", line 212, in _Popen

return _default_context.get_context().Process._Popen(process_obj)

File "/usr/lib/python3.5/multiprocessing/context.py", line 274, in _Popen

return Popen(process_obj)

File "/usr/lib/python3.5/multiprocessing/popen_spawn_posix.py", line 33, in __init__

super().__init__(process_obj)

File "/usr/lib/python3.5/multiprocessing/popen_fork.py", line 20, in __init__

self._launch(process_obj)

File "/usr/lib/python3.5/multiprocessing/popen_spawn_posix.py", line 48, in _launch

reduction.dump(process_obj, fp)

File "/usr/lib/python3.5/multiprocessing/reduction.py", line 59, in dump

ForkingPickler(file, protocol).dump(obj)

AttributeError: Can't pickle local object 'generate_dataset.<locals>.<lambda>'

It seems that the error is caused by lambda used for ThreeCrop transformation which mimics FiveCrop from pytorch. I follow the example code in the link.
I found that without ThreeCrop the error was not happened.
And before I used multiprocessing dataparallel, the error was not occured.

The code was tested on linux environment.

Could you try to write a transform class and replace the lambda method with it?
As far as I know there are some limitation in Python regarding pickling lambdas, which is apparently the case here.

3 Likes

Thanks for your help. Maybe I should write a new transform class as you suggested.