问题如下:
Windows10 运行 YOLOv8 出现如下错误:
Traceback (most recent call last):
File “”, line 1, in
File “D:\anaconda3\envs\yolov8\Lib\multiprocessing\spawn.py”, line 116, in spawn_main
exitcode = _main(fd, parent_sentinel)
^^^^^^^^^^^^^^^^^^^^^^^^^
File “D:\anaconda3\envs\yolov8\Lib\multiprocessing\spawn.py”, line 125, in _main
prepare(preparation_data)
…
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "D:\anaconda3\envs\yolov8\Lib\multiprocessing\spawn.py", line 122, in spawn_main
exitcode = _main(fd, parent_sentinel)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\anaconda3\envs\yolov8\Lib\multiprocessing\spawn.py", line 131, in _main
prepare(preparation_data)
File "D:\anaconda3\envs\yolov8\Lib\multiprocessing\spawn.py", line 242, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "D:\anaconda3\envs\yolov8\Lib\multiprocessing\spawn.py", line 293, in _fixup_main_from_path
main_content = runpy.run_path(main_path,
^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen runpy>", line 291, in run_path
File "<frozen runpy>", line 98, in _run_module_code
File "<frozen runpy>", line 88, in _run_code
File "F:\workspace\yolov8\train.py", line 8, in <module>
results = model.train(data="fsd.yaml", epochs=200, batch=16)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\anaconda3\envs\yolov8\Lib\site-packages\ultralytics\engine\model.py", line 377, in train
self.trainer.train()
File "D:\anaconda3\envs\yolov8\Lib\site-packages\ultralytics\engine\trainer.py", line 192, in train
self._do_train(world_size)
File "D:\anaconda3\envs\yolov8\Lib\site-packages\ultralytics\engine\trainer.py", line 294, in _do_train
self._setup_train(world_size)
File "D:\anaconda3\envs\yolov8\Lib\site-packages\ultralytics\engine\trainer.py", line 259, in _setup_train
self.train_loader = self.get_dataloader(self.trainset, batch_size=batch_size, rank=RANK, mode='train')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\anaconda3\envs\yolov8\Lib\site-packages\ultralytics\models\yolo\detect\train.py", line 40, in get_dataloader
return build_dataloader(dataset, batch_size, workers, shuffle, rank) # return dataloader
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\anaconda3\envs\yolov8\Lib\site-packages\ultralytics\data\build.py", line 101, in build_dataloader
return InfiniteDataLoader(dataset=dataset,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\anaconda3\envs\yolov8\Lib\site-packages\ultralytics\data\build.py", line 29, in __init__
self.iterator = super().__iter__()
^^^^^^^^^^^^^^^^^^
File "D:\anaconda3\envs\yolov8\Lib\site-packages\torch\utils\data\dataloader.py", line 441, in __iter__
return self._get_iterator()
^^^^^^^^^^^^^^^^^^^^
File "D:\anaconda3\envs\yolov8\Lib\site-packages\torch\utils\data\dataloader.py", line 388, in _get_iterator
return _MultiProcessingDataLoaderIter(self)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\anaconda3\envs\yolov8\Lib\site-packages\torch\utils\data\dataloader.py", line 1042, in __init__
w.start()
File "D:\anaconda3\envs\yolov8\Lib\multiprocessing\process.py", line 121, in start
self._popen = self._Popen(self)
^^^^^^^^^^^^^^^^^
File "D:\anaconda3\envs\yolov8\Lib\multiprocessing\context.py", line 224, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\anaconda3\envs\yolov8\Lib\multiprocessing\context.py", line 336, in _Popen
return Popen(process_obj)
^^^^^^^^^^^^^^^^^^
File "D:\anaconda3\envs\yolov8\Lib\multiprocessing\popen_spawn_win32.py", line 45, in __init__
prep_data = spawn.get_preparation_data(process_obj._name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\anaconda3\envs\yolov8\Lib\multiprocessing\spawn.py", line 160, in get_preparation_data
_check_not_importing_main()
File "D:\anaconda3\envs\yolov8\Lib\multiprocessing\spawn.py", line 140, in _check_not_importing_main
raise RuntimeError('''
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.
修改
D:\anaconda3\envs\yolov8\Lib\site-packages\ultralytics\data
路径下的 build.py
文件里的函数 build_dataloader
, 将 nw = min([os.cpu_count() // max(nd, 1), batch if batch > 1 else 0, workers]) # number of workers
直接修改为 nw=0
def build_dataloader(dataset, batch, workers, shuffle=True, rank=-1):
"""Return an InfiniteDataLoader or DataLoader for training or validation set."""
batch = min(batch, len(dataset))
nd = torch.cuda.device_count() # number of CUDA devices
# nw = min([os.cpu_count() // max(nd, 1), batch if batch > 1 else 0, workers]) # number of workers
nw = 0 # update by jw 20230815
.....
【参考】
解决RuntimeError: An attempt has been made to start a new process before…办法
python进程 - 调试报错 you are not using fork to start your child processes
WINDOWS FAQ-Usage (multiprocessing)