Pytorch Data Loader doesn't work for multiple workers

username: pytorchneophyte
cooked: <p>I’m trying to load images from the GTSRB dataset for image classification training. The data loading works when i set number of workers = 0. However, when I set it to anything more than 0, I get an error. The code is below.</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader, sampler
import torchvision
from torchvision import transforms, utils
import torchvision.utils as vutils
import torchvision.datasets as dset
from torch.autograd import Variable

workers = 4
batch_size = 128
image_size = 32
num_epochs = 15
num_classes = 43
num_gpu = 1

trainingClassifierRoot =  '../GTSRB/Final_Training/Images/'
testClassifierRoot = '../GTSRB/Online-Test/'


# Create the dataset
trainClassifierDataset = dset.ImageFolder(root=trainingClassifierRoot,
                           transform=transforms.Compose([
                               transforms.Resize(image_size),
                               transforms.CenterCrop(image_size),
                               transforms.ToTensor(),
                               transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
                           ]))

testClassifierDataset = dset.ImageFolder(root=testClassifierRoot,
                           transform=transforms.Compose([
                               transforms.Resize(image_size),
                               transforms.CenterCrop(image_size),
                               transforms.ToTensor(),
                               transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
                           ]))

# Create the dataloader
trainClassifierLoader = torch.utils.data.DataLoader(trainClassifierDataset, batch_size=batch_size, shuffle=True, num_workers=workers)
testClassifierLoader = torch.utils.data.DataLoader(testClassifierDataset, batch_size=batch_size, shuffle=True, num_workers=workers)
dataiter = iter(trainClassifierLoader)
</code></pre>
<p>And the error I get is below</p>
<pre><code class="lang-auto"> File "&lt;ipython-input-28-71c601936f6e&gt;", line 72, in &lt;module&gt;
    dataiter = iter(trainClassifierLoader)

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

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

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

  File "C:\ProgramData\Anaconda3\envs\Python3_5\lib\multiprocessing\context.py", line 212, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)

  File "C:\ProgramData\Anaconda3\envs\Python3_5\lib\multiprocessing\context.py", line 313, in _Popen
    return Popen(process_obj)

  File "C:\ProgramData\Anaconda3\envs\Python3_5\lib\multiprocessing\popen_spawn_win32.py", line 34, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)

  File "C:\ProgramData\Anaconda3\envs\Python3_5\lib\multiprocessing\spawn.py", line 173, in get_preparation_data
    main_mod_name = getattr(main_module.__spec__, "name", None)

AttributeError: module '__main__' has no attribute '__spec__'
</code></pre>
<p>Any ideas on what is causing this?</p>

username: ptrblck
cooked: <p>It seems so be a bug in Spyder as described <a href="https://github.com/spyder-ide/spyder/issues/8128" rel="nofollow noopener">here</a>.<br>
Could this be the case here?<br>
If so, could you try to set the options (current console instead of dedicated console)?<br>
I’m not sure, if this bug was already fixed in the current Spyder release, but you could also try to update it.</p>

username: Weenie
cooked: <p>Hi, I also meet this problem. Have you found the solution?</p>