0

I'm currently following a PyTorch tutorial, and I'm trying to get this example to work. However, upon running the line dataiter = iter(trainloader) my program first does something I don't understand how it's possible – it seems to jump backwards in the program and re-run lines of codes which it has already run even though there is no loop. Then it runs into a BrokenPipeError and crashes.

Here is a minimal working example that triggers the problem:

import torch
import torchvision
import torchvision.transforms as transforms

transform = transforms.Compose([transforms.ToTensor(),
                                transforms.Normalize((0.5, 0.5, 0.5),
                                                     (0.5, 0.5, 0.5))])
trainset = torchvision.datasets.CIFAR10(root='./data',
                                        train=True,
                                        download=True,
                                        transform=transform)
trainloader = torch.utils.data.DataLoader(trainset,
                                          batch_size=4,
                                          shuffle=True,
                                          num_workers=2)
dataiter = iter(trainloader)

and this is the output I'm getting:

"D:\Program Files\Python35\python.exe" D:/work/programs/pytorch/60_minute_blitz_tutorial/3_training_a_classifier/MWE.py
Files already downloaded and verified
Files already downloaded and verified
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "D:\Program Files\Python35\lib\multiprocessing\spawn.py", line 106, in spawn_main
    exitcode = _main(fd)
  File "D:\Program Files\Python35\lib\multiprocessing\spawn.py", line 115, in _main
    prepare(preparation_data)
  File "D:\Program Files\Python35\lib\multiprocessing\spawn.py", line 226, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "D:\Program Files\Python35\lib\multiprocessing\spawn.py", line 278, in _fixup_main_from_path
    run_name="__mp_main__")
  File "D:\Program Files\Python35\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "D:\Program Files\Python35\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "D:\Program Files\Python35\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\work\programs\pytorch\60_minute_blitz_tutorial\3_training_a_classifier\MWE.py", line 16, in <module>
    dataiter = iter(trainloader)
  File "D:\Program Files\Python35\lib\site-packages\torch\utils\data\dataloader.py", line 451, in __iter__
    return _DataLoaderIter(self)
  File "D:\Program Files\Python35\lib\site-packages\torch\utils\data\dataloader.py", line 239, in __init__
    w.start()
  File "D:\Program Files\Python35\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)
  File "D:\Program Files\Python35\lib\multiprocessing\context.py", line 212, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "D:\Program Files\Python35\lib\multiprocessing\context.py", line 313, in _Popen
    return Popen(process_obj)
  File "D:\Program Files\Python35\lib\multiprocessing\popen_spawn_win32.py", line 34, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "D:\Program Files\Python35\lib\multiprocessing\spawn.py", line 144, in get_preparation_data
    _check_not_importing_main()
  File "D:\Program Files\Python35\lib\multiprocessing\spawn.py", line 137, in _check_not_importing_main
    is not going to be frozen to produce an executable.''')
RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.
Traceback (most recent call last):
  File "D:/work/programs/pytorch/60_minute_blitz_tutorial/3_training_a_classifier/MWE.py", line 16, in <module>
    dataiter = iter(trainloader)
  File "D:\Program Files\Python35\lib\site-packages\torch\utils\data\dataloader.py", line 451, in __iter__
    return _DataLoaderIter(self)
  File "D:\Program Files\Python35\lib\site-packages\torch\utils\data\dataloader.py", line 239, in __init__
    w.start()
  File "D:\Program Files\Python35\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)
  File "D:\Program Files\Python35\lib\multiprocessing\context.py", line 212, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "D:\Program Files\Python35\lib\multiprocessing\context.py", line 313, in _Popen
    return Popen(process_obj)
  File "D:\Program Files\Python35\lib\multiprocessing\popen_spawn_win32.py", line 66, in __init__
    reduction.dump(process_obj, to_child)
  File "D:\Program Files\Python35\lib\multiprocessing\reduction.py", line 59, in dump
    ForkingPickler(file, protocol).dump(obj)
BrokenPipeError: [Errno 32] Broken pipe

Process finished with exit code 1

The output "Files already downloaded and verified" should only be printed once – when creating trainset – but it's printed twice.

Why is this? And why am I getting a BrokenPipeError? And most importantly, how do I fix this?

I'm running Windows 10, Python 3.5.3, CUDA 8.0.60 and a PyTorch 0.4.0 that I have installed by running

pip install http://download.pytorch.org/whl/cu80/torch-0.4.0-cp35-cp35m-win_amd64.whl
pip install torchvision

as instructed by the PyTorch webpage (well, they use pip3 instead of pip, but that shouldn't make any difference since Python 3.5 is the only Python version I have installed).

HelloGoodbye
  • 3,624
  • 8
  • 42
  • 57
  • 1
    In Windows, you need to wrap your code with if __name__ == '__main__': block. https://stackoverflow.com/questions/50701690/pytorch-tutorial-error-training-a-classifier/50716024#50716024 – Tyathalae Jun 14 '18 at 11:24
  • @Tyathalae Thanks, that was the cause of the problem! I marked this question as a duplicate of that one. – HelloGoodbye Jun 14 '18 at 17:33

0 Answers0