diff --git a/configs/ssod/README.md b/configs/semi_det/README.md similarity index 80% rename from configs/ssod/README.md rename to configs/semi_det/README.md index ab1fc9faae961896f76e3b00d2d94c5f296a463f..ffed5de7f7f5511fc2dc79cb2137849596af6a3e 100644 --- a/configs/ssod/README.md +++ b/configs/semi_det/README.md @@ -1,6 +1,6 @@ 简体中文 | [English](README_en.md) -# Semi-Supervised Object Detection (SSOD) 半监督目标检测 +# Semi-Supervised Detection (Semi DET) 半监督检测 ## 内容 - [简介](#简介) @@ -34,12 +34,12 @@ ### [DenseTeacher](denseteacher) -| 模型 | 基础检测器 | 监督数据比例 | Sup mAPval
0.5:0.95 | Semi mAPval
0.5:0.95 | Semi Epochs (Iters) | 模型下载 | 配置文件 | -| :------------: | :---------------------: | :-----------: | :-------------------------: |:---------------------------: |:--------------------: | :-------: |:---------: | -| DenseTeacher | [FCOS ResNet50-FPN](./baseline/fcos_r50_fpn_2x_coco_sup005.yml) | 5% | 21.3 | 30.6 | 240 (87120) | [download](https://paddledet.bj.bcebos.com/models/denseteacher_fcos_r50_fpn_coco_semi005.pdparams) | [config](denseteacher/denseteacher_fcos_r50_fpn_coco_semi005.yml) | -| DenseTeacher | [FCOS ResNet50-FPN](./baseline/fcos_r50_fpn_2x_coco_sup010.yml) | 10%| 26.3 | 35.1 | 240 (174240)| [download](https://paddledet.bj.bcebos.com/models/denseteacher_fcos_r50_fpn_coco_semi010.pdparams) | [config](denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml) | -| DenseTeacher(LSJ)| [FCOS ResNet50-FPN](./baseline/fcos_r50_fpn_2x_coco_sup010.yml) |10%| 26.3 | 37.1 | 240 (174240)| [download](https://paddledet.bj.bcebos.com/models/denseteacher_fcos_r50_fpn_coco_semi010_lsj.pdparams) | [config](denseteacher/denseteacher_fcos_r50_fpn_coco_semi010_lsj.yml) | -| DenseTeacher | [FCOS ResNet50-FPN](./../fcos/fcos_r50_fpn_iou_multiscale_2x_coco.ymll) |full| 42.6 | - | 36 (263844)| [download](https://paddledet.bj.bcebos.com/models/denseteacher_fcos_r50_fpn_coco_full.pdparams) | [config](denseteacher/denseteacher_fcos_r50_fpn_coco_full.yml) | +| 模型 | 监督数据比例 | Sup Baseline | Sup Epochs (Iters) | Sup mAPval
0.5:0.95 | Semi mAPval
0.5:0.95 | Semi Epochs (Iters) | 模型下载 | 配置文件 | +| :------------: | :---------: | :---------------------: | :---------------------: |:---------------------------: |:----------------------------: | :------------------: |:--------: |:----------: | +| DenseTeacher-FCOS | 5% | [sup_config](./baseline/fcos_r50_fpn_2x_coco_sup005.yml) | 24 (8712) | 21.3 | **30.6** | 240 (87120) | [download](https://paddledet.bj.bcebos.com/models/denseteacher_fcos_r50_fpn_coco_semi005.pdparams) | [config](denseteacher/denseteacher_fcos_r50_fpn_coco_semi005.yml) | +| DenseTeacher-FCOS | 5% | [sup_config](./baseline/fcos_r50_fpn_2x_coco_sup010.yml) | 24 (17424) | 26.3 | **35.1** | 240 (174240) | [download](https://paddledet.bj.bcebos.com/models/denseteacher_fcos_r50_fpn_coco_semi010.pdparams) | [config](denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml) | +| DenseTeacher-FCOS(LSJ)| 5% | [sup_config](./baseline/fcos_r50_fpn_2x_coco_sup010.yml) | 24 (17424) | 26.3 | **37.1(LSJ)** | 240 (174240) | [download](https://paddledet.bj.bcebos.com/models/denseteacher_fcos_r50_fpn_coco_semi010_lsj.pdparams) | [config](denseteacher/denseteacher_fcos_r50_fpn_coco_semi010_lsj.yml) | +| DenseTeacher-FCOS |100%(full)| [sup_config](./../fcos/fcos_r50_fpn_iou_multiscale_2x_coco.ymll) | 24 (175896) | 42.6 | **44.2** | 24 (175896)| [download](https://paddledet.bj.bcebos.com/models/denseteacher_fcos_r50_fpn_coco_full.pdparams) | [config](denseteacher/denseteacher_fcos_r50_fpn_coco_full.yml) | ## 半监督数据集准备 @@ -215,7 +215,7 @@ use_warmup: &use_warmup True ### 全局配置 -需要在配置文件中添加如下全局配置: +需要在配置文件中添加如下全局配置,并且注意 DenseTeacher 模型需要使用`use_simple_ema: True`而不是`use_ema: True`: ```python ### global config @@ -249,9 +249,9 @@ _BASE_: [ ### 数据增强配置 构建半监督训练集的Reader,需要在原先`TrainReader`的基础上,新增加`weak_aug`,`strong_aug`,`sup_batch_transforms`和`unsup_batch_transforms`,并且需要注意: -- **如果有`NormalizeImage`,需要单独从`sample_transforms`中抽出来放在`weak_aug`和`strong_aug`中; +- 如果有`NormalizeImage`,需要单独从`sample_transforms`中抽出来放在`weak_aug`和`strong_aug`中; - `sample_transforms`为**公用的基础数据增强**; -- 完整的弱数据增强为``sample_transforms + weak_aug`,完整的强数据增强为`sample_transforms + strong_aug`; +- 完整的弱数据增强为`sample_transforms + weak_aug`,完整的强数据增强为`sample_transforms + strong_aug`; 如以下所示: @@ -302,8 +302,6 @@ SemiTrainReader: object_sizes_boundary: [64, 128, 256, 512] center_sampling_radius: 1.5 downsample_ratios: [8, 16, 32, 64, 128] - num_shift: 0.5 - multiply_strides_reg_targets: False norm_reg_targets: True unsup_batch_transforms: - Permute: {} @@ -351,22 +349,22 @@ OptimizerBuilder: ```bash # 单卡训练 (不推荐,需按线性比例相应地调整学习率) -CUDA_VISIBLE_DEVICES=0 python tools/train.py -c ssod/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml --eval +CUDA_VISIBLE_DEVICES=0 python tools/train.py -c configs/semi_det/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml --eval # 多卡训练 -python -m paddle.distributed.launch --log_dir=denseteacher_fcos_semi010/ --gpus 0,1,2,3,4,5,6,7 tools/train.py -c ssod/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml --eval +python -m paddle.distributed.launch --log_dir=denseteacher_fcos_semi010/ --gpus 0,1,2,3,4,5,6,7 tools/train.py -c configs/semi_det/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml --eval ``` ### 评估 ```bash -CUDA_VISIBLE_DEVICES=0 python tools/eval.py -c ssod/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml -o weights=output/denseteacher_fcos_r50_fpn_coco_semi010/model_final.pdparams +CUDA_VISIBLE_DEVICES=0 python tools/eval.py -c configs/semi_det/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml -o weights=output/denseteacher_fcos_r50_fpn_coco_semi010/model_final.pdparams ``` ### 预测 ```bash -CUDA_VISIBLE_DEVICES=0 python tools/infer.py -c ssod/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml -o weights=output/denseteacher_fcos_r50_fpn_coco_semi010/model_final.pdparams --infer_img=demo/000000014439.jpg +CUDA_VISIBLE_DEVICES=0 python tools/infer.py -c configs/semi_det/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml -o weights=output/denseteacher_fcos_r50_fpn_coco_semi010/model_final.pdparams --infer_img=demo/000000014439.jpg ``` ### 部署 @@ -375,7 +373,7 @@ CUDA_VISIBLE_DEVICES=0 python tools/infer.py -c ssod/denseteacher/denseteacher_f ```bash # 导出模型 -CUDA_VISIBLE_DEVICES=0 python tools/export_model.py -c ssod/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml -o weights=https://paddledet.bj.bcebos.com/models/denseteacher_fcos_r50_fpn_coco_semi010.pdparams +CUDA_VISIBLE_DEVICES=0 python tools/export_model.py -c configs/semi_det/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml -o weights=https://paddledet.bj.bcebos.com/models/denseteacher_fcos_r50_fpn_coco_semi010.pdparams # 导出权重预测 CUDA_VISIBLE_DEVICES=0 python deploy/python/infer.py --model_dir=output_inference/denseteacher_fcos_r50_fpn_coco_semi010 --image_file=demo/000000014439_640x640.jpg --device=GPU diff --git a/configs/ssod/_base_/coco_detection_full.yml b/configs/semi_det/_base_/coco_detection_full.yml similarity index 100% rename from configs/ssod/_base_/coco_detection_full.yml rename to configs/semi_det/_base_/coco_detection_full.yml diff --git a/configs/ssod/_base_/coco_detection_percent_1.yml b/configs/semi_det/_base_/coco_detection_percent_1.yml similarity index 100% rename from configs/ssod/_base_/coco_detection_percent_1.yml rename to configs/semi_det/_base_/coco_detection_percent_1.yml diff --git a/configs/ssod/_base_/coco_detection_percent_10.yml b/configs/semi_det/_base_/coco_detection_percent_10.yml similarity index 100% rename from configs/ssod/_base_/coco_detection_percent_10.yml rename to configs/semi_det/_base_/coco_detection_percent_10.yml diff --git a/configs/ssod/_base_/coco_detection_percent_5.yml b/configs/semi_det/_base_/coco_detection_percent_5.yml similarity index 100% rename from configs/ssod/_base_/coco_detection_percent_5.yml rename to configs/semi_det/_base_/coco_detection_percent_5.yml diff --git a/configs/ssod/baseline/README.md b/configs/semi_det/baseline/README.md similarity index 99% rename from configs/ssod/baseline/README.md rename to configs/semi_det/baseline/README.md index 02a3fde8774c972ae63869d5c7e06b1f0baeb997..58e7795e0c7a861bf8dd01b25baa588f1c48923b 100644 --- a/configs/ssod/baseline/README.md +++ b/configs/semi_det/baseline/README.md @@ -62,7 +62,7 @@ 将以下命令写在一个脚本文件里如```run.sh```,一键运行命令为:```sh run.sh```,也可命令行一句句去运行: ```bash -model_type=ssod/baseline +model_type=semi_det/baseline job_name=ppyoloe_plus_crn_s_80e_coco_sup010 # 可修改,如 fcos_r50_fpn_2x_coco_sup010 config=configs/${model_type}/${job_name}.yml diff --git a/configs/ssod/baseline/faster_rcnn_r50_fpn_2x_coco_sup005.yml b/configs/semi_det/baseline/faster_rcnn_r50_fpn_2x_coco_sup005.yml similarity index 100% rename from configs/ssod/baseline/faster_rcnn_r50_fpn_2x_coco_sup005.yml rename to configs/semi_det/baseline/faster_rcnn_r50_fpn_2x_coco_sup005.yml diff --git a/configs/ssod/baseline/faster_rcnn_r50_fpn_2x_coco_sup010.yml b/configs/semi_det/baseline/faster_rcnn_r50_fpn_2x_coco_sup010.yml similarity index 100% rename from configs/ssod/baseline/faster_rcnn_r50_fpn_2x_coco_sup010.yml rename to configs/semi_det/baseline/faster_rcnn_r50_fpn_2x_coco_sup010.yml diff --git a/configs/ssod/baseline/fcos_r50_fpn_2x_coco_sup005.yml b/configs/semi_det/baseline/fcos_r50_fpn_2x_coco_sup005.yml similarity index 100% rename from configs/ssod/baseline/fcos_r50_fpn_2x_coco_sup005.yml rename to configs/semi_det/baseline/fcos_r50_fpn_2x_coco_sup005.yml diff --git a/configs/ssod/baseline/fcos_r50_fpn_2x_coco_sup010.yml b/configs/semi_det/baseline/fcos_r50_fpn_2x_coco_sup010.yml similarity index 100% rename from configs/ssod/baseline/fcos_r50_fpn_2x_coco_sup010.yml rename to configs/semi_det/baseline/fcos_r50_fpn_2x_coco_sup010.yml diff --git a/configs/ssod/baseline/ppyoloe_plus_crn_s_80e_coco_sup005.yml b/configs/semi_det/baseline/ppyoloe_plus_crn_s_80e_coco_sup005.yml similarity index 100% rename from configs/ssod/baseline/ppyoloe_plus_crn_s_80e_coco_sup005.yml rename to configs/semi_det/baseline/ppyoloe_plus_crn_s_80e_coco_sup005.yml diff --git a/configs/ssod/baseline/ppyoloe_plus_crn_s_80e_coco_sup010.yml b/configs/semi_det/baseline/ppyoloe_plus_crn_s_80e_coco_sup010.yml similarity index 100% rename from configs/ssod/baseline/ppyoloe_plus_crn_s_80e_coco_sup010.yml rename to configs/semi_det/baseline/ppyoloe_plus_crn_s_80e_coco_sup010.yml diff --git a/configs/ssod/baseline/retinanet_r50_fpn_2x_coco_sup005.yml b/configs/semi_det/baseline/retinanet_r50_fpn_2x_coco_sup005.yml similarity index 100% rename from configs/ssod/baseline/retinanet_r50_fpn_2x_coco_sup005.yml rename to configs/semi_det/baseline/retinanet_r50_fpn_2x_coco_sup005.yml diff --git a/configs/ssod/baseline/retinanet_r50_fpn_2x_coco_sup010.yml b/configs/semi_det/baseline/retinanet_r50_fpn_2x_coco_sup010.yml similarity index 100% rename from configs/ssod/baseline/retinanet_r50_fpn_2x_coco_sup010.yml rename to configs/semi_det/baseline/retinanet_r50_fpn_2x_coco_sup010.yml diff --git a/configs/semi_det/denseteacher/README.md b/configs/semi_det/denseteacher/README.md new file mode 100644 index 0000000000000000000000000000000000000000..982a7ca9eaa89b809664eb8ed73ff160b1b8b4bc --- /dev/null +++ b/configs/semi_det/denseteacher/README.md @@ -0,0 +1,91 @@ +简体中文 | [English](README_en.md) + +# Dense Teacher: Dense Pseudo-Labels for Semi-supervised Object Detection + +## 模型库 + +| 模型 | 监督数据比例 | Sup Baseline | Sup Epochs (Iters) | Sup mAPval
0.5:0.95 | Semi mAPval
0.5:0.95 | Semi Epochs (Iters) | 模型下载 | 配置文件 | +| :------------: | :---------: | :---------------------: | :---------------------: |:---------------------------: |:----------------------------: | :------------------: |:--------: |:----------: | +| DenseTeacher-FCOS | 5% | [sup_config](../baseline/fcos_r50_fpn_2x_coco_sup005.yml) | 24 (8712) | 21.3 | **30.6** | 240 (87120) | [download](https://paddledet.bj.bcebos.com/models/denseteacher_fcos_r50_fpn_coco_semi005.pdparams) | [config](./denseteacher_fcos_r50_fpn_coco_semi005.yml) | +| DenseTeacher-FCOS | 5% | [sup_config](../baseline/fcos_r50_fpn_2x_coco_sup010.yml) | 24 (17424) | 26.3 | **35.1** | 240 (174240) | [download](https://paddledet.bj.bcebos.com/models/denseteacher_fcos_r50_fpn_coco_semi010.pdparams) | [config](./denseteacher_fcos_r50_fpn_coco_semi010.yml) | +| DenseTeacher-FCOS(LSJ)| 5% | [sup_config](../baseline/fcos_r50_fpn_2x_coco_sup010.yml) | 24 (17424) | 26.3 | **37.1(LSJ)** | 240 (174240) | [download](https://paddledet.bj.bcebos.com/models/denseteacher_fcos_r50_fpn_coco_semi010_lsj.pdparams) | [config](./denseteacher_fcos_r50_fpn_coco_semi010_lsj.yml) | +| DenseTeacher-FCOS |100%(full)| [sup_config](../../fcos/fcos_r50_fpn_iou_multiscale_2x_coco.ymll) | 24 (175896) | 42.6 | **44.2** | 24 (175896)| [download](https://paddledet.bj.bcebos.com/models/denseteacher_fcos_r50_fpn_coco_full.pdparams) | [config](./denseteacher_fcos_r50_fpn_coco_full.yml) | + + +**注意:** + - 以上模型训练默认使用8 GPUs,监督数据总batch_size默认为16,无监督数据总batch_size默认也为16,默认初始学习率为0.01。如果改动了总batch_size,请按线性比例相应地调整学习率; + - **监督数据比例**是指使用的有标签COCO数据集占 COCO train2017 全量训练集的百分比,使用的无标签COCO数据集一般也是相同比例,但具体图片和有标签数据的图片不重合; + - `Semi Epochs (Iters)`表示**半监督训练**的模型的 Epochs (Iters),如果使用**自定义数据集**,需自行根据Iters换算到对应的Epochs调整,最好保证总Iters 和COCO数据集的设置较为接近; + - `Sup mAP`是**只使用有监督数据训练**的模型的精度,请参照**基础检测器的配置文件** 和 [baseline](../baseline); + - `Semi mAP`是**半监督训练**的模型的精度,模型下载和配置文件的链接均为**半监督模型**; + - `LSJ`表示 **large-scale jittering**,表示使用更大范围的多尺度训练,可进一步提升精度,但训练速度也会变慢; + - 半监督检测的配置和使用,请参照[文档](../README.md/#半监督检测配置); + - `Dense Teacher`原文使用`R50-va-caffe`预训练,PaddleDetection中默认使用`R50-vb`预训练,如果使用`R50-vd`结合[SSLD](../../../docs/feature_models/SSLD_PRETRAINED_MODEL.md)的预训练模型,可进一步显著提升检测精度,如: + ``` + pretrain_weights: https://paddledet.bj.bcebos.com/models/pretrained/ResNet50_vd_ssld_v2_pretrained.pdparams + ResNet: + depth: 50 + variant: d + norm_type: bn + freeze_at: 0 + return_idx: [0,1,2,3] + num_stages: 4 + lr_mult_list: [0.05, 0.05, 0.1, 0.15] + ``` + + +## 使用说明 + +仅训练时必须使用半监督检测的配置文件去训练,评估、预测、部署也可以按基础检测器的配置文件去执行。 + +### 训练 + +```bash +# 单卡训练 (不推荐,需按线性比例相应地调整学习率) +CUDA_VISIBLE_DEVICES=0 python tools/train.py -c configs/semi_det/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml --eval + +# 多卡训练 +python -m paddle.distributed.launch --log_dir=denseteacher_fcos_semi010/ --gpus 0,1,2,3,4,5,6,7 tools/train.py -c configs/semi_det/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml --eval +``` + +### 评估 + +```bash +CUDA_VISIBLE_DEVICES=0 python tools/eval.py -c configs/semi_det/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml -o weights=output/denseteacher_fcos_r50_fpn_coco_semi010/model_final.pdparams +``` + +### 预测 + +```bash +CUDA_VISIBLE_DEVICES=0 python tools/infer.py -c configs/semi_det/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml -o weights=output/denseteacher_fcos_r50_fpn_coco_semi010/model_final.pdparams --infer_img=demo/000000014439.jpg +``` + +### 部署 + +部署可以使用半监督检测配置文件,也可以使用基础检测器的配置文件去部署和使用。 + +```bash +# 导出模型 +CUDA_VISIBLE_DEVICES=0 python tools/export_model.py -c configs/semi_det/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml -o weights=https://paddledet.bj.bcebos.com/models/denseteacher_fcos_r50_fpn_coco_semi010.pdparams + +# 导出权重预测 +CUDA_VISIBLE_DEVICES=0 python deploy/python/infer.py --model_dir=output_inference/denseteacher_fcos_r50_fpn_coco_semi010 --image_file=demo/000000014439_640x640.jpg --device=GPU + +# 部署测速 +CUDA_VISIBLE_DEVICES=0 python deploy/python/infer.py --model_dir=output_inference/denseteacher_fcos_r50_fpn_coco_semi010 --image_file=demo/000000014439_640x640.jpg --device=GPU --run_benchmark=True # --run_mode=trt_fp16 + +# 导出ONNX +paddle2onnx --model_dir output_inference/denseteacher_fcos_r50_fpn_coco_semi010/ --model_filename model.pdmodel --params_filename model.pdiparams --opset_version 12 --save_file denseteacher_fcos_r50_fpn_coco_semi010.onnx +``` + + +## 引用 + +``` + @article{denseteacher2022, + title={Dense Teacher: Dense Pseudo-Labels for Semi-supervised Object Detection}, + author={Hongyu Zhou, Zheng Ge, Songtao Liu, Weixin Mao, Zeming Li, Haiyan Yu, Jian Sun}, + journal={arXiv preprint arXiv:2207.02541}, + year={2022} +} +``` diff --git a/configs/semi_det/denseteacher/denseteacher_fcos_r50_fpn_coco_full.yml b/configs/semi_det/denseteacher/denseteacher_fcos_r50_fpn_coco_full.yml new file mode 100644 index 0000000000000000000000000000000000000000..b6c027327adb9263445c99a1173b583823296b21 --- /dev/null +++ b/configs/semi_det/denseteacher/denseteacher_fcos_r50_fpn_coco_full.yml @@ -0,0 +1,159 @@ +_BASE_: [ + 'denseteacher_fcos_r50_fpn_coco_semi010.yml', + '../_base_/coco_detection_full.yml', +] +log_iter: 100 +snapshot_epoch: 2 +epochs: &epochs 24 +weights: output/denseteacher_fcos_r50_fpn_coco_full/model_final + + +### pretrain and warmup config, choose one and coment another +pretrain_weights: https://paddledet.bj.bcebos.com/models/pretrained/ResNet50_cos_pretrained.pdparams +semi_start_iters: 5000 +ema_start_iters: 3000 +use_warmup: &use_warmup True + + +### global config +use_simple_ema: True +ema_decay: 0.9996 +ssod_method: DenseTeacher +DenseTeacher: + train_cfg: + sup_weight: 1.0 + unsup_weight: 1.0 + loss_weight: {distill_loss_cls: 2.0, distill_loss_box: 1.0, distill_loss_quality: 1.0} + concat_sup_data: True + suppress: linear + ratio: 0.01 + gamma: 2.0 + test_cfg: + inference_on: teacher + + +### reader config +worker_num: 2 +SemiTrainReader: + sample_transforms: + - Decode: {} + - RandomResize: {target_size: [[640, 1333], [672, 1333], [704, 1333], [736, 1333], [768, 1333], [800, 1333]], keep_ratio: True, interp: 1} + - RandomFlip: {} + weak_aug: + - NormalizeImage: {mean: [0.485, 0.456, 0.406], std: [0.229, 0.224, 0.225], is_scale: true} + strong_aug: + - StrongAugImage: {transforms: [ + RandomColorJitter: {prob: 0.8, brightness: 0.4, contrast: 0.4, saturation: 0.4, hue: 0.1}, + RandomErasingCrop: {}, + RandomGaussianBlur: {prob: 0.5, sigma: [0.1, 2.0]}, + RandomGrayscale: {prob: 0.2}, + ]} + - NormalizeImage: {mean: [0.485, 0.456, 0.406], std: [0.229, 0.224, 0.225], is_scale: true} + sup_batch_transforms: + - Permute: {} + - PadBatch: {pad_to_stride: 32} + - Gt2FCOSTarget: + object_sizes_boundary: [64, 128, 256, 512] + center_sampling_radius: 1.5 + downsample_ratios: [8, 16, 32, 64, 128] + norm_reg_targets: True + unsup_batch_transforms: + - Permute: {} + - PadBatch: {pad_to_stride: 32} + sup_batch_size: 2 + unsup_batch_size: 2 + shuffle: True + drop_last: True + +EvalReader: + sample_transforms: + - Decode: {} + - Resize: {target_size: [800, 1333], keep_ratio: True, interp: 1} + - NormalizeImage: {mean: [0.485, 0.456, 0.406], std: [0.229, 0.224, 0.225], is_scale: True} + - Permute: {} + batch_transforms: + - PadBatch: {pad_to_stride: 32} + batch_size: 1 + +TestReader: + sample_transforms: + - Decode: {} + - Resize: {target_size: [800, 1333], keep_ratio: True, interp: 1} + - NormalizeImage: {mean: [0.485, 0.456, 0.406], std: [0.229, 0.224, 0.225], is_scale: True} + - Permute: {} + batch_transforms: + - PadBatch: {pad_to_stride: 32} + batch_size: 1 + fuse_normalize: True + + +### model config +architecture: FCOS +FCOS: + backbone: ResNet + neck: FPN + fcos_head: FCOSHead + +ResNet: + depth: 50 + variant: 'b' + norm_type: bn + freeze_at: 0 # res2 + return_idx: [1, 2, 3] + num_stages: 4 + +FPN: + out_channel: 256 + spatial_scales: [0.125, 0.0625, 0.03125] + extra_stage: 2 + has_extra_convs: True + use_c5: False + +FCOSHead: + fcos_feat: + name: FCOSFeat + feat_in: 256 + feat_out: 256 + num_convs: 4 + norm_type: "gn" + use_dcn: False + fpn_stride: [8, 16, 32, 64, 128] + prior_prob: 0.01 + norm_reg_targets: True + centerness_on_reg: True + fcos_loss: + name: FCOSLoss + loss_alpha: 0.25 + loss_gamma: 2.0 + iou_loss_type: "giou" + reg_weights: 1.0 + quality: "iou" + nms: + name: MultiClassNMS + nms_top_k: 1000 + keep_top_k: 100 + score_threshold: 0.05 + nms_threshold: 0.6 + + +### other config +epoch: *epochs +LearningRate: + base_lr: 0.01 + schedulers: + - !PiecewiseDecay + gamma: 0.1 + milestones: [*epochs] + use_warmup: *use_warmup + - !LinearWarmup + start_factor: 0.001 + steps: 1000 + +OptimizerBuilder: + optimizer: + momentum: 0.9 + type: Momentum + regularizer: + factor: 0.0001 + type: L2 + clip_grad_by_value: 1.0 diff --git a/configs/ssod/denseteacher/denseteacher_fcos_r50_fpn_coco_semi005.yml b/configs/semi_det/denseteacher/denseteacher_fcos_r50_fpn_coco_semi005.yml similarity index 100% rename from configs/ssod/denseteacher/denseteacher_fcos_r50_fpn_coco_semi005.yml rename to configs/semi_det/denseteacher/denseteacher_fcos_r50_fpn_coco_semi005.yml diff --git a/configs/ssod/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml b/configs/semi_det/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml similarity index 100% rename from configs/ssod/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml rename to configs/semi_det/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml diff --git a/configs/ssod/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010_lsj.yml b/configs/semi_det/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010_lsj.yml similarity index 100% rename from configs/ssod/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010_lsj.yml rename to configs/semi_det/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010_lsj.yml diff --git a/configs/ssod/denseteacher/README.md b/configs/ssod/denseteacher/README.md deleted file mode 100644 index 94e688b6163443b094db65c05b067b10bb5d5e50..0000000000000000000000000000000000000000 --- a/configs/ssod/denseteacher/README.md +++ /dev/null @@ -1,78 +0,0 @@ -简体中文 | [English](README_en.md) - -# DenseTeacher (Dense Teacher: Dense Pseudo-Labels for Semi-supervised Object Detection) - -## 模型库 - -| 模型 | 基础检测器 | 监督数据比例 | Sup mAPval
0.5:0.95 | Semi mAPval
0.5:0.95 | Semi Epochs (Iters) | 模型下载 | 配置文件 | -| :------------: | :---------------------: | :-----------: | :-------------------------: |:---------------------------: |:--------------------: | :-------: |:---------: | -| DenseTeacher | [FCOS R50-FPN](../baseline/fcos_r50_fpn_2x_coco_sup005.yml) | 5% | 21.3 | 30.6 | 240 (87120) | [download](https://paddledet.bj.bcebos.com/models/denseteacher_fcos_r50_fpn_coco_semi005.pdparams) | [config](./denseteacher_fcos_r50_fpn_coco_semi005.yml) | -| DenseTeacher | [FCOS R50-FPN](../baseline/fcos_r50_fpn_2x_coco_sup010.yml) | 10%| 26.3 | 35.1 | 240 (174240)| [download](https://paddledet.bj.bcebos.com/models/denseteacher_fcos_r50_fpn_coco_semi010.pdparams) | [config](./denseteacher_fcos_r50_fpn_coco_semi010.yml) | -| DenseTeacher(LSJ)| [FCOS R50-FPN](../baseline/fcos_r50_fpn_2x_coco_sup010.yml)| 10%| 26.3 | 37.1 | 240 (174240)| [download](https://paddledet.bj.bcebos.com/models/denseteacher_fcos_r50_fpn_coco_semi010_lsj.pdparams) | [config](./denseteacher_fcos_r50_fpn_coco_semi010_lsj.yml) | -| DenseTeacher | [FCOS R50-FPN](../../fcos/fcos_r50_fpn_iou_multiscale_2x_coco.ymll) |full| 42.6 | - | 36 (263844)| [download](https://paddledet.bj.bcebos.com/models/denseteacher_fcos_r50_fpn_coco_full.pdparams) | [config](./denseteacher_fcos_r50_fpn_coco_full.yml) | - - -**注意:** - - 以上模型训练默认使用8 GPUs,监督数据总batch_size默认为16,无监督数据总batch_size默认也为16,默认初始学习率为0.01。如果改动了总batch_size,请按线性比例相应地调整学习率; - - `Semi Epochs (Iters)`表示**半监督训练**的模型的 Epochs (Iters),如果使用**自定义数据集**,需自行根据Iters换算到对应的Epochs调整,最好保证总Iters 和COCO数据集的设置较为接近; - - `Sup mAP`是**只使用有监督数据训练**的模型的精度,请参照**基础检测器的配置文件** 和 [baseline](../baseline); - - `Semi mAP`是**半监督训练**的模型的精度,模型下载和配置文件的链接均为**半监督模型**; - - `LSJ`表示 large-scale jittering,表示更大范围的多尺度训练,可进一步提升精度,但训练速度也会变慢; - - 半监督检测的配置和使用,请参照[文档](../README.md/#半监督检测配置); - - -## 使用说明 - -仅训练时必须使用半监督检测的配置文件去训练,评估、预测、部署也可以按基础检测器的配置文件去执行。 - -### 训练 - -```bash -# 单卡训练 (不推荐,需按线性比例相应地调整学习率) -CUDA_VISIBLE_DEVICES=0 python tools/train.py -c ssod/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml --eval - -# 多卡训练 -python -m paddle.distributed.launch --log_dir=denseteacher_fcos_semi010/ --gpus 0,1,2,3,4,5,6,7 tools/train.py -c ssod/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml --eval -``` - -### 评估 - -```bash -CUDA_VISIBLE_DEVICES=0 python tools/eval.py -c ssod/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml -o weights=output/denseteacher_fcos_r50_fpn_coco_semi010/model_final.pdparams -``` - -### 预测 - -```bash -CUDA_VISIBLE_DEVICES=0 python tools/infer.py -c ssod/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml -o weights=output/denseteacher_fcos_r50_fpn_coco_semi010/model_final.pdparams --infer_img=demo/000000014439.jpg -``` - -### 部署 - -部署可以使用半监督检测配置文件,也可以使用基础检测器的配置文件去部署和使用。 - -```bash -# 导出模型 -CUDA_VISIBLE_DEVICES=0 python tools/export_model.py -c ssod/denseteacher/denseteacher_fcos_r50_fpn_coco_semi010.yml -o weights=https://paddledet.bj.bcebos.com/models/denseteacher_fcos_r50_fpn_coco_semi010.pdparams - -# 导出权重预测 -CUDA_VISIBLE_DEVICES=0 python deploy/python/infer.py --model_dir=output_inference/denseteacher_fcos_r50_fpn_coco_semi010 --image_file=demo/000000014439_640x640.jpg --device=GPU - -# 部署测速 -CUDA_VISIBLE_DEVICES=0 python deploy/python/infer.py --model_dir=output_inference/denseteacher_fcos_r50_fpn_coco_semi010 --image_file=demo/000000014439_640x640.jpg --device=GPU --run_benchmark=True # --run_mode=trt_fp16 - -# 导出ONNX -paddle2onnx --model_dir output_inference/denseteacher_fcos_r50_fpn_coco_semi010/ --model_filename model.pdmodel --params_filename model.pdiparams --opset_version 12 --save_file denseteacher_fcos_r50_fpn_coco_semi010.onnx -``` - - -## 引用 - -``` - @article{denseteacher2022, - title={Dense Teacher: Dense Pseudo-Labels for Semi-supervised Object Detection}, - author={Hongyu Zhou, Zheng Ge, Songtao Liu, Weixin Mao, Zeming Li, Haiyan Yu, Jian Sun}, - journal={arXiv preprint arXiv:2207.02541}, - year={2022} -} -``` diff --git a/ppdet/engine/callbacks.py b/ppdet/engine/callbacks.py index 14ece8cebe2c4a7fb56e93cf922c401134974b7e..c14f50026e1ac1e43fb5dccc36883740573fea0c 100644 --- a/ppdet/engine/callbacks.py +++ b/ppdet/engine/callbacks.py @@ -159,7 +159,6 @@ class LogPrinter(Callback): class Checkpointer(Callback): def __init__(self, model): super(Checkpointer, self).__init__(model) - cfg = self.model.cfg self.best_ap = -1000. self.save_dir = os.path.join(self.model.cfg.save_dir, self.model.cfg.filename) @@ -210,14 +209,32 @@ class Checkpointer(Callback): key, eval_func, abs(self.best_ap))) if weight: if self.model.use_ema: - # save model and ema_model - save_model( - status['weight'], - self.model.optimizer, - self.save_dir, - save_name, - epoch_id + 1, - ema_model=weight) + exchange_save_model = status.get('exchange_save_model', + False) + if not exchange_save_model: + # save model and ema_model + save_model( + status['weight'], + self.model.optimizer, + self.save_dir, + save_name, + epoch_id + 1, + ema_model=weight) + else: + # save model(student model) and ema_model(teacher model) + # in DenseTeacher SSOD, the teacher model will be higher, + # so exchange when saving pdparams + student_model = status['weight'] # model + teacher_model = weight # ema_model + save_model( + teacher_model, + self.model.optimizer, + self.save_dir, + save_name, + epoch_id + 1, + ema_model=student_model) + del teacher_model + del student_model else: save_model(weight, self.model.optimizer, self.save_dir, save_name, epoch_id + 1) diff --git a/ppdet/engine/trainer_ssod.py b/ppdet/engine/trainer_ssod.py index 981a17df8ff898f20c9d8a7d21dded67f56f759b..891a1eaf4cb9b2900a566d1fb1df13e32afdf099 100644 --- a/ppdet/engine/trainer_ssod.py +++ b/ppdet/engine/trainer_ssod.py @@ -191,8 +191,11 @@ class Trainer_DenseTeacher(Trainer): self.status.update({ 'epoch_id': self.start_epoch, 'step_id': 0, - 'steps_per_epoch': len(self.loader) + 'steps_per_epoch': len(self.loader), + 'exchange_save_model': True, }) + # Note: exchange_save_model + # in DenseTeacher SSOD, the teacher model will be higher, so exchange when saving pdparams self.status['batch_time'] = stats.SmoothedValue( self.cfg.log_iter, fmt='{avg:.4f}') @@ -254,8 +257,12 @@ class Trainer_DenseTeacher(Trainer): data_sup_w, data_sup_s = align_weak_strong_shape(data_sup_w, data_sup_s) + data_sup_w['epoch_id'] = epoch_id + data_sup_s['epoch_id'] = epoch_id if concat_sup_data: for k, v in data_sup_s.items(): + if k in ['epoch_id']: + continue data_sup_s[k] = paddle.concat([v, data_sup_w[k]]) loss_dict_sup = self.model(data_sup_s) else: @@ -264,8 +271,6 @@ class Trainer_DenseTeacher(Trainer): for k, v in loss_dict_sup_w.items(): loss_dict_sup[k] = (loss_dict_sup[k] + v) * 0.5 - data_sup_w['epoch_id'] = epoch_id - data_sup_s['epoch_id'] = epoch_id losses_sup = loss_dict_sup['loss'] * train_cfg['sup_weight'] losses_sup.backward()