首页 > 解决方案 > 如何修复 RuntimeError:在当前进程完成引导阶段之前尝试启动新进程?

问题描述

我正在学习 fastai,并且经常遇到“[Errno 32] Broken pipe”错误,我在 pip 和 conda 中有必要的软件包,并且还运行 Intel i5-2520M 2.5GHz,Windows 10 64 位,Python

3.7.4

,以及Pytorch的CPU版本,因为我没有专用的GPU,以及最新的Pytorch

torch           1.3.0+cpu
torchvision     0.4.1+cpu

还有法泰

fastai          1.0.58

在我的环境中在 conda 中启动脚本后,我在终端中得到以下信息

------->> biwi_head_pose\09\frame_00667_rgb.jpg
---- Done Gathering Data
epoch     train_loss  valid_loss  time
------->> biwi_head_pose\09\frame_00667_rgb.jpg--------------------------| 0.00% [0/237 00:00<00:00]
---- Done Gathering Data
epoch     train_loss  valid_loss  time
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\multiprocessing\spawn.py", line 114, in _main
    prepare(preparation_data)
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\multiprocessing\spawn.py", line 225, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
    run_name="__mp_main__")
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\Eric\Desktop\fastai Practice\L3\HeadPose.py", line 39, in <module>
    learn.lr_find()
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\site-packages\fastai\train.py", line 41, in lr_find
    learn.fit(epochs, start_lr, callbacks=[cb], wd=wd)
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\site-packages\fastai\basic_train.py", line 200, in fit
    fit(epochs, self, metrics=self.metrics, callbacks=self.callbacks+callbacks)
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\site-packages\fastai\basic_train.py", line 99, in fit
    for xb,yb in progress_bar(learn.data.train_dl, parent=pbar):
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\site-packages\fastprogress\fastprogress.py", line 72, in __iter__
    for i,o in enumerate(self._gen):
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\site-packages\fastai\basic_data.py", line 75, in __iter__
    for b in self.dl: yield self.proc_batch(b)
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\site-packages\torch\utils\data\dataloader.py", line 193, in __iter__
    return _DataLoaderIter(self)
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\site-packages\torch\utils\data\dataloader.py", line 469, in __init__
    w.start()
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\multiprocessing\process.py", line 112, in start
    self._popen = self._Popen(self)
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\multiprocessing\context.py", line 223, in _Popen
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
Traceback (most recent call last):
  File "HeadPose.py", line 39, in <module>
    learn.lr_find()
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\site-packages\fastai\train.py", line 41, in lr_find
    learn.fit(epochs, start_lr, callbacks=[cb], wd=wd)
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\site-packages\fastai\basic_train.py", line 200, in fit
    fit(epochs, self, metrics=self.metrics, callbacks=self.callbacks+callbacks)
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\site-packages\fastai\basic_train.py", line 99, in fit
    for xb,yb in progress_bar(learn.data.train_dl, parent=pbar):
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\site-packages\fastprogress\fastprogress.py", line 72, in __iter__
    for i,o in enumerate(self._gen):
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\site-packages\fastai\basic_data.py", line 75, in __iter__
return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\multiprocessing\context.py", line 322, in _Popen
    for b in self.dl: yield self.proc_batch(b)
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\site-packages\torch\utils\data\dataloader.py", line 193, in __iter__
    return Popen(process_obj)
      File "C:\Users\Eric\Anaconda3\envs\fastai\lib\multiprocessing\popen_spawn_win32.py", line 46, in __init__
return _DataLoaderIter(self)
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\site-packages\torch\utils\data\dataloader.py", line 469, in __init__
    w.start()
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\multiprocessing\process.py", line 112, in start
    self._popen = self._Popen(self)
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\multiprocessing\popen_spawn_win32.py", line 89, in __init__
        reduction.dump(process_obj, to_child)prep_data = spawn.get_preparation_data(process_obj._name)

  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\multiprocessing\reduction.py", line 60, in dump
  File "C:\Users\Eric\Anaconda3\envs\fastai\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
    _check_not_importing_main()
  File "C:\Users\Eric\Anaconda3\envs\fastai\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.
    ForkingPickler(file, protocol).dump(obj)
BrokenPipeError: [Errno 32] Broken pipe

正如你所看到的 Errno 32 打开了,我环顾四周,人们说把它放在if __name__ == '__main__':训练代码之前,但是当我这样做时,错误消失了,但它没有训练。

所以这就是我所做的(HeadPose.py),首先我们从路径中获取数据并将图像转换为坐标进行训练

from fastai.vision import *
import matplotlib.pyplot as plt

# Get Data & Convert to Matrices
path = 'C:\\Users\\Eric\\Desktop\\fastai Practice\\L3\\'
cal = np.genfromtxt(path+'biwi_head_pose\\01\\rgb.cal', skip_footer=6); cal
fname = 'biwi_head_pose\\09\\frame_00667_rgb.jpg'

def img2txt_name(f): return path+f'{str(f)[:-7]}pose.txt'
img=open_image(path+fname)
#img.show()

ctr = np.genfromtxt(img2txt_name(fname), skip_header=3); ctr
def convert_biwi(coords):
    c1 = coords[0] * cal[0][0]/coords[2] + cal[0][2]
    c2 = coords[1] * cal[1][1]/coords[2] + cal[1][2]
    return tensor([c2,c1])

def get_ctr(f):
    ctr = np.genfromtxt(img2txt_name(f), skip_header=3)
    return convert_biwi(ctr)

def get_ip(img,pts): return ImagePoints(FlowField(img.size, pts), scale=True)
get_ctr(fname)
ctr = get_ctr(fname)
#img.show(y=get_ip(img,ctr), figsize=(6,6))

然后此代码从文件biwi_head_pose夹中的文件创建一个数据集

# Creating a dataset
data = (PointsItemList.from_folder('biwi_head_pose')
        .split_by_valid_func(lambda o: o.parent.name=='13')
        .label_from_func(get_ctr)
        .transform(get_transforms(), tfm_y=True, size=(120,160))
        .databunch().normalize(imagenet_stats))

data.show_batch(3, figsize=(9,6))
print('---- Done Gathering Data')

这是使用卷积神经网络训练数据的代码。

#Train model
learn = cnn_learner(data, models.resnet34)
learn.lr_find()
learn.recorder.plot()
lr = 2e-2
learn.fit_one_cycle(5, slice(lr))
print('---- Saving as 3-stage-1')
learn.save(p+'3-stage-1')
print('---- Saved!')
print('---- Loading 3-stage-1')
learn.load(p+'3-stage-1');
learn.show_results()
print('---- Done!')
plt.show()

我正在关注这个 fastai github 页面中的教程: https ://github.com/fastai/course-v3/blob/master/nbs/dl1/lesson3-head-pose.ipynb

标签: pythonpytorchfast-ai

解决方案


推荐阅读