# 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:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![arslansadiq](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@arslansadiq](https://discuss.pytorch.org/u/arslansadiq)\
**Post date:** [December 11, 2018, 12:03pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/1 "2018-12-11T12:03:49Z")

</div>

Hi all,  
I hope everybody reading this is having a great day.

So I have a problem with **torchvision.transforms.Lambda()** function when used with python function: **enumerate**. I am using it to make my uni-channeled image into multi-channeled tensor. It works fine and produce data loader instance for torchvision datasets, but when I instantiate the batch’s index with the **command enumerate(\<batch\_name\>)** it given the following error:

```auto

  File "C:\Users\Arsalan\Desktop\project\mlmi\trainer\trainer.py", line 63, in _train_epoch
    for batch_idx, (data, target) in enumerate(self.data_loader):

  File "C:\Users\Arsalan\Anaconda3\lib\site-packages\torch\utils\data\dataloader.py", line 501, in __iter__
    return _DataLoaderIter(self)

  File "C:\Users\Arsalan\Anaconda3\lib\site-packages\torch\utils\data\dataloader.py", line 289, in __init__
    w.start()

  File "C:\Users\Arsalan\Anaconda3\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)

  File "C:\Users\Arsalan\Anaconda3\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)

  File "C:\Users\Arsalan\Anaconda3\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)

  File "C:\Users\Arsalan\Anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 65, in __init__
    reduction.dump(process_obj, to_child)

  File "C:\Users\Arsalan\Anaconda3\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)

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

```

On a side note:

This lambda transform works fine on my linux machine but on windows it is giving this problem.

Would anybody be kind enough to tell me what is problem is

---

<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:** [December 11, 2018, 12:15pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/2 "2018-12-11T12:15:37Z")

</div>

@peterjc123 lambda objects cannot be pickled on Windows?

---

<div class="post-metadata">

**Author:** ![arslansadiq](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@arslansadiq](https://discuss.pytorch.org/u/arslansadiq)\
**Post date:** [December 11, 2018, 12:18pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/3 "2018-12-11T12:18:09Z")

</div>

yes on windows it can’t be be at the moment.  
Do you know any solutions?

---

<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:** [December 11, 2018, 12:19pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/4 "2018-12-11T12:19:30Z")

</div>

> [@arslansadiq](#):
>
> yes on windows it can be at the moment.

So they can be pickled?  
The error then does not come from the fact that you try to pickle a lambda function?

---

<div class="post-metadata">

**Author:** ![arslansadiq](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@arslansadiq](https://discuss.pytorch.org/u/arslansadiq)\
**Post date:** [December 11, 2018, 12:21pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/5 "2018-12-11T12:21:59Z")

</div>

Sorry I miss-typed, my earlier comment is edited

---

<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:** [December 11, 2018, 12:22pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/6 "2018-12-11T12:22:50Z")

</div>

I guess you will need to change `NotMNISTDataLoader` and use a proper function there instead of a lambda.

---

<div class="post-metadata">

**Author:** ![arslansadiq](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@arslansadiq](https://discuss.pytorch.org/u/arslansadiq)\
**Post date:** [December 11, 2018, 12:25pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/7 "2018-12-11T12:25:52Z")

</div>

so, for my problem:  
transforming uni-channeled image into multi-channeled tensor, what can I do?  
because this works fine on linux, but for windows users what’s the alternative.

for example if my batch size is **tensor(128, 32, 32)**, after transformation I want **tensor(128, 3, 32, 32)**

---

<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:** [December 11, 2018, 12:27pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/8 "2018-12-11T12:27:47Z")

</div>

Do you have the code for the ` __init__ ` of `NotMNISTDataLoader` ? There is a lamda function in there like `lambda x: x+1` that needs to be changed to

```auto
def tmp_func(x):
    return x + 1

```

And replace the lambda by `tmp_func`.

---

<div class="post-metadata">

**Author:** ![arslansadiq](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@arslansadiq](https://discuss.pytorch.org/u/arslansadiq)\
**Post date:** [December 11, 2018, 12:28pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/9 "2018-12-11T12:28:31Z")

</div>

Here you go:

```auto
class NotMNISTDataLoader(BaseDataLoader):
    """
    NotMnist data loading demo using BaseDataLoader
    """
    def __init__ (self, data_dir, batch_size, shuffle, validation_split, num_workers, training=True):
        print("NotMnist is used")
        self.data_dir = data_dir + "NotMNIST/"
        
        trsfm = transforms.Compose([
            transforms.Resize((32, 32)),
            transforms.ToTensor(),
            transforms.Normalize((0.1307,), (0.3081,)),
            transforms.Lambda(lambda x: x.repeat(3, 1, 1))
            ])
       
        self.dataset = NotMNIST(self.data_dir, train = training, download = True, transform = trsfm)
        super(NotMNISTDataLoader, self). __init__ (self.dataset, batch_size, shuffle, validation_split, num_workers)

```

---

<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:** [December 11, 2018, 12:29pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/10 "2018-12-11T12:29:29Z")

</div>

```auto
        trsfm = transforms.Compose([
            transforms.Resize((32, 32)),
            transforms.ToTensor(),
            transforms.Normalize((0.1307,), (0.3081,)),
            transforms.Lambda(lambda x: x.repeat(3, 1, 1))
            ])

```

Should become

```auto
        def tmp_func(x):
            return x.repeat(3, 1, 1)

        trsfm = transforms.Compose([
            transforms.Resize((32, 32)),
            transforms.ToTensor(),
            transforms.Normalize((0.1307,), (0.3081,)),
            transforms.Lambda(tmp_func)
            ])

```

---

<div class="post-metadata">

**Author:** ![arslansadiq](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@arslansadiq](https://discuss.pytorch.org/u/arslansadiq)\
**Post date:** [December 11, 2018, 12:34pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/11 "2018-12-11T12:34:18Z")

</div>

Thank you so much man 🙂  
It’s working now.  
Have a great day.

By the way can you tell me what’s the problem was before, why it can’t be pickled on windows but does on linux?

---

<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:** [December 11, 2018, 12:37pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/12 "2018-12-11T12:37:27Z")

</div>

I have no idea 😕 That why I pinged peterjc which know windows much better.

---

<div class="post-metadata">

**Author:** ![arslansadiq](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@arslansadiq](https://discuss.pytorch.org/u/arslansadiq)\
**Post date:** [December 11, 2018, 12:38pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/13 "2018-12-11T12:38:05Z")

</div>

Ok thank you for your help.  
Let’s hope he replies

---

<div class="post-metadata">

**Author:** ![peterjc123](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/peterjc123/32/2790_2.png) [@peterjc123](https://discuss.pytorch.org/u/peterjc123)\
**Post date:** [December 11, 2018, 2:06pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/14 "2018-12-11T14:06:46Z")

</div>

No, it is not supported on Windows. The reason is that multiprocessing lib doesn’t have it implemented on Windows. There are some alternatives like dill that can pickle more objects.

---

<div class="post-metadata">

**Author:** ![peterjc123](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/peterjc123/32/2790_2.png) [@peterjc123](https://discuss.pytorch.org/u/peterjc123)\
**Post date:** [December 11, 2018, 2:07pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/15 "2018-12-11T14:07:59Z")

</div>

Could you please do me a favor and write this one into the official Windows doc?

---

<div class="post-metadata">

**Author:** ![arslansadiq](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@arslansadiq](https://discuss.pytorch.org/u/arslansadiq)\
**Post date:** [December 11, 2018, 2:32pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/16 "2018-12-11T14:32:59Z")

</div>

Excuse my ignorance on the matter, but would you like to tell me how exactly can one use dill for .  
I tried installing that too but that didn’t solve the problem either. May be I am not using it right and I have to make some changes in my code in order to use it.  
Can you tell me about it’s usage to solve my problem?

---

<div class="post-metadata">

**Author:** ![arslansadiq](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@arslansadiq](https://discuss.pytorch.org/u/arslansadiq)\
**Post date:** [December 11, 2018, 2:38pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/17 "2018-12-11T14:38:27Z")

</div>

You mean here:

[Windows Discussion Forum](https://answers.microsoft.com/en-us/windows/forum?sort=LastReplyDate&dir=Desc&tab=All&status=all&mod=&modAge=&advFil=&postedAfter=&postedBefore=&threadType=All&isFilterExpanded=false&page=1)  
Or if I am wrong, would you share the link?

---

<div class="post-metadata">

**Author:** ![peterjc123](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/peterjc123/32/2790_2.png) [@peterjc123](https://discuss.pytorch.org/u/peterjc123)\
**Post date:** [December 12, 2018, 5:09am UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/18 "2018-12-12T05:09:06Z")

</div>

No, things should be done at the backend. I don’t think we will introduce dill to resolve this problem. So what @albanD said is the correct solution, at least for now. As for the document, it can be accessed [here](https://pytorch.org/docs/master/notes/windows.html) and modified [here](https://github.com/pytorch/pytorch/blob/master/docs/source/notes/windows.rst).

---

<div class="post-metadata">

**Author:** ![sarthakpati](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sarthakpati/32/27366_2.png) [@sarthakpati](https://discuss.pytorch.org/u/sarthakpati)\
**Post date:** [May 11, 2020, 8:22pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/19 "2020-05-11T20:22:35Z")

</div>

Hi,

Has there been any update on this in the official repo? I just ran into this issue on the latest Torch from Conda.

Cheers!

---

<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, 6:54pm UTC](https://discuss.pytorch.org/t/cant-pickle-local-object-dataloader-init-locals-lambda/31857/20 "2020-09-10T18:54:11Z")

</div>

Hi @albanD, I am facing the same issue while using Pool method. As you suggested, I removed lamda functions. Still the error is persisting. (I am using Windows, I used Colab to try the same code, but it is also showing the same error there … ) Can you suggest, what can be done?

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