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/", line 19, in _wrap

fn(i, *args)

File "/app/", line 385, in main_worker

evaluate_model(args, root_dir)

File "/app/", line 343, in evaluate_model

dict_results = evaluator.inference()

File "/app/evaluators/", line 224, in inference

for data in self.dataloader:

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

return _DataLoaderIter(self)

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


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

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

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

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

return Popen(process_obj)

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


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


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

reduction.dump(process_obj, fp)

File "/usr/lib/python3.5/multiprocessing/", 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.


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