Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

11.8. FAQ

11.8.1. To fix this issue, refer to the "Safe importing of main module"

运行训练代码提示

			
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.

        To fix this issue, refer to the "Safe importing of main module"
        section in https://docs.python.org/3/library/multiprocessing.html
			
			
			

解决方案,设置参数 workers=0

			
from ultralytics import YOLO
# 指定模型
mode = YOLO('yolo11n-seg.pt')
# 让模型开始训练
mode.train(data='dataset.yaml',workers=0,epochs=100,batch=10)
# 验证模型
mode.val()
			
			

11.8.2. ModuleNotFoundError: No module named 'onnx'

			
pip install onnx
			
			

11.8.3. 字体问题

			
rm -f ~/.config/Ultralytics/Arial.ttf ~/.config/Ultralytics/Arial.Unicode.ttf
rm -rf ~/.cache/matplotlib			
			
			

11.8.4. FileNotFoundError: [Errno 2]

			
FileNotFoundError: [Errno 2] No such file or directory: '/opt/datasets0503/val/images/cba31ec7-d0f3-4f78-9a7c-e9afc88576ff.npy'
			
			

这个错误通常不是缺少原始图片,而是 Ultralytics 读取数据集缓存时找不到对应的 .npy 缓存文件。

训练时如果启用了 cache=disk,Ultralytics 会把图片解码后保存为 .npy 文件,下次训练直接读取 .npy 来加速。如果数据集目录被移动、图片被删除、缓存文件被清理,或者 labels.cache 中还保留旧路径,就会出现上面的 FileNotFoundError。

解决方法是删除数据集缓存,让 Ultralytics 重新扫描图片和标签。

			
find /opt/datasets0503 -name "*.cache" -delete
find /opt/datasets0503 -name "*.npy" -delete
			
			

然后重新训练,先关闭磁盘缓存确认数据集没有问题。

			
yolo task=detect mode=train model=yolo11n.pt data=/opt/datasets0503/dataset.yaml epochs=100 cache=False
			
			

如果使用 Python 代码训练,同样设置 cache=False。

			
from ultralytics import YOLO

model = YOLO("yolo11n.pt")
model.train(data="/opt/datasets0503/dataset.yaml", epochs=100, cache=False)
			
			

如果仍然报错,需要检查 dataset.yaml 中 train、val 路径是否正确,以及 images 目录下的图片文件是否真实存在。

			
find /opt/datasets0503/val/images -type f | head
ls -l /opt/datasets0503/val/images/cba31ec7-d0f3-4f78-9a7c-e9afc88576ff.*
			
			

11.8.5. GPU 数量

			
python3 -c "import torch; print(torch.__version__, torch.version.cuda, torch.cuda.is_available(), torch.cuda.device_count())"