文章目录[隐藏]
- 👋 只有实战,不讲理论 👋
- 🥇 版权: 本文由【墨理】原创、在CSDN首发、如需转载,请联系博主
- ❤️ 你只看一篇就够用的教程 感谢各位大佬一键三连
📔 认识 YOLOX
📕 环境搭建
服务器环境
## 服务器
cat /etc/issue
Ubuntu 18.04.5 LTS \n \l
## Cuda版本
nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
...
Cuda compilation tools, release 10.0, V10.0.130
# 显卡
Quadro RTX 5000 16G x 4
# 内存 128G
conda 环境独立搭建
git clone https://github.com/Megvii-BaseDetection/YOLOX.git
cd YOLOX
conda create -n torchYolo python=3.8.5
conda activate torchYolo
pip3 install -U pip && pip3 install -r requirements.txt
python3 setup.py develop
pip3 install cython
pip install pycocotools
项目目录结构
COCO 数据集准备【如果没有,可以参考该博文进行下载】
📗 测试
🟧 单图CPU测试
python tools/demo.py image -n yolox-s -c preModels/yolox_s.pth --path assets/dog.jpg --conf 0.25 --nms 0.45 --tsize 640 --save_result
输出如下
2021-08-25 01:56:16.305 | INFO | __main__:main:249 - Args: Namespace(camid=0, ckpt='preModels/yolox_s.pth', conf=0.25, demo='image', device='cpu', exp_file=None, experiment_name='yolox_s', fp16=False, fuse=False, legacy=False, name='yolox-s', nms=0.45, path='assets/dog.jpg', save_result=True, trt=False, tsize=640)
2021-08-25 01:56:16.507 | INFO | __main__:main:259 - Model Summary: Params: 8.97M, Gflops: 26.81
2021-08-25 01:56:16.508 | INFO | __main__:main:270 - loading checkpoint
2021-08-25 01:56:16.648 | INFO | __main__:main:274 - loaded checkpoint done.
2021-08-25 01:56:16.839 | INFO | __main__:inference:159 - Infer time: 0.1768s
2021-08-25 01:56:16.841 | INFO | __main__:image_demo:196 - Saving detection result in
./YOLOX_outputs/yolox_s/vis_res/2021_08_25_01_56_16/dog.jpg
🟨 单图GPU测试
python tools/demo.py image -n yolox-s -c preModels/yolox_s.pth --path assets/dog.jpg --conf 0.25 --nms 0.45 --tsize 640 --save_result --device gpu
# 或者 用指定 文件的方式 specify your detector's config
# 区别: -n 指定 yolo 模型名字 | -f 指定 yolo config 文件 ,这两种方式用其中一种即可
python tools/demo.py image -f exps/default/yolox_s.py -c preModels/yolox_s.pth --path assets/dog.jpg --conf 0.25 --nms 0.45 --tsize 640 --save_result --device gpu
输出如下
python tools/demo.py image -n yolox-s -c preModels/yolox_s.pth --path assets/dog.jpg --conf 0.25 --nms 0.45 --tsize 640 --save_result --device 0
2021-08-25 01:59:29.086 | INFO | __main__:main:249 - Args: Namespace(camid=0, ckpt='preModels/yolox_s.pth', conf=0.25, demo='image', device='0', exp_file=None, experiment_name='yolox_s', fp16=False, fuse=False, legacy=False, name='yolox-s', nms=0.45, path='assets/dog.jpg', save_result=True, trt=False, tsize=640)
2021-08-25 01:59:29.285 | INFO | __main__:main:259 - Model Summary: Params: 8.97M, Gflops: 26.81
2021-08-25 01:59:29.286 | INFO | __main__:main:270 - loading checkpoint
2021-08-25 01:59:29.427 | INFO | __main__:main:274 - loaded checkpoint done.
2021-08-25 01:59:29.619 | INFO | __main__:inference:159 - Infer time: 0.1775s
2021-08-25 01:59:29.621 | INFO | __main__:image_demo:196 - Saving detection result in ./YOLOX_outputs/yolox_s/vis_res/2021_08_25_01_59_29/dog.jpg
🟦 video GPU测试
-n yolox-s -c preModels/yolox_s.pth --device gpu 测试
time python tools/demo.py video -n yolox-s -c preModels/yolox_s.pth --path assets/obj.mp4 --conf 0.25 --nms 0.45 --tsize 640 --save_result --device gpu
# 输出如下
# GPU 占用 1199MiB
2021-08-25 03:28:35.486 | INFO | __main__:main:249 - Args: Namespace(camid=0, ckpt='preModels/yolox_s.pth', conf=0.25, demo='video', device='gpu', exp_file=None, experiment_name='yolox_s', fp16=False, fuse=False, legacy=False, name='yolox-s', nms=0.45, path='assets/obj.mp4', save_result=True, trt=False, tsize=640)
2021-08-25 03:28:35.680 | INFO | __main__:main:259 - Model Summary: Params: 8.97M, Gflops: 26.81
2021-08-25 03:28:39.340 | INFO | __main__:imageflow_demo:216 - video save_path is ./YOLOX_outputs/yolox_s/vis_res/2021_08_25_03_28_39/obj.mp4
2021-08-25 03:28:39.418 | INFO | __main__:inference:159 - Infer time: 0.0182s
2021-08-25 03:28:39.438 | INFO | __main__:inference:159 - Infer time: 0.0112s
2021-08-25 03:28:39.457 | INFO | __main__:inference:159 - Infer time: 0.0112s
2021-08-25 03:28:45.664 | INFO | __main__:inference:159 - Infer time: 0.0138s
2021-08-25 03:28:45.684 | INFO | __main__:inference:159 - Infer time: 0.0138s
2021-08-25 03:28:45.704 | INFO | __main__:inference:159 - Infer time: 0.0139s
real 0m11.579s
-n yolox-x -c preModels/yolox_x.pth --device gpu 测试
time python tools/demo.py video -n yolox-x -c preModels/yolox_x.pth --path assets/objDetect.m4v --conf 0.25 --nms 0.45 --tsize 640 --save_result --device gpu
# 输出如下
# GPU 占用 1671MiB
2021-08-25 03:21:50.171 | INFO | __main__:main:249 - Args: Namespace(camid=0, ckpt='preModels/yolox_x.pth', conf=0.25, demo='video', device='gpu', exp_file=None, experiment_name='yolox_x', fp16=False, fuse=False, legacy=False, name='yolox-x', nms=0.45, path='assets/obj.mp4', save_result=True, trt=False, tsize=640)
2021-08-25 03:21:51.248 | INFO | __main__:main:259 - Model Summary: Params: 99.07M, Gflops: 281.93
2021-08-25 03:21:54.904 | INFO | __main__:main:270 - loading checkpoint
2021-08-25 03:21:55.589 | INFO | __main__:main:274 - loaded checkpoint done.
2021-08-25 03:21:55.595 | INFO | __main__:imageflow_demo:216 - video save_path is ./YOLOX_outputs/yolox_x/vis_res/2021_08_25_03_21_55/obj.mp4
2021-08-25 03:21:55.712 | INFO | __main__:inference:159 - Infer time: 0.0553s
2021-08-25 03:21:55.770 | INFO | __main__:inference:159 - Infer time: 0.0485s
2021-08-25 03:21:55.826 | INFO | __main__:inference:159 - Infer time: 0.0477s
real 0m22.646s
可以直接生成得到 带检测框的 video
📘 Reproduce results on COCO 【COCO训练】
🔴 COCO数据集安置
🔵 vim tools/train.py 查看关键参数配置
🟣 训练命令
python tools/train.py -n yolox-s -d 4 -b 64 --fp16 -o --cache
参数说明
- d: number of gpu devices 【GPU个数】
- b: total batch size, the recommended number for -b is num-gpu * 8 【 建议 batch size = num-gpu * 8】
- – fp16: mixed precision training
- –cache: caching imgs into RAM to accelarate training, which need large system RAM.【128G内存可行】
效果如下
看了下服务器内存,128 G 似乎也能这样玩
free -h
total used free shared buff/cache available
Mem: 125G 17G 2.0G 187M 106G 107G
Swap: 8.0G 154M 7.8G
显卡勉强够用
🟡 训练输出
(torchYolo) yoloDir/YOLOX$ python tools/train.py -n yolox-s -d 4 -b 64 --fp16 -o --cache
# 输出如下
...
***************************************************************
We set `OMP_NUM_THREADS` for each process to 1 to speed up.
please further tune the variable for optimal performance.
***************************************************************
2021-08-25 03:46:59 | INFO | yolox.core.trainer:126 - args: Namespace(batch_size=64, cache=True, ckpt=None, devices=4, dist_backend='nccl', dist_url=None, exp_file=None, experiment_name='yolox_s', fp16=True, machine_rank=0, name='yolox-s', num_machines=1, occupy=True, opts=[], resume=False, start_epoch=None)
2021-08-25 03:46:59 | INFO | yolox.core.trainer:127 - exp value:
╒══════════════════╤════════════════════════════╕
│ keys │ values │
╞══════════════════╪════════════════════════════╡
│ seed │ None │
├──────────────────┼────────────────────────────┤
│ output_dir │ './YOLOX_outputs' │
├──────────────────┼────────────────────────────┤
│ print_interval │ 10 │
├──────────────────┼────────────────────────────┤
│ eval_interval │ 10 │
├──────────────────┼────────────────────────────┤
│ num_classes │ 80 │
├──────────────────┼────────────────────────────┤
│ depth │ 0.33 │
├──────────────────┼────────────────────────────┤
│ width │ 0.5 │
├──────────────────┼────────────────────────────┤
│ data_num_workers │ 4 │
├──────────────────┼────────────────────────────┤
│ input_size │ (640, 640) │
├──────────────────┼────────────────────────────┤
│ multiscale_range │ 5 │
├──────────────────┼────────────────────────────┤
│ data_dir │ None │
├──────────────────┼────────────────────────────┤
│ train_ann │ 'instances_train2017.json' │
├──────────────────┼────────────────────────────┤
│ val_ann │ 'instances_val2017.json' │
├──────────────────┼────────────────────────────┤
│ mosaic_prob │ 1.0 │
├──────────────────┼────────────────────────────┤
│ mixup_prob │ 1.0 │
├──────────────────┼────────────────────────────┤
│ degrees │ 10.0 │
├──────────────────┼────────────────────────────┤
│ translate │ 0.1 │
├──────────────────┼────────────────────────────┤
│ mosaic_scale │ (0.1, 2) │
├──────────────────┼────────────────────────────┤
│ mixup_scale │ (0.5, 1.5) │
├──────────────────┼────────────────────────────┤
│ shear │ 2.0 │
├──────────────────┼────────────────────────────┤
│ perspective │ 0.0 │
├──────────────────┼────────────────────────────┤
│ enable_mixup │ True │
├──────────────────┼────────────────────────────┤
│ warmup_epochs │ 5 │
├──────────────────┼────────────────────────────┤
│ max_epoch │ 300 │
├──────────────────┼────────────────────────────┤
│ warmup_lr │ 0 │
├──────────────────┼────────────────────────────┤
│ basic_lr_per_img │ 0.00015625 │
├──────────────────┼────────────────────────────┤
│ scheduler │ 'yoloxwarmcos' │
├──────────────────┼────────────────────────────┤
│ no_aug_epochs │ 15 │
├──────────────────┼────────────────────────────┤
│ min_lr_ratio │ 0.05 │
├──────────────────┼────────────────────────────┤
│ ema │ True │
├──────────────────┼────────────────────────────┤
│ weight_decay │ 0.0005 │
├──────────────────┼────────────────────────────┤
│ momentum │ 0.9 │
├──────────────────┼────────────────────────────┤
│ exp_name │ 'yolox_s' │
├──────────────────┼────────────────────────────┤
│ test_size │ (640, 640) │
├──────────────────┼────────────────────────────┤
│ test_conf │ 0.01 │
├──────────────────┼────────────────────────────┤
│ nmsthre │ 0.65 │
╘══════════════════╧════════════════════════════╛
2021-08-25 04:15:43 | INFO | yolox.core.trainer:238 - epoch: 1/300, iter: 1830/1849, mem: 13400Mb, iter_time: 0.534s, data_time: 0.002s, total_loss: 11.3, iou_loss: 3.4, l1_loss: 0.0, conf_loss: 5.1, cls_loss: 2.8, lr: 3.918e-04, size: 672, ETA: 4 days, 13:10:57
2021-08-25 04:15:53 | INFO | yolox.core.trainer:238 - epoch: 1/300, iter: 1840/1849, mem: 13400Mb, iter_time: 1.007s, data_time: 0.034s, total_loss: 11.4, iou_loss: 3.4, l1_loss: 0.0, conf_loss: 5.2, cls_loss: 2.7, lr: 3.961e-04, size: 672, ETA: 4 days, 13:25:39
2021-08-25 04:16:02 | INFO | yolox.core.trainer:318 - Save weights to ./YOLOX_outputs/yolox_s
2021-08-25 04:16:03 | INFO | yolox.core.trainer:188 - ---> start train epoch2
🔴 训练 10 个 epoch 统计训练时长
time python tools/train.py -n yolox-s -d 4 -b 64 --fp16 -o --cache
训练输出如下 【10 个 epoch 训练时长 140 分钟】
Average forward time: 5.46 ms, Average NMS time: 3.18 ms, Average inference time: 8.65 ms
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.145
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.258
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.145
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.054
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.153
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.204
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.176
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.288
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.309
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.110
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.336
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.418
2021-08-25 06:43:48 | INFO | yolox.core.trainer:318 - Save weights to ./YOLOX_outputs/yolox_s
2021-08-25 06:43:49 | INFO | yolox.core.trainer:183 - Training of experiment is done and the best AP is 14.46
# 10 个 epoch 训练时长 140 分钟
real 140m41.991s
user 668m9.676s
sys 519m4.609s
训练生成目录内容如下:
🔵 tensorboard 可视化查看 loss 曲线
cd YOLOX/YOLOX_outputs
# 运行如下命令
tensorboard --logdir yolox_s/ --bind_all
# 输出大致如下
TensorFlow installation not found - running with reduced feature set.
TensorBoard 2.6.0 at http://wave:6006/ (Press CTRL+C to quit)
浏览器中 使用 IP : 6006 即可成功查看
🟣 Resume 训练
比如遇到断电、网络类似突发情况,模型训练突然终止 ,这时,就需要用到
- 首先,我们可能想要 设定 最大 epoch 次数【如下图所示】
- 查阅 tools/train.py 文件,了解参数含义 ,-e 和 --start_epoch 都可以 指定 resume training start epoch
- 例如 我的 resume 训练 命令如下
python tools/train.py -n yolox-s -d 4 -b 64 --fp16 -o --cache --resume -e 10
# 可以看到输出, 继续 之前的 epoch10 开始训练了
2021-08-xx | INFO | yolox.core.trainer:188 - ---> start train epoch10
2021-08-xx | INFO | yolox.core.trainer:238 - epoch: 10/300, iter: 10/1849, mem: 13335Mb, iter_time: 6.747s, data_time: 0.003s, total_loss: 8.3, iou_loss: 2.7, l1_loss: 0.0, conf_loss: 3.6, cls_loss: 2.0, lr: 9.995e-03, size: 512, ETA: 42 days, 0:25:32
...
🟡 多机器联合训练
📙 Evaluation
🟧 batch testing for fast evaluation: 【批量快速测试】
这里是对 datasets/COCO/val2017/ 目录下 5000 张 xx.jpg 图片进行推理评估
- yolox-s 评估
python tools/eval.py -n yolox-s -c preModels/yolox_s.pth -b 64 -d 4 --conf 0.001 --fp16 --fuse
4卡 测试输出如下 GPU占用 1337MiB
...
2021-08-25 06:58:42.078 | INFO | yolox.core.launch:_distributed_worker:116 - Rank 0 initialization finished.
2021-08-25 06:58:42.134 | INFO | yolox.core.launch:_distributed_worker:116 - Rank 2 initialization finished.
2021-08-25 06:58:42.136 | INFO | yolox.core.launch:_distributed_worker:116 - Rank 1 initialization finished.
2021-08-25 06:58:42.155 | INFO | yolox.core.launch:_distributed_worker:116 - Rank 3 initialization finished.
...
...
Running per image evaluation...
Evaluate annotation type *bbox*
COCOeval_opt.evaluate() finished in 15.68 seconds.
Accumulating evaluation results...
COCOeval_opt.accumulate() finished in 2.10 seconds.
2021-08-25 06:59:53 | INFO | __mp_main__:187 -
Average forward time: 3.49 ms, Average NMS time: 2.27 ms, Average inference time: 5.76 ms
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.405
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.593
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.437
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.232
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.448
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.541
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.326
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.531
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.574
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.365
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.634
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.723
- yolox-x 评估
python tools/eval.py -n yolox-x -c preModels/yolox_x.pth -b 64 -d 4 --conf 0.001 --fp16 --fuse
4卡 测试输出如下 推理时,GPU占用 3527MiB
...
Running per image evaluation...
Evaluate annotation type *bbox*
COCOeval_opt.evaluate() finished in 12.79 seconds.
Accumulating evaluation results...
COCOeval_opt.accumulate() finished in 1.45 seconds.
2021-08-25 07:06:44 | INFO | __mp_main__:187 -
Average forward time: 15.65 ms, Average NMS time: 1.01 ms, Average inference time: 16.65 ms
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.511
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.691
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.554
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.347
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.562
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.671
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.381
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.621
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.659
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.486
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.710
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.819
- 也可以对刚刚训练得到的模型进行评估
python tools/eval.py -n yolox-s -c YOLOX_outputs/yolox_s/best_ckpt.pth -b 64 -d 4 --conf 0.001 --fp16 --fuse
🟨 单卡 batch_size = 1 进行评估
目的是 :To reproduce speed test 【复现官方测试速度,当然显卡一致才有的比】
python tools/eval.py -n yolox-s -c preModels/yolox_s.pth -b 1 -d 1 --conf 0.001 --fp16 --fuse
输出如下
单卡 GPU 占用 1319MiB
...
100%|##########| 5000/5000 [01:26<00:00, 57.98it/s]
Running per image evaluation...
Evaluate annotation type *bbox*
COCOeval_opt.evaluate() finished in 15.67 seconds.
Accumulating evaluation results...
COCOeval_opt.accumulate() finished in 2.04 seconds.
2021-08-25 07:49:54 | INFO | __main__:187 -
Average forward time: 12.25 ms, Average NMS time: 1.83 ms, Average inference time: 14.08 ms
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.405
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.593
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.437
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.232
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.448
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.541
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.326
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.531
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.574
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.365
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.634
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.724
📕 YOLOX 系列当前已经更新的文章
- 🍊 YOLOX 环境搭建 | 测试 | COCO训练复现 【YOLOX 实战】
- 🍊 YOLOX (pytorch)模型 ONNX export | ❤️运行推理❤️【YOLOX 实战二】
- 🍊 YOLOX (pytorch)模型 转 ONNX 转 ncnn 之C++运行推理【YOLOX 实战三】
- 🍊 YOLOX (pytorch)模型 转 tensorRT 之运行推理【YOLOX 实战四】【一文读懂】
- 🍊 这个专栏、很多知识点涉及工作、核心干货、相信能够为大家提供一些参考、节约相当多的时间
- 🍊 目标检测-模型部署
- 🍊 未完,本专栏持续更新,感谢三连
版权声明:本文为CSDN博主「墨理学AI」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sinat_28442665/article/details/119904148
暂无评论