PyTorch - Torchvision - BrokenPipeError:[Errno 32]管道破裂

时间:2018-05-23 05:49:50

标签: python-3.x pytorch

我正在尝试使用PyTorch执行名为"Training a classifier"的教程。 试图调试这部分代码时:

import matplotlib.pyplot as plt
import numpy as np

# functions to show an image


def imshow(img):
    img = img / 2 + 0.5     # unnormalize
    npimg = img.numpy()
    plt.imshow(np.transpose(npimg, (1, 2, 0)))


# get some random training images
dataiter = iter(trainloader)
images, labels = dataiter.next()

# show images
imshow(torchvision.utils.make_grid(images))
# print labels
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))

我收到此错误消息:

Files already downloaded and verified Files already downloaded and verified 
Files already downloaded and verified Files already downloaded and verified Traceback (most recent call last):   
File "<string>", line 1, in <module>   
File "D:\Anaconda\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)   
File "D:\Anaconda\lib\multiprocessing\spawn.py", line 114, in _main
    prepare(preparation_data)   
File "D:\Anaconda\lib\multiprocessing\spawn.py", line 225, in prepare
    _fixup_main_from_path(data['init_main_from_path'])   
File "D:\Anaconda\lib\multiprocessing\spawn.py", line 277, in
_fixup_main_from_path
    run_name="__mp_main__")   
File "D:\Anaconda\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)   
File "D:\Anaconda\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)   
File "D:\Anaconda\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)   
File "d:\Yggdrasil\Programmation\PyTorch\TutorialCIFAR10.py", line 36, in <module>
    dataiter = iter(trainloader)   
File "D:\Anaconda\lib\site-packages\torch\utils\data\dataloader.py", line 451, in __iter__
    return _DataLoaderIter(self)   
File "D:\Anaconda\lib\site-packages\torch\utils\data\dataloader.py", line 239, in __init__
    w.start()   
File "D:\Anaconda\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)   
File "D:\Anaconda\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)     
File "D:\Anaconda\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)   
File "D:\Anaconda\lib\multiprocessing\popen_spawn_win32.py", line 33, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)   
File "D:\Anaconda\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
    _check_not_importing_main()   
File "D:\Anaconda\lib\multiprocessing\spawn.py", line 136, 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:\Yggdrasil\Programmation\PyTorch\TutorialCIFAR10.py", line 36, in <module>
    dataiter = iter(trainloader)   
 File "D:\Anaconda\lib\site-packages\torch\utils\data\dataloader.py", line 451, in __iter__
    return _DataLoaderIter(self)   
 File "D:\Anaconda\lib\site-packages\torch\utils\data\dataloader.py", line 239, in __init__
    w.start()   
 File "D:\Anaconda\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)   
 File "D:\Anaconda\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)   File "D:\Anaconda\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)   
 File "D:\Anaconda\lib\multiprocessing\popen_spawn_win32.py", line 65, in
__init__
    reduction.dump(process_obj, to_child)   
 File "D:\Anaconda\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj) 
 BrokenPipeError: [Errno 32] Broken pipe

本教程中的所有前几行都运行良好。

有人知道如何解决这个问题吗? 非常感谢提前

3 个答案:

答案 0 :(得分:7)

发生此问题是因为Windows无法在大于0的'num_workers'中运行此DataLoader。 您可以看到火车装载机来自哪里。我们可以看到

trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
                                      shuffle=True, num_workers=2)

我们需要将'num_workers'更改为0,如下所示:

trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
                                      shuffle=True, num_workers=0)

每个火车司机都需要像这样改变。

答案 1 :(得分:0)

这看起来不像PyTorch问题。尝试在Jupyter笔记本中执行代码和其他环境故障排除。

答案 2 :(得分:0)

得到相同的错误。以下变通办法适用于我:

def run():
   # code goes here

if __name__ == '__main__':
    run()