使用 PaddleDetection和W&B进行目标检测
了解如何使用PaddleDetection从头训练YOLOX模型,同时使用W&B记录所有指标和模型检查点
Created on June 21|Last edited on August 14
Comment
目录(点击展开)
PaddleDetection 简介
PaddleDetection 是一种基于 PaddlePaddle 的端到端目标检测开发工具包。它在模块化设计中采用了各种主流的目标检测、实例分割、追踪和关键点检测算法,具有可配置模块,如:网络组件、数据增强和损失等。
PaddleDetection现配备有内置W&B集成,可记录您的所有训练和验证指标,以及模型检查点及其相应元数据。
您可通过以下两种方式为训练作业激活W&B记录器:
- 命令行:W&B记录器参数前必须带有 -o,且每个参数必须使用前缀“wandb-”,且应该使用`--use_wandb`标记。
python tools/train -c config.yml --use_wandb -o wandb-project=MyDetector wandb-entity=MyTeam wandb-save_dir=./logs
- YAML文件:将参数添加到配置YAML文件中wandb heade下,如下所示
wandb:project: MyProjectentity: MyTeamsave_dir: ./logs
完成设置
安装W&B SDK
首先我们来安装wandb SDK,然后登陆W&B 账户:
pip install wandbwandb login
安装 PaddleDetection
我们接下来克隆 PaddleDetection库并从源安装程序包,因为其中还会包含一个训练脚本以及多种预实现的模型配置!
pip install paddlepaddle-gpu pyclipper attrdict gdown -qqqgit clone https://github.com/PaddlePaddle/PaddleDetectioncd PaddleDetectionpip install -e .
如何下载COCO数据集
数据集已被记录为W&BArtifact,故可轻松下载。其中包含 1000张用于训练的图像和250张用于验证的带相应标注的图像,现在我们将使用这些来训练目标检测模型。
artifact = wandb.Api().artifact("manan-goel/PaddleDetectionYOLOX/COCOSubset:latest")path = artifact.download(root='./dataset/coco')
训练
训练模型
现在使用PaddleDetection库中的训练脚本来训练YOLOX模型。上面的配置在训练期间添加了W&B日志记录。同时我们还添加了 --eval 标记,以便每 5 个epoch进行一次评估步骤。
使用CLI
python tools/train.py -c configs/yolox/yolox_nano_300e_coco.yml --use_wandb -o wandb-project=PaddleDetectionYOLOX --eval
使用配置YAML文件
为了在训练计划安排中使用W&B自动启动实验追踪,可在配置YAML文件中加入以下代码片段,然后将其作为训练脚本的一个输入。
wandb:project: PaddleDetectionYOLOX
可视化
追踪训练和验证指标
在训练期间,训练集和验证集的指标将像这样记录至W&B仪表板上:
追踪系统指标
从W&B下载最佳模型💾
模型检查点已被记录为W&B artifact,所以可使用以下代码片段下载以供评估。
import wandbartifact = wandb.Api().artifact('manan-goel/PaddleDetectionYOLOX/model-26oqc38r:best', type='model')artifact_dir = artifact.download()
图像测试
下面的代码段使用从W&B拉取的YOLOX模型对 demo 目录中的所有图像运行了 PaddleDetection 资源库中的推理脚本并将其存储在了 infer_output 目录中
for i in $(ls demo/*.jpg)dopython tools/infer.py -c configs/yolox/yolox_nano_300e_coco.yml \--infer_img=$i \--output_dir=infer_output/ \--draw_threshold=0.5 \-o weights=./artifacts/model-26oqc38r:v1/modeldone
这将检索demo目录中的所有jpg文件,并使用下载的模型对其运行推理脚本并将标注图像存储在 infer_output 目录中。
奖励内容:在您的W&B仪表板中记录标注的图像
import globimport wandbwandb.init(project="PaddleDetectionYOLOX")wandb.use_artifact('manan-goel/PaddleDetectionYOLOX/model-26oqc38r:best')table = wandb.Table(columns=["Input Image", "Annotated Image"])inp_imgs = sorted(glob.glob("./demo/*.jpg"), key=lambda x: x.split("/")[-1])out_imgs = sorted(glob.glob("./infer_output/*.jpg"), key=lambda x: x.split("/")[-1])for inp in inp_imgs:for out in out_imgs:if out.split("/")[-1] != inp.split("/")[-1]:continuetable.add_data(wandb.Image(inp),wandb.Image(out))wandb.log({"Predictions": table})wandb.finish()
上述脚本将把输入图像和标记图像记录到一个W&B表格中,样式大致如下
Run set
16
结论
推荐阅读
YOLOv5 Object Detection on Windows (Step-By-Step Tutorial)
This tutorial guides you through installing and running YOLOv5 on Windows with PyTorch GPU support. Includes an easy-to-follow video and Google Colab.
Search and Rescue: Augmentation and Preprocessing on Drone-Based Water Rescue Images With YOLOv5
In this article, we look at achieving mAP scores of over 0.97 on large images with comparatively very small people, as used in drone-based water rescue.
Add a comment
Iterate on AI agents and models faster. Try Weights & Biases today.