之前使用yolov5和yolov9,它们在训练时数据集与代码的目录是这样的:
以yolov5中coco128.yaml为例
# Example usage: python train.py --data coco128.yaml
# parent
# ├── yolov5
# └── datasets
# └── coco128 ← downloads here (7 MB)
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: ../datasets/coco128 # dataset root dir
train: images/train2017 # train images (relative to 'path') 128 images
val: images/train2017 # val images (relative to 'path') 128 images
test: # test images (optional)
但是yolov8是这样的
# parent
# ├── ultralytics
# └── datasets
# └── coco128 ← downloads here (7 MB)
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: ../datasets/coco128 # dataset root dir
train: images/train2017 # train images (relative to 'path') 128 images
val: images/train2017 # val images (relative to 'path') 128 images
test: # test images (optional)
datasets和代码放在一起,移植的时候很不方便。虽然也可以把path改为绝对路径,移植的时候还要改。
因此尝试将datasets不在代码的文件夹里,和yolov5或v9一样,即在parent中:
用户数数据集配置文件XXX.yaml
首先,用户数据集配置文件,存放路径和其他yaml文件放在一起,在
parent/yolov8-main(原文件夹名是ultralytics-main)/ultralytics/cfg/datasets
比如这个用户定义的WSY-seg.yaml (wsy是我师姐,感谢她把她标注的数据集分享给我)
# parent
# ├── yolov8
# └── datasets
# └── WSY-seg
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: ../datasets/WSY-seg # dataset root dir
train: images/train # train images (relative to 'path') 128 images
val: images/train # val images (relative to 'path') 128 images
test: # test images (optional)
# Classes
names:
0: embryo
1: cell
更改parent/yolov8-main/ultralytics/utils/__init__.py
第933行左右“datasets_dir”
self.defaults = {
"settings_version": version,
'''这里'''
"datasets_dir": FILE.parents[2],#str(datasets_root / "datasets"),
"weights_dir": str(root / "weights"),
"runs_dir": str(root / "runs"),
"uuid": hashlib.sha256(str(uuid.getnode()).encode()).hexdigest(),
"sync": True,
"api_key": "",
"openai_api_key": "",
"clearml": True, # integrations
"comet": True,
"dvc": True,
"hub": True,
"mlflow": True,
"neptune": True,
"raytune": True,
"tensorboard": True,
"wandb": False,
}
理由是:该文件开头定义了
FILE = Path(__file__).resolve()
FILE是当前目录即parent/yolo-v8.main/ultralytics/utils
我们找到它的“曾祖父目录”就是在parent
因此,WSY-seg.yaml中的
path: ../datasets/WSY-seg
在运行时就实际上是
path: parent/datasets/WSY-seg
替换其他位置同理。
而且在这段代码里,还可以把tensorboard和wandb关闭(修改为False即可),这样在训练过程中就不会弹出。
开始训练
在yolov8-main下创建train_seg.py,对照文档实例创建即可
配置 -Ultralytics YOLOv8 文档https://docs.ultralytics.com/zh/usage/cfg/#modes该方法中,也要用Path获取当前路径,这样这里也不会出现绝对路径了。
from ultralytics import YOLO
import sys, os
from pathlib import Path
"""获取当前的文件的绝对路径,并将父目录作为根目录(变量:ROOT),方便其他文件引用"""
FILE = Path(__file__).resolve()
ROOT = FILE.parents[0] # YOLOv8 root directory
if str(ROOT) not in sys.path:
sys.path.append(str(ROOT)) # add ROOT to PATH
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
# Load a model
model = YOLO("yolov8n-seg.pt") # load a pretrained model (recommended for training)
# Train the model
if __name__=="__main__":
'''多线程要在main中执行'''
results = model.train(data = ROOT/"ultralytics/cfg/datasets/WSY-seg.yaml", epochs=500, batch= 4, imgsz=640)
运行的时候会有警告:
WARNING ⚠️ Ultralytics settings reset to default values.
但好像没什么影响,忽略就好了。
之后可以正常训练了。移植时候保证datasets和yolov8-main在同一路径即可。