DeepVAC-compliant Yolov5 implementation
本项目实现了符合DeepVAC规范的Yolov5
项目依赖
- deepvac >= 0.6.0
- pytorch >= 1.9.0
- torchvision >= 0.10.0
1. 阅读DeepVAC规范
可以粗略阅读,建立起第一印象
使用Deepvac规范指定Docker镜像
- 获取coco2017数据集
浏览器操作:
coco2017labels.zip
train2017.zip
val2017.zip
test2017.zip
命令行操作:
curl -L https://github.com/ultralytics/yolov5/releases/download/v1.0/coco2017labels.zip -o coco2017labels.zip
mkdir -p data/coco
unzip -q coco2017labels -d data/coco
rm coco2017labels.zip
-
解压coco2017数据集
-
数据集配置 在config.py文件中作如下配置:
from deepvac import AttrDict, new
# new("your train class name")
config = new("Yolov5Train")
# train dataset
train_sample_path = "data/coco/images/train2017"
train_target_path = "data/coco/instances_train2017.json"
config.core.Yolov5Train.train_dataset = Yolov5MosaicDataset(config, train_sample_path, train_target_path, config.core.Yolov5Train.img_size, config.core.Yolov5Train.border)
# val dataset
val_sample_path = "data/coco/images/val2017"
val_target_path = "data/coco/instances_val2017.json"
config.core.Yolov5Train.val_dataset = Yolov5Dataset(config, val_sample_path, val_target_path, config.core.Yolov5Train.img_size)
# test dataset
config.core.Yolov5Test = AttrDict()
config.core.Yolov5Test.test_sample_path = "your test images dir"
- 如果是自己的数据集,那么必须要符合标准coco标注格式
- 指定预训练模型路径(config.core.Yolov5Train.model_path)
yolov5s & yolov5l 提取码: g4tu - 指定训练分类数量(config.core.Yolov5Train.class_num)
- 是否采用混合精度训练(config.core.Yolov5Train.amp)
- 是否采用ema策略(config.core.Yolov5Train.ema)
- 是否采用梯度积攒到一定数量在进行反向更新梯度策略(config.core.Yolov5Train.nominal_batch_factor)
- dataloader相关配置(config.core.Yolov5Train.num_workers)
config.core.Yolov5Train.model_path = "output/pretrained.pth"
config.core.Yolov5Train.class_num = 80
config.core.Yolov5Train.amp = False
config.core.Yolov5Train.ema = True
# define ema_decay with other func
# config.ema_decay = lambda x: 0.9999 * (1 - math.exp(-x / 2000))
config.core.Yolov5Train.nominal_batch_factor = 4
config.core.Yolov5Train.shuffle = True
config.core.Yolov5Train.batch_size = 16
config.core.Yolov5Train.num_workers = 8
config.core.Yolov5Train.pin_memory = True
执行命令:
python3 train.py
- 测试相关配置
config.core.Yolov5Test.device = "cuda"
config.core.Yolov5Test.class_num = 80
config.core.Yolov5Test.img_size = 640
config.core.Yolov5Test.half = False
config.core.Yolov5Test.show_output_dir = "output/show"
config.core.Yolov5Test.iou_thres = 0.45
config.core.Yolov5Test.conf_thres = 0.25
config.core.Yolov5Test.idx2cat = ["cls{}".format(i) for i in range(config.core.Yolov5Test.class_num)]
- 运行测试脚本:
# 方法1
config.core.Yolov5Test.model_path = <trained-model>
config.core.Yolov5Test.test_sample_path = <test-sample-path>
python3 test.py
# 方法2
python3 test.py <trained-model(required)> <test-sample-path(required)> <label-path(optional)>
如果训练过程中未开启config.core.Yolov5Test.script_model_path开关,可以在测试过程中转化torchscript模型
- 转换torchscript模型(*.pt)
config.core.Yolov5Test.ema = False
config.core.Yolov5Test.script_model_path = "output/script.pt"
按照步骤6完成测试,torchscript模型将保存至config.core.Yolov5Test.script_model_path指定文件位置
- 加载torchscript模型
config.core.Yolov5Test.jit_model_path = <torchscript-model-path>
TODO
- 测试环境
torch == 1.8.1
numpy == 1.19.5
coremltools == 4.1
- 转换coreml模型(*.mlmodel)
# 如果指定路径,那么在训练或者测试过程中都会进行模型转换
import coremltools
config.cast.CoremlCast = AttrDict()
config.cast.TraceCast = AttrDict()
config.cast.TraceCast.model_dir = "output/trace.pt"
config.cast.CoremlCast.model_dir = "output/coreml.mlmodel"
config.cast.CoremlCast.input_type = None
config.cast.CoremlCast.scale = 1.0 / 255.0
config.cast.CoremlCast.color_layout = 'BGR'
config.cast.CoremlCast.blue_bias = 0
config.cast.CoremlCast.green_bias = 0
config.cast.CoremlCast.red_bias = 0
config.cast.CoremlCast.minimum_deployment_target = coremltools.target.iOS13
config.cast.CoremlCast.classfier_config = ["cls{}".format(i) for i in range(config.core.Yolov5Test.class_num)]
# 训练过程中转换(cast2cpu配置可配置在config.py文件也可以配置在train.py文件)
config.core.Yolov5Train.cast2cpu = True
python3 train.py
# 测试过程中转换(cast2cpu配置可配置在config.py文件也可以配置在train.py文件)
config.core.Yolov5Test.cast2cpu = True
python3 test.py <trained-model(required)> <test-sample-path(required)> <label-path(optional)>
- coreml模型推理
推理环境: macos13 or later
如果要在本项目中开启如下功能:
- 使用tensorboard
- 转换ONNX
- 转换NCNN
- 开启量化
- 开启自动混合精度训练
请参考DeepVAC
- 20210201 项目增加了对Yolov5S和Yolov5L的支持
- 20210219 修复了torchscript模型C++推理代码在cuda上CUDNN_STATUS_INTER_ERROR问题(在modules/model.py中重写Fcous模块)
- 修复在test过程中,静态量化模型报错问题
- 20210622 项目更新来适应新版本deepvac
- 20210625 项目增加coreml模型转换