赞
踩
mytrain.py文件:
from ultralytics import YOLO
import multiprocessing
# 加载一个模型
model = YOLO('yolov8n.yaml') # 从yaml构建一个新模型
model = YOLO('weights/yolov8n.pt') # 加载预先训练的模型(建议进行训练)
model = YOLO('yolov8n.yaml').load('weights/yolov8n.pt') # 根据YAML构建并传递权重
results = model.train(data='datas/my_data.yaml', epochs=100, imgsz=640)
canning D:\myDesktop\ultralytics-main2\datas\labels\train.cache... 1720 images, 0 backgrounds, 0 corrupt: 100%|██████████| 1720/1720 [00:00<?, ?it/s] Traceback (most recent call last): File "<string>", line 1, in <module> File "D:\MyTools\mylab\lib\multiprocessing\spawn.py", line 116, in spawn_main exitcode = _main(fd, parent_sentinel) File "D:\MyTools\mylab\lib\multiprocessing\spawn.py", line 125, in _main prepare(preparation_data) File "D:\MyTools\mylab\lib\multiprocessing\spawn.py", line 236, in prepare _fixup_main_from_path(data['init_main_from_path']) File "D:\MyTools\mylab\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path main_content = runpy.run_path(main_path, File "D:\MyTools\mylab\lib\runpy.py", line 265, in run_path return _run_module_code(code, init_globals, run_name, File "D:\MyTools\mylab\lib\runpy.py", line 97, in _run_module_code _run_code(code, mod_globals, init_globals, File "D:\MyTools\mylab\lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "D:\myDesktop\ultralytics-main2\mytrain.py", line 12, in <module> results = model.train(data='datas/my_data.yaml', epochs=100, imgsz=640) File "D:\myDesktop\ultralytics-main2\ultralytics\engine\model.py", line 338, in train self.trainer.train() File "D:\myDesktop\ultralytics-main2\ultralytics\engine\trainer.py", line 190, in train self._do_train(world_size) File "D:\myDesktop\ultralytics-main2\ultralytics\engine\trainer.py", line 286, in _do_train self._setup_train(world_size) File "D:\myDesktop\ultralytics-main2\ultralytics\engine\trainer.py", line 251, in _setup_train self.train_loader = self.get_dataloader(self.trainset, batch_size=batch_size, rank=RANK, mode='train') File "D:\myDesktop\ultralytics-main2\ultralytics\models\yolo\detect\train.py", line 52, in get_dataloader return build_dataloader(dataset, batch_size, workers, shuffle, rank) # return dataloader File "D:\myDesktop\ultralytics-main2\ultralytics\data\build.py", line 107, in build_dataloader return InfiniteDataLoader(dataset=dataset, File "D:\myDesktop\ultralytics-main2\ultralytics\data\build.py", line 33, in __init__ self.iterator = super().__iter__() File "D:\MyTools\mylab\lib\site-packages\torch\utils\data\dataloader.py", line 438, in __iter__ return self._get_iterator() File "D:\MyTools\mylab\lib\site-packages\torch\utils\data\dataloader.py", line 386, in _get_iterator return _MultiProcessingDataLoaderIter(self) File "D:\MyTools\mylab\lib\site-packages\torch\utils\data\dataloader.py", line 1039, in __init__ w.start() File "D:\MyTools\mylab\lib\multiprocessing\process.py", line 121, in start self._popen = self._Popen(self) File "D:\MyTools\mylab\lib\multiprocessing\context.py", line 224, in _Popen return _default_context.get_context().Process._Popen(process_obj) File "D:\MyTools\mylab\lib\multiprocessing\context.py", line 327, in _Popen return Popen(process_obj) File "D:\MyTools\mylab\lib\multiprocessing\popen_spawn_win32.py", line 45, in __init__ prep_data = spawn.get_preparation_data(process_obj._name) File "D:\MyTools\mylab\lib\multiprocessing\spawn.py", line 154, in get_preparation_data _check_not_importing_main() File "D:\MyTools\mylab\lib\multiprocessing\spawn.py", line 134, 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.
运行时错误:
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.
翻译:
RuntimeError:试图在当前进程完成其引导阶段之前启动新进程。这可能意味着您没有使用fork来启动子进程,并且忘记在主模块中使用正确的习惯用法:if __name__=='__main__':freeze_support()如果程序不会被冻结以生成可执行文件,则可以省略“freeze_support()”行。
解决方案:
这个错误通常是由于在Windows系统上使用了多进程模块而引起的。在Windows上,多进程模块需要满足:脚本必须包含一个if __name__ == '__main__':的条件判断,以确保只有在主模块中才会执行多进程相关的代码。我的脚本没有正确设置这些条件,导致在启动新进程之前,当前进程的引导阶段尚未完成。
正确的完整代码:
from ultralytics import YOLO
import multiprocessing
# 加载一个模型
def main():
model = YOLO('yolov8n.yaml') # 从yaml构建一个新模型
model = YOLO('weights/yolov8n.pt') # 加载预先训练的模型(建议进行训练)
model = YOLO('yolov8n.yaml').load('weights/yolov8n.pt') # 根据YAML构建并传递权重
results = model.train(data='datas/my_data.yaml', epochs=100, imgsz=640)
if __name__ == '__main__':
multiprocessing.freeze_support()
main()
也就是说如果没有下面红色方框里的代码,会报错
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。