我们先来看看Pycharm给出的报错
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "E:\Software\Anaconda\envs\yolov8\Lib\multiprocessing\spawn.py", line 122, in spawn_main
exitcode = _main(fd, parent_sentinel)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\Software\Anaconda\envs\yolov8\Lib\multiprocessing\spawn.py", line 131, in _main
prepare(preparation_data)
File "E:\Software\Anaconda\envs\yolov8\Lib\multiprocessing\spawn.py", line 246, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "E:\Software\Anaconda\envs\yolov8\Lib\multiprocessing\spawn.py", line 297, 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 "E:\DeepLearning\ultralytics-main\train-MoonCake.py", line 9, in <module>
model.train(data='E:/DeepLearning/ultralytics-main/ultralytics/cfg/datasets/MoonCake.yaml', epochs=100, imgsz=640)
File "E:\DeepLearning\ultralytics-main\ultralytics\engine\model.py", line 341, in train
self.trainer.train()
File "E:\DeepLearning\ultralytics-main\ultralytics\engine\trainer.py", line 191, in train
self._do_train(world_size)
File "E:\DeepLearning\ultralytics-main\ultralytics\engine
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.
我原始【train.py】代码如下
from ultralytics import YOLO
# Load a model
model = YOLO('E:/DeepLearning/ultralytics-main/ultralytics/cfg/models/v8/MoonCake.yaml') # build a new model from YAML
#model = YOLO('yolov8n.pt') # load a pretrained model (recommended for training)
#model = YOLO('yolov8n.yaml').load('yolov8n.pt') # build from YAML and transfer weights
# Train the model
model.train(data='E:/DeepLearning/ultralytics-main/ultralytics/cfg/datasets/MoonCake.yaml', epochs=100, imgsz=640)
关于这个报错,其实是因为代码出现了一个【多进程】的错误。这个错误可呢,其实编译器已经告诉我们了,是我们启动线程的方式不对。
根据pycharm给出的提示,我们将代码修改为
from ultralytics import YOLO
if __name__ == '__main__':
# Load a model
model = YOLO('E:/DeepLearning/ultralytics-main/ultralytics/cfg/models/v8/MoonCake.yaml') # build a new model from YAML
#model = YOLO('yolov8n.pt') # load a pretrained model (recommended for training)
#model = YOLO('yolov8n.yaml').load('yolov8n.pt') # build from YAML and transfer weights
# Train the model
model.train(data='E:/DeepLearning/ultralytics-main/ultralytics/cfg/datasets/MoonCake.yaml', epochs=100, imgsz=640)
接着,就可以直接训练了,以下截图是我开始训练的截图。
[3]Python Multiprocessing Traceback (most recent call last) - Stack Overflow