# Can't pickle local object 'DataLoader.\_\_init\_\_.\<locals\>.\<lambda\>'

**URL:** <https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857>\
**Category:** vision\
**Created:** [December 11, 2018, 12:03pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857 "2018-12-11T12:03:49Z")\
**Posts on this page:** 13\
**Page:** 2

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [September 10, 2020, 7:00pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/21 "2020-09-10T19:00:01Z")

</div>

Do you get a different error after removing the lambda? Because the error is explicitly about a lambda which cannot be serialized.

---

<div class="post-metadata">

**Author:** ![asura](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/asura/32/28647_2.png) [@asura](https://discuss.pytorch.org/u/asura)\
**Post date:** [September 10, 2020, 8:08pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/22 "2020-09-10T20:08:26Z")

</div>

@albanD No, it’s the same error. I have also made sure that no lambda functions remain in the code.

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [September 10, 2020, 9:39pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/23 "2020-09-10T21:39:38Z")

</div>

If the error still points at a lambda function. Then you have one left. If it points to something else, you can share it here so that we can take a look.

---

<div class="post-metadata">

**Author:** ![asura](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/asura/32/28647_2.png) [@asura](https://discuss.pytorch.org/u/asura)\
**Post date:** [September 11, 2020, 3:41am UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/24 "2020-09-11T03:41:31Z")

</div>

@albanD I have reproduced the error here : [https://colab.research.google.com/drive/10Zxe40Tl14fWAaCAYg1fgQA13hHIiBo\_?usp=sharing](https://colab.research.google.com/drive/10Zxe40Tl14fWAaCAYg1fgQA13hHIiBo_?usp=sharing)

The error is :

```auto
AttributeError Traceback (most recent call last)
<ipython-input-6-adca9abddcf0> in <module>()
    168 if __name__ == ' __main__':
    169 t1 = time.time()
--> 170 main()
    171 print("Time taken :", time.time() - t1)

5 frames
<ipython-input-6-adca9abddcf0> in main(ways, shots, meta_lr, fast_lr, meta_batch_size, adaptation_steps, num_iterations, cuda, seed)
    126 args = [maml, tasksets]
    127 with Pool(4) as pool:
--> 128 values = pool.map(partial(ParallelTasks, args), list(range(meta_batch_size)))
    129 
    130 #for i in range(values):

/usr/lib/python3.6/multiprocessing/pool.py in map(self, func, iterable, chunksize)
    264 in a list that is returned.
    265 '''
--> 266 return self._map_async(func, iterable, mapstar, chunksize).get()
    267 
    268 def starmap(self, func, iterable, chunksize=None):

/usr/lib/python3.6/multiprocessing/pool.py in get(self, timeout)
    642 return self._value
    643 else:
--> 644 raise self._value
    645 
    646 def _set(self, i, obj):

/usr/lib/python3.6/multiprocessing/pool.py in _handle_tasks(taskqueue, put, outqueue, pool, cache)
    422 break
    423 try:
--> 424 put(task)
    425 except Exception as e:
    426 job, idx = task[:2]

/usr/lib/python3.6/multiprocessing/connection.py in send(self, obj)
    204 self._check_closed()
    205 self._check_writable()
--> 206 self._send_bytes(_ForkingPickler.dumps(obj))
    207 
    208 def recv_bytes(self, maxlength=None):

/usr/lib/python3.6/multiprocessing/reduction.py in dumps(cls, obj, protocol)
     49 def dumps(cls, obj, protocol=None):
     50 buf = io.BytesIO()
---> 51 cls(buf, protocol).dump(obj)
     52 return buf.getbuffer()
     53 

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

```

---

<div class="post-metadata">

**Author:** ![asura](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/asura/32/28647_2.png) [@asura](https://discuss.pytorch.org/u/asura)\
**Post date:** [September 11, 2020, 5:15am UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/25 "2020-09-11T05:15:11Z")

</div>

@albanD The same error is seen in Linux environment too.

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [September 12, 2020, 3:06pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/26 "2020-09-12T15:06:38Z")

</div>

GIven the names, I guess the problem is that the taskset you’re using cannot be serialized. And so you cannot use it in the process Pool.  
You can either unpack these in a different object that you can serialize or change the libary to make the taskset serializable.

---

<div class="post-metadata">

**Author:** ![asura](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/asura/32/28647_2.png) [@asura](https://discuss.pytorch.org/u/asura)\
**Post date:** [September 12, 2020, 5:13pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/27 "2020-09-12T17:13:20Z")

</div>

Ah ok, I will try that approach. That’s for the insights @albanD. 😁  
That might be the case it seems.

---

<div class="post-metadata">

**Author:** ![asura](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/asura/32/28647_2.png) [@asura](https://discuss.pytorch.org/u/asura)\
**Post date:** [September 18, 2020, 1:05pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/28 "2020-09-18T13:05:27Z")

</div>

I will post my solution here in case someone also faces the same error. As @albanD said, the error was because dataset couldn’t be serialized. I used `import dill` and the error was solved. In case someone is still facing issue, they can try this too `from pathos.multiprocessing import ProcessingPool as Pool`. 🙂

---

<div class="post-metadata">

**Author:** ![lillepeder](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lillepeder/32/29921_2.png) [@lillepeder](https://discuss.pytorch.org/u/lillepeder)\
**Post date:** [October 16, 2020, 3:32pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/29 "2020-10-16T15:32:59Z")

</div>

Dear @Asura, in which file(s) specifically did you include the `import dill` statement?

---

<div class="post-metadata">

**Author:** ![asura](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/asura/32/28647_2.png) [@asura](https://discuss.pytorch.org/u/asura)\
**Post date:** [October 16, 2020, 5:14pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/30 "2020-10-16T17:14:54Z")

</div>

Hi @lillepeder, I was using Colab and added it in the top cell.  
But, I suppose it will work as long as you add it the file where serialization is taking place.  
Otherwise, try the later approach, `pathos` already has `dill`, so you won’t need to figure out where to import it specifically.

---

<div class="post-metadata">

**Author:** ![lillepeder](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lillepeder/32/29921_2.png) [@lillepeder](https://discuss.pytorch.org/u/lillepeder)\
**Post date:** [October 16, 2020, 5:17pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/31 "2020-10-16T17:17:22Z")

</div>

I found an alternative solution: pass `num_workers=0` into the DataLoader, in case anyone else get a similar problem! A little slower, but it overcomes the pickling issue.

---

<div class="post-metadata">

**Author:** ![lillepeder](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lillepeder/32/29921_2.png) [@lillepeder](https://discuss.pytorch.org/u/lillepeder)\
**Post date:** [October 16, 2020, 5:18pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/32 "2020-10-16T17:18:34Z")

</div>

Thank you for the quick reply, but I seem to have solved it for now with `num_workers=0` 🙂

---

<div class="post-metadata">

**Author:** ![Brando\_Miranda](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/brando_miranda/32/14355_2.png) [@Brando\_Miranda](https://discuss.pytorch.org/u/Brando_Miranda)\
**Post date:** [November 3, 2020, 9:14pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/33 "2020-11-03T21:14:46Z")

</div>

I also had this error in my mac with this transform:

```auto
self.train_transform = transforms.Compose([
                lambda x: Image.fromarray(x),
                transforms.RandomCrop(84, padding=8),
                transforms.ColorJitter(brightness=0.4, contrast=0.4, saturation=0.4),
                transforms.RandomHorizontalFlip(),
                lambda x: np.asarray(x),
                transforms.ToTensor(),
                self.normalize
            ])

```

I don’t understand why there is pickling going on…seems weird. But I will try to change the things above to non-lambdas. I will first try:

```auto
lambda x: Image.fromarray(x) ---> Image.fromarray

```

hopefully that works.

* * *

**yes it did work for me.**

[Previous page](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857.md?page=1)
