准备工作

详细内容见:训练自定义数据

从 Kaggle 下载 数据集

kaggle datasets download -d andrewmvd/face-mask-detection

解压到 root(可自行决定):

# 数据集根目录
root = r"/home/znjs/xinet/ai/face-mask-detection"
# 标签名称
names = ['with_mask', 'mask_weared_incorrect', 'without_mask']

导入一些库:

from pathlib import Path

from tools.file import ObjectPath
  1. 将数据集划分为训练集(\(80\%\))、验证集(\(20\%\))。

  2. 从验证集取出若干(如 show_count=5)作为测试集。

dataset = ObjectPath(root, names)
dataset.split(show_count=5)

查看测试集:

dataset.images_test
['/home/znjs/xinet/ai/face-mask-detection/images/maksssksksss753.png',
 '/home/znjs/xinet/ai/face-mask-detection/images/maksssksksss18.png',
 '/home/znjs/xinet/ai/face-mask-detection/images/maksssksksss76.png',
 '/home/znjs/xinet/ai/face-mask-detection/images/maksssksksss581.png',
 '/home/znjs/xinet/ai/face-mask-detection/images/maksssksksss17.png']
dataset.labels_test
['/home/znjs/xinet/ai/face-mask-detection/annotations/maksssksksss753.xml',
 '/home/znjs/xinet/ai/face-mask-detection/annotations/maksssksksss18.xml',
 '/home/znjs/xinet/ai/face-mask-detection/annotations/maksssksksss76.xml',
 '/home/znjs/xinet/ai/face-mask-detection/annotations/maksssksksss581.xml',
 '/home/znjs/xinet/ai/face-mask-detection/annotations/maksssksksss17.xml']

复制图片数据

new_root = 'datasets' # 目标目录
images_dir = f"{new_root}/images"

将图片数据按照已经划分好的规则复制到新的目录:

dataset.copy_images(images_dir)

制作标签数据

labels_dir = f"{new_root}/labels"

标签需要归一化:

dataset.write_labels(labels_dir)

制作 YAML

from tools.file import make_yaml
root = 'datasets'
config_name = 'face-mask'
# 标签名称
names = ['with_mask', 'mask_weared_incorrect', 'without_mask']
make_yaml(root, names, config_name)

克隆仓库

ROOT = 'yolov5/' # YOLOv5 根目录

if not Path(ROOT).exists():
    !git clone https://github.com.cnpmjs.org/ultralytics/yolov5.git {ROOT}
#!pip3 install -qr {yolo_path}requirements.txt
正克隆到 'yolov5'...
remote: Enumerating objects: 10555, done.
remote: Total 10555 (delta 0), reused 0 (delta 0), pack-reused 10555
接收对象中: 100% (10555/10555), 10.74 MiB | 5.13 MiB/s, 完成.
处理 delta 中: 100% (7292/7292), 完成.