Upload folder using huggingface_hub (part 10)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +1 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/common_usage/single_multi_channel_applications.md +188 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/common_usage/specify_device.md +23 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/common_usage/tta.md +87 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/conf.py +117 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/get_started/15_minutes_instance_segmentation.md +330 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/get_started/15_minutes_object_detection.md +533 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/get_started/15_minutes_rotated_object_detection.md +3 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/get_started/article.md +105 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/get_started/dependencies.md +60 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/get_started/installation.md +129 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/get_started/overview.md +81 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/index.rst +122 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/make.bat +35 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/model_zoo.md +94 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/notes/changelog.md +365 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/notes/code_style.md +606 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/notes/compatibility.md +47 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/notes/conventions.md +37 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/algorithm_descriptions/index.rst +10 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/algorithm_descriptions/rtmdet_description.md +645 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/algorithm_descriptions/yolov5_description.md +652 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/algorithm_descriptions/yolov6_description.md +452 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/algorithm_descriptions/yolov8_description.md +244 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/application_examples/index.rst +7 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/application_examples/ionogram_detection.md +306 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/complexity_analysis.md +117 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/contributing.md +325 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/dataset_preparation.md +144 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/deploy/easydeploy_guide.md +5 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/deploy/index.rst +16 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/deploy/mmdeploy_guide.md +415 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/deploy/mmdeploy_yolov5.md +572 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/labeling_to_deployment_tutorials.md +1326 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/mm_basics.md +1 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/model_design.md +107 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/replace_backbone.md +307 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/training_testing_tricks.md +303 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/troubleshooting_steps.md +111 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/visualization.md +542 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/stat.py +66 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/switch_language.md +3 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/tutorials/config.md +553 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/tutorials/custom_installation.md +111 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/tutorials/data_flow.md +121 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/tutorials/faq.md +111 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/tutorials/rotated_detection.md +329 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/tutorials/warning_notes.md +22 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/useful_tools/browse_coco_json.md +62 -0
- video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/useful_tools/browse_dataset.md +57 -0
.gitattributes
CHANGED
|
@@ -62,3 +62,4 @@ video_gen_14d/models/Wan2.1-VACE-1.3B/google/umt5-xxl/tokenizer.json filter=lfs
|
|
| 62 |
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/data/lvis/annotations/lvis_v1_minival_inserted_image_name.json filter=lfs diff=lfs merge=lfs -text
|
| 63 |
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/lvis/lvis_v1_minival_inserted_image_name.json filter=lfs diff=lfs merge=lfs -text
|
| 64 |
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/data/coco/lvis/lvis_v1_minival_inserted_image_name.json filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 62 |
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/data/lvis/annotations/lvis_v1_minival_inserted_image_name.json filter=lfs diff=lfs merge=lfs -text
|
| 63 |
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/lvis/lvis_v1_minival_inserted_image_name.json filter=lfs diff=lfs merge=lfs -text
|
| 64 |
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/data/coco/lvis/lvis_v1_minival_inserted_image_name.json filter=lfs diff=lfs merge=lfs -text
|
| 65 |
+
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/YOLO-World/data/coco/lvis/lvis_v1_minival_inserted_image_name.json filter=lfs diff=lfs merge=lfs -text
|
video_gen_14d/third_party/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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 单通道和多通道应用案例
|
| 2 |
+
|
| 3 |
+
## 在单通道图像数据集上训练示例
|
| 4 |
+
|
| 5 |
+
MMYOLO 中默认的训练图片均为彩色三通道数据,如果希望采用单通道数据集进行训练和测试,预计需要修改的地方包括:
|
| 6 |
+
|
| 7 |
+
1. 所有的图片处理 pipeline 都要支持单通道运算
|
| 8 |
+
2. 模型的骨干网络的第一个卷积层输入通道需要从 3 改成 1
|
| 9 |
+
3. 如果希望加载 COCO 预训练权重,则需要处理第一个卷积层权重尺寸不匹配问题
|
| 10 |
+
|
| 11 |
+
下面以 `cat` 数据集为例,描述整个修改过程,如果你使用的是自定义灰度图像数据集,你可以跳过数据集预处理这一步。
|
| 12 |
+
|
| 13 |
+
### 1 数据集预处理
|
| 14 |
+
|
| 15 |
+
自定义数据集的处理训练可参照[自定义数据集 标注+训练+测试+部署 全流程](../recommended_topics/labeling_to_deployment_tutorials.md)。
|
| 16 |
+
|
| 17 |
+
`cat` 是一个三通道彩色图片数据集,为了方便演示,你可以运行下面的代码和命令,将数据集图片替换为单通道图片,方便后续验证。
|
| 18 |
+
|
| 19 |
+
**1. 下载 `cat` 数据集进行解压**
|
| 20 |
+
|
| 21 |
+
```shell
|
| 22 |
+
python tools/misc/download_dataset.py --dataset-name cat --save-dir ./data/cat --unzip --delete
|
| 23 |
+
```
|
| 24 |
+
|
| 25 |
+
**2. 将数据集转换为灰度图**
|
| 26 |
+
|
| 27 |
+
```python
|
| 28 |
+
import argparse
|
| 29 |
+
import imghdr
|
| 30 |
+
import os
|
| 31 |
+
from typing import List
|
| 32 |
+
import cv2
|
| 33 |
+
|
| 34 |
+
def parse_args():
|
| 35 |
+
parser = argparse.ArgumentParser(description='data_path')
|
| 36 |
+
parser.add_argument('path', type=str, help='Original dataset path')
|
| 37 |
+
return parser.parse_args()
|
| 38 |
+
|
| 39 |
+
def main():
|
| 40 |
+
args = parse_args()
|
| 41 |
+
|
| 42 |
+
path = args.path + '/images/'
|
| 43 |
+
save_path = path
|
| 44 |
+
file_list: List[str] = os.listdir(path)
|
| 45 |
+
# Grayscale conversion of each imager
|
| 46 |
+
for file in file_list:
|
| 47 |
+
if imghdr.what(path + '/' + file) != 'jpeg':
|
| 48 |
+
continue
|
| 49 |
+
img = cv2.imread(path + '/' + file)
|
| 50 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 51 |
+
cv2.imwrite(save_path + '/' + file, img)
|
| 52 |
+
|
| 53 |
+
if __name__ == '__main__':
|
| 54 |
+
main()
|
| 55 |
+
```
|
| 56 |
+
|
| 57 |
+
将上述脚本命名为 `cvt_single_channel.py`, 运行命令为:
|
| 58 |
+
|
| 59 |
+
```shell
|
| 60 |
+
python cvt_single_channel.py data/cat
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
### 2 修改 base 配置文件
|
| 64 |
+
|
| 65 |
+
**目前 MMYOLO 的一些图像处理函数例如颜色空间变换还不兼容单通道图片,如果直接采用单通道数据训练需要修改部分 pipeline,工作量较大**。为了解决不兼容问题,推荐的做法是将单通道图片作为采用三通道图片方式读取将其加载为三通道数据,但是在输入到网络前将其转换为单通道格式。这种做法会稍微增加一些运算负担,但是用户基本不需要修改代码即可使用。
|
| 66 |
+
|
| 67 |
+
以 `projects/misc/custom_dataset/yolov5_s-v61_syncbn_fast_1xb32-100e_cat.py`为 `base` 配置,将其复制到 `configs/yolov5` 目录下,在同级配置路径下新增 `yolov5_s-v61_syncbn_fast_1xb32-100e_cat_single_channel.py` 文件。 我们可以 `mmyolo/models/data_preprocessors/data_preprocessor.py` 文件中继承 `YOLOv5DetDataPreprocessor` 并命名新类为 `YOLOv5SCDetDataPreprocessor`, 在其中将图片转成单通道,添加依赖库并在`mmyolo/models/data_preprocessors/__init__.py`中注册新类。 `YOLOv5SCDetDataPreprocessor` 示例代码为:
|
| 68 |
+
|
| 69 |
+
```python
|
| 70 |
+
@MODELS.register_module()
|
| 71 |
+
class YOLOv5SCDetDataPreprocessor(YOLOv5DetDataPreprocessor):
|
| 72 |
+
"""Rewrite collate_fn to get faster training speed.
|
| 73 |
+
|
| 74 |
+
Note: It must be used together with `mmyolo.datasets.utils.yolov5_collate`
|
| 75 |
+
"""
|
| 76 |
+
|
| 77 |
+
def forward(self, data: dict, training: bool = False) -> dict:
|
| 78 |
+
"""Perform normalization, padding, bgr2rgb conversion and convert to single channel image based on ``DetDataPreprocessor``.
|
| 79 |
+
|
| 80 |
+
Args:
|
| 81 |
+
data (dict): Data sampled from dataloader.
|
| 82 |
+
training (bool): Whether to enable training time augmentation.
|
| 83 |
+
|
| 84 |
+
Returns:
|
| 85 |
+
dict: Data in the same format as the model input.
|
| 86 |
+
"""
|
| 87 |
+
if not training:
|
| 88 |
+
return super().forward(data, training)
|
| 89 |
+
|
| 90 |
+
data = self.cast_data(data)
|
| 91 |
+
inputs, data_samples = data['inputs'], data['data_samples']
|
| 92 |
+
assert isinstance(data['data_samples'], dict)
|
| 93 |
+
|
| 94 |
+
# TODO: Supports multi-scale training
|
| 95 |
+
if self._channel_conversion and inputs.shape[1] == 3:
|
| 96 |
+
inputs = inputs[:, [2, 1, 0], ...]
|
| 97 |
+
|
| 98 |
+
if self._enable_normalize:
|
| 99 |
+
inputs = (inputs - self.mean) / self.std
|
| 100 |
+
|
| 101 |
+
if self.batch_augments is not None:
|
| 102 |
+
for batch_aug in self.batch_augments:
|
| 103 |
+
inputs, data_samples = batch_aug(inputs, data_samples)
|
| 104 |
+
|
| 105 |
+
img_metas = [{'batch_input_shape': inputs.shape[2:]}] * len(inputs)
|
| 106 |
+
data_samples = {
|
| 107 |
+
'bboxes_labels': data_samples['bboxes_labels'],
|
| 108 |
+
'img_metas': img_metas
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
# Convert to single channel image
|
| 112 |
+
inputs = inputs.mean(dim=1, keepdim=True)
|
| 113 |
+
|
| 114 |
+
return {'inputs': inputs, 'data_samples': data_samples}
|
| 115 |
+
```
|
| 116 |
+
|
| 117 |
+
此时 `yolov5_s-v61_syncbn_fast_1xb32-100e_cat_single_channel.py`配置文件内容为如下所示:
|
| 118 |
+
|
| 119 |
+
```python
|
| 120 |
+
_base_ = 'yolov5_s-v61_syncbn_fast_1xb32-100e_cat.py'
|
| 121 |
+
|
| 122 |
+
_base_.model.data_preprocessor.type = 'YOLOv5SCDetDataPreprocessor'
|
| 123 |
+
```
|
| 124 |
+
|
| 125 |
+
### 3 预训练模型加载问题
|
| 126 |
+
|
| 127 |
+
直接使用原三通道的预训练模型,理论上会导致精度有所降低(未实验验证)。可采用的解决思路:将输入层 3 通道每个通道的权重调整为原 3 通道权重的平均值, 或将输入层每个通道的权重调整为原3通道某一通道权重,也可以对输入层权重不做修改直接训练,具体效果根据实际情况有所不同。这里采用将输入层 3 个通道权重调整为预训练 3 通道权重平均值的方式。
|
| 128 |
+
|
| 129 |
+
```python
|
| 130 |
+
import torch
|
| 131 |
+
|
| 132 |
+
def main():
|
| 133 |
+
# 加载权重文件
|
| 134 |
+
state_dict = torch.load(
|
| 135 |
+
'checkpoints/yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth'
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
# 修改输入层权重
|
| 139 |
+
weights = state_dict['state_dict']['backbone.stem.conv.weight']
|
| 140 |
+
avg_weight = weights.mean(dim=1, keepdim=True)
|
| 141 |
+
state_dict['state_dict']['backbone.stem.conv.weight'] = avg_weight
|
| 142 |
+
|
| 143 |
+
# 保存修改后的权重到新文件
|
| 144 |
+
torch.save(
|
| 145 |
+
state_dict,
|
| 146 |
+
'checkpoints/yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187_single_channel.pth'
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
if __name__ == '__main__':
|
| 150 |
+
main()
|
| 151 |
+
```
|
| 152 |
+
|
| 153 |
+
此时 `yolov5_s-v61_syncbn_fast_1xb32-100e_cat_single_channel.py`配置文件内容为如下所示:
|
| 154 |
+
|
| 155 |
+
```python
|
| 156 |
+
_base_ = 'yolov5_s-v61_syncbn_fast_1xb32-100e_cat.py'
|
| 157 |
+
|
| 158 |
+
_base_.model.data_preprocessor.type = 'YOLOv5SCDetDataPreprocessor'
|
| 159 |
+
|
| 160 |
+
load_from = './checkpoints/yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187_single_channel.pth'
|
| 161 |
+
```
|
| 162 |
+
|
| 163 |
+
### 4 模型训练效果
|
| 164 |
+
|
| 165 |
+
<img src="https://raw.githubusercontent.com/landhill/mmyolo/main/resources/cat_single_channel_test.jpeg"/>
|
| 166 |
+
|
| 167 |
+
左图是实际标签,右图是目标检测结果。
|
| 168 |
+
|
| 169 |
+
```shell
|
| 170 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.958
|
| 171 |
+
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 1.000
|
| 172 |
+
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 1.000
|
| 173 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000
|
| 174 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000
|
| 175 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.958
|
| 176 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.881
|
| 177 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.969
|
| 178 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.969
|
| 179 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000
|
| 180 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000
|
| 181 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.969
|
| 182 |
+
bbox_mAP_copypaste: 0.958 1.000 1.000 -1.000 -1.000 0.958
|
| 183 |
+
Epoch(val) [100][116/116] coco/bbox_mAP: 0.9580 coco/bbox_mAP_50: 1.0000 coco/bbox_mAP_75: 1.0000 coco/bbox_mAP_s: -1.0000 coco/bbox_mAP_m: -1.0000 coco/bbox_mAP_l: 0.9580
|
| 184 |
+
```
|
| 185 |
+
|
| 186 |
+
## 在多通道图像数据集上训练示例
|
| 187 |
+
|
| 188 |
+
TODO
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/common_usage/specify_device.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 指定特定设备训练或推理
|
| 2 |
+
|
| 3 |
+
如果你有多张 GPU,比如 8 张,其编号分别为 `0, 1, 2, 3, 4, 5, 6, 7`,使用单卡训练或推理时会默认使用卡 0。如果想指定其他卡进行训练或推理,可以使用以下命令:
|
| 4 |
+
|
| 5 |
+
```shell
|
| 6 |
+
CUDA_VISIBLE_DEVICES=5 python ./tools/train.py ${CONFIG} #train
|
| 7 |
+
CUDA_VISIBLE_DEVICES=5 python ./tools/test.py ${CONFIG} ${CHECKPOINT_FILE} #test
|
| 8 |
+
```
|
| 9 |
+
|
| 10 |
+
如果设置`CUDA_VISIBLE_DEVICES`为 -1 或者一个大于 GPU 最大编号的数,比如 8,将会使用 CPU 进行训练或者推理。
|
| 11 |
+
|
| 12 |
+
如果你想使用其中几张卡并行训练,可以使用如下命令:
|
| 13 |
+
|
| 14 |
+
```shell
|
| 15 |
+
CUDA_VISIBLE_DEVICES=0,1,2,3 ./tools/dist_train.sh ${CONFIG} ${GPU_NUM}
|
| 16 |
+
```
|
| 17 |
+
|
| 18 |
+
这里 `GPU_NUM` 为 4。另外如果在一台机器上多个任务同时多卡训练,需要设置不同的端口,比如以下命令:
|
| 19 |
+
|
| 20 |
+
```shell
|
| 21 |
+
CUDA_VISIBLE_DEVICES=0,1,2,3 PORT=29500 ./tools/dist_train.sh ${CONFIG} 4
|
| 22 |
+
CUDA_VISIBLE_DEVICES=4,5,6,7 PORT=29501 ./tools/dist_train.sh ${CONFIG} 4
|
| 23 |
+
```
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/common_usage/tta.md
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 测试时增强相关说明
|
| 2 |
+
|
| 3 |
+
## 测试时增强 TTA
|
| 4 |
+
|
| 5 |
+
MMYOLO 在 v0.5.0+ 版本中增加对 TTA 的支持,用户可以在进行评估时候指定 `--tta` 参数使用。 以 `YOLOv5-s` 为例,其单卡 TTA 测试命令为:
|
| 6 |
+
|
| 7 |
+
```shell
|
| 8 |
+
python tools/test.py configs/yolov5/yolov5_n-v61_syncbn_fast_8xb16-300e_coco.py https://download.openmmlab.com/mmyolo/v0/yolov5/yolov5_n-v61_syncbn_fast_8xb16-300e_coco/yolov5_n-v61_syncbn_fast_8xb16-300e_coco_20220919_090739-b804c1ad.pth --tta
|
| 9 |
+
```
|
| 10 |
+
|
| 11 |
+
TTA 功能的正常运行必须确保配置中存在 `tta_model` 和 `tta_pipeline` 两个变量,详情可以参考 [det_p5_tta.py](https://github.com/open-mmlab/mmyolo/blob/dev/configs/_base_/det_p5_tta.py)。
|
| 12 |
+
|
| 13 |
+
MMYOLO 中默认的 TTA 会先执行 3 个多尺度增强,然后在每个尺度中执行 2 种水平翻转增强,一共 6 个并行的 pipeline。以 `YOLOv5-s` 为例,其 TTA 配置为:
|
| 14 |
+
|
| 15 |
+
```python
|
| 16 |
+
img_scales = [(640, 640), (320, 320), (960, 960)]
|
| 17 |
+
|
| 18 |
+
_multiscale_resize_transforms = [
|
| 19 |
+
dict(
|
| 20 |
+
type='Compose',
|
| 21 |
+
transforms=[
|
| 22 |
+
dict(type='YOLOv5KeepRatioResize', scale=s),
|
| 23 |
+
dict(
|
| 24 |
+
type='LetterResize',
|
| 25 |
+
scale=s,
|
| 26 |
+
allow_scale_up=False,
|
| 27 |
+
pad_val=dict(img=114))
|
| 28 |
+
]) for s in img_scales
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
tta_pipeline = [
|
| 32 |
+
dict(type='LoadImageFromFile'),
|
| 33 |
+
dict(
|
| 34 |
+
type='TestTimeAug',
|
| 35 |
+
transforms=[
|
| 36 |
+
_multiscale_resize_transforms,
|
| 37 |
+
[
|
| 38 |
+
dict(type='mmdet.RandomFlip', prob=1.),
|
| 39 |
+
dict(type='mmdet.RandomFlip', prob=0.)
|
| 40 |
+
], [dict(type='mmdet.LoadAnnotations', with_bbox=True)],
|
| 41 |
+
[
|
| 42 |
+
dict(
|
| 43 |
+
type='mmdet.PackDetInputs',
|
| 44 |
+
meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',
|
| 45 |
+
'scale_factor', 'pad_param', 'flip',
|
| 46 |
+
'flip_direction'))
|
| 47 |
+
]
|
| 48 |
+
])
|
| 49 |
+
]
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
其示意图如下所示:
|
| 53 |
+
|
| 54 |
+
```text
|
| 55 |
+
LoadImageFromFile
|
| 56 |
+
/ | \
|
| 57 |
+
(RatioResize,LetterResize) (RatioResize,LetterResize) (RatioResize,LetterResize)
|
| 58 |
+
/ \ / \ / \
|
| 59 |
+
RandomFlip RandomFlip RandomFlip RandomFlip RandomFlip RandomFlip
|
| 60 |
+
| | | | | |
|
| 61 |
+
LoadAnn LoadAnn LoadAnn LoadAnn LoadAnn LoadAnn
|
| 62 |
+
| | | | | |
|
| 63 |
+
PackDetIn PackDetIn PackDetIn PackDetIn PackDetIn PackDetIn
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
你可以修改 `img_scales` 来支持不同的多尺度增强,也可以插入新的 pipeline 从而实现自定义 TTA 需求。 假设你只想进行水平翻转增强,则配置应该修改为如下:
|
| 67 |
+
|
| 68 |
+
```python
|
| 69 |
+
tta_pipeline = [
|
| 70 |
+
dict(type='LoadImageFromFile'),
|
| 71 |
+
dict(
|
| 72 |
+
type='TestTimeAug',
|
| 73 |
+
transforms=[
|
| 74 |
+
[
|
| 75 |
+
dict(type='mmdet.RandomFlip', prob=1.),
|
| 76 |
+
dict(type='mmdet.RandomFlip', prob=0.)
|
| 77 |
+
], [dict(type='mmdet.LoadAnnotations', with_bbox=True)],
|
| 78 |
+
[
|
| 79 |
+
dict(
|
| 80 |
+
type='mmdet.PackDetInputs',
|
| 81 |
+
meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',
|
| 82 |
+
'scale_factor', 'pad_param', 'flip',
|
| 83 |
+
'flip_direction'))
|
| 84 |
+
]
|
| 85 |
+
])
|
| 86 |
+
]
|
| 87 |
+
```
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/conf.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Configuration file for the Sphinx documentation builder.
|
| 2 |
+
#
|
| 3 |
+
# This file only contains a selection of the most common options. For a full
|
| 4 |
+
# list see the documentation:
|
| 5 |
+
# https://www.sphinx-doc.org/en/master/usage/configuration.html
|
| 6 |
+
|
| 7 |
+
# -- Path setup --------------------------------------------------------------
|
| 8 |
+
|
| 9 |
+
# If extensions (or modules to document with autodoc) are in another directory,
|
| 10 |
+
# add these directories to sys.path here. If the directory is relative to the
|
| 11 |
+
# documentation root, use os.path.abspath to make it absolute, like shown here.
|
| 12 |
+
#
|
| 13 |
+
import os
|
| 14 |
+
import subprocess
|
| 15 |
+
import sys
|
| 16 |
+
|
| 17 |
+
import pytorch_sphinx_theme
|
| 18 |
+
|
| 19 |
+
sys.path.insert(0, os.path.abspath('../../'))
|
| 20 |
+
|
| 21 |
+
# -- Project information -----------------------------------------------------
|
| 22 |
+
|
| 23 |
+
project = 'MMYOLO'
|
| 24 |
+
copyright = '2022, OpenMMLab'
|
| 25 |
+
author = 'MMYOLO Authors'
|
| 26 |
+
version_file = '../../mmyolo/version.py'
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def get_version():
|
| 30 |
+
with open(version_file) as f:
|
| 31 |
+
exec(compile(f.read(), version_file, 'exec'))
|
| 32 |
+
return locals()['__version__']
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
# The full version, including alpha/beta/rc tags
|
| 36 |
+
release = get_version()
|
| 37 |
+
|
| 38 |
+
# -- General configuration ---------------------------------------------------
|
| 39 |
+
|
| 40 |
+
# Add any Sphinx extension module names here, as strings. They can be
|
| 41 |
+
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
|
| 42 |
+
# ones.
|
| 43 |
+
extensions = [
|
| 44 |
+
'sphinx.ext.autodoc',
|
| 45 |
+
'sphinx.ext.napoleon',
|
| 46 |
+
'sphinx.ext.viewcode',
|
| 47 |
+
'myst_parser',
|
| 48 |
+
'sphinx_markdown_tables',
|
| 49 |
+
'sphinx_copybutton',
|
| 50 |
+
]
|
| 51 |
+
|
| 52 |
+
myst_enable_extensions = ['colon_fence']
|
| 53 |
+
myst_heading_anchors = 3
|
| 54 |
+
|
| 55 |
+
autodoc_mock_imports = [
|
| 56 |
+
'matplotlib', 'pycocotools', 'terminaltables', 'mmyolo.version', 'mmcv.ops'
|
| 57 |
+
]
|
| 58 |
+
|
| 59 |
+
# Add any paths that contain templates here, relative to this directory.
|
| 60 |
+
templates_path = ['_templates']
|
| 61 |
+
|
| 62 |
+
# The suffix(es) of source filenames.
|
| 63 |
+
# You can specify multiple suffix as a list of string:
|
| 64 |
+
#
|
| 65 |
+
source_suffix = {
|
| 66 |
+
'.rst': 'restructuredtext',
|
| 67 |
+
'.md': 'markdown',
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
# The master toctree document.
|
| 71 |
+
master_doc = 'index'
|
| 72 |
+
|
| 73 |
+
# List of patterns, relative to source directory, that match files and
|
| 74 |
+
# directories to ignore when looking for source files.
|
| 75 |
+
# This pattern also affects html_static_path and html_extra_path.
|
| 76 |
+
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
|
| 77 |
+
|
| 78 |
+
# -- Options for HTML output -------------------------------------------------
|
| 79 |
+
|
| 80 |
+
# The theme to use for HTML and HTML Help pages. See the documentation for
|
| 81 |
+
# a list of builtin themes.
|
| 82 |
+
#
|
| 83 |
+
# html_theme = 'sphinx_rtd_theme'
|
| 84 |
+
html_theme = 'pytorch_sphinx_theme'
|
| 85 |
+
html_theme_path = [pytorch_sphinx_theme.get_html_theme_path()]
|
| 86 |
+
|
| 87 |
+
html_theme_options = {
|
| 88 |
+
'menu': [
|
| 89 |
+
{
|
| 90 |
+
'name': 'GitHub',
|
| 91 |
+
'url': 'https://github.com/open-mmlab/mmyolo'
|
| 92 |
+
},
|
| 93 |
+
],
|
| 94 |
+
# Specify the language of shared menu
|
| 95 |
+
'menu_lang': 'cn',
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
# Add any paths that contain custom static files (such as style sheets) here,
|
| 99 |
+
# relative to this directory. They are copied after the builtin static files,
|
| 100 |
+
# so a file named "default.css" will overwrite the builtin "default.css".
|
| 101 |
+
html_static_path = ['_static']
|
| 102 |
+
html_css_files = ['css/readthedocs.css']
|
| 103 |
+
|
| 104 |
+
language = 'zh_CN'
|
| 105 |
+
|
| 106 |
+
# -- Extension configuration -------------------------------------------------
|
| 107 |
+
# Ignore >>> when copying code
|
| 108 |
+
copybutton_prompt_text = r'>>> |\.\.\. '
|
| 109 |
+
copybutton_prompt_is_regexp = True
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def builder_inited_handler(app):
|
| 113 |
+
subprocess.run(['./stat.py'])
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def setup(app):
|
| 117 |
+
app.connect('builder-inited', builder_inited_handler)
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/get_started/15_minutes_instance_segmentation.md
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 15 分钟上手 MMYOLO 实例分割
|
| 2 |
+
|
| 3 |
+
实例分割是计算机视觉中的一个任务,旨在将图像中的每个对象都分割出来,并为每个对象分配一个唯一的标识符。与语义分割不同,实例分割不仅分割出图像中的不同类别,还将同一类别的不同实例分开。
|
| 4 |
+
|
| 5 |
+
<div align=center>
|
| 6 |
+
<img src="https://github.com/open-mmlab/mmyolo/assets/87774050/6fd6316f-d78d-48a5-a413-86e7a74583fd" alt="Instance Segmentation" width="100%"/>
|
| 7 |
+
</div>
|
| 8 |
+
|
| 9 |
+
以可供下载的气球 balloon 小数据集为例,带大家 15 分钟轻松上手 MMYOLO 实例分割。整个流程包含如下步骤:
|
| 10 |
+
|
| 11 |
+
- [环境安装](#环境安装)
|
| 12 |
+
- [数据集准备](#数据集准备)
|
| 13 |
+
- [配置准备](#配置准备)
|
| 14 |
+
- [模型训练](#模型训练)
|
| 15 |
+
- [模型测试](#模型测试)
|
| 16 |
+
- [EasyDeploy 模型部署](#easydeploy-模型部署)
|
| 17 |
+
|
| 18 |
+
本文以 YOLOv5-s 为例,其余 YOLO 系列算法的气球 balloon 小数据集 demo 配置请查看对应的算法配置文件夹下。
|
| 19 |
+
|
| 20 |
+
## 环境安装
|
| 21 |
+
|
| 22 |
+
假设你已经提前安装好了 Conda,接下来安装 PyTorch
|
| 23 |
+
|
| 24 |
+
```shell
|
| 25 |
+
conda create -n mmyolo python=3.8 -y
|
| 26 |
+
conda activate mmyolo
|
| 27 |
+
# 如果你有 GPU
|
| 28 |
+
conda install pytorch torchvision -c pytorch
|
| 29 |
+
# 如果你是 CPU
|
| 30 |
+
# conda install pytorch torchvision cpuonly -c pytorch
|
| 31 |
+
```
|
| 32 |
+
|
| 33 |
+
安装 MMYOLO 和依赖库
|
| 34 |
+
|
| 35 |
+
```shell
|
| 36 |
+
git clone https://github.com/open-mmlab/mmyolo.git
|
| 37 |
+
cd mmyolo
|
| 38 |
+
pip install -U openmim
|
| 39 |
+
mim install -r requirements/mminstall.txt
|
| 40 |
+
# Install albumentations
|
| 41 |
+
mim install -r requirements/albu.txt
|
| 42 |
+
# Install MMYOLO
|
| 43 |
+
mim install -v -e .
|
| 44 |
+
# "-v" 指详细说明,或更多的输出
|
| 45 |
+
# "-e" 表示在可编辑模式下安装项目,因此对代码所做的任何本地修改都会生效,从而无需重新安装。
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
```{note}
|
| 49 |
+
温馨提醒:由于本仓库采用的是 OpenMMLab 2.0,请最好新建一个 conda 虚拟环境,防止和 OpenMMLab 1.0 已经安装的仓库冲突。
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
详细环境配置操作请查看 [安装和验证](./installation.md)
|
| 53 |
+
|
| 54 |
+
## 数据集准备
|
| 55 |
+
|
| 56 |
+
Balloon 数据集是一个包括 74 张图片的单类别数据集, 包括了训练所需的标注信息。 样例图片如下所示:
|
| 57 |
+
|
| 58 |
+
<div align=center>
|
| 59 |
+
<img src="https://user-images.githubusercontent.com/87774050/236993643-f581b087-9231-48a5-810b-97a5f31abe63.png" alt="balloon dataset"/>
|
| 60 |
+
</div>
|
| 61 |
+
|
| 62 |
+
你只需执行如下命令即可下载并且直接用起来
|
| 63 |
+
|
| 64 |
+
```shell
|
| 65 |
+
python tools/misc/download_dataset.py --dataset-name balloon --save-dir ./data/balloon --unzip --delete
|
| 66 |
+
python ./tools/dataset_converters/balloon2coco.py
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
data 位于 mmyolo 工程目录下, `train.json`, `val.json` 中存放的是 COCO 格式的标注,`data/balloon/train`, `data/balloon/val` 中存放的是所有图片
|
| 70 |
+
|
| 71 |
+
## 配置准备
|
| 72 |
+
|
| 73 |
+
以 YOLOv5 算法为例,考虑到用户显存和内存有限,我们需要修改一些默认训练参数来让大家愉快的跑起来,核心需要修改的参数如下
|
| 74 |
+
|
| 75 |
+
- YOLOv5 是 Anchor-Based 类算法,不同的数据集需要自适应计算合适的 Anchor
|
| 76 |
+
- 默认配置是 8 卡,每张卡 batch size 为 16,现将其改成单卡,每张卡 batch size 为 4
|
| 77 |
+
- 原则上 batch size 改变后,学习率也需要进行线性缩放,但是实测发现不需要
|
| 78 |
+
|
| 79 |
+
具体操作为在 `configs/yolov5/ins_seg` 文件夹下新建 `yolov5_ins_s-v61_syncbn_fast_8xb16-300e_balloon_instance.py` 配置文件(为了方便大家直接使用,我们已经提供了该配置),并把以下内容复制配置文件中。
|
| 80 |
+
|
| 81 |
+
```python
|
| 82 |
+
_base_ = './yolov5_ins_s-v61_syncbn_fast_8xb16-300e_coco_instance.py' # noqa
|
| 83 |
+
|
| 84 |
+
data_root = 'data/balloon/'
|
| 85 |
+
# 训练集标注路径
|
| 86 |
+
train_ann_file = 'train.json'
|
| 87 |
+
train_data_prefix = 'train/' # 训练集图片路径
|
| 88 |
+
# 测试集标注路径
|
| 89 |
+
val_ann_file = 'val.json'
|
| 90 |
+
val_data_prefix = 'val/' # 验证集图片路径
|
| 91 |
+
metainfo = {
|
| 92 |
+
'classes': ('balloon', ), # 数据集类别名称
|
| 93 |
+
'palette': [
|
| 94 |
+
(220, 20, 60),
|
| 95 |
+
]
|
| 96 |
+
}
|
| 97 |
+
num_classes = 1
|
| 98 |
+
# 批处理大小batch size设置为 4
|
| 99 |
+
train_batch_size_per_gpu = 4
|
| 100 |
+
# dataloader 加载进程数
|
| 101 |
+
train_num_workers = 2
|
| 102 |
+
log_interval = 1
|
| 103 |
+
#####################
|
| 104 |
+
train_dataloader = dict(
|
| 105 |
+
batch_size=train_batch_size_per_gpu,
|
| 106 |
+
num_workers=train_num_workers,
|
| 107 |
+
dataset=dict(
|
| 108 |
+
data_root=data_root,
|
| 109 |
+
metainfo=metainfo,
|
| 110 |
+
data_prefix=dict(img=train_data_prefix),
|
| 111 |
+
ann_file=train_ann_file))
|
| 112 |
+
val_dataloader = dict(
|
| 113 |
+
dataset=dict(
|
| 114 |
+
data_root=data_root,
|
| 115 |
+
metainfo=metainfo,
|
| 116 |
+
data_prefix=dict(img=val_data_prefix),
|
| 117 |
+
ann_file=val_ann_file))
|
| 118 |
+
test_dataloader = val_dataloader
|
| 119 |
+
val_evaluator = dict(ann_file=data_root + val_ann_file)
|
| 120 |
+
test_evaluator = val_evaluator
|
| 121 |
+
default_hooks = dict(logger=dict(interval=log_interval))
|
| 122 |
+
#####################
|
| 123 |
+
|
| 124 |
+
model = dict(bbox_head=dict(head_module=dict(num_classes=num_classes)))
|
| 125 |
+
```
|
| 126 |
+
|
| 127 |
+
以上配置从 `yolov5_ins_s-v61_syncbn_fast_8xb16-300e_coco_instance.py` 中继承,并根据 balloon 数据的特点更新了 `data_root`、`metainfo`、`train_dataloader`、`val_dataloader`、`num_classes` 等配置。
|
| 128 |
+
|
| 129 |
+
## 模型训练
|
| 130 |
+
|
| 131 |
+
```shell
|
| 132 |
+
python tools/train.py configs/yolov5/ins_seg/yolov5_ins_s-v61_syncbn_fast_8xb16-300e_balloon_instance.py
|
| 133 |
+
```
|
| 134 |
+
|
| 135 |
+
运行以上训练命令 `work_dirs/yolov5_ins_s-v61_syncbn_fast_8xb16-300e_balloon_instance` 文件夹会被自动生成,权重文件以及此次的训练配置文件将会保存在此文件夹中。 在 1660 低端显卡上,整个训练过程大概需要 30 分钟。
|
| 136 |
+
|
| 137 |
+
<div align=center>
|
| 138 |
+
<img src="https://user-images.githubusercontent.com/87774050/236995027-00a16a9e-2a2d-44cc-be8a-e2c8a36ff77f.png" alt="image"/>
|
| 139 |
+
</div>
|
| 140 |
+
|
| 141 |
+
在 `val.json` 上性能如下所示:
|
| 142 |
+
|
| 143 |
+
```text
|
| 144 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.330
|
| 145 |
+
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.509
|
| 146 |
+
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.317
|
| 147 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.000
|
| 148 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.103
|
| 149 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.417
|
| 150 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.150
|
| 151 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.396
|
| 152 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.454
|
| 153 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.000
|
| 154 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.317
|
| 155 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.525
|
| 156 |
+
```
|
| 157 |
+
|
| 158 |
+
上述性能是通过 COCO API 打印,其中 -1 表示不存在对于尺度的物体。
|
| 159 |
+
|
| 160 |
+
### 一些注意事项
|
| 161 |
+
|
| 162 |
+
在训练过程中会打印如下关键警告:
|
| 163 |
+
|
| 164 |
+
- You are using `YOLOv5Head` with num_classes == 1. The loss_cls will be 0. This is a normal phenomenon.
|
| 165 |
+
|
| 166 |
+
这个警告都不会对性能有任何影响。第一个警告是说明由于当前训练的类别数是 1,根据 YOLOv5 算法的社区, 分类分支的 loss 始终是 0,这是正常现象。
|
| 167 |
+
|
| 168 |
+
### 中断后恢复训练
|
| 169 |
+
|
| 170 |
+
如果训练中途停止,可以在训练命令最后加上 `--resume` ,程序会自动从 `work_dirs` 中加载最新的权重文件恢复训练。
|
| 171 |
+
|
| 172 |
+
```shell
|
| 173 |
+
python tools/train.py configs/yolov5/ins_seg/yolov5_ins_s-v61_syncbn_fast_8xb16-300e_balloon_instance.py --resume
|
| 174 |
+
```
|
| 175 |
+
|
| 176 |
+
### 节省显存策略
|
| 177 |
+
|
| 178 |
+
上述配置大概需要 1.0G 显存,如果你的显存不够,可以考虑开启混合精度训练
|
| 179 |
+
|
| 180 |
+
```shell
|
| 181 |
+
python tools/train.py configs/yolov5/ins_seg/yolov5_ins_s-v61_syncbn_fast_8xb16-300e_balloon_instance.py --amp
|
| 182 |
+
```
|
| 183 |
+
|
| 184 |
+
### 训练可视化
|
| 185 |
+
|
| 186 |
+
MMYOLO 目前支持本地、TensorBoard 以及 WandB 等多种后端可视化,默认是采用本地可视化方式,你可以切换为 WandB 等实时可视化训练过程中各类指标。
|
| 187 |
+
|
| 188 |
+
#### 1 WandB 可视化使用
|
| 189 |
+
|
| 190 |
+
WandB 官网注册并在 https://wandb.ai/settings 获取到 WandB 的 API Keys。
|
| 191 |
+
|
| 192 |
+
<div align=center>
|
| 193 |
+
<img src="https://cdn.vansin.top/img/20220913212628.png" alt="image"/>
|
| 194 |
+
</div>
|
| 195 |
+
|
| 196 |
+
```shell
|
| 197 |
+
pip install wandb
|
| 198 |
+
# 运行了 wandb login 后输入上文中获取到的 API Keys ,便登录成功。
|
| 199 |
+
wandb login
|
| 200 |
+
```
|
| 201 |
+
|
| 202 |
+
在 `configs/yolov5/ins_seg/yolov5_ins_s-v61_syncbn_fast_8xb16-300e_balloon_instance.py` 配置文件最后添加 WandB 配置
|
| 203 |
+
|
| 204 |
+
```python
|
| 205 |
+
visualizer = dict(vis_backends = [dict(type='LocalVisBackend'), dict(type='WandbVisBackend')])
|
| 206 |
+
```
|
| 207 |
+
|
| 208 |
+
重新运行训练命令便可以在命令行中提示的网页链接中看到 loss、学习率和 coco/bbox_mAP 等数据可视化了。
|
| 209 |
+
|
| 210 |
+
```shell
|
| 211 |
+
python tools/train.py configs/yolov5/ins_seg/yolov5_ins_s-v61_syncbn_fast_8xb16-300e_balloon_instance.py
|
| 212 |
+
```
|
| 213 |
+
|
| 214 |
+
#### 2 Tensorboard 可视化使用
|
| 215 |
+
|
| 216 |
+
安装 Tensorboard 环境
|
| 217 |
+
|
| 218 |
+
```shell
|
| 219 |
+
pip install tensorboard
|
| 220 |
+
```
|
| 221 |
+
|
| 222 |
+
同上述在配置文件 `configs/yolov5/ins_seg/yolov5_ins_s-v61_syncbn_fast_8xb16-300e_balloon_instance.py`配置的最后添加 `tensorboard` 配置
|
| 223 |
+
|
| 224 |
+
```python
|
| 225 |
+
visualizer = dict(vis_backends=[dict(type='LocalVisBackend'), dict(type='TensorboardVisBackend')])
|
| 226 |
+
```
|
| 227 |
+
|
| 228 |
+
重新运行训练命令后,Tensorboard 文件会生成在可视化文件夹 `work_dirs/yolov5_ins_s-v61_syncbn_fast_8xb16-300e_balloon_instance/{timestamp}/vis_data` 下,
|
| 229 |
+
运行下面的命令便可以在网页链接使用 Tensorboard 查看 loss、学习率和 coco/bbox_mAP 等可视化数据了:
|
| 230 |
+
|
| 231 |
+
```shell
|
| 232 |
+
tensorboard --logdir=work_dirs/yolov5_ins_s-v61_syncbn_fast_8xb16-300e_balloon_instance
|
| 233 |
+
```
|
| 234 |
+
|
| 235 |
+
## 模型测试
|
| 236 |
+
|
| 237 |
+
```shell
|
| 238 |
+
python tools/test.py configs/yolov5/ins_seg/yolov5_ins_s-v61_syncbn_fast_8xb16-300e_balloon_instance.py \
|
| 239 |
+
work_dirs/yolov5_ins_s-v61_syncbn_fast_8xb16-300e_balloon_instance/best_coco_bbox_mAP_epoch_300.pth \
|
| 240 |
+
--show-dir show_results
|
| 241 |
+
```
|
| 242 |
+
|
| 243 |
+
运行以上测试命令, 你不仅可以得到**模型训练**部分所打印的 AP 性能,还可以将推理结果图片自动保存至 `work_dirs/yolov5_ins_s-v61_syncbn_fast_8xb16-300e_balloon_instance/{timestamp}/show_results` 文件夹中。下面为其中一张结果图片,左图为实际标注,右图为模型推理结果。
|
| 244 |
+
|
| 245 |
+
<div align=center>
|
| 246 |
+
<img src="https://user-images.githubusercontent.com/87774050/236996421-46cfd38c-0827-4251-8216-408dfa9e03dd.jpg" alt="result_img"/>
|
| 247 |
+
</div>
|
| 248 |
+
|
| 249 |
+
如果你使用了 `WandbVisBackend` 或者 `TensorboardVisBackend`,则还可以在浏览器窗口可视化模型推理结果。
|
| 250 |
+
|
| 251 |
+
## 特征图相关可视化
|
| 252 |
+
|
| 253 |
+
MMYOLO 中提供了特征图相关可视化脚本,用于分析当前模型训练效果。 详细使用流程请参考 [特征图可视化](../recommended_topics/visualization.md)
|
| 254 |
+
|
| 255 |
+
由于 `test_pipeline` 直接可视化会存在偏差,故将需要 `configs/yolov5/yolov5_s-v61_syncbn_8xb16-300e_coco.py` 中 `test_pipeline`
|
| 256 |
+
|
| 257 |
+
```python
|
| 258 |
+
test_pipeline = [
|
| 259 |
+
dict(
|
| 260 |
+
type='LoadImageFromFile',
|
| 261 |
+
backend_args=_base_.backend_args),
|
| 262 |
+
dict(type='YOLOv5KeepRatioResize', scale=img_scale),
|
| 263 |
+
dict(
|
| 264 |
+
type='LetterResize',
|
| 265 |
+
scale=img_scale,
|
| 266 |
+
allow_scale_up=False,
|
| 267 |
+
pad_val=dict(img=114)),
|
| 268 |
+
dict(type='LoadAnnotations', with_bbox=True, _scope_='mmdet'),
|
| 269 |
+
dict(
|
| 270 |
+
type='mmdet.PackDetInputs',
|
| 271 |
+
meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',
|
| 272 |
+
'scale_factor', 'pad_param'))
|
| 273 |
+
]
|
| 274 |
+
```
|
| 275 |
+
|
| 276 |
+
修改为如下配置:
|
| 277 |
+
|
| 278 |
+
```python
|
| 279 |
+
test_pipeline = [
|
| 280 |
+
dict(
|
| 281 |
+
type='LoadImageFromFile',
|
| 282 |
+
backend_args=_base_.backend_args),
|
| 283 |
+
dict(type='mmdet.Resize', scale=img_scale, keep_ratio=False), # 删除 YOLOv5KeepRatioResize, 将 LetterResize 修改成 mmdet.Resize
|
| 284 |
+
dict(type='LoadAnnotations', with_bbox=True, _scope_='mmdet'),
|
| 285 |
+
dict(
|
| 286 |
+
type='mmdet.PackDetInputs',
|
| 287 |
+
meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',
|
| 288 |
+
'scale_factor')) # 删除 pad_param
|
| 289 |
+
]
|
| 290 |
+
```
|
| 291 |
+
|
| 292 |
+
我们选择 `data/balloon/train/3927754171_9011487133_b.jpg` 图片作为例子,可视化 YOLOv5 backbone 和 neck 层的输出特征图。
|
| 293 |
+
|
| 294 |
+
```shell
|
| 295 |
+
python demo/featmap_vis_demo.py data/balloon/train/3927754171_9011487133_b.jpg \
|
| 296 |
+
configs/yolov5/ins_seg/yolov5_ins_s-v61_syncbn_fast_8xb16-300e_balloon_instance.py \
|
| 297 |
+
work_dirs/yolov5_ins_s-v61_syncbn_fast_8xb16-300e_balloon_instance/best_coco_bbox_mAP_epoch_300.pth \ --target-layers backbone \
|
| 298 |
+
--channel-reduction squeeze_mean
|
| 299 |
+
```
|
| 300 |
+
|
| 301 |
+
<div align=center>
|
| 302 |
+
<img src="https://user-images.githubusercontent.com/87774050/236997582-233e292f-5e96-4e44-9e92-9e0787f302fc.jpg" width="800" alt="image"/>
|
| 303 |
+
</div>
|
| 304 |
+
|
| 305 |
+
结果会保存到当前路径的 output 文件夹下。上图中绘制的 3 个输出特征图对应大中小输出特征图。
|
| 306 |
+
|
| 307 |
+
**2. 可视化 YOLOv5 neck 输出的 3 个通道**
|
| 308 |
+
|
| 309 |
+
```shell
|
| 310 |
+
python demo/featmap_vis_demo.py data/balloon/train/3927754171_9011487133_b.jpg \
|
| 311 |
+
configs/yolov5/ins_seg/yolov5_ins_s-v61_syncbn_fast_8xb16-300e_balloon_instance.py \
|
| 312 |
+
work_dirs/yolov5_ins_s-v61_syncbn_fast_8xb16-300e_balloon_instance/best_coco_bbox_mAP_epoch_300.pth \ --target-layers neck \
|
| 313 |
+
--channel-reduction squeeze_mean
|
| 314 |
+
```
|
| 315 |
+
|
| 316 |
+
<div align=center>
|
| 317 |
+
<img src="https://user-images.githubusercontent.com/87774050/236997860-719d2e18-7767-4129-a072-b21c97a5502a.jpg" width="800" alt="image"/>
|
| 318 |
+
</div>
|
| 319 |
+
|
| 320 |
+
**3. Grad-Based CAM 可视化**
|
| 321 |
+
|
| 322 |
+
TODO
|
| 323 |
+
|
| 324 |
+
## EasyDeploy 模型部署
|
| 325 |
+
|
| 326 |
+
TODO
|
| 327 |
+
|
| 328 |
+
至此本教程结束。
|
| 329 |
+
|
| 330 |
+
以上完整内容可以查看 [15_minutes_instance_segmentation.ipynb](../../../demo/15_minutes_instance_segmentation.ipynb)。 如果你在训练或者测试过程中碰到问题,请先查看 [常见错误排除步骤](../recommended_topics/troubleshooting_steps.md), 如果依然无法解决欢迎提 issue。
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/get_started/15_minutes_object_detection.md
ADDED
|
@@ -0,0 +1,533 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
| 1 |
+
# 15 分钟上手 MMYOLO 目标检测
|
| 2 |
+
|
| 3 |
+
目标检测任务是指给定一张图片,网络预测出图片中所包括的所有物体类别和对应的边界框
|
| 4 |
+
|
| 5 |
+
<div align=center>
|
| 6 |
+
<img src="https://user-images.githubusercontent.com/17425982/220232979-fffa480b-9ae6-4601-8af6-4116265dc650.png" alt="object detection" width="100%"/>
|
| 7 |
+
</div>
|
| 8 |
+
|
| 9 |
+
以我们提供的猫 cat 小数据集为例,带大家 15 分钟轻松上手 MMYOLO 目标检测。整个流程包含如下步骤:
|
| 10 |
+
|
| 11 |
+
- [环境安装](#环境安装)
|
| 12 |
+
- [数据集准备](#数据集准备)
|
| 13 |
+
- [配置准备](#配置准备)
|
| 14 |
+
- [模型训练](#模型训练)
|
| 15 |
+
- [模型测试](#模型测试)
|
| 16 |
+
- [EasyDeploy 模型部署](#easydeploy-模型部署)
|
| 17 |
+
|
| 18 |
+
本文以 YOLOv5-s 为例,其余 YOLO 系列算法的猫 cat 小数据集 demo 配置请查看对应的算法配置文件夹下。
|
| 19 |
+
|
| 20 |
+
## 环境安装
|
| 21 |
+
|
| 22 |
+
假设你已经提前安装好了 Conda,接下来安装 PyTorch
|
| 23 |
+
|
| 24 |
+
```shell
|
| 25 |
+
conda create -n mmyolo python=3.8 -y
|
| 26 |
+
conda activate mmyolo
|
| 27 |
+
# 如果你有 GPU
|
| 28 |
+
conda install pytorch torchvision -c pytorch
|
| 29 |
+
# 如果你是 CPU
|
| 30 |
+
# conda install pytorch torchvision cpuonly -c pytorch
|
| 31 |
+
```
|
| 32 |
+
|
| 33 |
+
安装 MMYOLO 和依赖库
|
| 34 |
+
|
| 35 |
+
```shell
|
| 36 |
+
git clone https://github.com/open-mmlab/mmyolo.git
|
| 37 |
+
cd mmyolo
|
| 38 |
+
pip install -U openmim
|
| 39 |
+
mim install -r requirements/mminstall.txt
|
| 40 |
+
# Install albumentations
|
| 41 |
+
mim install -r requirements/albu.txt
|
| 42 |
+
# Install MMYOLO
|
| 43 |
+
mim install -v -e .
|
| 44 |
+
# "-v" 指详细说明,或更多的输出
|
| 45 |
+
# "-e" 表示在可编辑模式下安装项目,因此对代码所做的任何本地修改都会生效,从而无需重新安装。
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
```{note}
|
| 49 |
+
温馨提醒:由于本仓库采用的是 OpenMMLab 2.0,请最好新建一个 conda 虚拟环境,防止和 OpenMMLab 1.0 已经安装的仓库冲突。
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
详细环境配置操作请查看 [安装和验证](./installation.md)
|
| 53 |
+
|
| 54 |
+
## 数据集准备
|
| 55 |
+
|
| 56 |
+
Cat 数据集是一个包括 144 张图片的单类别数据集(本 cat 数据集由 @RangeKing 提供原始图片,由 @PeterH0323 进行数据清洗), 包括了训练所需的标注信息。 样例图片如下所示:
|
| 57 |
+
|
| 58 |
+
<div align=center>
|
| 59 |
+
<img src="https://user-images.githubusercontent.com/25873202/205423220-c4b8f2fd-22ba-4937-8e47-1b3f6a8facd8.png" alt="cat dataset"/>
|
| 60 |
+
</div>
|
| 61 |
+
|
| 62 |
+
你只需执行如下命令即可下载并且直接用起来
|
| 63 |
+
|
| 64 |
+
```shell
|
| 65 |
+
python tools/misc/download_dataset.py --dataset-name cat --save-dir ./data/cat --unzip --delete
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
数据集组织格式如下所示:
|
| 69 |
+
|
| 70 |
+
<div align=center>
|
| 71 |
+
<img src="https://user-images.githubusercontent.com/17425982/220072078-48b88a08-6179-483e-b8d3-0549e1b465de.png" alt="image"/>
|
| 72 |
+
</div>
|
| 73 |
+
|
| 74 |
+
data 位于 mmyolo 工程目录下, `data/cat/annotations` 中存放的是 COCO 格式的标注,`data/cat/images` 中存放的是所有图片
|
| 75 |
+
|
| 76 |
+
## 配置准备
|
| 77 |
+
|
| 78 |
+
以 YOLOv5 算法为例,考虑到用户显存和内存有限,我们需要修改一些默认训练参数来让大家愉快的跑起来,核心需要修改的参数如下
|
| 79 |
+
|
| 80 |
+
- YOLOv5 是 Anchor-Based 类算法,不同的数据集需要自适应计算合适的 Anchor
|
| 81 |
+
- 默认配置是 8 卡,每张卡 batch size 为 16,现将其改成单卡,每张卡 batch size 为 12
|
| 82 |
+
- 默认训练 epoch 是 300,将其改成 40 epoch
|
| 83 |
+
- 由于数据集太小,我们选择固定 backbone 网络权重
|
| 84 |
+
- 原则上 batch size 改变后,学习率也需要进行线性缩放,但是实测发现不需要
|
| 85 |
+
|
| 86 |
+
具体操作为在 `configs/yolov5` 文件夹下新建 `yolov5_s-v61_fast_1xb12-40e_cat.py` 配置文件(为了方便大家直接使用,我们已经提供了该配置),并把以下内容复制配置文件中。
|
| 87 |
+
|
| 88 |
+
```python
|
| 89 |
+
# 基于该配置进行继承并重写部分配置
|
| 90 |
+
_base_ = 'yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py'
|
| 91 |
+
|
| 92 |
+
data_root = './data/cat/' # 数据集根路径
|
| 93 |
+
class_name = ('cat', ) # 数据集类别名称
|
| 94 |
+
num_classes = len(class_name) # 数据集类别数
|
| 95 |
+
# metainfo 必须要传给后面的 dataloader 配置,否则无效
|
| 96 |
+
# palette 是可视化时候对应类别的显示颜色
|
| 97 |
+
# palette 长度必须大于或等于 classes 长度
|
| 98 |
+
metainfo = dict(classes=class_name, palette=[(20, 220, 60)])
|
| 99 |
+
|
| 100 |
+
# 基于 tools/analysis_tools/optimize_anchors.py 自适应计算的 anchor
|
| 101 |
+
anchors = [
|
| 102 |
+
[(68, 69), (154, 91), (143, 162)], # P3/8
|
| 103 |
+
[(242, 160), (189, 287), (391, 207)], # P4/16
|
| 104 |
+
[(353, 337), (539, 341), (443, 432)] # P5/32
|
| 105 |
+
]
|
| 106 |
+
# 最大训练 40 epoch
|
| 107 |
+
max_epochs = 40
|
| 108 |
+
# bs 为 12
|
| 109 |
+
train_batch_size_per_gpu = 12
|
| 110 |
+
# dataloader 加载进程数
|
| 111 |
+
train_num_workers = 4
|
| 112 |
+
|
| 113 |
+
# 加载 COCO 预训练权重
|
| 114 |
+
load_from = 'https://download.openmmlab.com/mmyolo/v0/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco/yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth' # noqa
|
| 115 |
+
|
| 116 |
+
model = dict(
|
| 117 |
+
# 固定整个 backbone 权重,不进行训练
|
| 118 |
+
backbone=dict(frozen_stages=4),
|
| 119 |
+
bbox_head=dict(
|
| 120 |
+
head_module=dict(num_classes=num_classes),
|
| 121 |
+
prior_generator=dict(base_sizes=anchors)
|
| 122 |
+
))
|
| 123 |
+
|
| 124 |
+
train_dataloader = dict(
|
| 125 |
+
batch_size=train_batch_size_per_gpu,
|
| 126 |
+
num_workers=train_num_workers,
|
| 127 |
+
dataset=dict(
|
| 128 |
+
data_root=data_root,
|
| 129 |
+
metainfo=metainfo,
|
| 130 |
+
# 数据集标注文件 json 路径
|
| 131 |
+
ann_file='annotations/trainval.json',
|
| 132 |
+
# 数据集前缀
|
| 133 |
+
data_prefix=dict(img='images/')))
|
| 134 |
+
|
| 135 |
+
val_dataloader = dict(
|
| 136 |
+
dataset=dict(
|
| 137 |
+
metainfo=metainfo,
|
| 138 |
+
data_root=data_root,
|
| 139 |
+
ann_file='annotations/test.json',
|
| 140 |
+
data_prefix=dict(img='images/')))
|
| 141 |
+
|
| 142 |
+
test_dataloader = val_dataloader
|
| 143 |
+
|
| 144 |
+
_base_.optim_wrapper.optimizer.batch_size_per_gpu = train_batch_size_per_gpu
|
| 145 |
+
|
| 146 |
+
val_evaluator = dict(ann_file=data_root + 'annotations/test.json')
|
| 147 |
+
test_evaluator = val_evaluator
|
| 148 |
+
|
| 149 |
+
default_hooks = dict(
|
| 150 |
+
# 每隔 10 个 epoch 保存一次权重,并且最多保存 2 个权重
|
| 151 |
+
# 模型评估时候自动保存最佳模型
|
| 152 |
+
checkpoint=dict(interval=10, max_keep_ckpts=2, save_best='auto'),
|
| 153 |
+
# warmup_mim_iter 参数非常关键,因为 cat 数据集非常小,默认的最小 warmup_mim_iter 是 1000,导致训练过程学习率偏小
|
| 154 |
+
param_scheduler=dict(max_epochs=max_epochs, warmup_mim_iter=10),
|
| 155 |
+
# 日志打印间隔为 5
|
| 156 |
+
logger=dict(type='LoggerHook', interval=5))
|
| 157 |
+
# 评估间隔为 10
|
| 158 |
+
train_cfg = dict(max_epochs=max_epochs, val_interval=10)
|
| 159 |
+
```
|
| 160 |
+
|
| 161 |
+
以上配置从 `yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py` 中继承,并根据 cat 数据的特点更新了 `data_root`、`metainfo`、`train_dataloader`、`val_dataloader`、`num_classes` 等配置。
|
| 162 |
+
|
| 163 |
+
## 模型训练
|
| 164 |
+
|
| 165 |
+
```shell
|
| 166 |
+
python tools/train.py configs/yolov5/yolov5_s-v61_fast_1xb12-40e_cat.py
|
| 167 |
+
```
|
| 168 |
+
|
| 169 |
+
运行以上训练命令 `work_dirs/yolov5_s-v61_fast_1xb12-40e_cat` 文件夹会被自动生成,权重文件以及此次的训练配置文件将会保存在此文件夹中。 在 1660 低端显卡上,整个训练过程大概需要 8 分钟。
|
| 170 |
+
|
| 171 |
+
<div align=center>
|
| 172 |
+
<img src="https://user-images.githubusercontent.com/17425982/220236361-bd113606-248e-4a0e-a484-c0dc9e355b5b.png" alt="image"/>
|
| 173 |
+
</div>
|
| 174 |
+
|
| 175 |
+
在 `test.json` 上性能如下所示:
|
| 176 |
+
|
| 177 |
+
```text
|
| 178 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.631
|
| 179 |
+
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.909
|
| 180 |
+
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.747
|
| 181 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000
|
| 182 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000
|
| 183 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.631
|
| 184 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.627
|
| 185 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.703
|
| 186 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.703
|
| 187 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000
|
| 188 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000
|
| 189 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.703
|
| 190 |
+
```
|
| 191 |
+
|
| 192 |
+
上述性能是通过 COCO API 打印,其中 -1 表示不存在对于尺度的物体。根据 COCO 定义的规则,Cat 数据集里面全部是大物体,不存在小和中等规模物体。
|
| 193 |
+
|
| 194 |
+
### 一些注意事项
|
| 195 |
+
|
| 196 |
+
在训练过程中会打印如下两个关键警告:
|
| 197 |
+
|
| 198 |
+
- You are using `YOLOv5Head` with num_classes == 1. The loss_cls will be 0. This is a normal phenomenon.
|
| 199 |
+
- The model and loaded state dict do not match exactly
|
| 200 |
+
|
| 201 |
+
这两个警告都不会对性能有任何影响。第一个警告是说明由于当前训练的类别数是 1,根据 YOLOv5 算法的社区, 分类分支的 loss 始终是 0,这是正常现象。第二个警告是因为目前是采用微调模式进行训练,我们加载了 COCO 80 个类的预训练权重,
|
| 202 |
+
这会导致最后的 Head 模块卷积通道数不对应,从而导致这部分权重无法加载,这也是正常现象。
|
| 203 |
+
|
| 204 |
+
### 中断后恢复训练
|
| 205 |
+
|
| 206 |
+
如果训练中途停止,可以在训练命令最后加上 `--resume` ,程序会自动从 `work_dirs` 中加载最新的权重文件恢复训练。
|
| 207 |
+
|
| 208 |
+
```shell
|
| 209 |
+
python tools/train.py configs/yolov5/yolov5_s-v61_fast_1xb12-40e_cat.py --resume
|
| 210 |
+
```
|
| 211 |
+
|
| 212 |
+
### 节省显存策略
|
| 213 |
+
|
| 214 |
+
上述配置大概需要 3.0G 显存,如果你的显存不够,可以考虑开启混合精度训练
|
| 215 |
+
|
| 216 |
+
```shell
|
| 217 |
+
python tools/train.py configs/yolov5/yolov5_s-v61_fast_1xb12-40e_cat.py --amp
|
| 218 |
+
```
|
| 219 |
+
|
| 220 |
+
### 训练可视化
|
| 221 |
+
|
| 222 |
+
MMYOLO 目前支持本地、TensorBoard 以及 WandB 等多种后端可视化,默认是采用本地可视化方式,你可以切换为 WandB 等实时可视化训练过程中各类指标。
|
| 223 |
+
|
| 224 |
+
#### 1 WandB 可视化使用
|
| 225 |
+
|
| 226 |
+
WandB 官网注册并在 https://wandb.ai/settings 获取到 WandB 的 API Keys。
|
| 227 |
+
|
| 228 |
+
<div align=center>
|
| 229 |
+
<img src="https://cdn.vansin.top/img/20220913212628.png" alt="image"/>
|
| 230 |
+
</div>
|
| 231 |
+
|
| 232 |
+
```shell
|
| 233 |
+
pip install wandb
|
| 234 |
+
# 运行了 wandb login 后输入上文中获取到的 API Keys ,便登录成功。
|
| 235 |
+
wandb login
|
| 236 |
+
```
|
| 237 |
+
|
| 238 |
+
在 `configs/yolov5/yolov5_s-v61_fast_1xb12-40e_cat.py` 配置文件最后添加 WandB 配置
|
| 239 |
+
|
| 240 |
+
```python
|
| 241 |
+
visualizer = dict(vis_backends = [dict(type='LocalVisBackend'), dict(type='WandbVisBackend')])
|
| 242 |
+
```
|
| 243 |
+
|
| 244 |
+
重新运行训练命令便可以在命令行中提示的网页链接中看到 loss、学习率和 coco/bbox_mAP 等数据可视化了。
|
| 245 |
+
|
| 246 |
+
```shell
|
| 247 |
+
python tools/train.py configs/yolov5/yolov5_s-v61_fast_1xb12-40e_cat.py
|
| 248 |
+
```
|
| 249 |
+
|
| 250 |
+
<div align=center>
|
| 251 |
+
<img src="https://user-images.githubusercontent.com/17425982/222131114-30a79285-56bc-427d-a38d-8d6a6982ad60.png" alt="image"/>
|
| 252 |
+
</div>
|
| 253 |
+
<div align=center>
|
| 254 |
+
<img src="https://user-images.githubusercontent.com/17425982/222132585-4b4962f1-211b-46f7-86b3-7534fc52a1b4.png" alt="image"/>
|
| 255 |
+
</div>
|
| 256 |
+
|
| 257 |
+
#### 2 Tensorboard 可视化使用
|
| 258 |
+
|
| 259 |
+
安装 Tensorboard 依赖
|
| 260 |
+
|
| 261 |
+
```shell
|
| 262 |
+
pip install tensorboard
|
| 263 |
+
```
|
| 264 |
+
|
| 265 |
+
同上述在配置文件 `configs/yolov5/yolov5_s-v61_fast_1xb12-40e_cat.py`配置的最后添加 `tensorboard` 配置
|
| 266 |
+
|
| 267 |
+
```python
|
| 268 |
+
visualizer = dict(vis_backends=[dict(type='LocalVisBackend'), dict(type='TensorboardVisBackend')])
|
| 269 |
+
```
|
| 270 |
+
|
| 271 |
+
重新运行训练命令后,Tensorboard 文件会生成在可视化文件夹 `work_dirs/yolov5_s-v61_fast_1xb12-40e_cat/{timestamp}/vis_data` 下,
|
| 272 |
+
运行下面的命令便可以在网页链接使用 Tensorboard 查看 loss、学习率和 coco/bbox_mAP 等可视化数据了:
|
| 273 |
+
|
| 274 |
+
```shell
|
| 275 |
+
tensorboard --logdir=work_dirs/yolov5_s-v61_fast_1xb12-40e_cat
|
| 276 |
+
```
|
| 277 |
+
|
| 278 |
+
## 模型测试
|
| 279 |
+
|
| 280 |
+
```shell
|
| 281 |
+
python tools/test.py configs/yolov5/yolov5_s-v61_fast_1xb12-40e_cat.py \
|
| 282 |
+
work_dirs/yolov5_s-v61_fast_1xb12-40e_cat/epoch_40.pth \
|
| 283 |
+
--show-dir show_results
|
| 284 |
+
```
|
| 285 |
+
|
| 286 |
+
运行以上测试命令, 你不仅可以得到**模型训练**部分所打印的 AP 性能,还可以将推理结果图片自动保存至 `work_dirs/yolov5_s-v61_fast_1xb12-40e_cat/{timestamp}/show_results` 文件夹中。下面为其中一张结果图片,左图为实际标注,右图为模型推理结果。
|
| 287 |
+
|
| 288 |
+
<div align=center>
|
| 289 |
+
<img src="https://user-images.githubusercontent.com/17425982/220251677-6c7e5c8f-9417-4803-97fc-a968d0172ab7.png" alt="result_img"/>
|
| 290 |
+
</div>
|
| 291 |
+
|
| 292 |
+
如果你使用了 `WandbVisBackend` 或者 `TensorboardVisBackend`,则还可以在浏览器窗口可视化模型推理结果。
|
| 293 |
+
|
| 294 |
+
## 特征图相关可视化
|
| 295 |
+
|
| 296 |
+
MMYOLO 中提供了特征图相关可视化脚本,用于分析当前模型训练效果。 详细使用流程请参考 [特征图可视化](../recommended_topics/visualization.md)
|
| 297 |
+
|
| 298 |
+
由于 `test_pipeline` 直接可视化会存在偏差,故将需要 `configs/yolov5/yolov5_s-v61_syncbn_8xb16-300e_coco.py` 中 `test_pipeline`
|
| 299 |
+
|
| 300 |
+
```python
|
| 301 |
+
test_pipeline = [
|
| 302 |
+
dict(
|
| 303 |
+
type='LoadImageFromFile',
|
| 304 |
+
backend_args=_base_.backend_args),
|
| 305 |
+
dict(type='YOLOv5KeepRatioResize', scale=img_scale),
|
| 306 |
+
dict(
|
| 307 |
+
type='LetterResize',
|
| 308 |
+
scale=img_scale,
|
| 309 |
+
allow_scale_up=False,
|
| 310 |
+
pad_val=dict(img=114)),
|
| 311 |
+
dict(type='LoadAnnotations', with_bbox=True, _scope_='mmdet'),
|
| 312 |
+
dict(
|
| 313 |
+
type='mmdet.PackDetInputs',
|
| 314 |
+
meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',
|
| 315 |
+
'scale_factor', 'pad_param'))
|
| 316 |
+
]
|
| 317 |
+
```
|
| 318 |
+
|
| 319 |
+
修改为如下配置:
|
| 320 |
+
|
| 321 |
+
```python
|
| 322 |
+
test_pipeline = [
|
| 323 |
+
dict(
|
| 324 |
+
type='LoadImageFromFile',
|
| 325 |
+
backend_args=_base_.backend_args),
|
| 326 |
+
dict(type='mmdet.Resize', scale=img_scale, keep_ratio=False), # 删除 YOLOv5KeepRatioResize, 将 LetterResize 修改成 mmdet.Resize
|
| 327 |
+
dict(type='LoadAnnotations', with_bbox=True, _scope_='mmdet'),
|
| 328 |
+
dict(
|
| 329 |
+
type='mmdet.PackDetInputs',
|
| 330 |
+
meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',
|
| 331 |
+
'scale_factor')) # 删除 pad_param
|
| 332 |
+
]
|
| 333 |
+
```
|
| 334 |
+
|
| 335 |
+
我们选择 `data/cat/images/IMG_20221020_112705.jpg` 图片作为例子,可视化 YOLOv5 backbone 和 neck 层的输出特征图。
|
| 336 |
+
|
| 337 |
+
**1. 可视化 YOLOv5 backbone 输出的 3 个通道**
|
| 338 |
+
|
| 339 |
+
```shell
|
| 340 |
+
python demo/featmap_vis_demo.py data/cat/images/IMG_20221020_112705.jpg \
|
| 341 |
+
configs/yolov5/yolov5_s-v61_fast_1xb12-40e_cat.py \
|
| 342 |
+
work_dirs/yolov5_s-v61_fast_1xb12-40e_cat/epoch_40.pth \
|
| 343 |
+
--target-layers backbone \
|
| 344 |
+
--channel-reduction squeeze_mean
|
| 345 |
+
```
|
| 346 |
+
|
| 347 |
+
<div align=center>
|
| 348 |
+
<img src="https://user-images.githubusercontent.com/17425982/220292217-b343a6f4-0c88-4fdb-9680-35d0ff8e5bdb.png" width="800" alt="image"/>
|
| 349 |
+
</div>
|
| 350 |
+
|
| 351 |
+
结果会保存到当前路径的 output 文件夹下。上图中绘制的 3 个输出特征图对应大中小输出特征图。由于本次训练的 backbone 实际上没有参与训练,从上图可以看到,大物体 cat 是在小特征图进行预测,这符合目标检测分层检测思想。
|
| 352 |
+
|
| 353 |
+
**2. 可视化 YOLOv5 neck 输出的 3 个通道**
|
| 354 |
+
|
| 355 |
+
```shell
|
| 356 |
+
python demo/featmap_vis_demo.py data/cat/images/IMG_20221020_112705.jpg \
|
| 357 |
+
configs/yolov5/yolov5_s-v61_fast_1xb12-40e_cat.py \
|
| 358 |
+
work_dirs/yolov5_s-v61_fast_1xb12-40e_cat/epoch_40.pth \
|
| 359 |
+
--target-layers neck \
|
| 360 |
+
--channel-reduction squeeze_mean
|
| 361 |
+
```
|
| 362 |
+
|
| 363 |
+
<div align=center>
|
| 364 |
+
<img src="https://user-images.githubusercontent.com/17425982/220293382-0a241415-e717-4688-a718-5f6d5c844785.png" width="800" alt="image"/>
|
| 365 |
+
</div>
|
| 366 |
+
|
| 367 |
+
从上图可以看出,由于 neck 是参与训练的,并且由于我们重新设置了 anchor, 强行让 3 个输出特征图都拟合同一个尺度的物体,导致 neck 输出的 3 个图类似,破坏了 backbone 原先的预训练分布。同时也可以看出 40 epoch 训练上述数据集是不够的,特征图效果不佳。
|
| 368 |
+
|
| 369 |
+
**3. Grad-Based CAM 可视化**
|
| 370 |
+
|
| 371 |
+
基于上述特征图可视化效果,我们可以分析特征层 bbox 级别的 Grad CAM。
|
| 372 |
+
|
| 373 |
+
安装 `grad-cam` 依赖:
|
| 374 |
+
|
| 375 |
+
```shell
|
| 376 |
+
pip install "grad-cam"
|
| 377 |
+
```
|
| 378 |
+
|
| 379 |
+
(a) 查看 neck 输出的最小输出特征图的 Grad CAM
|
| 380 |
+
|
| 381 |
+
```shell
|
| 382 |
+
python demo/boxam_vis_demo.py data/cat/images/IMG_20221020_112705.jpg \
|
| 383 |
+
configs/yolov5/yolov5_s-v61_fast_1xb12-40e_cat.py \
|
| 384 |
+
work_dirs/yolov5_s-v61_fast_1xb12-40e_cat/epoch_40.pth \
|
| 385 |
+
--target-layer neck.out_layers[2]
|
| 386 |
+
```
|
| 387 |
+
|
| 388 |
+
<div align=center>
|
| 389 |
+
<img src="https://user-images.githubusercontent.com/17425982/220298462-b0631f27-2366-4864-915a-a4ee21acd4b9.png" width="800" alt="image"/>
|
| 390 |
+
</div>
|
| 391 |
+
|
| 392 |
+
(b) 查看 neck 输出的中等输出特征图的 Grad CAM
|
| 393 |
+
|
| 394 |
+
```shell
|
| 395 |
+
python demo/boxam_vis_demo.py data/cat/images/IMG_20221020_112705.jpg \
|
| 396 |
+
configs/yolov5/yolov5_s-v61_fast_1xb12-40e_cat.py \
|
| 397 |
+
work_dirs/yolov5_s-v61_fast_1xb12-40e_cat/epoch_40.pth \
|
| 398 |
+
--target-layer neck.out_layers[1]
|
| 399 |
+
```
|
| 400 |
+
|
| 401 |
+
<div align=center>
|
| 402 |
+
<img src="https://user-images.githubusercontent.com/17425982/220298090-6f335786-0b35-4ab8-9c5a-0dbdb6b6c967.png" width="800" alt="image"/>
|
| 403 |
+
</div>
|
| 404 |
+
|
| 405 |
+
(c) 查看 neck 输出的最大输出特征图的 Grad CAM
|
| 406 |
+
|
| 407 |
+
```shell
|
| 408 |
+
python demo/boxam_vis_demo.py data/cat/images/IMG_20221020_112705.jpg \
|
| 409 |
+
configs/yolov5/yolov5_s-v61_fast_1xb12-40e_cat.py \
|
| 410 |
+
work_dirs/yolov5_s-v61_fast_1xb12-40e_cat/epoch_40.pth \
|
| 411 |
+
--target-layer neck.out_layers[0]
|
| 412 |
+
```
|
| 413 |
+
|
| 414 |
+
<div align=center>
|
| 415 |
+
<img src="https://user-images.githubusercontent.com/17425982/220297905-e23369db-d383-48f9-b15e-528a70ec7b23.png" width="800" alt="image"/>
|
| 416 |
+
</div>
|
| 417 |
+
|
| 418 |
+
## EasyDeploy 模型部署
|
| 419 |
+
|
| 420 |
+
此处我们将通过 MMYOLO 的 [EasyDeploy](../../../projects/easydeploy/) 来演示模型的转换部署和基本推理。
|
| 421 |
+
|
| 422 |
+
首先需要在当前 MMYOLO 的虚拟环境中按照 EasyDeploy 的 [基本文档](../../../projects/easydeploy/docs/model_convert.md) 对照自己的设备安装好所需的各个库。
|
| 423 |
+
|
| 424 |
+
```shell
|
| 425 |
+
pip install onnx onnxruntime
|
| 426 |
+
pip install onnx-simplifier # 如果需要使用 simplify 功能需要安装
|
| 427 |
+
pip install tensorrt # 如果有 GPU 环境并且需要输出 TensorRT 模型需要继续执行
|
| 428 |
+
```
|
| 429 |
+
|
| 430 |
+
完成安装后就可以用以下命令对已经训练好的针对 cat 数据集的模型一键转换部署,当前设备的 ONNX 版本为 1.13.0,TensorRT 版本为 8.5.3.1,故可保持 `--opset` 为 11,其余各项参数的具体含义和参数值需要对照使用的 config 文件进行调整。此处我们先导出 CPU 版本的 ONNX 模型,`--backend` 为 ONNXRUNTIME。
|
| 431 |
+
|
| 432 |
+
```shell
|
| 433 |
+
python projects/easydeploy/tools/export_onnx.py \
|
| 434 |
+
configs/yolov5/yolov5_s-v61_fast_1xb12-40e_cat.py \
|
| 435 |
+
work_dirs/yolov5_s-v61_fast_1xb12-40e_cat/epoch_40.pth \
|
| 436 |
+
--work-dir work_dirs/yolov5_s-v61_fast_1xb12-40e_cat \
|
| 437 |
+
--img-size 640 640 \
|
| 438 |
+
--batch 1 \
|
| 439 |
+
--device cpu \
|
| 440 |
+
--simplify \
|
| 441 |
+
--opset 11 \
|
| 442 |
+
--backend ONNXRUNTIME \
|
| 443 |
+
--pre-topk 1000 \
|
| 444 |
+
--keep-topk 100 \
|
| 445 |
+
--iou-threshold 0.65 \
|
| 446 |
+
--score-threshold 0.25
|
| 447 |
+
```
|
| 448 |
+
|
| 449 |
+
成功运行后就可以在 `work-dir` 下得到转换后的 ONNX 模型,默认使用 `end2end.onnx` 命名。
|
| 450 |
+
|
| 451 |
+
接下来我们使用此 `end2end.onnx` 模型来进行一个基本的图片推理:
|
| 452 |
+
|
| 453 |
+
```shell
|
| 454 |
+
python projects/easydeploy/tools/image-demo.py \
|
| 455 |
+
data/cat/images/IMG_20210728_205117.jpg \
|
| 456 |
+
configs/yolov5/yolov5_s-v61_fast_1xb12-40e_cat.py \
|
| 457 |
+
work_dirs/yolov5_s-v61_fast_1xb12-40e_cat/end2end.onnx \
|
| 458 |
+
--device cpu
|
| 459 |
+
```
|
| 460 |
+
|
| 461 |
+
成功完成推理后会在默认的 MMYOLO 根目录下的 `output` 文件夹生成推理结果图,如果想直观看到结果而不需要保存,可以在上述命令结尾加上 `--show` ,为了方便展示,下图是生成结果的截取部分。
|
| 462 |
+
|
| 463 |
+
<div align=center>
|
| 464 |
+
<img src="https://user-images.githubusercontent.com/7219519/221061210-b91e0b5b-652d-4dfc-8451-86a9a36f7d04.png" width="800" alt="image"/>
|
| 465 |
+
</div>
|
| 466 |
+
|
| 467 |
+
我们继续转换对应 TensorRT 的 engine 文件,因为 TensorRT 需要对应当前环境以及部署使用的版本进行,所以一定要确认导出参数,这里我们导出对应 TensorRT8 版本的文件,`--backend` 为 2。
|
| 468 |
+
|
| 469 |
+
```shell
|
| 470 |
+
python projects/easydeploy/tools/export.py \
|
| 471 |
+
configs/yolov5/yolov5_s-v61_fast_1xb12-40e_cat.py \
|
| 472 |
+
work_dirs/yolov5_s-v61_fast_1xb12-40e_cat/epoch_40.pth \
|
| 473 |
+
--work-dir work_dirs/yolov5_s-v61_fast_1xb12-40e_cat \
|
| 474 |
+
--img-size 640 640 \
|
| 475 |
+
--batch 1 \
|
| 476 |
+
--device cuda:0 \
|
| 477 |
+
--simplify \
|
| 478 |
+
--opset 11 \
|
| 479 |
+
--backend 2 \
|
| 480 |
+
--pre-topk 1000 \
|
| 481 |
+
--keep-topk 100 \
|
| 482 |
+
--iou-threshold 0.65 \
|
| 483 |
+
--score-threshold 0.25
|
| 484 |
+
```
|
| 485 |
+
|
| 486 |
+
成功执行后得到的 `end2end.onnx` 就是对应 TensorRT8 部署需要的 ONNX 文件,我们使用这个文件完成 TensorRT engine 的转换。
|
| 487 |
+
|
| 488 |
+
```shell
|
| 489 |
+
python projects/easydeploy/tools/build_engine.py \
|
| 490 |
+
work_dirs/yolov5_s-v61_fast_1xb12-40e_cat/end2end.onnx \
|
| 491 |
+
--img-size 640 640 \
|
| 492 |
+
--device cuda:0
|
| 493 |
+
```
|
| 494 |
+
|
| 495 |
+
成功执行后会在 `work-dir` 下生成 `end2end.engine` 文件:
|
| 496 |
+
|
| 497 |
+
```text
|
| 498 |
+
work_dirs/yolov5_s-v61_fast_1xb12-40e_cat
|
| 499 |
+
├── 202302XX_XXXXXX
|
| 500 |
+
│ ├── 202302XX_XXXXXX.log
|
| 501 |
+
│ └── vis_data
|
| 502 |
+
│ ├── 202302XX_XXXXXX.json
|
| 503 |
+
│ ├── config.py
|
| 504 |
+
│ └── scalars.json
|
| 505 |
+
├── best_coco
|
| 506 |
+
│ └── bbox_mAP_epoch_40.pth
|
| 507 |
+
├── end2end.engine
|
| 508 |
+
├── end2end.onnx
|
| 509 |
+
├── epoch_30.pth
|
| 510 |
+
├── epoch_40.pth
|
| 511 |
+
├── last_checkpoint
|
| 512 |
+
└── yolov5_s-v61_fast_1xb12-40e_cat.py
|
| 513 |
+
```
|
| 514 |
+
|
| 515 |
+
我们继续使用 `image-demo.py` 进行图片推理:
|
| 516 |
+
|
| 517 |
+
```shell
|
| 518 |
+
python projects/easydeploy/tools/image-demo.py \
|
| 519 |
+
data/cat/images/IMG_20210728_205312.jpg \
|
| 520 |
+
configs/yolov5/yolov5_s-v61_fast_1xb12-40e_cat.py \
|
| 521 |
+
work_dirs/yolov5_s-v61_fast_1xb12-40e_cat/end2end.engine \
|
| 522 |
+
--device cuda:0
|
| 523 |
+
```
|
| 524 |
+
|
| 525 |
+
此处依旧选择在 `output` 下保存推理结果而非直接显示结果,同样为了方便展示,下图是生成结果的截取部分。
|
| 526 |
+
|
| 527 |
+
<div align=center>
|
| 528 |
+
<img src="https://user-images.githubusercontent.com/7219519/221061291-e7490bb6-5f0c-45ab-9fc4-caf2b62419d6.png" width="800" alt="image"/>
|
| 529 |
+
</div>
|
| 530 |
+
|
| 531 |
+
这样我们就完成了将训练完成的模型进行转换部署并且检查推理结果的工作。至此本教程结束。
|
| 532 |
+
|
| 533 |
+
以上完整内容可以查看 [15_minutes_object_detection.ipynb](../../..//demo/15_minutes_object_detection.ipynb)。 如果你在训练或者测试过程中碰到问题,请先查看 [常见错误排除步骤](../recommended_topics/troubleshooting_steps.md),如果依然无法解决欢迎提 [issue](https://github.com/open-mmlab/mmyolo/issues/new/choose)。
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/get_started/15_minutes_rotated_object_detection.md
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 15 分钟上手 MMYOLO 旋转框目标检测
|
| 2 |
+
|
| 3 |
+
TODO
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/get_started/article.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 中文解读资源汇总
|
| 2 |
+
|
| 3 |
+
本文汇总了 MMYOLO 或相关的 [OpenMMLab](https://www.zhihu.com/people/openmmlab) 解读的部分文章(更多文章和视频见 [OpenMMLabCourse](https://github.com/open-mmlab/OpenMMLabCourse) ),如果您有推荐的文章(不一定是 OpenMMLab 发布的文章,可以是自己写的文章),非常欢迎提 Pull Request 添加到这里。
|
| 4 |
+
|
| 5 |
+
## MMYOLO 解读文章和资源
|
| 6 |
+
|
| 7 |
+
### 脚本命令速查表
|
| 8 |
+
|
| 9 |
+
<div align=center>
|
| 10 |
+
<img src="https://user-images.githubusercontent.com/27466624/213104312-3580c783-2423-442f-b5f6-79204a06adb5.png">
|
| 11 |
+
</div>
|
| 12 |
+
|
| 13 |
+
你可以点击[链接](https://pan.baidu.com/s/1QEaqT7YayUdEvh1an0gjHg?pwd=yolo),下载高清版 PDF 文件。
|
| 14 |
+
|
| 15 |
+
### 文章
|
| 16 |
+
|
| 17 |
+
- [社区协作,简洁易用,快来开箱新一代 YOLO 系列开源库](https://zhuanlan.zhihu.com/p/575615805)
|
| 18 |
+
- [MMYOLO 社区倾情贡献,RTMDet 原理社区开发者解读来啦!](https://zhuanlan.zhihu.com/p/569777684)
|
| 19 |
+
- [MMYOLO 自定义数据集从标注到部署保姆级教程](https://zhuanlan.zhihu.com/p/595497726)
|
| 20 |
+
- [满足一切需求的 MMYOLO 可视化:测试过程可视化](https://zhuanlan.zhihu.com/p/593179372)
|
| 21 |
+
- [MMYOLO 想你所想: 训练过程可视化](https://zhuanlan.zhihu.com/p/608586878)
|
| 22 |
+
- [YOLOv8 深度详解!一文看懂,快速上手](https://zhuanlan.zhihu.com/p/598566644)
|
| 23 |
+
- [玩转 MMYOLO 基础类第一期: 配置文件太复杂?继承用法看不懂?配置全解读来了](https://zhuanlan.zhihu.com/p/577715188)
|
| 24 |
+
- [玩转 MMYOLO 工具类第一期: 特征图可视化](https://zhuanlan.zhihu.com/p/578141381?)
|
| 25 |
+
- [玩转 MMYOLO 实用类第一期:源码阅读和调试「必备」技巧文档](https://zhuanlan.zhihu.com/p/580885852)
|
| 26 |
+
- [玩转 MMYOLO 基础类第二期:工程文件结构简析](https://zhuanlan.zhihu.com/p/584807195)
|
| 27 |
+
- [玩转 MMYOLO 实用类第二期:10分钟换遍主干网络文档](https://zhuanlan.zhihu.com/p/585641598)
|
| 28 |
+
|
| 29 |
+
### 视频
|
| 30 |
+
|
| 31 |
+
#### 工具类
|
| 32 |
+
|
| 33 |
+
| | 内容 | 视频 | 课程中的代码/文档 |
|
| 34 |
+
| :---: | :------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
|
| 35 |
+
| 第1讲 | 特征图可视化 | [](https://www.bilibili.com/video/BV188411s7o8) [](https://www.bilibili.com/video/BV188411s7o8) | [特征图可视化文档](https://zhuanlan.zhihu.com/p/578141381)<br>[特征图可视化.ipynb](https://github.com/open-mmlab/OpenMMLabCourse/blob/main/codes/MMYOLO_tutorials/%5B%E5%B7%A5%E5%85%B7%E7%B1%BB%E7%AC%AC%E4%B8%80%E6%9C%9F%5D%E7%89%B9%E5%BE%81%E5%9B%BE%E5%8F%AF%E8%A7%86%E5%8C%96.ipynb) |
|
| 36 |
+
| 第2讲 | 基于 sahi 的大图推理 | [](https://www.bilibili.com/video/BV1EK411R7Ws/) [](https://www.bilibili.com/video/BV1EK411R7Ws/) | [10分钟轻松掌握大图推理.ipynb](https://github.com/open-mmlab/OpenMMLabCourse/blob/main/codes/MMYOLO_tutorials/[工具类第二期]10分钟轻松掌握大图推理.ipynb) |
|
| 37 |
+
|
| 38 |
+
#### 基础类
|
| 39 |
+
|
| 40 |
+
| | 内容 | 视频 | 课程中的代码/文档 |
|
| 41 |
+
| :---: | :--------------: | :-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :------------------------------------------------------------: |
|
| 42 |
+
| 第1讲 | 配置全解读 | [](https://www.bilibili.com/video/BV1214y157ck) [](https://www.bilibili.com/video/BV1214y157ck) | [配置全解读文档](https://zhuanlan.zhihu.com/p/577715188) |
|
| 43 |
+
| 第2讲 | 工程文件结构简析 | [](https://www.bilibili.com/video/BV1LP4y117jS)[](https://www.bilibili.com/video/BV1LP4y117jS) | [工程文件结构简析文档](https://zhuanlan.zhihu.com/p/584807195) |
|
| 44 |
+
|
| 45 |
+
#### 实用类
|
| 46 |
+
|
| 47 |
+
| | 内容 | 视频 | 课程中的代码/文档 |
|
| 48 |
+
| :---: | :--------------------------------: | :-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
|
| 49 |
+
| 第1讲 | 源码阅读和调试「必备」技巧 | [](https://www.bilibili.com/video/BV1N14y1V7mB) [](https://www.bilibili.com/video/BV1N14y1V7mB) | [源码阅读和调试「必备」技巧文档](https://zhuanlan.zhihu.com/p/580885852) |
|
| 50 |
+
| 第2讲 | 10分钟换遍主干网络 | [](https://www.bilibili.com/video/BV1JG4y1d7GC) [](https://www.bilibili.com/video/BV1JG4y1d7GC) | [10分钟换遍主干网络文档](https://zhuanlan.zhihu.com/p/585641598)<br>[10分钟换遍主干网络.ipynb](https://github.com/open-mmlab/OpenMMLabCourse/blob/main/codes/MMYOLO_tutorials/[实用类第二期]10分钟换遍主干网络.ipynb) |
|
| 51 |
+
| 第3讲 | 自定义数据集从标注到部署保姆级教程 | [](https://www.bilibili.com/video/BV1RG4y137i5) [](https://www.bilibili.com/video/BV1RG4y137i5) | [自定义数据集从标注到部署保姆级教程](../recommended_topics/labeling_to_deployment_tutorials.md) |
|
| 52 |
+
| 第4讲 | 顶会第一步 · 模块自定义 | [](https://www.bilibili.com/video/BV1yd4y1j7VD) [](https://www.bilibili.com/video/BV1yd4y1j7VD) | [顶会第一步·模块自定义.ipynb](https://github.com/open-mmlab/OpenMMLabCourse/blob/main/codes/MMYOLO_tutorials/[实用类第四期]顶会第一步·模块自定义.ipynb) |
|
| 53 |
+
|
| 54 |
+
#### 源码解读类
|
| 55 |
+
|
| 56 |
+
#### 演示类
|
| 57 |
+
|
| 58 |
+
| | 内容 | 视频 |
|
| 59 |
+
| :---: | :----------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
|
| 60 |
+
| 第1期 | 特征图可视化 | [](https://www.bilibili.com/video/BV1je4y1478R/) [](https://www.bilibili.com/video/BV1je4y1478R/) |
|
| 61 |
+
|
| 62 |
+
## MMDetection 解读文章和资源
|
| 63 |
+
|
| 64 |
+
### 文章
|
| 65 |
+
|
| 66 |
+
- [MMDetection 3.0:目标检测新基准与前沿](https://zhuanlan.zhihu.com/p/575246786)
|
| 67 |
+
- [目标检测、实例分割、旋转框样样精通!详解高性能检测算法 RTMDet](https://zhuanlan.zhihu.com/p/598846422)
|
| 68 |
+
- [MMDetection 支持数据增强神器 Simple Copy Paste 全过程](https://zhuanlan.zhihu.com/p/559940982)
|
| 69 |
+
|
| 70 |
+
### 知乎问答和资源
|
| 71 |
+
|
| 72 |
+
- [深度学习科研,如何高效进行代码和实验管理?](https://www.zhihu.com/question/269707221/answer/2480772257)
|
| 73 |
+
- [深度学习方面的科研工作中的实验代码有什么规范和写作技巧?如何妥善管理实验数据?](https://www.zhihu.com/question/268193800/answer/2586000037)
|
| 74 |
+
- [COCO 数据集上 1x 模式下为什么不采用多尺度训练?](https://www.zhihu.com/question/462170786/answer/1915119662)
|
| 75 |
+
- [MMDetection 中 SOTA 论文源码中将训练过程中 BN 层的 eval 打开?](https://www.zhihu.com/question/471189603/answer/2195540892)
|
| 76 |
+
- [基于 PyTorch 的 MMDetection 中训练的随机性来自何处?](https://www.zhihu.com/question/453511684/answer/1839683634)
|
| 77 |
+
|
| 78 |
+
## MMEngine 解读文章和资源
|
| 79 |
+
|
| 80 |
+
- [从 MMCV 到 MMEngine,架构升级,体验升级!](https://zhuanlan.zhihu.com/p/571830155)
|
| 81 |
+
|
| 82 |
+
## MMCV 解读文章和资源
|
| 83 |
+
|
| 84 |
+
- [MMCV 全新升级,新增超全数据变换功能,还有两大变化](https://zhuanlan.zhihu.com/p/572550592)
|
| 85 |
+
- [手把手教你如何高效地在 MMCV 中贡献算子](https://zhuanlan.zhihu.com/p/464492627)
|
| 86 |
+
|
| 87 |
+
## PyTorch 解读文章和资源
|
| 88 |
+
|
| 89 |
+
- [PyTorch1.11 亮点一览:TorchData、functorch、DDP 静态图](https://zhuanlan.zhihu.com/p/486222256)
|
| 90 |
+
- [PyTorch1.12 亮点一览:DataPipe + TorchArrow 新的数据加载与处理范式](https://zhuanlan.zhihu.com/p/537868554)
|
| 91 |
+
- [PyTorch 源码解读之 nn.Module:核心网络模块接口详解](https://zhuanlan.zhihu.com/p/340453841)
|
| 92 |
+
- [PyTorch 源码解读之 torch.autograd:梯度计算详解](https://zhuanlan.zhihu.com/p/321449610)
|
| 93 |
+
- [PyTorch 源码解读之 torch.utils.data:解析数据处理全流程](https://zhuanlan.zhihu.com/p/337850513)
|
| 94 |
+
- [PyTorch 源码解读之 torch.optim:优化算法接口详解](https://zhuanlan.zhihu.com/p/346205754)
|
| 95 |
+
- [PyTorch 源码解读之 DP & DDP:模型并行和分布式训练解析](https://zhuanlan.zhihu.com/p/343951042)
|
| 96 |
+
- [PyTorch 源码解读之 BN & SyncBN:BN 与 多卡同步 BN 详解](https://zhuanlan.zhihu.com/p/337732517)
|
| 97 |
+
- [PyTorch 源码解读之 torch.cuda.amp: 自动混合精度详解](https://zhuanlan.zhihu.com/p/348554267)
|
| 98 |
+
- [PyTorch 源码解读之 cpp_extension:揭秘 C++/CUDA 算子实现和调用全流程](https://zhuanlan.zhihu.com/p/348555597)
|
| 99 |
+
- [PyTorch 源码解读之即时编译篇](https://zhuanlan.zhihu.com/p/361101354)
|
| 100 |
+
- [PyTorch 源码解读之分布式训练了解一下?](https://zhuanlan.zhihu.com/p/361314953)
|
| 101 |
+
- [PyTorch 源码解读之 torch.serialization & torch.hub](https://zhuanlan.zhihu.com/p/364239544)
|
| 102 |
+
|
| 103 |
+
## 其他
|
| 104 |
+
|
| 105 |
+
- [Type Hints 入门教程,让代码更加规范整洁](https://zhuanlan.zhihu.com/p/519335398)
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/get_started/dependencies.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 依赖
|
| 2 |
+
|
| 3 |
+
下表为 MMYOLO 和 MMEngine, MMCV, MMDetection 依赖库的版本要求,请安装正确的版本以避免安装问题。
|
| 4 |
+
|
| 5 |
+
| MMYOLO version | MMDetection version | MMEngine version | MMCV version |
|
| 6 |
+
| :------------: | :----------------------: | :----------------------: | :---------------------: |
|
| 7 |
+
| main | mmdet>=3.0.0, \<3.1.0 | mmengine>=0.7.1, \<1.0.0 | mmcv>=2.0.0rc4, \<2.1.0 |
|
| 8 |
+
| 0.6.0 | mmdet>=3.0.0, \<3.1.0 | mmengine>=0.7.1, \<1.0.0 | mmcv>=2.0.0rc4, \<2.1.0 |
|
| 9 |
+
| 0.5.0 | mmdet>=3.0.0rc6, \<3.1.0 | mmengine>=0.6.0, \<1.0.0 | mmcv>=2.0.0rc4, \<2.1.0 |
|
| 10 |
+
| 0.4.0 | mmdet>=3.0.0rc5, \<3.1.0 | mmengine>=0.3.1, \<1.0.0 | mmcv>=2.0.0rc0, \<2.1.0 |
|
| 11 |
+
| 0.3.0 | mmdet>=3.0.0rc5, \<3.1.0 | mmengine>=0.3.1, \<1.0.0 | mmcv>=2.0.0rc0, \<2.1.0 |
|
| 12 |
+
| 0.2.0 | mmdet>=3.0.0rc3, \<3.1.0 | mmengine>=0.3.1, \<1.0.0 | mmcv>=2.0.0rc0, \<2.1.0 |
|
| 13 |
+
| 0.1.3 | mmdet>=3.0.0rc3, \<3.1.0 | mmengine>=0.3.1, \<1.0.0 | mmcv>=2.0.0rc0, \<2.1.0 |
|
| 14 |
+
| 0.1.2 | mmdet>=3.0.0rc2, \<3.1.0 | mmengine>=0.3.0, \<1.0.0 | mmcv>=2.0.0rc0, \<2.1.0 |
|
| 15 |
+
| 0.1.1 | mmdet==3.0.0rc1 | mmengine>=0.1.0, \<0.2.0 | mmcv>=2.0.0rc0, \<2.1.0 |
|
| 16 |
+
| 0.1.0 | mmdet==3.0.0rc0 | mmengine>=0.1.0, \<0.2.0 | mmcv>=2.0.0rc0, \<2.1.0 |
|
| 17 |
+
|
| 18 |
+
本节中,我们将演示如何用 PyTorch 准备一个环境。
|
| 19 |
+
|
| 20 |
+
MMYOLO 支持在 Linux,Windows 和 macOS 上运行。它的基本环境依赖为:
|
| 21 |
+
|
| 22 |
+
- Python 3.7+
|
| 23 |
+
- PyTorch 1.7+
|
| 24 |
+
- CUDA 9.2+
|
| 25 |
+
- GCC 5.4+
|
| 26 |
+
|
| 27 |
+
```{note}
|
| 28 |
+
如果你对 PyTorch 有经验并且已经安装了它,你可以直接跳转到下一小节。否则,你可以按照下述步骤进行准备
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
**步骤 0.** 从 [官方网站](https://docs.conda.io/en/latest/miniconda.html) 下载并安装 Miniconda。
|
| 32 |
+
|
| 33 |
+
**步骤 1.** 创建并激活一个 conda 环境。
|
| 34 |
+
|
| 35 |
+
```shell
|
| 36 |
+
conda create -n mmyolo python=3.8 -y
|
| 37 |
+
conda activate mmyolo
|
| 38 |
+
```
|
| 39 |
+
|
| 40 |
+
**步骤 2.** 基于 [PyTorch 官方说明](https://pytorch.org/get-started/locally/) 安装 PyTorch。
|
| 41 |
+
|
| 42 |
+
在 GPU 平台上:
|
| 43 |
+
|
| 44 |
+
```shell
|
| 45 |
+
conda install pytorch torchvision -c pytorch
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
在 CPU 平台上:
|
| 49 |
+
|
| 50 |
+
```shell
|
| 51 |
+
conda install pytorch torchvision cpuonly -c pytorch
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
**步骤 3.** 验证 PyTorch 安装
|
| 55 |
+
|
| 56 |
+
```shell
|
| 57 |
+
python -c "import torch; print(torch.__version__); print(torch.cuda.is_available())"
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
如果是在 GPU 平台上,那么会打印版本信息和 True 字符,否则打印版本信息和 False 字符。
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/get_started/installation.md
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 安装和验证
|
| 2 |
+
|
| 3 |
+
## 最佳实践
|
| 4 |
+
|
| 5 |
+
**步骤 0.** 使用 [MIM](https://github.com/open-mmlab/mim) 安装 [MMEngine](https://github.com/open-mmlab/mmengine)、 [MMCV](https://github.com/open-mmlab/mmcv) 和 [MMDetection](https://github.com/open-mmlab/mmdetection) 。
|
| 6 |
+
|
| 7 |
+
```shell
|
| 8 |
+
pip install -U openmim
|
| 9 |
+
mim install "mmengine>=0.6.0"
|
| 10 |
+
mim install "mmcv>=2.0.0rc4,<2.1.0"
|
| 11 |
+
mim install "mmdet>=3.0.0,<4.0.0"
|
| 12 |
+
```
|
| 13 |
+
|
| 14 |
+
如果你当前已经处于 mmyolo 工程目录下,则可以采用如下简化写法
|
| 15 |
+
|
| 16 |
+
```shell
|
| 17 |
+
cd mmyolo
|
| 18 |
+
pip install -U openmim
|
| 19 |
+
mim install -r requirements/mminstall.txt
|
| 20 |
+
```
|
| 21 |
+
|
| 22 |
+
**注意:**
|
| 23 |
+
|
| 24 |
+
a. 在 MMCV-v2.x 中,`mmcv-full` 改名为 `mmcv`,如果你想安装不包含 CUDA 算子精简版,可以通过 `mim install mmcv-lite>=2.0.0rc1` 来安装。
|
| 25 |
+
|
| 26 |
+
b. 如果使用 `albumentations`,我们建议使用 `pip install -r requirements/albu.txt` 或者 `pip install -U albumentations --no-binary qudida,albumentations` 进行安装。 如果简单地使用 `pip install albumentations==1.0.1` 进行安装,则会同时安装 `opencv-python-headless`(即便已经安装了 `opencv-python` 也会再次安装)。我们建议在安装 albumentations 后检查环境,以确保没有同时安装 `opencv-python` 和 `opencv-python-headless`,因为同时安装可能会导致一些问题。更多细节请参考 [官方文档](https://albumentations.ai/docs/getting_started/installation/#note-on-opencv-dependencies) 。
|
| 27 |
+
|
| 28 |
+
**步骤 1.** 安装 MMYOLO
|
| 29 |
+
|
| 30 |
+
方案 1. 如果你基于 MMYOLO 框架开发自己的任务,建议从源码安装
|
| 31 |
+
|
| 32 |
+
```shell
|
| 33 |
+
git clone https://github.com/open-mmlab/mmyolo.git
|
| 34 |
+
cd mmyolo
|
| 35 |
+
# Install albumentations
|
| 36 |
+
mim install -r requirements/albu.txt
|
| 37 |
+
# Install MMYOLO
|
| 38 |
+
mim install -v -e .
|
| 39 |
+
# "-v" 指详细说明,或更多的输出
|
| 40 |
+
# "-e" 表示在可编辑模式下安装项目,因此对代码所做的任何本地修改都会生效,从而无需重新安装。
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
方案 2. 如果你将 MMYOLO 作为依赖或第三方 Python 包,使用 MIM 安装
|
| 44 |
+
|
| 45 |
+
```shell
|
| 46 |
+
mim install "mmyolo"
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
+
## 验证安装
|
| 50 |
+
|
| 51 |
+
为了验证 MMYOLO 是否安装正确,我们提供了一些示例代码来执行模型推理。
|
| 52 |
+
|
| 53 |
+
**步骤 1.** 我们需要下载配置文件和模型权重文件。
|
| 54 |
+
|
| 55 |
+
```shell
|
| 56 |
+
mim download mmyolo --config yolov5_s-v61_syncbn_fast_8xb16-300e_coco --dest .
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
下载将需要几秒钟或更长时间,这取决于你的网络环境。完成后,你会在当前文件夹中发现两个文件 `yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py` 和 `yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth`。
|
| 60 |
+
|
| 61 |
+
**步骤 2.** 推理验证
|
| 62 |
+
|
| 63 |
+
方案 1. 如果你通过源码安装的 MMYOLO,那么直接运行以下命令进行验证:
|
| 64 |
+
|
| 65 |
+
```shell
|
| 66 |
+
python demo/image_demo.py demo/demo.jpg \
|
| 67 |
+
yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py \
|
| 68 |
+
yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth
|
| 69 |
+
|
| 70 |
+
# 可选参数
|
| 71 |
+
# --out-dir ./output *检测结果输出到指定目录下,默认为./output, 当--show参数存在时,不保存检测结果
|
| 72 |
+
# --device cuda:0 *使用的计算资源,包括cuda, cpu等,默认为cuda:0
|
| 73 |
+
# --show *使用该参数表示在屏幕上显示检测结果,默认为False
|
| 74 |
+
# --score-thr 0.3 *置信度阈值,默认为0.3
|
| 75 |
+
```
|
| 76 |
+
|
| 77 |
+
运行结束后,在 `output` 文件夹中可以看到检测结果图像,图像中包含有网络预测的检测框。
|
| 78 |
+
|
| 79 |
+
支持输入类型包括
|
| 80 |
+
|
| 81 |
+
- 单张图片, 支持 `jpg`, `jpeg`, `png`, `ppm`, `bmp`, `pgm`, `tif`, `tiff`, `webp`。
|
| 82 |
+
- 文件目录,会遍历文件目录下所有图片文件,并输出对应结果。
|
| 83 |
+
- 网址,会自动从对应网址下载图片,并输出结果。
|
| 84 |
+
|
| 85 |
+
方案 2. 如果你通过 MIM 安装的 MMYOLO, 那么可以打开你的 Python 解析器,复制并粘贴以下代码:
|
| 86 |
+
|
| 87 |
+
```python
|
| 88 |
+
from mmdet.apis import init_detector, inference_detector
|
| 89 |
+
|
| 90 |
+
config_file = 'yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py'
|
| 91 |
+
checkpoint_file = 'yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth'
|
| 92 |
+
model = init_detector(config_file, checkpoint_file, device='cpu') # or device='cuda:0'
|
| 93 |
+
inference_detector(model, 'demo/demo.jpg')
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
你将会看到一个包含 `DetDataSample` 的列表,预测结果在 `pred_instance` 里,包含有预测框、预测分数 和 预测类别。
|
| 97 |
+
|
| 98 |
+
## 通过 Docker 使用 MMYOLO
|
| 99 |
+
|
| 100 |
+
我们提供了一个 [Dockerfile](https://github.com/open-mmlab/mmyolo/blob/main/docker/Dockerfile) 来构建一个镜像。请确保你的 [docker 版本](https://docs.docker.com/engine/install/) >=`19.03`。
|
| 101 |
+
|
| 102 |
+
温馨提示;国内用户建议取消掉 [Dockerfile](https://github.com/open-mmlab/mmyolo/blob/main/docker/Dockerfile#L19-L20) 里面 `Optional` 后两行的注释,可以获得火箭一般的下载提速:
|
| 103 |
+
|
| 104 |
+
```dockerfile
|
| 105 |
+
# (Optional)
|
| 106 |
+
RUN sed -i 's/http:\/\/archive.ubuntu.com\/ubuntu\//http:\/\/mirrors.aliyun.com\/ubuntu\//g' /etc/apt/sources.list && \
|
| 107 |
+
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
|
| 108 |
+
```
|
| 109 |
+
|
| 110 |
+
构建命令:
|
| 111 |
+
|
| 112 |
+
```shell
|
| 113 |
+
# build an image with PyTorch 1.9, CUDA 11.1
|
| 114 |
+
# If you prefer other versions, just modified the Dockerfile
|
| 115 |
+
docker build -t mmyolo docker/
|
| 116 |
+
```
|
| 117 |
+
|
| 118 |
+
用以下命令运行 Docker 镜像:
|
| 119 |
+
|
| 120 |
+
```shell
|
| 121 |
+
export DATA_DIR=/path/to/your/dataset
|
| 122 |
+
docker run --gpus all --shm-size=8g -it -v ${DATA_DIR}:/mmyolo/data mmyolo
|
| 123 |
+
```
|
| 124 |
+
|
| 125 |
+
其余自定义安装流程请查看 [自定义安装](../tutorials/custom_installation.md)
|
| 126 |
+
|
| 127 |
+
## 排除故障
|
| 128 |
+
|
| 129 |
+
如果你在安装过程中遇到一些问题,你可以在 GitHub 上 [打开一个问题](https://github.com/open-mmlab/mmyolo/issues/new/choose)。
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/get_started/overview.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 概述
|
| 2 |
+
|
| 3 |
+
## MMYOLO 介绍
|
| 4 |
+
|
| 5 |
+
<div align=center>
|
| 6 |
+
<img src="https://user-images.githubusercontent.com/45811724/190993591-bd3f1f11-1c30-4b93-b5f4-05c9ff64ff7f.gif" alt="图片"/>
|
| 7 |
+
</div>
|
| 8 |
+
|
| 9 |
+
MMYOLO 是一个基于 PyTorch 和 MMDetection 的 YOLO 系列算法开源工具箱,它是 [OpenMMLab](https://openmmlab.com/) 项目的一部分。 MMYOLO 定位为 YOLO 系列热门开源库以及工业应用核心库,其愿景图如下所示:
|
| 10 |
+
|
| 11 |
+
<div align=center>
|
| 12 |
+
<img src="https://user-images.githubusercontent.com/17425982/220060451-d50414e5-a239-45b7-a4db-ed8699820300.png" alt="愿景图"/>
|
| 13 |
+
</div>
|
| 14 |
+
|
| 15 |
+
目前支持的任务如下:
|
| 16 |
+
|
| 17 |
+
<details open>
|
| 18 |
+
<summary><b>支持的任务</b></summary>
|
| 19 |
+
|
| 20 |
+
- 目标检测
|
| 21 |
+
- 旋转框目标检测
|
| 22 |
+
|
| 23 |
+
</details>
|
| 24 |
+
|
| 25 |
+
目前支持的 YOLO 系列算法如下:
|
| 26 |
+
|
| 27 |
+
<details open>
|
| 28 |
+
<summary><b>支持的算法</b></summary>
|
| 29 |
+
|
| 30 |
+
- YOLOv5
|
| 31 |
+
- YOLOX
|
| 32 |
+
- RTMDet
|
| 33 |
+
- RTMDet-Rotated
|
| 34 |
+
- YOLOv6
|
| 35 |
+
- YOLOv7
|
| 36 |
+
- PPYOLOE
|
| 37 |
+
- YOLOv8
|
| 38 |
+
|
| 39 |
+
</details>
|
| 40 |
+
|
| 41 |
+
目前支持的数据集如下:
|
| 42 |
+
|
| 43 |
+
<details open>
|
| 44 |
+
<summary><b>支持的数据集</b></summary>
|
| 45 |
+
|
| 46 |
+
- COCO Dataset
|
| 47 |
+
- VOC Dataset
|
| 48 |
+
- CrowdHuman Dataset
|
| 49 |
+
- DOTA 1.0 Dataset
|
| 50 |
+
|
| 51 |
+
</details>
|
| 52 |
+
|
| 53 |
+
MMYOLO 支持在 Linux、Windows、macOS 上运行, 支持 PyTorch 1.7 及其以上版本运行。它具有如下三个特性:
|
| 54 |
+
|
| 55 |
+
- 🕹️ **统一便捷的算法评测**
|
| 56 |
+
|
| 57 |
+
MMYOLO 统一了各类 YOLO 算法模块的实现,并提供了统一的评测流程,用户可以公平便捷地进行对比分析。
|
| 58 |
+
|
| 59 |
+
- 📚 **丰富的入门和进阶文档**
|
| 60 |
+
|
| 61 |
+
MMYOLO 提供了从入门到部署到进阶和算法解析等一系列文档,方便不同用户快速上手和扩展。
|
| 62 |
+
|
| 63 |
+
- 🧩 **模块化设计**
|
| 64 |
+
|
| 65 |
+
MMYOLO 将框架解耦成不同的模块组件,通过组合不同的模块和训练测试策略,用户可以便捷地构建自定义模型。
|
| 66 |
+
|
| 67 |
+
<img src="https://user-images.githubusercontent.com/27466624/199999337-0544a4cb-3cbd-4f3e-be26-bcd9e74db7ff.jpg" alt="基类-P5"/>
|
| 68 |
+
图为 RangeKing@GitHub 提供,非常感谢!
|
| 69 |
+
|
| 70 |
+
## 本文档使用指南
|
| 71 |
+
|
| 72 |
+
MMYOLO 中将文档结构分成 6 个部分,对应不同需求的用户。
|
| 73 |
+
|
| 74 |
+
- **开启 MMYOLO 之旅**。本部分是第一次使用 MMYOLO 用户的必读文档,请全文仔细阅读
|
| 75 |
+
- **推荐专题**。本部分是 MMYOLO 中提供的以主题形式的精华文档,包括了 MMYOLO 中大量的特性等。强烈推荐使用 MMYOLO 的所有用户阅读
|
| 76 |
+
- **常用功能**。本部分提供了训练测试过程中用户经常会用到的各类常用功能,用户可以在用到时候再次查阅
|
| 77 |
+
- **实用工具**。本部分是 tools 下使用工具的汇总文档,便于大家能够快速的愉快使用 MMYOLO 中提供的各类脚本
|
| 78 |
+
- **基础和进阶教程**。本部分涉及到 MMYOLO 中的一些基本概念和进阶教程等,适合想详细了解 MMYOLO 设计思想和结构设计的用户
|
| 79 |
+
- **其他**。其余部分包括模型仓库、说明和接口文档等等
|
| 80 |
+
|
| 81 |
+
不同需求的用户可以按需选择你心怡的内容阅读。如果你对本文档有异议或者更好的优化办法,欢迎给 MMYOLO 提 PR ~, 请参考 [如何给 MMYOLO 贡献代码](../recommended_topics/contributing.md)
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/index.rst
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
欢迎来到 MMYOLO 中文文档!
|
| 2 |
+
=======================================
|
| 3 |
+
您可以在页面右上角切换中英文文档。
|
| 4 |
+
|
| 5 |
+
.. toctree::
|
| 6 |
+
:maxdepth: 2
|
| 7 |
+
:caption: 开启 MMYOLO 之旅
|
| 8 |
+
|
| 9 |
+
get_started/overview.md
|
| 10 |
+
get_started/dependencies.md
|
| 11 |
+
get_started/installation.md
|
| 12 |
+
get_started/15_minutes_object_detection.md
|
| 13 |
+
get_started/15_minutes_rotated_object_detection.md
|
| 14 |
+
get_started/15_minutes_instance_segmentation.md
|
| 15 |
+
get_started/article.md
|
| 16 |
+
|
| 17 |
+
.. toctree::
|
| 18 |
+
:maxdepth: 2
|
| 19 |
+
:caption: 推荐专题
|
| 20 |
+
|
| 21 |
+
recommended_topics/contributing.md
|
| 22 |
+
recommended_topics/training_testing_tricks.md
|
| 23 |
+
recommended_topics/model_design.md
|
| 24 |
+
recommended_topics/algorithm_descriptions/index.rst
|
| 25 |
+
recommended_topics/application_examples/index.rst
|
| 26 |
+
recommended_topics/replace_backbone.md
|
| 27 |
+
recommended_topics/complexity_analysis.md
|
| 28 |
+
recommended_topics/labeling_to_deployment_tutorials.md
|
| 29 |
+
recommended_topics/visualization.md
|
| 30 |
+
recommended_topics/deploy/index.rst
|
| 31 |
+
recommended_topics/troubleshooting_steps.md
|
| 32 |
+
recommended_topics/mm_basics.md
|
| 33 |
+
recommended_topics/dataset_preparation.md
|
| 34 |
+
|
| 35 |
+
.. toctree::
|
| 36 |
+
:maxdepth: 2
|
| 37 |
+
:caption: 常用功能
|
| 38 |
+
|
| 39 |
+
common_usage/resume_training.md
|
| 40 |
+
common_usage/syncbn.md
|
| 41 |
+
common_usage/amp_training.md
|
| 42 |
+
common_usage/ms_training_testing.md
|
| 43 |
+
common_usage/tta.md
|
| 44 |
+
common_usage/plugins.md
|
| 45 |
+
common_usage/freeze_layers.md
|
| 46 |
+
common_usage/output_predictions.md
|
| 47 |
+
common_usage/set_random_seed.md
|
| 48 |
+
common_usage/module_combination.md
|
| 49 |
+
common_usage/mim_usage.md
|
| 50 |
+
common_usage/multi_necks.md
|
| 51 |
+
common_usage/specify_device.md
|
| 52 |
+
common_usage/single_multi_channel_applications.md
|
| 53 |
+
common_usage/registries_info.md
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
.. toctree::
|
| 57 |
+
:maxdepth: 2
|
| 58 |
+
:caption: 实用工具
|
| 59 |
+
|
| 60 |
+
useful_tools/browse_coco_json.md
|
| 61 |
+
useful_tools/browse_dataset.md
|
| 62 |
+
useful_tools/print_config.md
|
| 63 |
+
useful_tools/dataset_analysis.md
|
| 64 |
+
useful_tools/optimize_anchors.md
|
| 65 |
+
useful_tools/extract_subcoco.md
|
| 66 |
+
useful_tools/vis_scheduler.md
|
| 67 |
+
useful_tools/dataset_converters.md
|
| 68 |
+
useful_tools/download_dataset.md
|
| 69 |
+
useful_tools/log_analysis.md
|
| 70 |
+
useful_tools/model_converters.md
|
| 71 |
+
|
| 72 |
+
.. toctree::
|
| 73 |
+
:maxdepth: 2
|
| 74 |
+
:caption: 基础教程
|
| 75 |
+
|
| 76 |
+
tutorials/config.md
|
| 77 |
+
tutorials/data_flow.md
|
| 78 |
+
tutorials/rotated_detection.md
|
| 79 |
+
tutorials/custom_installation.md
|
| 80 |
+
tutorials/warning_notes.md
|
| 81 |
+
tutorials/faq.md
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
.. toctree::
|
| 85 |
+
:maxdepth: 2
|
| 86 |
+
:caption: 进阶教程
|
| 87 |
+
|
| 88 |
+
advanced_guides/cross-library_application.md
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
.. toctree::
|
| 92 |
+
:maxdepth: 2
|
| 93 |
+
:caption: 模型仓库
|
| 94 |
+
|
| 95 |
+
model_zoo.md
|
| 96 |
+
|
| 97 |
+
.. toctree::
|
| 98 |
+
:maxdepth: 1
|
| 99 |
+
:caption: 说明
|
| 100 |
+
|
| 101 |
+
notes/changelog.md
|
| 102 |
+
notes/compatibility.md
|
| 103 |
+
notes/conventions.md
|
| 104 |
+
notes/code_style.md
|
| 105 |
+
|
| 106 |
+
.. toctree::
|
| 107 |
+
:maxdepth: 1
|
| 108 |
+
:caption: 接口文档(英文)
|
| 109 |
+
|
| 110 |
+
api.rst
|
| 111 |
+
|
| 112 |
+
.. toctree::
|
| 113 |
+
:caption: 语言切换
|
| 114 |
+
|
| 115 |
+
switch_language.md
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
Indices and tables
|
| 119 |
+
==================
|
| 120 |
+
|
| 121 |
+
* :ref:`genindex`
|
| 122 |
+
* :ref:`search`
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/make.bat
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
@ECHO OFF
|
| 2 |
+
|
| 3 |
+
pushd %~dp0
|
| 4 |
+
|
| 5 |
+
REM Command file for Sphinx documentation
|
| 6 |
+
|
| 7 |
+
if "%SPHINXBUILD%" == "" (
|
| 8 |
+
set SPHINXBUILD=sphinx-build
|
| 9 |
+
)
|
| 10 |
+
set SOURCEDIR=.
|
| 11 |
+
set BUILDDIR=_build
|
| 12 |
+
|
| 13 |
+
if "%1" == "" goto help
|
| 14 |
+
|
| 15 |
+
%SPHINXBUILD% >NUL 2>NUL
|
| 16 |
+
if errorlevel 9009 (
|
| 17 |
+
echo.
|
| 18 |
+
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
|
| 19 |
+
echo.installed, then set the SPHINXBUILD environment variable to point
|
| 20 |
+
echo.to the full path of the 'sphinx-build' executable. Alternatively you
|
| 21 |
+
echo.may add the Sphinx directory to PATH.
|
| 22 |
+
echo.
|
| 23 |
+
echo.If you don't have Sphinx installed, grab it from
|
| 24 |
+
echo.http://sphinx-doc.org/
|
| 25 |
+
exit /b 1
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
|
| 29 |
+
goto end
|
| 30 |
+
|
| 31 |
+
:help
|
| 32 |
+
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
|
| 33 |
+
|
| 34 |
+
:end
|
| 35 |
+
popd
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/model_zoo.md
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 模型库和评测
|
| 2 |
+
|
| 3 |
+
本页面用于汇总 MMYOLO 中支持的各类模型性能和相关评测指标,方便用户对比分析。
|
| 4 |
+
|
| 5 |
+
## COCO 数据集
|
| 6 |
+
|
| 7 |
+
<div align=center>
|
| 8 |
+
<img src="https://user-images.githubusercontent.com/17425982/222087414-168175cc-dae6-4c5c-a8e3-3109a152dd19.png"/>
|
| 9 |
+
</div>
|
| 10 |
+
|
| 11 |
+
| Model | Arch | Size | Batch Size | Epoch | SyncBN | AMP | Mem (GB) | Params(M) | FLOPs(G) | TRT-FP16-GPU-Latency(ms) | Box AP | TTA Box AP |
|
| 12 |
+
| :--------------: | :--: | :--: | :--------: | :---: | :----: | :-: | :------: | :-------: | :------: | :----------------------: | :----: | :--------: |
|
| 13 |
+
| YOLOv5-n | P5 | 640 | 8xb16 | 300 | Yes | Yes | 1.5 | 1.87 | 2.26 | 1.14 | 28.0 | 30.7 |
|
| 14 |
+
| YOLOv6-v2.0-n | P5 | 640 | 8xb32 | 400 | Yes | Yes | 6.04 | 4.32 | 5.52 | 1.37 | 36.2 | |
|
| 15 |
+
| YOLOv8-n | P5 | 640 | 8xb16 | 500 | Yes | Yes | 2.5 | 3.16 | 4.4 | 1.53 | 37.4 | 39.9 |
|
| 16 |
+
| RTMDet-tiny | P5 | 640 | 8xb32 | 300 | Yes | No | 11.9 | 4.90 | 8.09 | 2.31 | 41.8 | 43.2 |
|
| 17 |
+
| YOLOv6-v2.0-tiny | P5 | 640 | 8xb32 | 400 | Yes | Yes | 8.13 | 9.70 | 12.37 | 2.19 | 41.0 | |
|
| 18 |
+
| YOLOv7-tiny | P5 | 640 | 8xb16 | 300 | Yes | Yes | 2.7 | 6.23 | 6.89 | 1.88 | 37.5 | |
|
| 19 |
+
| YOLOX-tiny | P5 | 416 | 8xb32 | 300 | No | Yes | 4.9 | 5.06 | 7.63 | 1.19 | 34.3 | |
|
| 20 |
+
| RTMDet-s | P5 | 640 | 8xb32 | 300 | Yes | No | 16.3 | 8.89 | 14.84 | 2.89 | 45.7 | 47.3 |
|
| 21 |
+
| YOLOv5-s | P5 | 640 | 8xb16 | 300 | Yes | Yes | 2.7 | 7.24 | 8.27 | 1.89 | 37.7 | 40.2 |
|
| 22 |
+
| YOLOv6-v2.0-s | P5 | 640 | 8xb32 | 400 | Yes | Yes | 8.88 | 17.22 | 21.94 | 2.67 | 44.0 | |
|
| 23 |
+
| YOLOv8-s | P5 | 640 | 8xb16 | 500 | Yes | Yes | 4.0 | 11.17 | 14.36 | 2.61 | 45.1 | 46.8 |
|
| 24 |
+
| YOLOX-s | P5 | 640 | 8xb32 | 300 | No | Yes | 9.8 | 8.97 | 13.40 | 2.38 | 41.9 | |
|
| 25 |
+
| PPYOLOE+ -s | P5 | 640 | 8xb8 | 80 | Yes | No | 4.7 | 7.93 | 8.68 | 2.54 | 43.5 | |
|
| 26 |
+
| RTMDet-m | P5 | 640 | 8xb32 | 300 | Yes | No | 29.0 | 24.71 | 39.21 | 6.23 | 50.2 | 51.9 |
|
| 27 |
+
| YOLOv5-m | P5 | 640 | 8xb16 | 300 | Yes | Yes | 5.0 | 21.19 | 24.53 | 4.28 | 45.3 | 46.9 |
|
| 28 |
+
| YOLOv6-v2.0-m | P5 | 640 | 8xb32 | 300 | Yes | Yes | 16.69 | 34.25 | 40.7 | 5.12 | 48.4 | |
|
| 29 |
+
| YOLOv8-m | P5 | 640 | 8xb16 | 500 | Yes | Yes | 7.0 | 25.9 | 39.57 | 5.78 | 50.6 | 52.3 |
|
| 30 |
+
| YOLOX-m | P5 | 640 | 8xb32 | 300 | No | Yes | 17.6 | 25.33 | 36.88 | 5.31 | 47.5 | |
|
| 31 |
+
| PPYOLOE+ -m | P5 | 640 | 8xb8 | 80 | Yes | No | 8.4 | 23.43 | 24.97 | 5.47 | 49.5 | |
|
| 32 |
+
| RTMDet-l | P5 | 640 | 8xb32 | 300 | Yes | No | 45.2 | 52.32 | 80.12 | 10.13 | 52.3 | 53.7 |
|
| 33 |
+
| YOLOv5-l | P5 | 640 | 8xb16 | 300 | Yes | Yes | 8.1 | 46.56 | 54.65 | 6.8 | 48.8 | 49.9 |
|
| 34 |
+
| YOLOv6-v2.0-l | P5 | 640 | 8xb32 | 300 | Yes | Yes | 20.86 | 58.53 | 71.43 | 8.78 | 51.0 | |
|
| 35 |
+
| YOLOv7-l | P5 | 640 | 8xb16 | 300 | Yes | Yes | 10.3 | 36.93 | 52.42 | 6.63 | 50.9 | |
|
| 36 |
+
| YOLOv8-l | P5 | 640 | 8xb16 | 500 | Yes | Yes | 9.1 | 43.69 | 82.73 | 8.97 | 53.0 | 54.4 |
|
| 37 |
+
| YOLOX-l | P5 | 640 | 8xb8 | 300 | No | Yes | 8.0 | 54.21 | 77.83 | 9.23 | 50.1 | |
|
| 38 |
+
| PPYOLOE+ -l | P5 | 640 | 8xb8 | 80 | Yes | No | 13.2 | 52.20 | 55.05 | 8.2 | 52.6 | |
|
| 39 |
+
| RTMDet-x | P5 | 640 | 8xb32 | 300 | Yes | No | 63.4 | 94.86 | 145.41 | 17.89 | 52.8 | 54.2 |
|
| 40 |
+
| YOLOv7-x | P5 | 640 | 8xb16 | 300 | Yes | Yes | 13.7 | 71.35 | 95.06 | 11.63 | 52.8 | |
|
| 41 |
+
| YOLOv8-x | P5 | 640 | 8xb16 | 500 | Yes | Yes | 12.4 | 68.23 | 132.10 | 14.22 | 54.0 | 55.0 |
|
| 42 |
+
| YOLOX-x | P5 | 640 | 8xb8 | 300 | No | Yes | 9.8 | 99.07 | 144.39 | 15.35 | 51.4 | |
|
| 43 |
+
| PPYOLOE+ -x | P5 | 640 | 8xb8 | 80 | Yes | No | 19.1 | 98.42 | 105.48 | 14.02 | 54.2 | |
|
| 44 |
+
| YOLOv5-n | P6 | 1280 | 8xb16 | 300 | Yes | Yes | 5.8 | 3.25 | 2.30 | | 35.9 | |
|
| 45 |
+
| YOLOv5-s | P6 | 1280 | 8xb16 | 300 | Yes | Yes | 10.5 | 12.63 | 8.45 | | 44.4 | |
|
| 46 |
+
| YOLOv5-m | P6 | 1280 | 8xb16 | 300 | Yes | Yes | 19.1 | 35.73 | 25.05 | | 51.3 | |
|
| 47 |
+
| YOLOv5-l | P6 | 1280 | 8xb16 | 300 | Yes | Yes | 30.5 | 76.77 | 55.77 | | 53.7 | |
|
| 48 |
+
| YOLOv7-w | P6 | 1280 | 8xb16 | 300 | Yes | Yes | 27.0 | 82.31 | 45.07 | | 54.1 | |
|
| 49 |
+
| YOLOv7-e | P6 | 1280 | 8xb16 | 300 | Yes | Yes | 42.5 | 114.69 | 64.48 | | 55.1 | |
|
| 50 |
+
|
| 51 |
+
- 所有模型均使用 COCO train2017 作为训练集,在 COCO val2017 上验证精度
|
| 52 |
+
- TRT-FP16-GPU-Latency(ms) 是指在 NVIDIA Tesla T4 设备上采用 TensorRT 8.4,batch size 为 1, 测试 shape 为 640x640 且仅包括模型 forward 的 GPU Compute time (YOLOX-tiny 测试 shape 是 416x416)
|
| 53 |
+
- 模型参数量和 FLOPs 是采用 [get_flops](https://github.com/open-mmlab/mmyolo/blob/dev/tools/analysis_tools/get_flops.py) 脚本得到,不同的运算方式可能略有不同
|
| 54 |
+
- RTMDet 性能是通过 [MMRazor 知识蒸馏](https://github.com/open-mmlab/mmyolo/blob/dev/configs/rtmdet/distillation/README.md) 训练后的结果
|
| 55 |
+
- MMYOLO 中暂时只实现了 YOLOv6 2.0 版本,并且 L 和 M 为没有经过知识蒸馏的结果
|
| 56 |
+
- YOLOv8 是引入了实例分割标注优化后的结果,YOLOv5、YOLOv6 和 YOLOv7 没有采用实例分割标注优化
|
| 57 |
+
- PPYOLOE+ 使用 Obj365 作为预训练权重,因此 COCO 训练的 epoch 数只需要 80
|
| 58 |
+
- YOLOX-tiny、YOLOX-s 和 YOLOX-m 为采用了 RTMDet 中所提优化器参数训练所得,性能相比原始实现有不同程度提升
|
| 59 |
+
|
| 60 |
+
详情见如下内容
|
| 61 |
+
|
| 62 |
+
- [RTMDet](https://github.com/open-mmlab/mmyolo/blob/main/configs/rtmdet)
|
| 63 |
+
- [YOLOv5](https://github.com/open-mmlab/mmyolo/blob/main/configs/yolov5)
|
| 64 |
+
- [YOLOv6](https://github.com/open-mmlab/mmyolo/blob/main/configs/yolov6)
|
| 65 |
+
- [YOLOv7](https://github.com/open-mmlab/mmyolo/blob/main/configs/yolov7)
|
| 66 |
+
- [YOLOv8](https://github.com/open-mmlab/mmyolo/blob/main/configs/yolov8)
|
| 67 |
+
- [YOLOX](https://github.com/open-mmlab/mmyolo/blob/main/configs/yolox)
|
| 68 |
+
- [PPYOLO-E](https://github.com/open-mmlab/mmyolo/blob/main/configs/ppyoloe)
|
| 69 |
+
|
| 70 |
+
## VOC 数据集
|
| 71 |
+
|
| 72 |
+
| Backbone | size | Batchsize | AMP | Mem (GB) | box AP(COCO metric) |
|
| 73 |
+
| :------: | :--: | :-------: | :-: | :------: | :-----------------: |
|
| 74 |
+
| YOLOv5-n | 512 | 64 | Yes | 3.5 | 51.2 |
|
| 75 |
+
| YOLOv5-s | 512 | 64 | Yes | 6.5 | 62.7 |
|
| 76 |
+
| YOLOv5-m | 512 | 64 | Yes | 12.0 | 70.1 |
|
| 77 |
+
| YOLOv5-l | 512 | 32 | Yes | 10.0 | 73.1 |
|
| 78 |
+
|
| 79 |
+
详情见如下内容
|
| 80 |
+
|
| 81 |
+
- [YOLOv5](https://github.com/open-mmlab/mmyolo/blob/main/configs/yolov5)
|
| 82 |
+
|
| 83 |
+
## CrowdHuman 数据集
|
| 84 |
+
|
| 85 |
+
| Backbone | size | SyncBN | AMP | Mem (GB) | ignore_iof_thr | box AP50(CrowDHuman Metric) | MR | JI |
|
| 86 |
+
| :------: | :--: | :----: | :-: | :------: | :------------: | :-------------------------: | :--: | :---: |
|
| 87 |
+
| YOLOv5-s | 640 | Yes | Yes | 2.6 | -1 | 85.79 | 48.7 | 75.33 |
|
| 88 |
+
| YOLOv5-s | 640 | Yes | Yes | 2.6 | 0.5 | 86.17 | 48.8 | 75.87 |
|
| 89 |
+
|
| 90 |
+
详情见如下内容
|
| 91 |
+
|
| 92 |
+
- [YOLOv5](https://github.com/open-mmlab/mmyolo/blob/main/configs/yolov5)
|
| 93 |
+
|
| 94 |
+
## DOTA 1.0 数据集
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/notes/changelog.md
ADDED
|
@@ -0,0 +1,365 @@
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 更新日志
|
| 2 |
+
|
| 3 |
+
## v0.6.0 (15/8/2023)
|
| 4 |
+
|
| 5 |
+
### 亮点
|
| 6 |
+
|
| 7 |
+
- 支持 YOLOv5 实例分割
|
| 8 |
+
- 基于 MMPose 支持 YOLOX-Pose
|
| 9 |
+
- 添加 15 分钟的实例分割教程
|
| 10 |
+
- YOLOv5 支持使用 mask 标注来优化边界框
|
| 11 |
+
- 添加多尺度训练和测试文档
|
| 12 |
+
|
| 13 |
+
### 新特性
|
| 14 |
+
|
| 15 |
+
- 添加训练和测试技巧文档 (#659)
|
| 16 |
+
- 支持设置 `cache_size_limit` 参数,并支持 mmdet 3.0.0 (#707)
|
| 17 |
+
- 支持 YOLOv5u 和 YOLOv6 3.0 推理 (#624, #744)
|
| 18 |
+
- 支持仅模型推断 (#733)
|
| 19 |
+
- 添加 YOLOv8 deepstream 配置 (#633)
|
| 20 |
+
- 在 MMYOLO 应用程序中添加电离图示例 (#643)
|
| 21 |
+
|
| 22 |
+
### Bug 修复
|
| 23 |
+
|
| 24 |
+
- 修复 browse_dataset 以可视化测试和验证集的问题 (#641)
|
| 25 |
+
- 修复安装文档错误 (#662)
|
| 26 |
+
- 修复 yolox-l ckpt 链接 (#677)
|
| 27 |
+
- 修正 YOLOv7 和 YOLOv8 图表中的拼写错误 (#621, #710)
|
| 28 |
+
- 调整 `boxam_vis_demo.py` 中包导入的顺序 (#655)
|
| 29 |
+
|
| 30 |
+
### 完善
|
| 31 |
+
|
| 32 |
+
- 优化 `convert_kd_ckpt_to_student.py` 文件 (#647)
|
| 33 |
+
- 添加 FAQ 和 training_testing_tricks 的英文文档 (#691, #693)
|
| 34 |
+
|
| 35 |
+
### 贡献者
|
| 36 |
+
|
| 37 |
+
总共 21 位开发者参与了本次版本
|
| 38 |
+
|
| 39 |
+
感谢 @Lum1104,@azure-wings,@FeiGeChuanShu,@Lingrui Gu,@Nioolek,@huayuan4396,@RangeKing,@danielhonies,@yechenzhi,@JosonChan1998,@kitecats,@Qingrenn,@triple-Mu,@kikefdezl,@zhangrui-wolf,@xin-li-67,@Ben-Louis,@zgzhengSEU,@VoyagerXvoyagerx,@tang576225574,@hhaAndroid
|
| 40 |
+
|
| 41 |
+
## v0.5.0 (2/3/2023)
|
| 42 |
+
|
| 43 |
+
### 亮点
|
| 44 |
+
|
| 45 |
+
1. 支持了 [RTMDet-R](https://github.com/open-mmlab/mmyolo/blob/dev/configs/rtmdet/README.md#rotated-object-detection) 旋转框目标检测任务和算法
|
| 46 |
+
2. [YOLOv8](https://github.com/open-mmlab/mmyolo/blob/dev/configs/yolov8/README.md) 支持使用 mask 标注提升目标检测模型性能
|
| 47 |
+
3. 支持 [MMRazor](https://github.com/open-mmlab/mmyolo/blob/dev/configs/razor/subnets/README.md) 搜索的 NAS 子网络作为 YOLO 系列算法的 backbone
|
| 48 |
+
4. 支持调用 [MMRazor](https://github.com/open-mmlab/mmyolo/blob/dev/configs/rtmdet/distillation/README.md) 对 RTMDet 进行知识蒸馏
|
| 49 |
+
5. [MMYOLO](https://mmyolo.readthedocs.io/zh_CN/dev/) 文档结构优化,内容全面升级
|
| 50 |
+
6. 基于 RTMDet 训练超参提升 YOLOX 精度和训练速度
|
| 51 |
+
7. 支持模型参数量、FLOPs 计算和提供 T4 设备上 GPU 延时数据,并更新了 [Model Zoo](https://github.com/open-mmlab/mmyolo/blob/dev/docs/zh_cn/model_zoo.md)
|
| 52 |
+
8. 支持测试时增强 TTA
|
| 53 |
+
9. 支持 RTMDet、YOLOv8 和 YOLOv7 assigner 可视化
|
| 54 |
+
|
| 55 |
+
### 新特性
|
| 56 |
+
|
| 57 |
+
01. 支持 RTMDet 实例分割任务的推理 (#583)
|
| 58 |
+
02. 美化 MMYOLO 中配置文件并增加更多注释 (#501, #506, #516, #529, #531, #539)
|
| 59 |
+
03. 重构并优化中英文文档 (#568, #573, #579, #584, #587, #589, #596, #599, #600)
|
| 60 |
+
04. 支持 fast 版本的 YOLOX (#518)
|
| 61 |
+
05. EasyDeploy 中支持 DeepStream,并添加说明文档 (#485, #545, #571)
|
| 62 |
+
06. 新增混淆矩阵绘制脚本 (#572)
|
| 63 |
+
07. 新增单通道应用案例 (#460)
|
| 64 |
+
08. 支持 auto registration (#597)
|
| 65 |
+
09. Box CAM 支持 YOLOv7、YOLOv8 和 PPYOLOE (#601)
|
| 66 |
+
10. 新增自动化生成 MM 系列 repo 注册信息和 tools 脚本 (#559)
|
| 67 |
+
11. 新增 YOLOv7 模型结构图 (#504)
|
| 68 |
+
12. 新增如何指定特定 GPU 训练和推理文档 (#503)
|
| 69 |
+
13. 新增训练或者测试时检查 `metainfo` 是否全为小写 (#535)
|
| 70 |
+
14. 增加 Twitter、Discord、Medium 和 YouTube 等链接 (#555)
|
| 71 |
+
|
| 72 |
+
### Bug 修复
|
| 73 |
+
|
| 74 |
+
1. 修复 isort 版本问题 (#492, #497)
|
| 75 |
+
2. 修复 assigner 可视化模块的 type 错误 (#509)
|
| 76 |
+
3. 修复 YOLOv8 文档链接错误 (#517)
|
| 77 |
+
4. 修复 EasyDeploy 中的 RTMDet Decoder 错误 (#519)
|
| 78 |
+
5. 修复一些文档链接错误 (#537)
|
| 79 |
+
6. 修复 RTMDet-Tiny 权重路径错误 (#580)
|
| 80 |
+
|
| 81 |
+
### 完善
|
| 82 |
+
|
| 83 |
+
1. 完善更新 `contributing.md`
|
| 84 |
+
2. 优化 `DetDataPreprocessor` 支使其支持多任务 (#511)
|
| 85 |
+
3. 优化 `gt_instances_preprocess` 使其可以用于其他 YOLO 算法 (#532)
|
| 86 |
+
4. 新增 `yolov7-e6e` 权重转换脚本 (#570)
|
| 87 |
+
5. 参考 YOLOv8 推理代码修改 PPYOLOE (#614)
|
| 88 |
+
|
| 89 |
+
### 贡献者
|
| 90 |
+
|
| 91 |
+
总共 22 位开发者参与了本次版本
|
| 92 |
+
|
| 93 |
+
@triple-Mu, @isLinXu, @Audrey528, @TianWen580, @yechenzhi, @RangeKing, @lyviva, @Nioolek, @PeterH0323, @tianleiSHI, @aptsunny, @satuoqaq, @vansin, @xin-li-67, @VoyagerXvoyagerx,
|
| 94 |
+
@landhill, @kitecats, @tang576225574, @HIT-cwh, @AI-Tianlong, @RangiLyu, @hhaAndroid
|
| 95 |
+
|
| 96 |
+
## v0.4.0 (18/1/2023)
|
| 97 |
+
|
| 98 |
+
### 亮点
|
| 99 |
+
|
| 100 |
+
1. 实现了 [YOLOv8](https://github.com/open-mmlab/mmyolo/blob/dev/configs/yolov8/README.md) 目标检测模型,并通过 [projects/easydeploy](https://github.com/open-mmlab/mmyolo/blob/dev/projects/easydeploy) 支持了模型部署
|
| 101 |
+
2. 新增了中英文版本的 [YOLOv8 原理和实现全解析文档](https://github.com/open-mmlab/mmyolo/blob/dev/docs/zh_cn/algorithm_descriptions/yolov8_description.md)
|
| 102 |
+
|
| 103 |
+
### 新特性
|
| 104 |
+
|
| 105 |
+
1. 新增 YOLOv8 和 PPYOLOE 模型结构图 (#459, #471)
|
| 106 |
+
2. 调整最低支持 Python 版本从 3.6 升级为 3.7 (#449)
|
| 107 |
+
3. TensorRT-8 中新增新的 YOLOX decoder 写法 (#450)
|
| 108 |
+
4. 新增学习率可视化曲线脚本 (#479)
|
| 109 |
+
5. 新增脚本命令速查表 (#481)
|
| 110 |
+
|
| 111 |
+
### Bug 修复
|
| 112 |
+
|
| 113 |
+
1. 修复 `optimize_anchors.py` 脚本导入错误问题 (#452)
|
| 114 |
+
2. 修复 `get_started.md` 中安装步骤��误问题 (#474)
|
| 115 |
+
3. 修复使用 `RTMDet` P6 模型时候 neck 报错问题 (#480)
|
| 116 |
+
|
| 117 |
+
### 视频
|
| 118 |
+
|
| 119 |
+
1. 发布了 [玩转 MMYOLO 之实用篇(四):顶会第一步 · 模块自定义](https://www.bilibili.com/video/BV1yd4y1j7VD/)
|
| 120 |
+
|
| 121 |
+
### 贡献者
|
| 122 |
+
|
| 123 |
+
总共 9 位开发者参与了本次版本
|
| 124 |
+
|
| 125 |
+
谢谢 @VoyagerXvoyagerx, @tianleiSHI, @RangeKing, @PeterH0323, @Nioolek, @triple-Mu, @lyviva, @Zheng-LinXiao, @hhaAndroid
|
| 126 |
+
|
| 127 |
+
## v0.3.0 (8/1/2023)
|
| 128 |
+
|
| 129 |
+
### 亮点
|
| 130 |
+
|
| 131 |
+
1. 实现了 [RTMDet](https://github.com/open-mmlab/mmyolo/blob/dev/configs/rtmdet/README.md) 的快速版本。RTMDet-s 8xA100 训练只需要 14 个小时,训练速度相比原先版本提升 2.6 倍。
|
| 132 |
+
2. 支持 [PPYOLOE](https://github.com/open-mmlab/mmyolo/blob/dev/configs/ppyoloe/README.md) 训练。
|
| 133 |
+
3. 支持 [YOLOv5](https://github.com/open-mmlab/mmyolo/blob/dev/configs/yolov5/crowdhuman/yolov5_s-v61_8xb16-300e_ignore_crowdhuman.py) 的 `iscrowd` 属性训练。
|
| 134 |
+
4. 支持 [YOLOv5 正样本分配结果可视化](https://github.com/open-mmlab/mmyolo/blob/dev/projects/assigner_visualization/README.md)
|
| 135 |
+
5. 新增 [YOLOv6 原理和实现全解析文档](https://github.com/open-mmlab/mmyolo/blob/dev/docs/zh_cn/algorithm_descriptions/yolov6_description.md)
|
| 136 |
+
|
| 137 |
+
### 新特性
|
| 138 |
+
|
| 139 |
+
01. 新增 `crowdhuman` 数据集 (#368)
|
| 140 |
+
02. EasyDeploy 中支持 TensorRT 推理 (#377)
|
| 141 |
+
03. 新增 `YOLOX` 结构图描述 (#402)
|
| 142 |
+
04. 新增视频推理脚本 (#392)
|
| 143 |
+
05. EasyDeploy 中支持 `YOLOv7` 部署 (#427)
|
| 144 |
+
06. 支持从 CLI 中的特定检查点恢复训练 (#393)
|
| 145 |
+
07. 将元信息字段设置为小写(#362、#412)
|
| 146 |
+
08. 新增模块组合文档 (#349, #352, #345)
|
| 147 |
+
09. 新增关于如何冻结 backbone 或 neck 权重的文档 (#418)
|
| 148 |
+
10. 在 `how_to.md` 中添加不使用预训练权重的文档 (#404)
|
| 149 |
+
11. 新增关于如何设置随机种子的文档 (#386)
|
| 150 |
+
12. 将 `rtmdet_description.md` 文档翻译成英文 (#353)
|
| 151 |
+
|
| 152 |
+
### Bug 修复
|
| 153 |
+
|
| 154 |
+
01. 修复设置 `--class-id-txt` 时输出注释文件中的错误 (#430)
|
| 155 |
+
02. 修复 `YOLOv5` head 中的批量推理错误 (#413)
|
| 156 |
+
03. 修复某些 head 的类型提示(#415、#416、#443)
|
| 157 |
+
04. 修复 expected a non-empty list of Tensors 错误 (#376)
|
| 158 |
+
05. 修复 `YOLOv7` 训练中的设备不一致错误(#397)
|
| 159 |
+
06. 修复 `LetterResize` 中的 `scale_factor` 和 `pad_param` 值 (#387)
|
| 160 |
+
07. 修复 readthedocs 的 docstring 图形渲染错误 (#400)
|
| 161 |
+
08. 修复 `YOLOv6` 从训练到验证时的断言错误 (#378)
|
| 162 |
+
09. 修复 `np.int` 和旧版 builder.py 导致的 CI 错误 (#389)
|
| 163 |
+
10. 修复 MMDeploy 重写器 (#366)
|
| 164 |
+
11. 修复 MMYOLO 单元测试错误 (#351)
|
| 165 |
+
12. 修复 `pad_param` 错误 (#354)
|
| 166 |
+
13. 修复 head 推理两次的错误(#342)
|
| 167 |
+
14. 修复自定义数据集训练 (#428)
|
| 168 |
+
|
| 169 |
+
### 完善
|
| 170 |
+
|
| 171 |
+
01. 更新 `useful_tools.md` (#384)
|
| 172 |
+
02. 更新英文版 `custom_dataset.md` (#381)
|
| 173 |
+
03. 重写函数删除上下文参数 (#395)
|
| 174 |
+
04. 弃用 `np.bool` 类型别名 (#396)
|
| 175 |
+
05. 为自定义数据集添加新的视频链接 (#365)
|
| 176 |
+
06. 仅为模型导出 onnx (#361)
|
| 177 |
+
07. 添加 MMYOLO 回归测试 yml (#359)
|
| 178 |
+
08. 更新 `article.md` 中的视频教程 (#350)
|
| 179 |
+
09. 添加部署 demo (#343)
|
| 180 |
+
10. 优化 debug 模式下大图的可视化效果(#346)
|
| 181 |
+
11. 改进 `browse_dataset` 的参数并支持 `RepeatDataset` (#340, #338)
|
| 182 |
+
|
| 183 |
+
### 视频
|
| 184 |
+
|
| 185 |
+
1. 发布了 [基于 sahi 的大图推理](https://www.bilibili.com/video/BV1EK411R7Ws/)
|
| 186 |
+
2. 发布了 [自定义数据集从标注到部署保姆级教程](https://www.bilibili.com/video/BV1RG4y137i5)
|
| 187 |
+
|
| 188 |
+
### 贡献者
|
| 189 |
+
|
| 190 |
+
总共 28 位开发者参与了本次版本
|
| 191 |
+
|
| 192 |
+
谢谢 @RangeKing, @PeterH0323, @Nioolek, @triple-Mu, @matrixgame2018, @xin-li-67, @tang576225574, @kitecats, @Seperendity, @diplomatist, @vaew, @wzr-skn, @VoyagerXvoyagerx, @MambaWong, @tianleiSHI, @caj-github, @zhubochao, @lvhan028, @dsghaonan, @lyviva, @yuewangg, @wang-tf, @satuoqaq, @grimoire, @RunningLeon, @hanrui1sensetime, @RangiLyu, @hhaAndroid
|
| 193 |
+
|
| 194 |
+
## v0.2.0(1/12/2022)
|
| 195 |
+
|
| 196 |
+
### 亮点
|
| 197 |
+
|
| 198 |
+
1. 支持 [YOLOv7](https://github.com/open-mmlab/mmyolo/tree/dev/configs/yolov7) P5 和 P6 模型
|
| 199 |
+
2. 支持 [YOLOv6](https://github.com/open-mmlab/mmyolo/blob/dev/configs/yolov6/README.md) 中的 ML 大模型
|
| 200 |
+
3. 支持 [Grad-Based CAM 和 Grad-Free CAM](https://github.com/open-mmlab/mmyolo/blob/dev/demo/boxam_vis_demo.py)
|
| 201 |
+
4. 基于 sahi 支持 [大图推理](https://github.com/open-mmlab/mmyolo/blob/dev/demo/large_image_demo.py)
|
| 202 |
+
5. projects 文件夹下新增 [easydeploy](https://github.com/open-mmlab/mmyolo/blob/dev/projects/easydeploy/README.md) 项目
|
| 203 |
+
6. 新增 [自定义数据集教程](https://github.com/open-mmlab/mmyolo/blob/dev/docs/zh_cn/user_guides/custom_dataset.md)
|
| 204 |
+
|
| 205 |
+
### 新特性
|
| 206 |
+
|
| 207 |
+
1. `browse_dataset.py` 脚本支持可视化原图、数据增强后和中间结果功能 (#304)
|
| 208 |
+
2. `image_demo.py` 新增预测结果保存为 labelme 格式功能 (#288, #314)
|
| 209 |
+
3. 新增 labelme 格式转 COCO 格式脚本 `labelme2coco` (#308, #313)
|
| 210 |
+
4. 新增 COCO 数据集切分脚本 `coco_split.py` (#311)
|
| 211 |
+
5. `how-to.md` 文档中新增两个 backbone 替换案例以及更新 `plugin.md` (#291)
|
| 212 |
+
6. 新增贡献者文档 `contributing.md` and 代码规范文档 `code_style.md` (#322)
|
| 213 |
+
7. 新增如何通过 mim 跨库调用脚本文档 (#321)
|
| 214 |
+
8. `YOLOv5` 支持 RV1126 设备部署 (#262)
|
| 215 |
+
|
| 216 |
+
### Bug 修复
|
| 217 |
+
|
| 218 |
+
1. 修复 `MixUp` padding 错误 (#319)
|
| 219 |
+
2. 修复 `LetterResize` 和 `YOLOv5KeepRatioResize` 中 `scale_factor` 参数顺序错误 (#305)
|
| 220 |
+
3. 修复 `YOLOX Nano` 模型训练错误问题 (#285)
|
| 221 |
+
4. 修复 `RTMDet` 部署没有导包的错误 (#287)
|
| 222 |
+
5. 修复 int8 部署配置错误 (#315)
|
| 223 |
+
6. 修复 `basebackbone` 中 `make_stage_plugins` 注释 (#296)
|
| 224 |
+
7. 部署模块支持切换为 deploy 模式功能 (#324)
|
| 225 |
+
8. 修正 `RTMDet` 模型结构图中的错误 (#317)
|
| 226 |
+
|
| 227 |
+
### 完善
|
| 228 |
+
|
| 229 |
+
1. `test.py` 中新增 json 格式导出选项 (#316)
|
| 230 |
+
2. `extract_subcoco.py` 脚本中新增基于面积阈值过滤规则 (#286)
|
| 231 |
+
3. 部署相关中文文档翻译为英文 (#289)
|
| 232 |
+
4. 新增 `YOLOv6` 算法描述大纲文档 (#252)
|
| 233 |
+
5. 完善 `config.md` (#297, #303)
|
| 234 |
+
6. 完善 `mosiac9` 的 docstring (#307)
|
| 235 |
+
7. 完善 `browse_coco_json.py` 脚本输入参数 (#309)
|
| 236 |
+
8. 重构 `dataset_analysis.py` 中部分函数使其更加通用 (#294)
|
| 237 |
+
|
| 238 |
+
### 视频
|
| 239 |
+
|
| 240 |
+
1. 发布了 [工程文件结构简析](https://www.bilibili.com/video/BV1LP4y117jS)
|
| 241 |
+
2. 发布了 [10分钟换遍主干网络文档](https://www.bilibili.com/video/BV1JG4y1d7GC)
|
| 242 |
+
|
| 243 |
+
### 贡献者
|
| 244 |
+
|
| 245 |
+
总共 14 位开发者参与了本次版本
|
| 246 |
+
|
| 247 |
+
谢谢 @fcakyon, @matrixgame2018, @MambaWong, @imAzhou, @triple-Mu, @RangeKing, @PeterH0323, @xin-li-67, @kitecats, @hanrui1sensetime, @AllentDan, @Zheng-LinXiao, @hhaAndroid, @wanghonglie
|
| 248 |
+
|
| 249 |
+
## v0.1.3(10/11/2022)
|
| 250 |
+
|
| 251 |
+
### 新特性
|
| 252 |
+
|
| 253 |
+
1. 支持 CBAM 插件并提供插件文档 (#246)
|
| 254 |
+
2. 新增 YOLOv5 P6 模型结构图和相关说明 (#273)
|
| 255 |
+
|
| 256 |
+
### Bug 修复
|
| 257 |
+
|
| 258 |
+
1. 基于 mmengine 0.3.1 修复保存最好权重时训练失败问题
|
| 259 |
+
2. 基于 mmdet 3.0.0rc3 修复 `add_dump_metric` 报错 (#253)
|
| 260 |
+
3. 修复 backbone 不支持 `init_cfg` 问题 (#272)
|
| 261 |
+
4. 基于 mmdet 3.0.0rc3 改变 typing 导入方式 (#261)
|
| 262 |
+
|
| 263 |
+
### 完善
|
| 264 |
+
|
| 265 |
+
1. `featmap_vis_demo` 支持文件夹和 url 输入 (#248)
|
| 266 |
+
2. 部署 docker 文件完善 (#242)
|
| 267 |
+
|
| 268 |
+
### 贡献者
|
| 269 |
+
|
| 270 |
+
总共 10 位开发者参与了本次版本
|
| 271 |
+
|
| 272 |
+
谢谢 @kitecats, @triple-Mu, @RangeKing, @PeterH0323, @Zheng-LinXiao, @tkhe, @weikai520, @zytx121, @wanghonglie, @hhaAndroid
|
| 273 |
+
|
| 274 |
+
## v0.1.2(3/11/2022)
|
| 275 |
+
|
| 276 |
+
### 亮点
|
| 277 |
+
|
| 278 |
+
1. 支持 ONNXRuntime 和 TensorRT 的 [YOLOv5/YOLOv6/YOLOX/RTMDet 部署](https://github.com/open-mmlab/mmyolo/blob/main/configs/deploy)
|
| 279 |
+
2. 支持 [YOLOv6](https://github.com/open-mmlab/mmyolo/blob/main/configs/yolov6) s/t/n 模型训练
|
| 280 |
+
3. YOLOv5 支持 [P6 大分辨率 1280 尺度训练](https://github.com/open-mmlab/mmyolo/blob/main/configs/yolov5)
|
| 281 |
+
4. YOLOv5 支持 [VOC 数据集训练](https://github.com/open-mmlab/mmyolo/blob/main/configs/yolov5/voc)
|
| 282 |
+
5. 支持 [PPYOLOE](https://github.com/open-mmlab/mmyolo/blob/main/configs/ppyoloe) 和 [YOLOv7](https://github.com/open-mmlab/mmyolo/blob/main/configs/yolov7) 模型推理和官方权重转化
|
| 283 |
+
6. How-to 文档中新增 YOLOv5 替换 [backbone 教程](https://github.com/open-mmlab/mmyolo/blob/dev/docs/zh_cn/advanced_guides/how_to.md#%E8%B7%A8%E5%BA%93%E4%BD%BF%E7%94%A8%E4%B8%BB%E5%B9%B2%E7%BD%91%E7%BB%9C)
|
| 284 |
+
|
| 285 |
+
### 新特性
|
| 286 |
+
|
| 287 |
+
1. 新增 `optimize_anchors` 脚本 (#175)
|
| 288 |
+
2. 新增 `extract_subcoco` 脚本 (#186)
|
| 289 |
+
3. 新增 `yolo2coco` 转换脚本 (#161)
|
| 290 |
+
4. 新增 `dataset_analysis` 脚本 (#172)
|
| 291 |
+
5. 移除 Albu 版本限制 (#187)
|
| 292 |
+
|
| 293 |
+
### Bug 修复
|
| 294 |
+
|
| 295 |
+
1. 修复当设置 `cfg.resume` 时候不生效问题 (#221)
|
| 296 |
+
2. 修复特征图可视化脚本中不显示 bbox 问题 (#204)
|
| 297 |
+
3. 更新 RTMDet 的 metafile (#188)
|
| 298 |
+
4. 修复 test_pipeline 中的可视化错误 (#166)
|
| 299 |
+
5. 更新 badges (#140)
|
| 300 |
+
|
| 301 |
+
### 完善
|
| 302 |
+
|
| 303 |
+
1. 优化 Readthedoc 显示页面 (#209)
|
| 304 |
+
2. 为 base model 添加模块结构图的 docstring (#196)
|
| 305 |
+
3. 支持 LoadAnnotations 中不包括任何实例逻辑 (#161)
|
| 306 |
+
4. 更新 `image_demo` 脚本以支持文件夹和 url 路径 (#128)
|
| 307 |
+
5. 更新 pre-commit hook (#129)
|
| 308 |
+
|
| 309 |
+
### 文档
|
| 310 |
+
|
| 311 |
+
1. 将 `yolov5_description.md`、 `yolov5_tutorial.md` 和 `visualization.md` 翻译为英文 (#138, #198, #206)
|
| 312 |
+
2. 新增部署相关中文文档 (#220)
|
| 313 |
+
3. 更新 `config.md`、`faq.md` 和 `pull_request_template.md` (#190, #191, #200)
|
| 314 |
+
4. 更新 `article` 页面 (#133)
|
| 315 |
+
|
| 316 |
+
### 视频
|
| 317 |
+
|
| 318 |
+
1. 发布了[特征图可视化视频](https://www.bilibili.com/video/BV188411s7o8)
|
| 319 |
+
2. 发布了 [YOLOv5 配置文件解读视频](https://www.bilibili.com/video/BV1214y157ck)
|
| 320 |
+
3. 发布了 [RTMDet-s 特征图可视化 demo 视频](https://www.bilibili.com/video/BV1je4y1478R)
|
| 321 |
+
4. 发布了[源码解读和必备调试技巧视频](https://www.bilibili.com/video/BV1N14y1V7mB)
|
| 322 |
+
|
| 323 |
+
### 贡献者
|
| 324 |
+
|
| 325 |
+
总共 14 位开发者参与了本次版本
|
| 326 |
+
|
| 327 |
+
谢谢 @imAzhou, @triple-Mu, @RangeKing, @PeterH0323, @xin-li-67, @Nioolek, @kitecats, @Bin-ze, @JiayuXu0, @cydiachen, @zhiqwang, @Zheng-LinXiao, @hhaAndroid, @wanghonglie
|
| 328 |
+
|
| 329 |
+
## v0.1.1(29/9/2022)
|
| 330 |
+
|
| 331 |
+
基于 MMDetection 的 RTMDet 高精度低延时目标检测算法,我们也同步发布了 RTMDet,并提供了 RTMDet 原理和实现全解析中文文档
|
| 332 |
+
|
| 333 |
+
### ���点
|
| 334 |
+
|
| 335 |
+
1. 支持了 [RTMDet](https://github.com/open-mmlab/mmyolo/blob/main/configs/rtmdet)
|
| 336 |
+
2. 新增了 [RTMDet 原理和实现全解析中文文档](https://github.com/open-mmlab/mmyolo/blob/main/docs/zh_cn/algorithm_descriptions/rtmdet_description.md)
|
| 337 |
+
3. 支持对 backbone 自定义插件,并更新了 How-to 文档 (#75)
|
| 338 |
+
|
| 339 |
+
### Bug 修复
|
| 340 |
+
|
| 341 |
+
1. 修复一些文档错误 (#66, #72, #76, #83, #86)
|
| 342 |
+
2. 修复权重链接错误 (#63)
|
| 343 |
+
3. 修复 `LetterResize` 使用 `imscale` api 时候输出不符合预期的 bug (#105)
|
| 344 |
+
|
| 345 |
+
### 完善
|
| 346 |
+
|
| 347 |
+
1. 缩减 docker 镜像尺寸 (#67)
|
| 348 |
+
2. 简化 BaseMixImageTransform 中 Compose 逻辑 (#71)
|
| 349 |
+
3. test 脚本支持 dump 结果 (#84)
|
| 350 |
+
|
| 351 |
+
#### 贡献者
|
| 352 |
+
|
| 353 |
+
总共 13 位开发者参与了本次版本
|
| 354 |
+
|
| 355 |
+
谢谢 @wanghonglie, @hhaAndroid, @yang-0201, @PeterH0323, @RangeKing, @satuoqaq, @Zheng-LinXiao, @xin-li-67, @suibe-qingtian, @MambaWong, @MichaelCai0912, @rimoire, @Nioolek
|
| 356 |
+
|
| 357 |
+
## v0.1.0(21/9/2022)
|
| 358 |
+
|
| 359 |
+
我们发布了 MMYOLO 开源库,其基于 MMEngine, MMCV 2.x 和 MMDetection 3.x 库. 目前实现了目标检测功能,后续会扩展为多任务。
|
| 360 |
+
|
| 361 |
+
### 亮点
|
| 362 |
+
|
| 363 |
+
1. 支持 YOLOv5/YOLOX 训练,支持 YOLOv6 推理。部署即将支持。
|
| 364 |
+
2. 重构了 MMDetection 的 YOLOX,提供了更快的训练和推理速度。
|
| 365 |
+
3. 提供了详细入门和进阶教程, 包括 YOLOv5 从入门到部署、YOLOv5 算法原理和实现全解析、 特征图可视化等教程。
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/notes/code_style.md
ADDED
|
@@ -0,0 +1,606 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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https://www.python.org/dev/peps/pep-0008/),包含了以下几个方面的内容:
|
| 8 |
+
|
| 9 |
+
- 代码布局,介绍了 Python 中空行、断行以及导入相关的代码风格规范。比如一个常见的问题:当我的代码较长,无法在一行写下时,何处可以断行?
|
| 10 |
+
|
| 11 |
+
- 表达式,介绍了 Python 中表达式空格相关的一些风格规范。
|
| 12 |
+
|
| 13 |
+
- 尾随逗号相关的规范。当列表较长,无法一行写下而写成如下逐行列表时,推荐在末项后加逗号,从而便于追加选项、版本控制等。
|
| 14 |
+
|
| 15 |
+
```python
|
| 16 |
+
# Correct:
|
| 17 |
+
FILES = ['setup.cfg', 'tox.ini']
|
| 18 |
+
# Correct:
|
| 19 |
+
FILES = [
|
| 20 |
+
'setup.cfg',
|
| 21 |
+
'tox.ini',
|
| 22 |
+
]
|
| 23 |
+
# Wrong:
|
| 24 |
+
FILES = ['setup.cfg', 'tox.ini',]
|
| 25 |
+
# Wrong:
|
| 26 |
+
FILES = [
|
| 27 |
+
'setup.cfg',
|
| 28 |
+
'tox.ini'
|
| 29 |
+
]
|
| 30 |
+
```
|
| 31 |
+
|
| 32 |
+
- 命名相关规范、注释相关规范、类型注解相关规范,我们将在后续章节中做详细介绍。
|
| 33 |
+
|
| 34 |
+
"A style guide is about consistency. Consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is the most important." PEP 8 -- Style Guide for Python Code
|
| 35 |
+
|
| 36 |
+
:::{note}
|
| 37 |
+
PEP 8 的代码规范并不是绝对的,项目内的一致性要优先于 PEP 8 的规范。OpenMMLab 各个项目都在 setup.cfg 设定了一些代码规范的设置,请遵照这些设置。一个例子是在 PEP 8 中有如下一个例子:
|
| 38 |
+
|
| 39 |
+
```python
|
| 40 |
+
# Correct:
|
| 41 |
+
hypot2 = x*x + y*y
|
| 42 |
+
# Wrong:
|
| 43 |
+
hypot2 = x * x + y * y
|
| 44 |
+
```
|
| 45 |
+
|
| 46 |
+
这一规范是为了指示不同优先级,但 OpenMMLab 的设置中通常没有启用 yapf 的 `ARITHMETIC_PRECEDENCE_INDICATION` 选项,因而格式规范工具不会按照推荐样式格式化,以设置为准。
|
| 47 |
+
:::
|
| 48 |
+
|
| 49 |
+
### Google 开源项目风格指南
|
| 50 |
+
|
| 51 |
+
[Google 使用的编程风格指南](https://google.github.io/styleguide/pyguide.html),包括了 Python 相关的章节。相较于 PEP 8,该指南提供了更为详尽的代码指南。该指南包括了语言规范和风格规范两个部分。
|
| 52 |
+
|
| 53 |
+
其中,语言规范对 Python 中很多语言特性进行了优缺点的分析,并给出了使用指导意见,如异常、Lambda 表达式、列表推导式、metaclass 等。
|
| 54 |
+
|
| 55 |
+
风格规范的内容与 PEP 8 较为接近,大部分约定建立在 PEP 8 的基础上,也有一些更为详细的约定,如函数长度、TODO 注释、文件与 socket 对象的访问等。
|
| 56 |
+
|
| 57 |
+
推荐将该指南作为参考进行开发,但不必严格遵照,一来该指南存在一些 Python 2 兼容需求,例如指南中要求所有无基类的类应当显式地继承 Object, 而在仅使用 Python 3 的环境中,这一要求是不必要的,依本项目中的惯例即可。二来 OpenMMLab 的项目作为框架级的开源软件,不必对一些高级技巧过于避讳,尤其是 MMCV。但尝试使用这些技巧前应当认真考虑是否真的有必要,并寻求其他开发人员的广泛评估。
|
| 58 |
+
|
| 59 |
+
另外需要注意的一处规范是关于包的导入,在该指南中,要求导入本地包时必须使用路径全称,且导入的每一个模块都应当单独成行,通常这是不必要的,而且也不符合目前项目的开发惯例,此处进行如下约定:
|
| 60 |
+
|
| 61 |
+
```python
|
| 62 |
+
# Correct
|
| 63 |
+
from mmcv.cnn.bricks import (Conv2d, build_norm_layer, DropPath, MaxPool2d,
|
| 64 |
+
Linear)
|
| 65 |
+
from ..utils import ext_loader
|
| 66 |
+
|
| 67 |
+
# Wrong
|
| 68 |
+
from mmcv.cnn.bricks import Conv2d, build_norm_layer, DropPath, MaxPool2d, \
|
| 69 |
+
Linear # 使用括号进行连接,而不是反斜杠
|
| 70 |
+
from ...utils import is_str # 最多向上回溯一层,过多的回溯容易导致结构混乱
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
OpenMMLab 项目使用 pre-commit 工具自动格式化代码,详情见[贡献代码](../recommended_topics/contributing.md#代码风格)。
|
| 74 |
+
|
| 75 |
+
## 命名规范
|
| 76 |
+
|
| 77 |
+
### 命名规范的重要性
|
| 78 |
+
|
| 79 |
+
优秀的命名是良好代码可读的基础。基础的命名规范对各类变量的命名做了要求,使读者可以方便地根据代码名了解变量是一个类 / 局部变量 / 全局变量等。而优秀的命名则需要代码作者对于变量的功能有清晰的认识,以及良好的表达能力,从而使读者根据名称就能了解其含义,甚至帮助了解该段代码的功能。
|
| 80 |
+
|
| 81 |
+
### 基础命名规范
|
| 82 |
+
|
| 83 |
+
| 类型 | 公有 | 私有 |
|
| 84 |
+
| --------------- | ---------------- | ------------------ |
|
| 85 |
+
| 模块 | lower_with_under | \_lower_with_under |
|
| 86 |
+
| 包 | lower_with_under | |
|
| 87 |
+
| 类 | CapWords | \_CapWords |
|
| 88 |
+
| 异常 | CapWordsError | |
|
| 89 |
+
| 函数(方法) | lower_with_under | \_lower_with_under |
|
| 90 |
+
| 函数 / 方法参数 | lower_with_under | |
|
| 91 |
+
| 全局 / 类内常量 | CAPS_WITH_UNDER | \_CAPS_WITH_UNDER |
|
| 92 |
+
| 全局 / 类内变量 | lower_with_under | \_lower_with_under |
|
| 93 |
+
| 变量 | lower_with_under | \_lower_with_under |
|
| 94 |
+
| 局部变量 | lower_with_under | |
|
| 95 |
+
|
| 96 |
+
注意:
|
| 97 |
+
|
| 98 |
+
- 尽量避免变量名与保留字冲突,特殊情况下如不可避免,可使用一个后置下划线,如 class\_
|
| 99 |
+
- 尽量不要使用过于简单的命名,除了约定俗成的循环变量 i,文件变量 f,错误变量 e 等。
|
| 100 |
+
- 不会被用到的变量可以命名为 \_,逻辑检查器会将其忽略。
|
| 101 |
+
|
| 102 |
+
### 命名技巧
|
| 103 |
+
|
| 104 |
+
良好的变量命名需要保证三点:
|
| 105 |
+
|
| 106 |
+
1. 含义准确,没有歧义
|
| 107 |
+
2. 长短适中
|
| 108 |
+
3. 前后统一
|
| 109 |
+
|
| 110 |
+
```python
|
| 111 |
+
# Wrong
|
| 112 |
+
class Masks(metaclass=ABCMeta): # 命名无法表现基类;Instance or Semantic?
|
| 113 |
+
pass
|
| 114 |
+
|
| 115 |
+
# Correct
|
| 116 |
+
class BaseInstanceMasks(metaclass=ABCMeta):
|
| 117 |
+
pass
|
| 118 |
+
|
| 119 |
+
# Wrong,不同地方含义相同的变量尽量用统一的命名
|
| 120 |
+
def __init__(self, inplanes, planes):
|
| 121 |
+
pass
|
| 122 |
+
|
| 123 |
+
def __init__(self, in_channels, out_channels):
|
| 124 |
+
pass
|
| 125 |
+
```
|
| 126 |
+
|
| 127 |
+
常见的函数命名方法:
|
| 128 |
+
|
| 129 |
+
- 动宾命名法:crop_img, init_weights
|
| 130 |
+
- 动宾倒置命名法:imread, bbox_flip
|
| 131 |
+
|
| 132 |
+
注意函数命名与参数的顺序,保证主语在前,符合语言习惯:
|
| 133 |
+
|
| 134 |
+
- check_keys_exist(key, container)
|
| 135 |
+
- check_keys_contain(container, key)
|
| 136 |
+
|
| 137 |
+
注意避免非常规或统一约定的缩写,如 nb -> num_blocks,in_nc -> in_channels
|
| 138 |
+
|
| 139 |
+
## docstring 规范
|
| 140 |
+
|
| 141 |
+
### 为什么要写 docstring
|
| 142 |
+
|
| 143 |
+
docstring 是对一个类、一个函数功能与 API 接口的详细描述,有两个功能,一是帮助其他开发者了解代码功能,方便 debug 和复用代码;二是在 Readthedocs 文档中自动生成相关的 API reference 文档,帮助不了解源代码的社区用户使用相关功能。
|
| 144 |
+
|
| 145 |
+
### 如何写 docstring
|
| 146 |
+
|
| 147 |
+
与注释不同,一份规范的 docstring 有着严格的格式要求,以便于 Python 解释器以及 sphinx 进行文档解析,详细的 docstring 约定参见 [PEP 257](https://www.python.org/dev/peps/pep-0257/)。此处以例子的形式介绍各种文档的标准格式,参考格式为 [Google 风格](https://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_style_rules/#comments)。
|
| 148 |
+
|
| 149 |
+
1. 模块文档
|
| 150 |
+
|
| 151 |
+
代码风格规范推荐为每一个模块(即 Python 文件)编写一个 docstring,但目前 OpenMMLab 项目大部分没有此类 docstring,因此不做硬性要求。
|
| 152 |
+
|
| 153 |
+
```python
|
| 154 |
+
"""A one line summary of the module or program, terminated by a period.
|
| 155 |
+
|
| 156 |
+
Leave one blank line. The rest of this docstring should contain an
|
| 157 |
+
overall description of the module or program. Optionally, it may also
|
| 158 |
+
contain a brief description of exported classes and functions and/or usage
|
| 159 |
+
examples.
|
| 160 |
+
|
| 161 |
+
Typical usage example:
|
| 162 |
+
|
| 163 |
+
foo = ClassFoo()
|
| 164 |
+
bar = foo.FunctionBar()
|
| 165 |
+
"""
|
| 166 |
+
```
|
| 167 |
+
|
| 168 |
+
2. 类文档
|
| 169 |
+
|
| 170 |
+
类文档是我们最常需要编写的,此处,按照 OpenMMLab 的惯例,我们使用了与 Google 风格不同的写法。如下例所示,文档中没有使用 Attributes 描述类属性,而是使用 Args 描述 __init__ 函数的参数。
|
| 171 |
+
|
| 172 |
+
在 Args 中,遵照 `parameter (type): Description.` 的格式,描述每一个参数类型和功能。其中,多种类型可使用 `(float or str)` 的写法,可以为 None 的参数可以写为 `(int, optional)`。
|
| 173 |
+
|
| 174 |
+
```python
|
| 175 |
+
class BaseRunner(metaclass=ABCMeta):
|
| 176 |
+
"""The base class of Runner, a training helper for PyTorch.
|
| 177 |
+
|
| 178 |
+
All subclasses should implement the following APIs:
|
| 179 |
+
|
| 180 |
+
- ``run()``
|
| 181 |
+
- ``train()``
|
| 182 |
+
- ``val()``
|
| 183 |
+
- ``save_checkpoint()``
|
| 184 |
+
|
| 185 |
+
Args:
|
| 186 |
+
model (:obj:`torch.nn.Module`): The model to be run.
|
| 187 |
+
batch_processor (callable, optional): A callable method that process
|
| 188 |
+
a data batch. The interface of this method should be
|
| 189 |
+
``batch_processor(model, data, train_mode) -> dict``.
|
| 190 |
+
Defaults to None.
|
| 191 |
+
optimizer (dict or :obj:`torch.optim.Optimizer`, optional): It can be
|
| 192 |
+
either an optimizer (in most cases) or a dict of optimizers
|
| 193 |
+
(in models that requires more than one optimizer, e.g., GAN).
|
| 194 |
+
Defaults to None.
|
| 195 |
+
work_dir (str, optional): The working directory to save checkpoints
|
| 196 |
+
and logs. Defaults to None.
|
| 197 |
+
logger (:obj:`logging.Logger`): Logger used during training.
|
| 198 |
+
Defaults to None. (The default value is just for backward
|
| 199 |
+
compatibility)
|
| 200 |
+
meta (dict, optional): A dict records some import information such as
|
| 201 |
+
environment info and seed, which will be logged in logger hook.
|
| 202 |
+
Defaults to None.
|
| 203 |
+
max_epochs (int, optional): Total training epochs. Defaults to None.
|
| 204 |
+
max_iters (int, optional): Total training iterations. Defaults to None.
|
| 205 |
+
"""
|
| 206 |
+
|
| 207 |
+
def __init__(self,
|
| 208 |
+
model,
|
| 209 |
+
batch_processor=None,
|
| 210 |
+
optimizer=None,
|
| 211 |
+
work_dir=None,
|
| 212 |
+
logger=None,
|
| 213 |
+
meta=None,
|
| 214 |
+
max_iters=None,
|
| 215 |
+
max_epochs=None):
|
| 216 |
+
...
|
| 217 |
+
```
|
| 218 |
+
|
| 219 |
+
另外,在一些算法实现的主体类中,建议加入原论文的链接;如果参考了其他开源代码的实现,则应加入 modified from,而如果是直接复制了其他代码库的实现,则应加入 copied from ,并注意源码的 License。如有必要,也可以通过 .. math:: 来加入数学公式
|
| 220 |
+
|
| 221 |
+
```python
|
| 222 |
+
# 参考实现
|
| 223 |
+
# This func is modified from `detectron2
|
| 224 |
+
# <https://github.com/facebookresearch/detectron2/blob/ffff8acc35ea88ad1cb1806ab0f00b4c1c5dbfd9/detectron2/structures/masks.py#L387>`_.
|
| 225 |
+
|
| 226 |
+
# 复制代码
|
| 227 |
+
# This code was copied from the `ubelt
|
| 228 |
+
# library<https://github.com/Erotemic/ubelt>`_.
|
| 229 |
+
|
| 230 |
+
# 引用论文 & 添加公式
|
| 231 |
+
class LabelSmoothLoss(nn.Module):
|
| 232 |
+
r"""Initializer for the label smoothed cross entropy loss.
|
| 233 |
+
|
| 234 |
+
Refers to `Rethinking the Inception Architecture for Computer Vision
|
| 235 |
+
<https://arxiv.org/abs/1512.00567>`_.
|
| 236 |
+
|
| 237 |
+
This decreases gap between output scores and encourages generalization.
|
| 238 |
+
Labels provided to forward can be one-hot like vectors (NxC) or class
|
| 239 |
+
indices (Nx1).
|
| 240 |
+
And this accepts linear combination of one-hot like labels from mixup or
|
| 241 |
+
cutmix except multi-label task.
|
| 242 |
+
|
| 243 |
+
Args:
|
| 244 |
+
label_smooth_val (float): The degree of label smoothing.
|
| 245 |
+
num_classes (int, optional): Number of classes. Defaults to None.
|
| 246 |
+
mode (str): Refers to notes, Options are "original", "classy_vision",
|
| 247 |
+
"multi_label". Defaults to "classy_vision".
|
| 248 |
+
reduction (str): The method used to reduce the loss.
|
| 249 |
+
Options are "none", "mean" and "sum". Defaults to 'mean'.
|
| 250 |
+
loss_weight (float): Weight of the loss. Defaults to 1.0.
|
| 251 |
+
|
| 252 |
+
Note:
|
| 253 |
+
if the ``mode`` is "original", this will use the same label smooth
|
| 254 |
+
method as the original paper as:
|
| 255 |
+
|
| 256 |
+
.. math::
|
| 257 |
+
(1-\epsilon)\delta_{k, y} + \frac{\epsilon}{K}
|
| 258 |
+
|
| 259 |
+
where :math:`\epsilon` is the ``label_smooth_val``, :math:`K` is
|
| 260 |
+
the ``num_classes`` and :math:`\delta_{k,y}` is Dirac delta,
|
| 261 |
+
which equals 1 for k=y and 0 otherwise.
|
| 262 |
+
|
| 263 |
+
if the ``mode`` is "classy_vision", this will use the same label
|
| 264 |
+
smooth method as the `facebookresearch/ClassyVision
|
| 265 |
+
<https://github.com/facebookresearch/ClassyVision/blob/main/classy_vision/losses/label_smoothing_loss.py>`_ repo as:
|
| 266 |
+
|
| 267 |
+
.. math::
|
| 268 |
+
\frac{\delta_{k, y} + \epsilon/K}{1+\epsilon}
|
| 269 |
+
|
| 270 |
+
if the ``mode`` is "multi_label", this will accept labels from
|
| 271 |
+
multi-label task and smoothing them as:
|
| 272 |
+
|
| 273 |
+
.. math::
|
| 274 |
+
(1-2\epsilon)\delta_{k, y} + \epsilon
|
| 275 |
+
```
|
| 276 |
+
|
| 277 |
+
```{note}
|
| 278 |
+
注意 \`\`here\`\`、\`here\`、"here" 三种引号功能是不同。
|
| 279 |
+
|
| 280 |
+
在 reStructured 语法中,\`\`here\`\` 表示一段代码;\`here\` 表示斜体;"here" 无特殊含义,一般可用来表示字符串。其中 \`here\` 的用法与 Markdown 中不同,需要多加留意。
|
| 281 |
+
另外还有 :obj:\`type\` 这种更规范的表示类的写法,但鉴于长度,不做特别要求,一般仅用于表示非常用类型。
|
| 282 |
+
```
|
| 283 |
+
|
| 284 |
+
3. 方法(函数)文档
|
| 285 |
+
|
| 286 |
+
函数文档与类文档的结构基本一致,但需要加入返回值文档。对于较为复杂的函数和类,可以使用 Examples 字段加入示例;如果需要对参数加入一些较长的备注,可以加入 Note 字段进行说明。
|
| 287 |
+
|
| 288 |
+
对于使用较为复杂的类或函数,比起看大段大段的说明文字和参数文档,添加合适的示例更能帮助用户迅速了解其用法。需要注意的是,这些示例最好是能够直接在 Python 交互式环境中运行的,并给出一些相对应的结果。如果存在多个示例,可以使用注释简单说明每段示例,也能起到分隔作用。
|
| 289 |
+
|
| 290 |
+
```python
|
| 291 |
+
def import_modules_from_strings(imports, allow_failed_imports=False):
|
| 292 |
+
"""Import modules from the given list of strings.
|
| 293 |
+
|
| 294 |
+
Args:
|
| 295 |
+
imports (list | str | None): The given module names to be imported.
|
| 296 |
+
allow_failed_imports (bool): If True, the failed imports will return
|
| 297 |
+
None. Otherwise, an ImportError is raise. Defaults to False.
|
| 298 |
+
|
| 299 |
+
Returns:
|
| 300 |
+
List[module] | module | None: The imported modules.
|
| 301 |
+
All these three lines in docstring will be compiled into the same
|
| 302 |
+
line in readthedocs.
|
| 303 |
+
|
| 304 |
+
Examples:
|
| 305 |
+
>>> osp, sys = import_modules_from_strings(
|
| 306 |
+
... ['os.path', 'sys'])
|
| 307 |
+
>>> import os.path as osp_
|
| 308 |
+
>>> import sys as sys_
|
| 309 |
+
>>> assert osp == osp_
|
| 310 |
+
>>> assert sys == sys_
|
| 311 |
+
"""
|
| 312 |
+
...
|
| 313 |
+
```
|
| 314 |
+
|
| 315 |
+
如果函数接口在某个版本发生了变化,需要在 docstring 中加入相关的说明,必要时添加 Note 或者 Warning 进行说明,例如:
|
| 316 |
+
|
| 317 |
+
```python
|
| 318 |
+
class CheckpointHook(Hook):
|
| 319 |
+
"""Save checkpoints periodically.
|
| 320 |
+
|
| 321 |
+
Args:
|
| 322 |
+
out_dir (str, optional): The root directory to save checkpoints. If
|
| 323 |
+
not specified, ``runner.work_dir`` will be used by default. If
|
| 324 |
+
specified, the ``out_dir`` will be the concatenation of
|
| 325 |
+
``out_dir`` and the last level directory of ``runner.work_dir``.
|
| 326 |
+
Defaults to None. `Changed in version 1.3.15.`
|
| 327 |
+
|
| 328 |
+
Warning:
|
| 329 |
+
Before v1.3.15, the ``out_dir`` argument indicates the path where the
|
| 330 |
+
checkpoint is stored. However, in v1.3.15 and later, ``out_dir``
|
| 331 |
+
indicates the root directory and the final path to save checkpoint is
|
| 332 |
+
the concatenation of out_dir and the last level directory of
|
| 333 |
+
``runner.work_dir``. Suppose the value of ``out_dir`` is
|
| 334 |
+
"/path/of/A" and the value of ``runner.work_dir`` is "/path/of/B",
|
| 335 |
+
then the final path will be "/path/of/A/B".
|
| 336 |
+
```
|
| 337 |
+
|
| 338 |
+
如果参数或返回值里带有需要展开描述字段的 dict,则应该采用如下格式:
|
| 339 |
+
|
| 340 |
+
```python
|
| 341 |
+
def func(x):
|
| 342 |
+
r"""
|
| 343 |
+
Args:
|
| 344 |
+
x (None): A dict with 2 keys, ``padded_targets``, and ``targets``.
|
| 345 |
+
|
| 346 |
+
- ``targets`` (list[Tensor]): A list of tensors.
|
| 347 |
+
Each tensor has the shape of :math:`(T_i)`. Each
|
| 348 |
+
element is the index of a character.
|
| 349 |
+
- ``padded_targets`` (Tensor): A tensor of shape :math:`(N)`.
|
| 350 |
+
Each item is the length of a word.
|
| 351 |
+
|
| 352 |
+
Returns:
|
| 353 |
+
dict: A dict with 2 keys, ``padded_targets``, and ``targets``.
|
| 354 |
+
|
| 355 |
+
- ``targets`` (list[Tensor]): A list of tensors.
|
| 356 |
+
Each tensor has the shape of :math:`(T_i)`. Each
|
| 357 |
+
element is the index of a character.
|
| 358 |
+
- ``padded_targets`` (Tensor): A tensor of shape :math:`(N)`.
|
| 359 |
+
Each item is the length of a word.
|
| 360 |
+
"""
|
| 361 |
+
return x
|
| 362 |
+
```
|
| 363 |
+
|
| 364 |
+
```{important}
|
| 365 |
+
为了生成 readthedocs 文档,文档的编写需要按照 ReStructrued 文档格式,否则会产生文档渲染错误,在提交 PR 前,最好生成并预览一下文档效果。
|
| 366 |
+
语法规范参考:
|
| 367 |
+
|
| 368 |
+
- [reStructuredText Primer - Sphinx documentation](https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html#)
|
| 369 |
+
- [Example Google Style Python Docstrings ‒ napoleon 0.7 documentation](https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html#example-google)
|
| 370 |
+
```
|
| 371 |
+
|
| 372 |
+
## 注释规范
|
| 373 |
+
|
| 374 |
+
### 为什么要写注释
|
| 375 |
+
|
| 376 |
+
对于一个开源项目,团队合作以及社区之间的合作是必不可少的,因而尤其要重视合理的注释。不写注释的代码,很有可能过几个月自己也难以理解,造成额外的阅读和修改成本。
|
| 377 |
+
|
| 378 |
+
### 如何写注释
|
| 379 |
+
|
| 380 |
+
最需要写注释的是代码中那些技巧性的部分。如果你在下次代码审查的时候必须解释一下,那么你应该现在就给它写注释。对于复杂的操作,应该在其操作开始前写上若干行注释。对于不是一目了然的代码,应在其行尾添加注释。
|
| 381 |
+
—— Google 开源项目风格指南
|
| 382 |
+
|
| 383 |
+
```python
|
| 384 |
+
# We use a weighted dictionary search to find out where i is in
|
| 385 |
+
# the array. We extrapolate position based on the largest num
|
| 386 |
+
# in the array and the array size and then do binary search to
|
| 387 |
+
# get the exact number.
|
| 388 |
+
if i & (i-1) == 0: # True if i is 0 or a power of 2.
|
| 389 |
+
```
|
| 390 |
+
|
| 391 |
+
为了提高可读性, 注释应该至少离开代码2个空格.
|
| 392 |
+
另一方面, 绝不要描述代码. 假设阅读代码的人比你更懂Python, 他只是不知道你的代码要做什么.
|
| 393 |
+
—— Google 开源项目风格指南
|
| 394 |
+
|
| 395 |
+
```python
|
| 396 |
+
# Wrong:
|
| 397 |
+
# Now go through the b array and make sure whenever i occurs
|
| 398 |
+
# the next element is i+1
|
| 399 |
+
|
| 400 |
+
# Wrong:
|
| 401 |
+
if i & (i-1) == 0: # True if i bitwise and i-1 is 0.
|
| 402 |
+
```
|
| 403 |
+
|
| 404 |
+
在注释中,可以使用 Markdown 语法,因为开发人员通常熟悉 Markdown 语法,这样可以便于交流理解,如可使用单反引号表示代码和变量(注意不要和 docstring 中的 ReStructured 语法混淆)
|
| 405 |
+
|
| 406 |
+
```python
|
| 407 |
+
# `_reversed_padding_repeated_twice` is the padding to be passed to
|
| 408 |
+
# `F.pad` if needed (e.g., for non-zero padding types that are
|
| 409 |
+
# implemented as two ops: padding + conv). `F.pad` accepts paddings in
|
| 410 |
+
# reverse order than the dimension.
|
| 411 |
+
self._reversed_padding_repeated_twice = _reverse_repeat_tuple(self.padding, 2)
|
| 412 |
+
```
|
| 413 |
+
|
| 414 |
+
### 注释示例
|
| 415 |
+
|
| 416 |
+
1. 出自 `mmcv/utils/registry.py`,对于较为复杂的逻辑结构,通过注释,明确了优先级关系。
|
| 417 |
+
|
| 418 |
+
```python
|
| 419 |
+
# self.build_func will be set with the following priority:
|
| 420 |
+
# 1. build_func
|
| 421 |
+
# 2. parent.build_func
|
| 422 |
+
# 3. build_from_cfg
|
| 423 |
+
if build_func is None:
|
| 424 |
+
if parent is not None:
|
| 425 |
+
self.build_func = parent.build_func
|
| 426 |
+
else:
|
| 427 |
+
self.build_func = build_from_cfg
|
| 428 |
+
else:
|
| 429 |
+
self.build_func = build_func
|
| 430 |
+
```
|
| 431 |
+
|
| 432 |
+
2. 出自 `mmcv/runner/checkpoint.py`,对于 bug 修复中的一些特殊处理,可以附带相关的 issue 链接,帮助其他人了解 bug 背景。
|
| 433 |
+
|
| 434 |
+
```python
|
| 435 |
+
def _save_ckpt(checkpoint, file):
|
| 436 |
+
# The 1.6 release of PyTorch switched torch.save to use a new
|
| 437 |
+
# zipfile-based file format. It will cause RuntimeError when a
|
| 438 |
+
# checkpoint was saved in high version (PyTorch version>=1.6.0) but
|
| 439 |
+
# loaded in low version (PyTorch version<1.6.0). More details at
|
| 440 |
+
# https://github.com/open-mmlab/mmpose/issues/904
|
| 441 |
+
if digit_version(TORCH_VERSION) >= digit_version('1.6.0'):
|
| 442 |
+
torch.save(checkpoint, file, _use_new_zipfile_serialization=False)
|
| 443 |
+
else:
|
| 444 |
+
torch.save(checkpoint, file)
|
| 445 |
+
```
|
| 446 |
+
|
| 447 |
+
## 类型注解
|
| 448 |
+
|
| 449 |
+
### 为什么要写类型注解
|
| 450 |
+
|
| 451 |
+
类型注解是对函数中变量的类型做限定或提示,为代码的安全性提供保障、增强代码的可读性、避免出现类型相关的错误。
|
| 452 |
+
Python 没有对类型做强制限制,类型注解只起到一个提示作用,通常你的 IDE 会解析这些类型注解,然后在你调用相关代码时对类型做提示。另外也有类型注解检查工具,这些工具会根据类型注解,对代码中可能出现的问题进行检查,减少 bug 的出现。
|
| 453 |
+
需要注意的是,通常我们不需要注释模块中的所有函数:
|
| 454 |
+
|
| 455 |
+
1. 公共的 API 需要注释
|
| 456 |
+
2. 在代码的安全性,清晰性和灵活性上进行权衡是否注释
|
| 457 |
+
3. 对于容易出现类型相关的错误的代码进行注释
|
| 458 |
+
4. 难以理解的代码请进行注释
|
| 459 |
+
5. 若代码中的类型已经稳定,可以进行注释. 对于一份成熟的代码,多数情况下,即使注释了所有的函数,也不会丧失太多的灵活性.
|
| 460 |
+
|
| 461 |
+
### 如何写类型注解
|
| 462 |
+
|
| 463 |
+
1. 函数 / 方法类型注解,通常不对 self 和 cls 注释。
|
| 464 |
+
|
| 465 |
+
```python
|
| 466 |
+
from typing import Optional, List, Tuple
|
| 467 |
+
|
| 468 |
+
# 全部位于一行
|
| 469 |
+
def my_method(self, first_var: int) -> int:
|
| 470 |
+
pass
|
| 471 |
+
|
| 472 |
+
# 另起一行
|
| 473 |
+
def my_method(
|
| 474 |
+
self, first_var: int,
|
| 475 |
+
second_var: float) -> Tuple[MyLongType1, MyLongType1, MyLongType1]:
|
| 476 |
+
pass
|
| 477 |
+
|
| 478 |
+
# 单独成行(具体的应用场合与行宽有关,建议结合 yapf 自动化格式使用)
|
| 479 |
+
def my_method(
|
| 480 |
+
self, first_var: int, second_var: float
|
| 481 |
+
) -> Tuple[MyLongType1, MyLongType1, MyLongType1]:
|
| 482 |
+
pass
|
| 483 |
+
|
| 484 |
+
# 引用尚未被定义的类型
|
| 485 |
+
class MyClass:
|
| 486 |
+
def __init__(self,
|
| 487 |
+
stack: List["MyClass"]) -> None:
|
| 488 |
+
pass
|
| 489 |
+
```
|
| 490 |
+
|
| 491 |
+
注:类型注解中的类型可以是 Python 内置类型,也可以是自定义类,还可以使用 Python 提供的 wrapper 类对类型注解进行装饰,一些常见的注解如下:
|
| 492 |
+
|
| 493 |
+
```python
|
| 494 |
+
# 数值类型
|
| 495 |
+
from numbers import Number
|
| 496 |
+
|
| 497 |
+
# 可选类型,指参数可以为 None
|
| 498 |
+
from typing import Optional
|
| 499 |
+
def foo(var: Optional[int] = None):
|
| 500 |
+
pass
|
| 501 |
+
|
| 502 |
+
# 联合类型,指同时接受多种类型
|
| 503 |
+
from typing import Union
|
| 504 |
+
def foo(var: Union[float, str]):
|
| 505 |
+
pass
|
| 506 |
+
|
| 507 |
+
from typing import Sequence # 序列类型
|
| 508 |
+
from typing import Iterable # 可迭代类型
|
| 509 |
+
from typing import Any # 任意类型
|
| 510 |
+
from typing import Callable # 可调用类型
|
| 511 |
+
|
| 512 |
+
from typing import List, Dict # 列表和字典的泛型类型
|
| 513 |
+
from typing import Tuple # 元组的特殊格式
|
| 514 |
+
# 虽然在 Python 3.9 中,list, tuple 和 dict 本身已支持泛型,但为了支持之前的版本
|
| 515 |
+
# 我们在进行类型注解时还是需要使用 List, Tuple, Dict 类型
|
| 516 |
+
# 另外,在对参数类型进行注解时,尽量使用 Sequence & Iterable & Mapping
|
| 517 |
+
# List, Tuple, Dict 主要用于返回值类型注解
|
| 518 |
+
# 参见 https://docs.python.org/3/library/typing.html#typing.List
|
| 519 |
+
```
|
| 520 |
+
|
| 521 |
+
2. 变量类型注解,一般用于难以直接推断其类型时
|
| 522 |
+
|
| 523 |
+
```python
|
| 524 |
+
# Recommend: 带类型注解的赋值
|
| 525 |
+
a: Foo = SomeUndecoratedFunction()
|
| 526 |
+
a: List[int]: [1, 2, 3] # List 只支持单一类型泛型,可使用 Union
|
| 527 |
+
b: Tuple[int, int] = (1, 2) # 长度固定为 2
|
| 528 |
+
c: Tuple[int, ...] = (1, 2, 3) # 变长
|
| 529 |
+
d: Dict[str, int] = {'a': 1, 'b': 2}
|
| 530 |
+
|
| 531 |
+
# Not Recommend:行尾类型注释
|
| 532 |
+
# 虽然这种方式被写在了 Google 开源指南中,但这是一种为了支持 Python 2.7 版本
|
| 533 |
+
# 而补充的注释方式,鉴于我们只支持 Python 3, 为了风格统一,不推荐使用这种方式。
|
| 534 |
+
a = SomeUndecoratedFunction() # type: Foo
|
| 535 |
+
a = [1, 2, 3] # type: List[int]
|
| 536 |
+
b = (1, 2, 3) # type: Tuple[int, ...]
|
| 537 |
+
c = (1, "2", 3.5) # type: Tuple[int, Text, float]
|
| 538 |
+
```
|
| 539 |
+
|
| 540 |
+
3. 泛型
|
| 541 |
+
|
| 542 |
+
上文中我们知道,typing 中提供了 list 和 dict 的泛型类型,那么我们自己是否可以定义类似的泛型呢?
|
| 543 |
+
|
| 544 |
+
```python
|
| 545 |
+
from typing import TypeVar, Generic
|
| 546 |
+
|
| 547 |
+
KT = TypeVar('KT')
|
| 548 |
+
VT = TypeVar('VT')
|
| 549 |
+
|
| 550 |
+
class Mapping(Generic[KT, VT]):
|
| 551 |
+
def __init__(self, data: Dict[KT, VT]):
|
| 552 |
+
self._data = data
|
| 553 |
+
|
| 554 |
+
def __getitem__(self, key: KT) -> VT:
|
| 555 |
+
return self._data[key]
|
| 556 |
+
```
|
| 557 |
+
|
| 558 |
+
使用上述方法,我们定义了一个拥有泛型能力的映射类,实际用法���下:
|
| 559 |
+
|
| 560 |
+
```python
|
| 561 |
+
mapping = Mapping[str, float]({'a': 0.5})
|
| 562 |
+
value: float = example['a']
|
| 563 |
+
```
|
| 564 |
+
|
| 565 |
+
另外,我们也可以利用 TypeVar 在函数签名中指定联动的多个类型:
|
| 566 |
+
|
| 567 |
+
```python
|
| 568 |
+
from typing import TypeVar, List
|
| 569 |
+
|
| 570 |
+
T = TypeVar('T') # Can be anything
|
| 571 |
+
A = TypeVar('A', str, bytes) # Must be str or bytes
|
| 572 |
+
|
| 573 |
+
|
| 574 |
+
def repeat(x: T, n: int) -> List[T]:
|
| 575 |
+
"""Return a list containing n references to x."""
|
| 576 |
+
return [x]*n
|
| 577 |
+
|
| 578 |
+
|
| 579 |
+
def longest(x: A, y: A) -> A:
|
| 580 |
+
"""Return the longest of two strings."""
|
| 581 |
+
return x if len(x) >= len(y) else y
|
| 582 |
+
```
|
| 583 |
+
|
| 584 |
+
更多关于类型注解的写法请参考 [typing](https://docs.python.org/3/library/typing.html)。
|
| 585 |
+
|
| 586 |
+
### 类型注解检查工具
|
| 587 |
+
|
| 588 |
+
[mypy](https://mypy.readthedocs.io/en/stable/) 是一个 Python 静态类型检查工具。根据你的类型注解,mypy 会检查传参、赋值等操作是否符合类型注解,从而避免可能出现的 bug。
|
| 589 |
+
|
| 590 |
+
例如如下的一个 Python 脚本文件 test.py:
|
| 591 |
+
|
| 592 |
+
```python
|
| 593 |
+
def foo(var: int) -> float:
|
| 594 |
+
return float(var)
|
| 595 |
+
|
| 596 |
+
a: str = foo('2.0')
|
| 597 |
+
b: int = foo('3.0') # type: ignore
|
| 598 |
+
```
|
| 599 |
+
|
| 600 |
+
运行 mypy test.py 可以得到如下检查结果,分别指出了第 4 行在函数调用和返回值赋值两处类型错误。而第 5 行同样存在两个类型错误,由于使用了 type: ignore 而被忽略了,只有部分特殊情况可能需要此类忽略。
|
| 601 |
+
|
| 602 |
+
```
|
| 603 |
+
test.py:4: error: Incompatible types in assignment (expression has type "float", variable has type "int")
|
| 604 |
+
test.py:4: error: Argument 1 to "foo" has incompatible type "str"; expected "int"
|
| 605 |
+
Found 2 errors in 1 file (checked 1 source file)
|
| 606 |
+
```
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/notes/compatibility.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# MMYOLO 兼容性说明
|
| 2 |
+
|
| 3 |
+
## MMYOLO v0.3.0
|
| 4 |
+
|
| 5 |
+
### METAINFO 修改
|
| 6 |
+
|
| 7 |
+
为了和 OpenMMLab 其他仓库统一,将 Dataset 里 `METAINFO` 的所有键从大写改为小写。
|
| 8 |
+
|
| 9 |
+
| 在 v0.3.0 之前 | v0.3.0 及之后 |
|
| 10 |
+
| :------------: | :-----------: |
|
| 11 |
+
| CLASSES | classes |
|
| 12 |
+
| PALETTE | palette |
|
| 13 |
+
| DATASET_TYPE | dataset_type |
|
| 14 |
+
|
| 15 |
+
### 关于图片 shape 顺序的说明
|
| 16 |
+
|
| 17 |
+
在 OpenMMLab 2.0 中, 为了与 OpenCV 的输入参数相一致,图片处理 pipeline 中关于图像 shape 的输入参数总是以 `(width, height)` 的顺序排列。
|
| 18 |
+
相反,为了计算方便,经过 pipeline 和 model 的字段的顺序是 `(height, width)`。具体来说在每个数据 pipeline 处理的结果中,字段和它们的值含义如下:
|
| 19 |
+
|
| 20 |
+
- img_shape: (height, width)
|
| 21 |
+
- ori_shape: (height, width)
|
| 22 |
+
- pad_shape: (height, width)
|
| 23 |
+
- batch_input_shape: (height, width)
|
| 24 |
+
|
| 25 |
+
以 `Mosaic` 为例,其初始化参数如下所示:
|
| 26 |
+
|
| 27 |
+
```python
|
| 28 |
+
@TRANSFORMS.register_module()
|
| 29 |
+
class Mosaic(BaseTransform):
|
| 30 |
+
def __init__(self,
|
| 31 |
+
img_scale: Tuple[int, int] = (640, 640),
|
| 32 |
+
center_ratio_range: Tuple[float, float] = (0.5, 1.5),
|
| 33 |
+
bbox_clip_border: bool = True,
|
| 34 |
+
pad_val: float = 114.0,
|
| 35 |
+
prob: float = 1.0) -> None:
|
| 36 |
+
...
|
| 37 |
+
|
| 38 |
+
# img_scale 顺序应该是 (width, height)
|
| 39 |
+
self.img_scale = img_scale
|
| 40 |
+
|
| 41 |
+
def transform(self, results: dict) -> dict:
|
| 42 |
+
...
|
| 43 |
+
|
| 44 |
+
results['img'] = mosaic_img
|
| 45 |
+
# (height, width)
|
| 46 |
+
results['img_shape'] = mosaic_img.shape[:2]
|
| 47 |
+
```
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/notes/conventions.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 默认约定
|
| 2 |
+
|
| 3 |
+
如果你想把 MMYOLO 修改为自己的项目,请遵循下面的约定。
|
| 4 |
+
|
| 5 |
+
## 关于图片 shape 顺序的说明
|
| 6 |
+
|
| 7 |
+
在OpenMMLab 2.0中, 为了与 OpenCV 的输入参数相一致,图片处理 pipeline 中关于图像 shape 的输入参数总是以 `(width, height)` 的顺序排列。
|
| 8 |
+
相反,为了计算方便,经过 pipeline 和 model 的字段的顺序是 `(height, width)`。具体来说在每个数据 pipeline 处理的结果中,字段和它们的值含义如下:
|
| 9 |
+
|
| 10 |
+
- img_shape: (height, width)
|
| 11 |
+
- ori_shape: (height, width)
|
| 12 |
+
- pad_shape: (height, width)
|
| 13 |
+
- batch_input_shape: (height, width)
|
| 14 |
+
|
| 15 |
+
以 `Mosaic` 为例,其初始化参数如下所示:
|
| 16 |
+
|
| 17 |
+
```python
|
| 18 |
+
@TRANSFORMS.register_module()
|
| 19 |
+
class Mosaic(BaseTransform):
|
| 20 |
+
def __init__(self,
|
| 21 |
+
img_scale: Tuple[int, int] = (640, 640),
|
| 22 |
+
center_ratio_range: Tuple[float, float] = (0.5, 1.5),
|
| 23 |
+
bbox_clip_border: bool = True,
|
| 24 |
+
pad_val: float = 114.0,
|
| 25 |
+
prob: float = 1.0) -> None:
|
| 26 |
+
...
|
| 27 |
+
|
| 28 |
+
# img_scale 顺序应该是 (width, height)
|
| 29 |
+
self.img_scale = img_scale
|
| 30 |
+
|
| 31 |
+
def transform(self, results: dict) -> dict:
|
| 32 |
+
...
|
| 33 |
+
|
| 34 |
+
results['img'] = mosaic_img
|
| 35 |
+
# (height, width)
|
| 36 |
+
results['img_shape'] = mosaic_img.shape[:2]
|
| 37 |
+
```
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/algorithm_descriptions/index.rst
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
算法原理和实现全解析
|
| 2 |
+
********************
|
| 3 |
+
|
| 4 |
+
.. toctree::
|
| 5 |
+
:maxdepth: 1
|
| 6 |
+
|
| 7 |
+
yolov5_description.md
|
| 8 |
+
yolov6_description.md
|
| 9 |
+
rtmdet_description.md
|
| 10 |
+
yolov8_description.md
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/algorithm_descriptions/rtmdet_description.md
ADDED
|
@@ -0,0 +1,645 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# RTMDet 原理和实现全解析
|
| 2 |
+
|
| 3 |
+
## 0 简介
|
| 4 |
+
|
| 5 |
+
高性能,低延时的单阶段目标检测器
|
| 6 |
+
|
| 7 |
+
<div align=center>
|
| 8 |
+
<img alt="RTMDet_structure_v1.3" src="https://user-images.githubusercontent.com/27466624/204126145-cb4ff4f1-fb16-455e-96b5-17620081023a.jpg"/>
|
| 9 |
+
</div>
|
| 10 |
+
|
| 11 |
+
以上结构图由 RangeKing@github 绘制。
|
| 12 |
+
|
| 13 |
+
最近一段时间,开源界涌现出了大量的高精度目标检测项目,其中最突出的就是 YOLO 系列,OpenMMLab 也在与社区的合作下推出了 MMYOLO。
|
| 14 |
+
在调研了当前 YOLO 系列的诸多改进模型后,MMDetection 核心开发者针对这些设计以及训练方式进行了经验性的总结,并进行了优化,推出了高精度、低延时的单阶段目标检测器 RTMDet, **R**eal-**t**ime **M**odels for Object **Det**ection
|
| 15 |
+
(**R**elease **t**o **M**anufacture)
|
| 16 |
+
|
| 17 |
+
RTMDet 由 tiny/s/m/l/x 一系列不同大小的模型组成,为不同的应用场景提供了不同的选择。
|
| 18 |
+
其中,RTMDet-x 在 52.6 mAP 的精度下达到了 300+ FPS 的推理速度。
|
| 19 |
+
|
| 20 |
+
```{note}
|
| 21 |
+
注:推理速度和精度测试(不包含 NMS)是在 1 块 NVIDIA 3090 GPU 上的 `TensorRT 8.4.3, cuDNN 8.2.0, FP16, batch size=1` 条件里测试的。
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
而最轻量的模型 RTMDet-tiny,在仅有 4M 参数量的情况下也能够达到 40.9 mAP,且推理速度 \< 1 ms。
|
| 25 |
+
|
| 26 |
+
<div align=center>
|
| 27 |
+
<img alt="RTMDet_精度图" src="https://user-images.githubusercontent.com/12907710/192182907-f9a671d6-89cb-4d73-abd8-c2b9dada3c66.png"/>
|
| 28 |
+
</div>
|
| 29 |
+
|
| 30 |
+
上图中的精度是和 300 epoch 训练下的公平对比,为不使用蒸馏的结果。
|
| 31 |
+
|
| 32 |
+
| | mAP | Params | Flops | Inference speed |
|
| 33 |
+
| -------------------------- | --------------- | -------------- | ------------ | --------------- |
|
| 34 |
+
| Baseline(YOLOX) | 40.2 | 9M | 13.4G | 1.2ms |
|
| 35 |
+
| + AdamW + Flat Cosine | 40.6 (+0.4) | 9M | 13.4G | 1.2ms |
|
| 36 |
+
| + CSPNeXt backbone & PAFPN | 41.8 (+1.2) | 10.07M (+1.07) | 14.8G (+1.4) | 1.22ms (+0.02) |
|
| 37 |
+
| + SepBNHead | 41.8 (+0) | 8.89M (-1.18) | 14.8G | 1.22ms |
|
| 38 |
+
| + Label Assign & Loss | 42.9 (+1.1) | 8.89M | 14.8G | 1.22ms |
|
| 39 |
+
| + Cached Mosaic & MixUp | 44.2 (+1.3) | 8.89M | 14.8G | 1.22ms |
|
| 40 |
+
| + RSB-pretrained backbone | **44.5 (+0.3)** | 8.89M | 14.8G | 1.22ms |
|
| 41 |
+
|
| 42 |
+
- 官方开源地址: https://github.com/open-mmlab/mmdetection/blob/3.x/configs/rtmdet/README.md
|
| 43 |
+
- MMYOLO 开源地址: https://github.com/open-mmlab/mmyolo/blob/main/configs/rtmdet/README.md
|
| 44 |
+
|
| 45 |
+
## 1 v1.0 算法原理和 MMYOLO 实现解析
|
| 46 |
+
|
| 47 |
+
### 1.1 数据增强模块
|
| 48 |
+
|
| 49 |
+
RTMDet 采用了多种数据增强的方式来增加模型的性能,主要包括单图数据增强:
|
| 50 |
+
|
| 51 |
+
- **RandomResize 随机尺度变换**
|
| 52 |
+
- **RandomCrop 随机裁剪**
|
| 53 |
+
- **HSVRandomAug 颜色空间增强**
|
| 54 |
+
- **RandomFlip 随机水平翻转**
|
| 55 |
+
|
| 56 |
+
以及混合类数据增强:
|
| 57 |
+
|
| 58 |
+
- **Mosaic 马赛克**
|
| 59 |
+
- **MixUp 图像混合**
|
| 60 |
+
|
| 61 |
+
数据增强流程如下:
|
| 62 |
+
|
| 63 |
+
<div align=center>
|
| 64 |
+
<img alt="image" src="https://user-images.githubusercontent.com/33799979/192956011-78f89d89-ac9f-4a40-b4f1-056b49b704ef.png" width=800 />
|
| 65 |
+
</div>
|
| 66 |
+
|
| 67 |
+
其中 RandomResize 超参在大模型 M,L,X 和小模型 S, Tiny 上是不一样的,大模型由于参数较多,可以使用 large scale jitter 策略即参数为 (0.1,2.0),而小模型采用 stand scale jitter 策略即 (0.5, 2.0) 策略。
|
| 68 |
+
MMDetection 开源库中已经对单图数据增强进行了封装,用户通过简单的修改配置即可使用库中提供的任何数据增强功能,且都是属于比较常规的数据增强,不需要特殊介绍。下面将具体介绍混合类数据增强的具体实现。
|
| 69 |
+
|
| 70 |
+
与 YOLOv5 不同的是,YOLOv5 认为在 S 和 Nano 模型上使用 MixUp 是过剩的,小模型不需要这么强的数据增强。而 RTMDet 在 S 和 Tiny 上也使用了 MixUp,这是因为 RTMDet 在最后 20 epoch 会切换为正常的 aug, 并通过训练证明这个操作是有效的。 并且 RTMDet 为混合类数据增强引入了 Cache 方案,有效地减少了图像处理的时间, 和引入了可调超参 `max_cached_images` ,当使用较小的 cache 时,其效果类似 `repeated augmentation`。具体介绍如下:
|
| 71 |
+
|
| 72 |
+
| | Use cache | ms / 100 imgs |
|
| 73 |
+
| ------ | --------- | ------------- |
|
| 74 |
+
| Mosaic | | 87.1 |
|
| 75 |
+
| Mosaic | √ | **24.0** |
|
| 76 |
+
| MixUp | | 19.3 |
|
| 77 |
+
| MixUp | √ | **12.4** |
|
| 78 |
+
|
| 79 |
+
| | RTMDet-s | RTMDet-l |
|
| 80 |
+
| ----------------------------- | -------- | -------- |
|
| 81 |
+
| Mosaic + MixUp + 20e finetune | 43.9 | **51.3** |
|
| 82 |
+
|
| 83 |
+
#### 1.1.1 为图像混合数据增强引入 Cache
|
| 84 |
+
|
| 85 |
+
Mosaic&MixUp 涉及到多张图片的混合,它们的耗时会是普通数据增强的 K 倍(K 为混入图片的数量)。 如在 YOLOv5 中,每次做 Mosaic 时, 4 张图片的信息都需要从硬��中重新加载。 而 RTMDet 只需要重新载入当前的一张图片,其余参与混合增强的图片则从缓存队列中获取,通过牺牲一定内存空间的方式大幅提升了效率。 另外通过调整 cache 的大小以及 pop 的方式,也可以调整增强的强度。
|
| 86 |
+
|
| 87 |
+
<div align=center>
|
| 88 |
+
<img alt="data cache" src="https://user-images.githubusercontent.com/33799979/192730011-90e2a28d-e163-4399-bf87-d3012007d8c3.png" width=800 />
|
| 89 |
+
</div>
|
| 90 |
+
|
| 91 |
+
如图所示,cache 队列中预先储存了 N 张已加载的图像与标签数据,每一个训练 step 中只需加载一张新的图片及其标签数据并更新到 cache 队列中(cache 队列中的图像可重复,如图中出现两次 img3),同时如果 cache 队列长度超过预设长度,则随机 pop 一张图(为了 Tiny 模型训练更稳定,在 Tiny 模型中不采用随机 pop 的方式, 而是移除最先加入的图片),当需要进行混合数据增强时,只需要从 cache 中随机选择需要的图像进行拼接等处理,而不需要全部从硬盘中加载,节省了图像加载的时间。
|
| 92 |
+
|
| 93 |
+
```{note}
|
| 94 |
+
cache 队列的最大长度 N 为可调整参数,根据经验性的原则,当为每一张需要混合的图片提供十个缓存时,可以认为提供了足够的随机性,而 Mosaic 增强是四张图混合,因此 cache 数量默认 N=40, 同理 MixUp 的 cache 数量默认为20, tiny 模型需要更稳定的训练条件,因此其 cache 数量也为其余规格模型的一半( MixUp 为10,Mosaic 为20)
|
| 95 |
+
```
|
| 96 |
+
|
| 97 |
+
在具体实现中,MMYOLO 设计了 `BaseMiximageTransform` 类来支持多张图像混合数据增强:
|
| 98 |
+
|
| 99 |
+
```python
|
| 100 |
+
if self.use_cached:
|
| 101 |
+
# Be careful: deep copying can be very time-consuming
|
| 102 |
+
# if results includes dataset.
|
| 103 |
+
dataset = results.pop('dataset', None)
|
| 104 |
+
self.results_cache.append(copy.deepcopy(results)) # 将当前加载的图片数据缓存到 cache 中
|
| 105 |
+
if len(self.results_cache) > self.max_cached_images:
|
| 106 |
+
if self.random_pop: # 除了tiny模型,self.random_pop=True
|
| 107 |
+
index = random.randint(0, len(self.results_cache) - 1)
|
| 108 |
+
else:
|
| 109 |
+
index = 0
|
| 110 |
+
self.results_cache.pop(index)
|
| 111 |
+
|
| 112 |
+
if len(self.results_cache) <= 4:
|
| 113 |
+
return results
|
| 114 |
+
else:
|
| 115 |
+
assert 'dataset' in results
|
| 116 |
+
# Be careful: deep copying can be very time-consuming
|
| 117 |
+
# if results includes dataset.
|
| 118 |
+
dataset = results.pop('dataset', None)
|
| 119 |
+
```
|
| 120 |
+
|
| 121 |
+
#### 1.1.2 Mosaic
|
| 122 |
+
|
| 123 |
+
Mosaic 是将 4 张图拼接为 1 张大图,相当于变相的增加了 batch size,具体步骤为:
|
| 124 |
+
|
| 125 |
+
1. 根据索引随机从自定义数据集中再采样3个图像,可能重复
|
| 126 |
+
|
| 127 |
+
```python
|
| 128 |
+
def get_indexes(self, dataset: Union[BaseDataset, list]) -> list:
|
| 129 |
+
"""Call function to collect indexes.
|
| 130 |
+
|
| 131 |
+
Args:
|
| 132 |
+
dataset (:obj:`Dataset` or list): The dataset or cached list.
|
| 133 |
+
|
| 134 |
+
Returns:
|
| 135 |
+
list: indexes.
|
| 136 |
+
"""
|
| 137 |
+
indexes = [random.randint(0, len(dataset)) for _ in range(3)]
|
| 138 |
+
return indexes
|
| 139 |
+
```
|
| 140 |
+
|
| 141 |
+
2. 随机选出 4 幅图像相交的中点。
|
| 142 |
+
|
| 143 |
+
```python
|
| 144 |
+
# mosaic center x, y
|
| 145 |
+
center_x = int(
|
| 146 |
+
random.uniform(*self.center_ratio_range) * self.img_scale[1])
|
| 147 |
+
center_y = int(
|
| 148 |
+
random.uniform(*self.center_ratio_range) * self.img_scale[0])
|
| 149 |
+
center_position = (center_x, center_y)
|
| 150 |
+
```
|
| 151 |
+
|
| 152 |
+
3. 根据采样的 index 读取图片并拼接, 拼接前会先进行 `keep-ratio` 的 resize 图片(即为最大边一定是 640)。
|
| 153 |
+
|
| 154 |
+
```python
|
| 155 |
+
# keep_ratio resize
|
| 156 |
+
scale_ratio_i = min(self.img_scale[0] / h_i,
|
| 157 |
+
self.img_scale[1] / w_i)
|
| 158 |
+
img_i = mmcv.imresize(
|
| 159 |
+
img_i, (int(w_i * scale_ratio_i), int(h_i * scale_ratio_i)))
|
| 160 |
+
```
|
| 161 |
+
|
| 162 |
+
4. 拼接后,把 bbox 和 label 全部拼接起来,然后对 bbox 进行裁剪但是不过滤(可能出现一些无效框)
|
| 163 |
+
|
| 164 |
+
```python
|
| 165 |
+
mosaic_bboxes.clip_([2 * self.img_scale[0], 2 * self.img_scale[1]])
|
| 166 |
+
```
|
| 167 |
+
|
| 168 |
+
更多的关于 Mosaic 原理的详情可以参考 [YOLOv5 原理和实现全解析](./yolov5_description.md) 中的 Mosaic 原理分析。
|
| 169 |
+
|
| 170 |
+
#### 1.1.3 MixUp
|
| 171 |
+
|
| 172 |
+
RTMDet 的 MixUp 实现方式与 YOLOX 中一样,只不过增加了类似上文中提到的 cache 功能。
|
| 173 |
+
|
| 174 |
+
更多的关于 MixUp 原理的详情也可以参考 [YOLOv5 原理和实现全解析](./yolov5_description.md) 中的 MixUp 原理分析。
|
| 175 |
+
|
| 176 |
+
#### 1.1.4 强弱两阶段训练
|
| 177 |
+
|
| 178 |
+
Mosaic+MixUp 失真度比较高,持续用太强的数据增强对模型并不一定有益。YOLOX 中率先使用了强弱两阶段的训练方式,但由于引入了旋转,错切导致 box 标注产生误差,需要在第二阶段引入额外的 L1 loss 来纠正回归分支的性能。
|
| 179 |
+
|
| 180 |
+
为了使数据增强的方式更为通用,RTMDet 在前 280 epoch 使用不带旋转的 Mosaic+MixUp, 且通过混入 8 张图片来提升强度以及正样本数。后 20 epoch 使用比较小的学习率在比较弱的增强下进行微调,同时在 EMA 的作用下将参数缓慢更新至模型,能够得到比较大的提升。
|
| 181 |
+
|
| 182 |
+
| | RTMDet-s | RTMDet-l |
|
| 183 |
+
| ----------------------------- | -------- | -------- |
|
| 184 |
+
| LSJ + rand crop | 42.3 | 46.7 |
|
| 185 |
+
| Mosaic+MixUp | 41.9 | 49.8 |
|
| 186 |
+
| Mosaic + MixUp + 20e finetune | 43.9 | **51.3** |
|
| 187 |
+
|
| 188 |
+
### 1.2 模型结构
|
| 189 |
+
|
| 190 |
+
RTMDet 模型整体结构和 [YOLOX](https://arxiv.org/abs/2107.08430) 几乎一致,由 `CSPNeXt` + `CSPNeXtPAFPN` + `共享卷积权重但分别计算 BN 的 SepBNHead` 构成。内部核心模块也是 `CSPLayer`,但对其中的 `Basic Block` 进行了改进,提出了 `CSPNeXt Block`。
|
| 191 |
+
|
| 192 |
+
#### 1.2.1 Backbone
|
| 193 |
+
|
| 194 |
+
`CSPNeXt` 整体以 `CSPDarknet` 为基础,共 5 层结构,包含 1 个 `Stem Layer` 和 4 个 `Stage Layer`:
|
| 195 |
+
|
| 196 |
+
- `Stem Layer` 是 3 层 3x3 kernel 的 `ConvModule` ,不同于之前的 `Focus` 模块或者 1 层 6x6 kernel 的 `ConvModule` 。
|
| 197 |
+
|
| 198 |
+
- `Stage Layer` 总体结构与已有模型类似,前 3 个 `Stage Layer` 由 1 个 `ConvModule` 和 1 个 `CSPLayer` 组成。第 4 个 `Stage Layer` 在 `ConvModule` 和 `CSPLayer` 中间增加了 `SPPF` 模块(MMDetection 版本为 `SPP` 模块)。
|
| 199 |
+
|
| 200 |
+
- 如模型图 Details 部分所示,`CSPLayer` 由 3 个 `ConvModule` + n 个 `CSPNeXt Block`(带残差连接) + 1 个 `Channel Attention` 模块组成。`ConvModule` 为 1 层 3x3 `Conv2d` + `BatchNorm` + `SiLU` 激活函数。`Channel Attention` 模块为 1 层 `AdaptiveAvgPool2d` + 1 层 1x1 `Conv2d` + `Hardsigmoid` 激活函数。`CSPNeXt Block` 模块在下节详细讲述。
|
| 201 |
+
|
| 202 |
+
- 如果想阅读 Backbone - `CSPNeXt` 的源码,可以 [**点此**](https://github.com/open-mmlab/mmyolo/blob/main/mmyolo/models/backbones/cspnext.py#L16-L171) 跳转。
|
| 203 |
+
|
| 204 |
+
#### 1.2.2 CSPNeXt Block
|
| 205 |
+
|
| 206 |
+
Darknet (图 a)使用 1x1 与 3x3 卷积的 `Basic Block`。[YOLOv6](https://arxiv.org/abs/2209.02976) 、[YOLOv7](https://arxiv.org/abs/2207.02696) 、[PPYOLO-E](https://arxiv.org/abs/2203.16250) (图 b & c)使用了重参数化 Block。但重参数化的训练代价高,且不易量化,需要其他方式来弥补量化误差。
|
| 207 |
+
RTMDet 则借鉴了最近比较热门的 [ConvNeXt](https://arxiv.org/abs/2201.03545) 、[RepLKNet](https://arxiv.org/abs/2203.06717) 的做法,为 `Basic Block` 加入了大 kernel 的 `depth-wise` 卷积(图 d),并将其命名为 `CSPNeXt Block`。
|
| 208 |
+
|
| 209 |
+
<div align=center>
|
| 210 |
+
<img alt="BasicBlock" src="https://user-images.githubusercontent.com/27466624/192752976-4c20f944-1ef0-4746-892e-ba814cdcda20.png"/>
|
| 211 |
+
</div>
|
| 212 |
+
|
| 213 |
+
关于不同 kernel 大小的实验结果,如下表所示。
|
| 214 |
+
|
| 215 |
+
| Kernel size | params | flops | latency-bs1-TRT-FP16 / ms | mAP |
|
| 216 |
+
| ------------ | ---------- | --------- | ------------------------- | -------- |
|
| 217 |
+
| 3x3 | 50.8 | 79.61G | 2.1 | 50.0 |
|
| 218 |
+
| **5x5** | **50.92M** | **79.7G** | **2.11** | **50.9** |
|
| 219 |
+
| 7x7 | 51.1 | 80.34G | 2.73 | 51.1 |
|
| 220 |
+
|
| 221 |
+
如果想阅读 `Basic Block` 和 `CSPNeXt Block` 源码,可以[**点此**](https://github.com/open-mmlab/mmdetection/blob/3.x/mmdet/models/layers/csp_layer.py#L79-L146)跳转。
|
| 222 |
+
|
| 223 |
+
#### 1.2.3 调整检测器不同 stage 间的 block 数
|
| 224 |
+
|
| 225 |
+
由于 `CSPNeXt Block` 内使用了 `depth-wise` 卷积,单个 block 内的层数增多。如果保持原有的 stage 内的 block 数,则会导致模型的推理速度大幅降低。
|
| 226 |
+
|
| 227 |
+
RTMDet 重新调整了不同 stage 间的 block 数,并调整了通道的超参,在保证了精度的情况下提升了推理速度。
|
| 228 |
+
|
| 229 |
+
关于不同 block 数的实验结果,如下表所示。
|
| 230 |
+
|
| 231 |
+
| Num blocks | params | flops | latency-bs1-TRT-FP16 / ms | mAP |
|
| 232 |
+
| ---------------------------------- | --------- | --------- | ------------------------- | -------- |
|
| 233 |
+
| L+3-9-9-3 | 53.4 | 86.28 | 2.6 | 51.4 |
|
| 234 |
+
| L+3-6-6-3 | 50.92M | 79.7G | 2.11 | 50.9 |
|
| 235 |
+
| **L+3-6-6-3 + channel attention** | **52.3M** | **79.9G** | **2.4** | **51.3** |
|
| 236 |
+
|
| 237 |
+
最后不同大小模型的 block 数设置,可以参见[源码](https://github.com/open-mmlab/mmyolo/blob/main/mmyolo/models/backbones/cspnext.py#L50-L56) 。
|
| 238 |
+
|
| 239 |
+
#### 1.2.4 Neck
|
| 240 |
+
|
| 241 |
+
Neck 模型结构和 YOLOX 几乎一样,只不过内部的 block 进行了替换。
|
| 242 |
+
|
| 243 |
+
#### 1.2.5 Backbone 与 Neck 之间的参数量和计算量的均衡
|
| 244 |
+
|
| 245 |
+
[EfficientDet](https://arxiv.org/abs/1911.09070) 、[NASFPN](https://arxiv.org/abs/1904.07392) 等工作在改进 Neck 时往往聚焦于如何修改特征融合的方式。 但引入过多的连接会增加检测器的延时,并增加内存开销。
|
| 246 |
+
|
| 247 |
+
所以 RTMDet 选择不引入额外的连接,而是改变 Backbone 与 Neck 间参数量的配比。该配比是通过手动调整 Backbone 和 Neck 的 `expand_ratio` 参数来实现的,其数值在 Backbone 和 Neck 中都为 0.5。`expand_ratio` 实际上是改变 `CSPLayer` 中各层通道数的参数(具体可见模型图 `CSPLayer` 部分)。如果想进行不同配比的实验,可以通过调整配置文件中的 [backbone {expand_ratio}](https://github.com/open-mmlab/mmyolo/blob/main/configs/rtmdet/rtmdet_l_8xb32-300e_coco.py#L32) 和 [neck {expand_ratio}](https://github.com/open-mmlab/mmyolo/blob/main/configs/rtmdet/rtmdet_l_8xb32-300e_coco.py#L45) 参数完成。
|
| 248 |
+
|
| 249 |
+
实验发现,当 Neck 在整个模型中的参数量占比更高时,延时更低,且对精度的影响很小。作者在直播答疑时回复,RTMDet 在 Neck 这一部分的实验参考了 [GiraffeDet](https://arxiv.org/abs/2202.04256) 的做法,但没有像 GiraffeDet 一样引入额外连接(详细可参见 [RTMDet 发布视频](https://www.bilibili.com/video/BV1e841147GD) 31分40秒左右的内容)。
|
| 250 |
+
|
| 251 |
+
关于不同参数量配比的实验结果,如下表所示。
|
| 252 |
+
|
| 253 |
+
| Model size | Backbone | Neck | params | flops | latency / ms | mAP |
|
| 254 |
+
| ----------- | -------- | ------- | ---------- | ---------- | ------------- | -------- |
|
| 255 |
+
| **S** | **47%** | **45%** | **8.54M** | **15.76G** | **1.21** | **43.9** |
|
| 256 |
+
| S | 63% | 29% | 9.01M | 15.85G | 1.37 | 43.7 |
|
| 257 |
+
| **L** | **47%** | **45%** | **50.92M** | **79.7G** | **2.11** | **50.9** |
|
| 258 |
+
| L | 63% | 29% | 57.43M | 93.73 | 2.57 | 51.0 |
|
| 259 |
+
|
| 260 |
+
如果想阅读 Neck - `CSPNeXtPAFPN` 的源码,可以[**点此**](https://github.com/open-mmlab/mmyolo/blob/main/mmyolo/models/necks/cspnext_pafpn.py#L15-L201) 跳转。
|
| 261 |
+
|
| 262 |
+
#### 1.2.6 Head
|
| 263 |
+
|
| 264 |
+
传统的 YOLO 系列都使用同一 Head 进行分类和回归。YOLOX 则将分类和回归分支解耦,PPYOLO-E 和 YOLOv6 则引入了 [TOOD](https://arxiv.org/abs/2108.07755) 中的结构。它们在不同特征层级之间都使用独立的 Head,因此 Head 在模型中也占有较多的参数量。
|
| 265 |
+
|
| 266 |
+
RTMDet 参考了 [NAS-FPN](https://arxiv.org/abs/1904.07392) 中的做法,使用了 `SepBNHead`,在不同层之间共享卷积权重,但是独立计算 BN(BatchNorm) 的统计量。
|
| 267 |
+
|
| 268 |
+
关于不同结构 Head 的实验结果,如下表所示。
|
| 269 |
+
|
| 270 |
+
| Head type | params | flops | latency / ms | mAP |
|
| 271 |
+
| ------------------ | --------- | --------- | ------------- | -------- |
|
| 272 |
+
| Fully-shared head | 52.32 | 80.23 | 2.44 | 48.0 |
|
| 273 |
+
| Separated head | 57.03 | 80.23 | 2.44 | 51.2 |
|
| 274 |
+
| **SepBN** **head** | **52.32** | **80.23** | **2.44** | **51.3** |
|
| 275 |
+
|
| 276 |
+
同时,RTMDet 也延续了作者之前在 [NanoDet](https://zhuanlan.zhihu.com/p/306530300) 中的思想,使用 [Quality Focal Loss](https://arxiv.org/abs/2011.12885) ,并去掉 Objectness 分支,进一步将 Head 轻量化。
|
| 277 |
+
|
| 278 |
+
如果想阅读 Head 中 `RTMDetSepBNHeadModule` 的源码,可以[**点此**](https://github.com/open-mmlab/mmyolo/blob/main/mmyolo/models/dense_heads/rtmdet_head.py#L24-L189) 跳转。
|
| 279 |
+
|
| 280 |
+
```{note}
|
| 281 |
+
注:MMYOLO 和 MMDetection 中 Neck 和 Head 的具体实现稍有不同。
|
| 282 |
+
```
|
| 283 |
+
|
| 284 |
+
### 1.3 正负样本匹配策略
|
| 285 |
+
|
| 286 |
+
正负样本匹配策略或者称为标签匹配策略 `Label Assignment` 是目标检测模型训练中最核心的问题之一, 更好的标签匹配策略往往能够使得网络更好学习到物体的特征以提高检测能力。
|
| 287 |
+
|
| 288 |
+
早期的样本标签匹配策略一般都是基于 `空间以及尺度信息的先验` 来决定样本的选取。 典型案例如下:
|
| 289 |
+
|
| 290 |
+
- `FCOS` 中先限定网格中心点在 `GT` 内筛选后然后再通过不同特征层限制尺寸来决定正负样本
|
| 291 |
+
- `RetinaNet` 则是通过 `Anchor` 与 `GT` 的最大 `IOU` 匹配来划分正负样本
|
| 292 |
+
- `YOLOV5` 的正负样本则是通过样本的宽高比先筛选一部分, 然后通过位置信息选取 `GT` 中心落在的 `Grid` 以及临近的两个作为正样本
|
| 293 |
+
|
| 294 |
+
但是上述方法都是属于基于 `先验` 的静态匹配策略, 就是样本的选取方式是根据人的经验规定的。 不会随着网络的优化而进行自动优化选取到更好的样本, 近些年涌现了许多优秀的动态标签匹配策略:
|
| 295 |
+
|
| 296 |
+
- `OTA` 提出使用 `Sinkhorn` 迭代求解匹配中的最优传输问题
|
| 297 |
+
- `YOLOX` 中使用 `OTA` 的近似算法 `SimOTA` , `TOOD` 将分类分数以及 `IOU` 相乘计算 `Cost` 矩阵进行标签匹配等等
|
| 298 |
+
|
| 299 |
+
这些算法将 `预测的 Bboxes 与 GT 的 IOU ` 和 `分类分数` 或者是对应 `分类 Loss` 和 `回归 Loss` 拿来计算 `Matching Cost` 矩阵再通过 `top-k` 的方式动态决定样本选取以及样本个数。通过这种方式,
|
| 300 |
+
在网络优化的过程中会自动选取对分类或者回归更加敏感有效的位置的样本, 它不再只依赖先验的静态的信息, 而是使用当前的预测结果去动态寻找最优的匹配, 只要模型的预测越准确, 匹配算法求得的结果也会更优秀。但是在网络训练的初期, 网络的分类以及回归是随机初始化, 这个时候还是需要 `先验` 来约束, 以达到 `冷启动` 的效果。
|
| 301 |
+
|
| 302 |
+
`RTMDet` 作者也是采用了动态的 `SimOTA` 做法,不过其对动态的正负样本分配策略进行了改进。 之前的动态匹配策略( `HungarianAssigner` 、`OTA` )往往使用与 `Loss` 完全一致的代价函数作为匹配的依据,但我们经过实验发现这并不一定时最优的。 使用更多 `Soften` 的 `Cost` 以及先验,能够提升性能。
|
| 303 |
+
|
| 304 |
+
#### 1.3.1 Bbox 编解码过程
|
| 305 |
+
|
| 306 |
+
RTMDet 的 BBox Coder 采用的是 `mmdet.DistancePointBBoxCoder`。
|
| 307 |
+
|
| 308 |
+
该类的 docstring 为 `This coder encodes gt bboxes (x1, y1, x2, y2) into (top, bottom, left, right) and decode it back to the original.`
|
| 309 |
+
|
| 310 |
+
编码器将 gt bboxes (x1, y1, x2, y2) 编码为 (top, bottom, left, right),并且解码至原图像上。
|
| 311 |
+
|
| 312 |
+
MMDet 编码的核心源码:
|
| 313 |
+
|
| 314 |
+
```python
|
| 315 |
+
def bbox2distance(points: Tensor, bbox: Tensor, ...) -> Tensor:
|
| 316 |
+
"""
|
| 317 |
+
points (Tensor): 相当于 scale 值 stride ,且每个预测点仅为一个正方形 anchor 的 anchor point [x, y],Shape (n, 2) or (b, n, 2).
|
| 318 |
+
bbox (Tensor): Bbox 为乘上 stride 的网络预测值,格式为 xyxy,Shape (n, 4) or (b, n, 4).
|
| 319 |
+
"""
|
| 320 |
+
# 计算点距离四边的距离
|
| 321 |
+
left = points[..., 0] - bbox[..., 0]
|
| 322 |
+
top = points[..., 1] - bbox[..., 1]
|
| 323 |
+
right = bbox[..., 2] - points[..., 0]
|
| 324 |
+
bottom = bbox[..., 3] - points[..., 1]
|
| 325 |
+
|
| 326 |
+
...
|
| 327 |
+
|
| 328 |
+
return torch.stack([left, top, right, bottom], -1)
|
| 329 |
+
```
|
| 330 |
+
|
| 331 |
+
MMDetection 解码的核心源码:
|
| 332 |
+
|
| 333 |
+
```python
|
| 334 |
+
def distance2bbox(points: Tensor, distance: Tensor, ...) -> Tensor:
|
| 335 |
+
"""
|
| 336 |
+
通过距离反算 bbox 的 xyxy
|
| 337 |
+
points (Tensor): 正方形的预测 anchor 的 anchor point [x, y],Shape (B, N, 2) or (N, 2).
|
| 338 |
+
distance (Tensor): 距离四边的距离。(left, top, right, bottom). Shape (B, N, 4) or (N, 4)
|
| 339 |
+
"""
|
| 340 |
+
|
| 341 |
+
# 反算 bbox xyxy
|
| 342 |
+
x1 = points[..., 0] - distance[..., 0]
|
| 343 |
+
y1 = points[..., 1] - distance[..., 1]
|
| 344 |
+
x2 = points[..., 0] + distance[..., 2]
|
| 345 |
+
y2 = points[..., 1] + distance[..., 3]
|
| 346 |
+
|
| 347 |
+
bboxes = torch.stack([x1, y1, x2, y2], -1)
|
| 348 |
+
|
| 349 |
+
...
|
| 350 |
+
|
| 351 |
+
return bboxes
|
| 352 |
+
```
|
| 353 |
+
|
| 354 |
+
#### 1.3.2 匹配策略
|
| 355 |
+
|
| 356 |
+
`RTMDet` 提出了 `Dynamic Soft Label Assigner` 来实现标签的动态匹配策略, 该方法主要包括使用 **位置先验信息损失** , **样本回归损失** , **样本分类损失** , 同时对三个损失进行了 `Soft` 处理进行参数调优, 以达到最佳的动态匹配效果。
|
| 357 |
+
|
| 358 |
+
该方法 Matching Cost 矩阵由如下损失构成:
|
| 359 |
+
|
| 360 |
+
```python
|
| 361 |
+
cost_matrix = soft_cls_cost + iou_cost + soft_center_prior
|
| 362 |
+
```
|
| 363 |
+
|
| 364 |
+
1. Soft_Center_Prior
|
| 365 |
+
|
| 366 |
+
```{math}
|
| 367 |
+
C\_{center} = \\alpha^{|x\_{pred}-x\_{gt}|-\\beta}
|
| 368 |
+
```
|
| 369 |
+
|
| 370 |
+
```python
|
| 371 |
+
# valid_prior Tensor[N,4] 表示anchor point
|
| 372 |
+
# 4分别表示 x, y, 以及对应的特征层的 stride, stride
|
| 373 |
+
gt_center = (gt_bboxes[:, :2] + gt_bboxes[:, 2:]) / 2.0
|
| 374 |
+
valid_prior = priors[valid_mask]
|
| 375 |
+
strides = valid_prior[:, 2]
|
| 376 |
+
# 计算gt与anchor point的中心距离并转换到特征图尺度
|
| 377 |
+
distance = (valid_prior[:, None, :2] - gt_center[None, :, :]
|
| 378 |
+
).pow(2).sum(-1).sqrt() / strides[:, None]
|
| 379 |
+
# 以10为底计算位置的软化损失,限定在gt的6个单元格以内
|
| 380 |
+
soft_center_prior = torch.pow(10, distance - 3)
|
| 381 |
+
```
|
| 382 |
+
|
| 383 |
+
2. IOU_Cost
|
| 384 |
+
|
| 385 |
+
```{math}
|
| 386 |
+
C\_{reg} = -log(IOU)
|
| 387 |
+
```
|
| 388 |
+
|
| 389 |
+
```python
|
| 390 |
+
# 计算回归 bboxes 和 gts 的 iou
|
| 391 |
+
pairwise_ious = self.iou_calculator(valid_decoded_bbox, gt_bboxes)
|
| 392 |
+
# 将 iou 使用 log 进行 soft , iou 越小 cost 更小
|
| 393 |
+
iou_cost = -torch.log(pairwise_ious + EPS) * 3
|
| 394 |
+
```
|
| 395 |
+
|
| 396 |
+
3. Soft_Cls_Cost
|
| 397 |
+
|
| 398 |
+
```{math}
|
| 399 |
+
C\_{cls} = CE(P,Y\_{soft}) \*(Y\_{soft}-P)^2
|
| 400 |
+
```
|
| 401 |
+
|
| 402 |
+
```python
|
| 403 |
+
# 生成分类标签
|
| 404 |
+
gt_onehot_label = (
|
| 405 |
+
F.one_hot(gt_labels.to(torch.int64),
|
| 406 |
+
pred_scores.shape[-1]).float().unsqueeze(0).repeat(
|
| 407 |
+
num_valid, 1, 1))
|
| 408 |
+
valid_pred_scores = valid_pred_scores.unsqueeze(1).repeat(1, num_gt, 1)
|
| 409 |
+
# 不单单将分类标签为01,而是换成与 gt 的 iou
|
| 410 |
+
soft_label = gt_onehot_label * pairwise_ious[..., None]
|
| 411 |
+
# 使用 quality focal loss 计算分类损失 cost ,与实际的分类损失计算保持一致
|
| 412 |
+
scale_factor = soft_label - valid_pred_scores.sigmoid()
|
| 413 |
+
soft_cls_cost = F.binary_cross_entropy_with_logits(
|
| 414 |
+
valid_pred_scores, soft_label,
|
| 415 |
+
reduction='none') * scale_factor.abs().pow(2.0)
|
| 416 |
+
soft_cls_cost = soft_cls_cost.sum(dim=-1)
|
| 417 |
+
```
|
| 418 |
+
|
| 419 |
+
通过计算上述三个损失的和得到最终的 `cost_matrix` 后, 再使用 `SimOTA` 决定每一个 `GT` 匹配的样本的个数并决定最终的样本。具体操作如下所示:
|
| 420 |
+
|
| 421 |
+
1. 首先通过自适应计算每一个 `gt` 要选取的样本数量: 取每一个 `gt` 与所有 `bboxes` 前 `13` 大的 `iou`, 得到它们的和取整后作为这个 `gt` 的 `样本数目` , 最少为 `1` 个, 记为 `dynamic_ks`。
|
| 422 |
+
2. 对于每一个 `gt` , 将其 `cost_matrix` 矩阵前 `dynamic_ks` 小的位置作为该 `gt` 的正样本。
|
| 423 |
+
3. 对于某一个 `bbox`, 如果被匹配到多个 `gt` 就将与这些 `gts` 的 `cost_marix` 中最小的那个作为其 `label`。
|
| 424 |
+
|
| 425 |
+
在网络训练初期,因参数初始化,回归和分类的损失值 `Cost` 往往较大, 这时候 `IOU` 比较小, 选取的样本较少,主要起作用的是 `Soft_center_prior` 也就是位置信息,优先选取位置距离 `GT` 比较近的样本作为正样本,这也符合���们的理解,在网络前期给少量并且有足够质量的样本,以达到冷启动。
|
| 426 |
+
当网络进行训练一段时间过后,分类分支和回归分支都进行了一定的优化后,这时 `IOU` 变大, 选取的样本也逐渐增多,这时网络也有能力学习到更多的样本,同时因为 `IOU_Cost` 以及 `Soft_Cls_Cost` 变小,网络也会动态的找到更有利优化分类以及回归的样本点。
|
| 427 |
+
|
| 428 |
+
在 `Resnet50-1x` 的三种损失的消融实验:
|
| 429 |
+
|
| 430 |
+
| Soft_cls_cost | Soft_center_prior | Log_IoU_cost | mAP |
|
| 431 |
+
| :------------ | :---------------- | :----------- | :--- |
|
| 432 |
+
| × | × | × | 39.9 |
|
| 433 |
+
| √ | × | × | 40.3 |
|
| 434 |
+
| √ | √ | × | 40.8 |
|
| 435 |
+
| √ | √ | √ | 41.3 |
|
| 436 |
+
|
| 437 |
+
与其他主流 `Assign` 方法在 `Resnet50-1x` 的对比实验:
|
| 438 |
+
|
| 439 |
+
| method | mAP |
|
| 440 |
+
| :-----------: | :--- |
|
| 441 |
+
| ATSS | 39.2 |
|
| 442 |
+
| PAA | 40.4 |
|
| 443 |
+
| OTA | 40.7 |
|
| 444 |
+
| TOOD(w/o TAH) | 40.7 |
|
| 445 |
+
| Ours | 41.3 |
|
| 446 |
+
|
| 447 |
+
无论是 `Resnet50-1x` 还是标准的设置下,还是在`300epoch` + `havy augmentation`, 相比于 `SimOTA` 、 `OTA` 以及 `TOOD` 中的 `TAL` 均有提升。
|
| 448 |
+
|
| 449 |
+
| 300e + Mosaic & MixUP | mAP |
|
| 450 |
+
| :-------------------- | :--- |
|
| 451 |
+
| RTMDet-s + SimOTA | 43.2 |
|
| 452 |
+
| RTMDet-s + DSLA | 44.5 |
|
| 453 |
+
|
| 454 |
+
### 1.4 Loss 设计
|
| 455 |
+
|
| 456 |
+
参与 Loss 计算的共有两个值:`loss_cls` 和 `loss_bbox`,其各自使用的 Loss 方法如下:
|
| 457 |
+
|
| 458 |
+
- `loss_cls`:`mmdet.QualityFocalLoss`
|
| 459 |
+
- `loss_bbox`:`mmdet.GIoULoss`
|
| 460 |
+
|
| 461 |
+
权重比例是:`loss_cls` : `loss_bbox` = `1 : 2`
|
| 462 |
+
|
| 463 |
+
#### QualityFocalLoss
|
| 464 |
+
|
| 465 |
+
Quality Focal Loss (QFL) 是 [Generalized Focal Loss: Learning Qualified and Distributed Bounding Boxes for Dense Object Detection](https://arxiv.org/abs/2006.04388) 的一部分。
|
| 466 |
+
|
| 467 |
+
<div align=center>
|
| 468 |
+
<img src="https://user-images.githubusercontent.com/25873202/192767279-4e69f935-1685-4089-82a3-0add201f98cc.png" alt="image"/>
|
| 469 |
+
</div>
|
| 470 |
+
|
| 471 |
+
普通的 Focal Loss 公式:
|
| 472 |
+
|
| 473 |
+
```{math}
|
| 474 |
+
{FL}(p) = -(1-p_t)^\gamma\log(p_t),p_t = \begin{cases}
|
| 475 |
+
p, & {when} \ y = 1 \\
|
| 476 |
+
1 - p, & {when} \ y = 0
|
| 477 |
+
\end{cases}
|
| 478 |
+
```
|
| 479 |
+
|
| 480 |
+
其中 {math}`y\in{1,0}` 指定真实类,{math}`p\in[0,1]` 表示标签 {math}`y = 1` 的类估计概率。{math}`\gamma` 是可调聚焦参数。具体来说,FL 由标准交叉熵部分 {math}`-\log(p_t)` 和动态比例因子部分 {math}`-(1-p_t)^\gamma` 组成,其中比例因子 {math}`-(1-p_t)^\gamma` 在训练期间自动降低简单类对于 loss 的比重,并且迅速将模型集中在困难类上。
|
| 481 |
+
|
| 482 |
+
首先 {math}`y = 0` 表示质量得分为 0 的负样本,{math}`0 < y \leq 1` 表示目标 IoU 得分为 y 的正样本。为了针对连续的标签,扩展 FL 的两个部分:
|
| 483 |
+
|
| 484 |
+
1. 交叉熵部分 {math}`-\log(p_t)` 扩展为完整版本 {math}`-((1-y)\log(1-\sigma)+y\log(\sigma))`
|
| 485 |
+
2. 比例因子部分 {math}`-(1-p_t)^\gamma` 被泛化为估计 {math}`\gamma` 与其连续标签 {math}`y` 的绝对距离,即 {math}`|y-\sigma|^\beta (\beta \geq 0)` 。
|
| 486 |
+
|
| 487 |
+
结合上面两个部分之后,我们得出 QFL 的公式:
|
| 488 |
+
|
| 489 |
+
```{math}
|
| 490 |
+
{QFL}(\sigma) = -|y-\sigma|^\beta((1-y)\log(1-\sigma)+y\log(\sigma))
|
| 491 |
+
```
|
| 492 |
+
|
| 493 |
+
具体作用是:可以将离散标签的 `focal loss` 泛化到连续标签上,将 bboxes 与 gt 的 IoU 的作为分类分数的标签,使得分类分数为表征回归质量的分数。
|
| 494 |
+
|
| 495 |
+
MMDetection 实现源码的核心部分:
|
| 496 |
+
|
| 497 |
+
```python
|
| 498 |
+
@weighted_loss
|
| 499 |
+
def quality_focal_loss(pred, target, beta=2.0):
|
| 500 |
+
"""
|
| 501 |
+
pred (torch.Tensor): 用形状(N,C)联合表示预测分类和质量(IoU),C是类的数量。
|
| 502 |
+
target (tuple([torch.Tensor])): 目标类别标签的形状为(N,),目标质量标签的形状是(N,,)。
|
| 503 |
+
beta (float): 计算比例因子的 β 参数.
|
| 504 |
+
"""
|
| 505 |
+
...
|
| 506 |
+
|
| 507 |
+
# label表示类别id,score表示质量分数
|
| 508 |
+
label, score = target
|
| 509 |
+
|
| 510 |
+
# 负样本质量分数0来进行监督
|
| 511 |
+
pred_sigmoid = pred.sigmoid()
|
| 512 |
+
scale_factor = pred_sigmoid
|
| 513 |
+
zerolabel = scale_factor.new_zeros(pred.shape)
|
| 514 |
+
|
| 515 |
+
# 计算交叉熵部分的值
|
| 516 |
+
loss = F.binary_cross_entropy_with_logits(
|
| 517 |
+
pred, zerolabel, reduction='none') * scale_factor.pow(beta)
|
| 518 |
+
|
| 519 |
+
# 得出 IoU 在区间 (0,1] 的 bbox
|
| 520 |
+
# FG cat_id: [0, num_classes -1], BG cat_id: num_classes
|
| 521 |
+
bg_class_ind = pred.size(1)
|
| 522 |
+
pos = ((label >= 0) & (label < bg_class_ind)).nonzero().squeeze(1)
|
| 523 |
+
pos_label = label[pos].long()
|
| 524 |
+
|
| 525 |
+
# 正样本由 IoU 范围在 (0,1] 的 bbox 来监督
|
| 526 |
+
# 计算动态比例因子
|
| 527 |
+
scale_factor = score[pos] - pred_sigmoid[pos, pos_label]
|
| 528 |
+
|
| 529 |
+
# 计算两部分的 loss
|
| 530 |
+
loss[pos, pos_label] = F.binary_cross_entropy_with_logits(
|
| 531 |
+
pred[pos, pos_label], score[pos],
|
| 532 |
+
reduction='none') * scale_factor.abs().pow(beta)
|
| 533 |
+
|
| 534 |
+
# 得出最终 loss
|
| 535 |
+
loss = loss.sum(dim=1, keepdim=False)
|
| 536 |
+
return loss
|
| 537 |
+
```
|
| 538 |
+
|
| 539 |
+
#### GIoULoss
|
| 540 |
+
|
| 541 |
+
论文:[Generalized Intersection over Union: A Metric and A Loss for Bounding Box Regression](https://arxiv.org/abs/1902.09630)
|
| 542 |
+
|
| 543 |
+
GIoU Loss 用于计算两个框重叠区域的关系,重叠区域越大,损失越小,反之越大。而且 GIoU 是在 \[0,2\] 之间,因为其值被限制在了一个较小的范围内,所以网络不会出现剧烈的波动,证明了其具有比较好的稳定性。
|
| 544 |
+
|
| 545 |
+
下图是基本的实现流程图:
|
| 546 |
+
|
| 547 |
+
<div align=center>
|
| 548 |
+
<img src="https://user-images.githubusercontent.com/25873202/192568784-3884b677-d8e1-439c-8bd2-20943fcedd93.png" alt="image"/>
|
| 549 |
+
</div>
|
| 550 |
+
|
| 551 |
+
MMDetection 实现源码的核心部分:
|
| 552 |
+
|
| 553 |
+
```python
|
| 554 |
+
def bbox_overlaps(bboxes1, bboxes2, mode='iou', is_aligned=False, eps=1e-6):
|
| 555 |
+
...
|
| 556 |
+
|
| 557 |
+
# 求两个区域的面积
|
| 558 |
+
area1 = (bboxes1[..., 2] - bboxes1[..., 0]) * (
|
| 559 |
+
bboxes1[..., 3] - bboxes1[..., 1])
|
| 560 |
+
area2 = (bboxes2[..., 2] - bboxes2[..., 0]) * (
|
| 561 |
+
bboxes2[..., 3] - bboxes2[..., 1])
|
| 562 |
+
|
| 563 |
+
if is_aligned:
|
| 564 |
+
# 得出两个 bbox 重合的左上角 lt 和右下角 rb
|
| 565 |
+
lt = torch.max(bboxes1[..., :2], bboxes2[..., :2]) # [B, rows, 2]
|
| 566 |
+
rb = torch.min(bboxes1[..., 2:], bboxes2[..., 2:]) # [B, rows, 2]
|
| 567 |
+
|
| 568 |
+
# 求重合面积
|
| 569 |
+
wh = fp16_clamp(rb - lt, min=0)
|
| 570 |
+
overlap = wh[..., 0] * wh[..., 1]
|
| 571 |
+
|
| 572 |
+
if mode in ['iou', 'giou']:
|
| 573 |
+
...
|
| 574 |
+
else:
|
| 575 |
+
union = area1
|
| 576 |
+
if mode == 'giou':
|
| 577 |
+
# 得出两个 bbox 最小凸闭合框的左上角 lt 和右下角 rb
|
| 578 |
+
enclosed_lt = torch.min(bboxes1[..., :2], bboxes2[..., :2])
|
| 579 |
+
enclosed_rb = torch.max(bboxes1[..., 2:], bboxes2[..., 2:])
|
| 580 |
+
else:
|
| 581 |
+
...
|
| 582 |
+
|
| 583 |
+
# 求重合面积 / gt bbox 面积 的比率,即 IoU
|
| 584 |
+
eps = union.new_tensor([eps])
|
| 585 |
+
union = torch.max(union, eps)
|
| 586 |
+
ious = overlap / union
|
| 587 |
+
|
| 588 |
+
...
|
| 589 |
+
|
| 590 |
+
# 求最小凸闭合框面积
|
| 591 |
+
enclose_wh = fp16_clamp(enclosed_rb - enclosed_lt, min=0)
|
| 592 |
+
enclose_area = enclose_wh[..., 0] * enclose_wh[..., 1]
|
| 593 |
+
enclose_area = torch.max(enclose_area, eps)
|
| 594 |
+
|
| 595 |
+
# 计算 giou
|
| 596 |
+
gious = ious - (enclose_area - union) / enclose_area
|
| 597 |
+
return gious
|
| 598 |
+
|
| 599 |
+
@weighted_loss
|
| 600 |
+
def giou_loss(pred, target, eps=1e-7):
|
| 601 |
+
gious = bbox_overlaps(pred, target, mode='giou', is_aligned=True, eps=eps)
|
| 602 |
+
loss = 1 - gious
|
| 603 |
+
return loss
|
| 604 |
+
```
|
| 605 |
+
|
| 606 |
+
### 1.5 优化策略和训练过程
|
| 607 |
+
|
| 608 |
+
<div align=center>
|
| 609 |
+
<img src="https://user-images.githubusercontent.com/89863442/192943607-74952731-4eb7-45f5-b86d-2dad46732614.png" width="800"/>
|
| 610 |
+
</div>
|
| 611 |
+
|
| 612 |
+
### 1.6 推理和后处理过程
|
| 613 |
+
|
| 614 |
+
<div align=center>
|
| 615 |
+
<img src="https://user-images.githubusercontent.com/89863442/192943600-98c3a8f9-e42c-47ea-8e12-d20f686e9318.png" width="800"/>
|
| 616 |
+
</div>
|
| 617 |
+
|
| 618 |
+
**(1) 特征图输入**
|
| 619 |
+
|
| 620 |
+
预测的图片输入大小为 640 x 640, 通道数为 3 ,经过 CSPNeXt, CSPNeXtPAFPN 层的 8 倍、16 倍、32 倍下采样得到 80 x 80, 40 x 40, 20 x 20 三个尺寸的特征图。以 rtmdet-l 模型为例,此时三层通道数都为 256,经过 `bbox_head` 层得到两个分支,分别为 `rtm_cls` 类别预测分支,将通道数从 256 变为 80,80 对应所有类别数量; `rtm_reg` 边框回归分支将通道数从 256 变为 4,4 代表框的坐标。
|
| 621 |
+
|
| 622 |
+
**(2) 初始化网格**
|
| 623 |
+
|
| 624 |
+
根据特征图尺寸初始化三个网格,大小分别为 6400 (80 x 80)、1600 (40 x 40)、400 (20 x 20),如第一个层 shape 为 torch.Size(\[ 6400, 2 \]),最后一个维度是 2,为网格点的横纵坐标,而 6400 表示当前特征层的网格点数量。
|
| 625 |
+
|
| 626 |
+
**(3) 维度变换**
|
| 627 |
+
|
| 628 |
+
经过 `_predict_by_feat_single` 函数,将从 head 提取的单一图像的特征转换为 bbox 结果输入,得到三个列表 `cls_score_list`,`bbox_pred_list`,`mlvl_priors`,详细大小如图所示。之后分别遍历三个特征层,分别对 class 类别预测分支、bbox 回归分支进行处理。以第一层为例,对 bbox 预测分支 \[ 4,80,80 \] 维度变换为 \[ 6400,4 \],对类别预测分支 \[ 80,80,80 \] 变化为 \[ 6400,80 \],并对其做归一化,确保类别置信度在 0 - 1 之间。
|
| 629 |
+
|
| 630 |
+
**(4) 阈值过滤**
|
| 631 |
+
|
| 632 |
+
先使用一个 `nms_pre` 操作,先过滤大部分置信度比较低的预测结果(比如 `score_thr` 阈值设置为 0.05,则去除当前预测置信度低于 0.05 的结果),然后得到 bbox 坐标、所在网格的坐标、置信度、标签的信息。经过三个特征层遍历之后,分别整合这三个层得到的的四个信息放入 results 列表中。
|
| 633 |
+
|
| 634 |
+
**(5) 还原到原图尺度**
|
| 635 |
+
|
| 636 |
+
最后将网络的预测结果映射到整图当中,得到 bbox 在整图中的坐标值
|
| 637 |
+
|
| 638 |
+
**(6) NMS**
|
| 639 |
+
|
| 640 |
+
进行 nms 操作,最终预测得到的返回值为经过后处理的每张图片的检测结果,包含分类置信度,框的 labels,框的四个坐标
|
| 641 |
+
|
| 642 |
+
## 2 总结
|
| 643 |
+
|
| 644 |
+
本文对 RTMDet 原理和在 MMYOLO 实现进行了详细解析,希望能帮助用户理解算法实现过程。同时请注意:由于 RTMDet 本身也在不断更新,
|
| 645 |
+
本开源库也会不断迭代,请及时阅读和同步最新版本。
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/algorithm_descriptions/yolov5_description.md
ADDED
|
@@ -0,0 +1,652 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# YOLOv5 原理和实现全解析
|
| 2 |
+
|
| 3 |
+
## 0 简介
|
| 4 |
+
|
| 5 |
+
<div align=center >
|
| 6 |
+
<img alt="YOLOv5-P5_structure_v3.4" src="https://user-images.githubusercontent.com/27466624/200000324-70ae078f-cea7-4189-8baa-440656797dad.jpg"/>
|
| 7 |
+
图 1:YOLOv5-l-P5 模型结构
|
| 8 |
+
</div>
|
| 9 |
+
|
| 10 |
+
<div align=center >
|
| 11 |
+
<img alt="YOLOv5-P6_structure_v1.1" src="https://user-images.githubusercontent.com/27466624/211143533-1725c1b2-6189-4c3a-a046-ad968e03cb9d.jpg"/>
|
| 12 |
+
图 2:YOLOv5-l-P6 模型结构
|
| 13 |
+
</div>
|
| 14 |
+
|
| 15 |
+
以上结构图由 RangeKing@github 绘制。
|
| 16 |
+
|
| 17 |
+
YOLOv5 是一个面向实时工业应用而开源的目标检测算法,受到了广泛关注。我们认为让 YOLOv5 爆火的原因不单纯在于 YOLOv5 算法本身的优异性,更多的在于开源库的实用和鲁棒性。简单来说 YOLOv5 开源库的主要特点为:
|
| 18 |
+
|
| 19 |
+
1. **友好和完善的部署支持**
|
| 20 |
+
2. **算法训练速度极快**,在 300 epoch 情况下训练时长和大部分 one-stage 算法如 RetinaNet、ATSS 和 two-stage 算法如 Faster R-CNN 在 12 epoch 的训练时间接近
|
| 21 |
+
3. 框架进行了**非常多的 corner case 优化**,功能和文档也比较丰富
|
| 22 |
+
|
| 23 |
+
如图 1 和 2 所示,YOLOv5 的 P5 和 P6 版本主要差异在于网络结构和图片输入分辨率。其他区别,如 anchors 个数和 loss 权重可详见[配置文件](https://github.com/open-mmlab/mmyolo/blob/main/configs/yolov5/yolov5_s-p6-v62_syncbn_fast_8xb16-300e_coco.py)。本文将从 YOLOv5 算法本身原理讲起,然后重点分析 MMYOLO 中的实现。关于 YOLOv5 的使用指南和速度等对比请阅读本文的后续内容。
|
| 24 |
+
|
| 25 |
+
```{hint}
|
| 26 |
+
没有特殊说明情况下,本文默认描述的是 P5 模型。
|
| 27 |
+
```
|
| 28 |
+
|
| 29 |
+
希望本文能够成为你入门和掌握 YOLOv5 的核心文档。由于 YOLOv5 本身也在不断迭代更新,我们也会不断的更新本文档。请注意阅读最新版本。
|
| 30 |
+
|
| 31 |
+
MMYOLO 实现配置:https://github.com/open-mmlab/mmyolo/blob/main/configs/yolov5/
|
| 32 |
+
|
| 33 |
+
YOLOv5 官方开源库地址:https://github.com/ultralytics/yolov5
|
| 34 |
+
|
| 35 |
+
## 1 v6.1 算法原理和 MMYOLO 实现解析
|
| 36 |
+
|
| 37 |
+
YOLOv5 官方 release 地址:https://github.com/ultralytics/yolov5/releases/tag/v6.1
|
| 38 |
+
|
| 39 |
+
<div align=center >
|
| 40 |
+
<img alt="YOLOv5精度图" src="https://user-images.githubusercontent.com/40284075/190542120-29d46b7e-ce3c-436a-9933-cfc9f86787bf.png"/>
|
| 41 |
+
</div>
|
| 42 |
+
|
| 43 |
+
<div align=center >
|
| 44 |
+
<img alt="YOLOv5精度速度图" src="https://user-images.githubusercontent.com/40284075/190542279-37734629-2b59-4bd8-a9bf-757875a93eed.png"/>
|
| 45 |
+
</div>
|
| 46 |
+
|
| 47 |
+
性能如上表所示。YOLOv5 有 P5 和 P6 两个不同训练输入尺度的模型,P6 即为 1280x1280 输入的大模型,通常用的是 P5 常规模型,输入尺寸是 640x640 。本文解读的也是 P5 模型结构。
|
| 48 |
+
|
| 49 |
+
通常来说,目标检测算法都可以分成数据增强、模型结构、loss 计算等组件,YOLOv5 也一样,如下所示:
|
| 50 |
+
|
| 51 |
+
<div align=center >
|
| 52 |
+
<img alt="训练测试策略" src="https://user-images.githubusercontent.com/40284075/190542423-f6b20d8e-c82a-4a34-9065-c161c5e29e7c.png"/>
|
| 53 |
+
</div>
|
| 54 |
+
|
| 55 |
+
下面将从原理和结合 MMYOLO 的具体实现方面进行简要分析。
|
| 56 |
+
|
| 57 |
+
### 1.1 数据增强模块
|
| 58 |
+
|
| 59 |
+
YOLOv5 目标检测算法中使用的数据增强比较多,包括:
|
| 60 |
+
|
| 61 |
+
- **Mosaic 马赛克**
|
| 62 |
+
- **RandomAffine 随机仿射变换**
|
| 63 |
+
- **MixUp**
|
| 64 |
+
- **图像模糊等采用 Albu 库实现的变换**
|
| 65 |
+
- **HSV 颜色空间增强**
|
| 66 |
+
- **随机水平翻转**
|
| 67 |
+
|
| 68 |
+
其中 Mosaic 数据增强概率为 1,表示一定会触发,而对于 small 和 nano 两个版本的模型不使用 MixUp,其他的 l/m/x 系列模型则采用了 0.1 的概率触发 MixUp。小模型能力有限,一般不会采用 MixUp 等强数据增强策略。
|
| 69 |
+
|
| 70 |
+
其核心的 Mosaic + RandomAffine + MixUp 过程简要绘制如下:
|
| 71 |
+
|
| 72 |
+
<div align=center >
|
| 73 |
+
<img alt="image" src="https://user-images.githubusercontent.com/40284075/190542598-bbf4a159-cc9d-4bac-892c-46ef99267994.png"/>
|
| 74 |
+
</div>
|
| 75 |
+
|
| 76 |
+
下面对其进行简要分析。
|
| 77 |
+
|
| 78 |
+
#### 1.1.1 Mosaic 马赛克
|
| 79 |
+
|
| 80 |
+
<div align=center >
|
| 81 |
+
<img alt="image" src="https://user-images.githubusercontent.com/40284075/190542619-d777894f-8928-4244-b39a-158eea416ccd.png"/>
|
| 82 |
+
</div>
|
| 83 |
+
|
| 84 |
+
Mosaic 属于混合类数据增强,因为它在运行时候需要 4 张图片拼接,变相的相当于增加了训练的 batch size。其运行过程简要概况为:
|
| 85 |
+
|
| 86 |
+
1. 随机生成拼接后 4 张图的交接中心点坐标,此时就相当于确定了 4 张拼接图片的交接点
|
| 87 |
+
2. 随机选出另外 3 张图片的索引以及读取对应的标注
|
| 88 |
+
3. 对每张图片采用保持宽高比的 resize 操作将其缩放到指定大小
|
| 89 |
+
4. 按照上下左右规则,计算每张图片在待输出图片中应该放置的位置,因为图片可能出界故还需要计算裁剪坐标
|
| 90 |
+
5. 利用裁剪坐标将缩放后的图片裁剪,然后贴到前面计算出的位置,其余位置全部补 114 像素值
|
| 91 |
+
6. 对每张图片的标注也进行相应处理
|
| 92 |
+
|
| 93 |
+
注意:由于拼接了 4 张图,所以输出图片面积会扩大 4 倍,从 640x640 变成 1280x1280,因此要想恢复为 640x640,
|
| 94 |
+
必须要再接一个 **RandomAffine 随机仿射变换,否则图片面积就一直是扩大 4 倍的**。
|
| 95 |
+
|
| 96 |
+
#### 1.1.2 RandomAffine 随机仿射变换
|
| 97 |
+
|
| 98 |
+
<div align=center >
|
| 99 |
+
<img alt="image" src="https://user-images.githubusercontent.com/40284075/190542871-14e91a42-329f-4084-aec5-b3e412e5364b.png"/>
|
| 100 |
+
</div>
|
| 101 |
+
|
| 102 |
+
随机仿射变换有两个目的:
|
| 103 |
+
|
| 104 |
+
1. 对图片进行随机几何仿射变换
|
| 105 |
+
2. 将 Mosaic 输出的扩大 4 倍的图片还原为 640x640 尺寸
|
| 106 |
+
|
| 107 |
+
随机仿射变换包括平移、旋转、缩放、错切等几何增强操作,同时由于 Mosaic 和 RandomAffine 属于比较强的增强操作,会引入较大噪声,因此需要对增强后的标注进行处理,过滤规则为:
|
| 108 |
+
|
| 109 |
+
1. 增强后的 gt bbox 宽高要大于 wh_thr
|
| 110 |
+
2. 增强后的 gt bbox 面积和增强前的 gt bbox 面积比要大于 ar_thr,防止增强太严重
|
| 111 |
+
3. 最大宽高比要小于 area_thr,防止宽高比改变太多
|
| 112 |
+
|
| 113 |
+
由于旋转后标注框会变大导致不准确,因此目标检测里面很少会使用旋转数据增强。
|
| 114 |
+
|
| 115 |
+
#### 1.1.3 MixUp
|
| 116 |
+
|
| 117 |
+
<div align=center >
|
| 118 |
+
<img alt="image" src="https://user-images.githubusercontent.com/40284075/190543076-db60e4b2-0552-4cf4-ab45-259d1ccbd5a6.png"/>
|
| 119 |
+
</div>
|
| 120 |
+
|
| 121 |
+
MixUp 和 Mosaic 类似也属于混合图片类增强方法。随机选出另外一张图后将两图再随机混合。具体实现方法有多种,常见的做法是要么将 label 直接拼接起来,要么将 label 也采用 alpha 方法混合。原作者的做法非常简单,对 label 即直接拼接,而图片通过分布采样混合。
|
| 122 |
+
|
| 123 |
+
需要特别注意的是:
|
| 124 |
+
**YOLOv5 实现的 MixUp 中,随机出来的另一张图也需要经过 Mosaic 马赛克 + RandomAffine 随机仿射变换 的增强后才能混合。这个和其他开源库实现可能不太一样**。
|
| 125 |
+
|
| 126 |
+
#### 1.1.4 图像模糊和其他数据增强策略
|
| 127 |
+
|
| 128 |
+
<div align=center >
|
| 129 |
+
<img alt="image" src="https://user-images.githubusercontent.com/40284075/190543533-8b9ece51-676b-4a7d-a7d0-597e2dd1d42e.png"/>
|
| 130 |
+
</div>
|
| 131 |
+
|
| 132 |
+
剩下的数据增强包括
|
| 133 |
+
|
| 134 |
+
- **图像模糊等采用 Albu 库实现的变换**
|
| 135 |
+
- **HSV 颜色空间增强**
|
| 136 |
+
- **随机水平翻转**
|
| 137 |
+
|
| 138 |
+
MMDetection 开源库中已经对 Albu 第三方数据增强库进行了封装,使用户可以简单的通过配置即可使用 Albu 库中提供的任何数据增强功能。而 HSV 颜色空间增强和随机水平翻转都是属于比较常规的数据增强,不需要特殊介绍。
|
| 139 |
+
|
| 140 |
+
#### 1.1.5 MMYOLO 实现解析
|
| 141 |
+
|
| 142 |
+
常规的单图数据增强例如随机翻转等比较容易实现,而 Mosaic 类的混合数据增强则不太容易。在 MMDetection 复现的 YOLOX 算法中提出了 MultiImageMixDataset 数据集包装器的概念,其实现过程如下:
|
| 143 |
+
|
| 144 |
+
<div align=center >
|
| 145 |
+
<img alt="image" src="https://user-images.githubusercontent.com/40284075/190543666-d5a22ed7-46a0-4696-990a-12ebde7f8907.png"/>
|
| 146 |
+
</div>
|
| 147 |
+
|
| 148 |
+
对于 Mosaic 等混合类数据增强策略,会需要额外实现一个 `get_indexes` 方法来获取其他图片索引,然后用得到的 4 张图片信息就可以进行 Mosaic 增强了。
|
| 149 |
+
以 MMDetection 中实现的 YOLOX 为例,其配置文件写法如下所示:
|
| 150 |
+
|
| 151 |
+
```python
|
| 152 |
+
train_pipeline = [
|
| 153 |
+
dict(type='Mosaic', img_scale=img_scale, pad_val=114.0),
|
| 154 |
+
dict(
|
| 155 |
+
type='RandomAffine',
|
| 156 |
+
scaling_ratio_range=(0.1, 2),
|
| 157 |
+
border=(-img_scale[0] // 2, -img_scale[1] // 2)),
|
| 158 |
+
dict(
|
| 159 |
+
type='MixUp',
|
| 160 |
+
img_scale=img_scale,
|
| 161 |
+
ratio_range=(0.8, 1.6),
|
| 162 |
+
pad_val=114.0),
|
| 163 |
+
...
|
| 164 |
+
]
|
| 165 |
+
|
| 166 |
+
train_dataset = dict(
|
| 167 |
+
# use MultiImageMixDataset wrapper to support mosaic and mixup
|
| 168 |
+
type='MultiImageMixDataset',
|
| 169 |
+
dataset=dict(
|
| 170 |
+
type='CocoDataset',
|
| 171 |
+
pipeline=[
|
| 172 |
+
dict(type='LoadImageFromFile'),
|
| 173 |
+
dict(type='LoadAnnotations', with_bbox=True)
|
| 174 |
+
]),
|
| 175 |
+
pipeline=train_pipeline)
|
| 176 |
+
```
|
| 177 |
+
|
| 178 |
+
MultiImageMixDataset 数据集包装器传入一个包括 Mosaic 和 RandAffine 等数据增强,而 CocoDataset 中也需要传入一个包括图片和标注加载的 pipeline。通过这种方式就可以快速的实现混合类数据增强。
|
| 179 |
+
|
| 180 |
+
但是上述实现有一个缺点:
|
| 181 |
+
**对于不熟悉 MMDetection 的用户来说,其经常会忘记 Mosaic 必须要和 MultiImageMixDataset 配合使用,否则会报错,而且这样会加大复杂度和理解难度**。
|
| 182 |
+
|
| 183 |
+
为了解决这个问题,在 MMYOLO 中我们进一步进行了简化。直接让 pipeline 能够获取到 dataset 对象,此时就可以将 Mosaic 等混合类数据增强的实现和使用变成和随机翻转一样。
|
| 184 |
+
此时在 MMYOLO 中 YOLOX 的配置写法变成如下所示:
|
| 185 |
+
|
| 186 |
+
```python
|
| 187 |
+
pre_transform = [
|
| 188 |
+
dict(type='LoadImageFromFile'),
|
| 189 |
+
dict(type='LoadAnnotations', with_bbox=True)
|
| 190 |
+
]
|
| 191 |
+
|
| 192 |
+
train_pipeline = [
|
| 193 |
+
*pre_transform,
|
| 194 |
+
dict(
|
| 195 |
+
type='Mosaic',
|
| 196 |
+
img_scale=img_scale,
|
| 197 |
+
pad_val=114.0,
|
| 198 |
+
pre_transform=pre_transform),
|
| 199 |
+
dict(
|
| 200 |
+
type='mmdet.RandomAffine',
|
| 201 |
+
scaling_ratio_range=(0.1, 2),
|
| 202 |
+
border=(-img_scale[0] // 2, -img_scale[1] // 2)),
|
| 203 |
+
dict(
|
| 204 |
+
type='YOLOXMixUp',
|
| 205 |
+
img_scale=img_scale,
|
| 206 |
+
ratio_range=(0.8, 1.6),
|
| 207 |
+
pad_val=114.0,
|
| 208 |
+
pre_transform=pre_transform),
|
| 209 |
+
...
|
| 210 |
+
]
|
| 211 |
+
```
|
| 212 |
+
|
| 213 |
+
这样就不再需要 MultiImageMixDataset 了,使用和理解上会更加简单。
|
| 214 |
+
|
| 215 |
+
回到 YOLOv5 配置上,因为 YOLOv5 实现的 MixUp 中,随机选出来的另一张图也需要经过 Mosaic 马赛克+RandomAffine 随机仿射变换 增强后才能混合,故YOLOv5-m 数据增强配置如下所示:
|
| 216 |
+
|
| 217 |
+
```python
|
| 218 |
+
pre_transform = [
|
| 219 |
+
dict(type='LoadImageFromFile'),
|
| 220 |
+
dict(type='LoadAnnotations', with_bbox=True)
|
| 221 |
+
]
|
| 222 |
+
|
| 223 |
+
mosaic_transform= [
|
| 224 |
+
dict(
|
| 225 |
+
type='Mosaic',
|
| 226 |
+
img_scale=img_scale,
|
| 227 |
+
pad_val=114.0,
|
| 228 |
+
pre_transform=pre_transform),
|
| 229 |
+
dict(
|
| 230 |
+
type='YOLOv5RandomAffine',
|
| 231 |
+
max_rotate_degree=0.0,
|
| 232 |
+
max_shear_degree=0.0,
|
| 233 |
+
scaling_ratio_range=(0.1, 1.9), # scale = 0.9
|
| 234 |
+
border=(-img_scale[0] // 2, -img_scale[1] // 2),
|
| 235 |
+
border_val=(114, 114, 114))
|
| 236 |
+
]
|
| 237 |
+
|
| 238 |
+
train_pipeline = [
|
| 239 |
+
*pre_transform,
|
| 240 |
+
*mosaic_transform,
|
| 241 |
+
dict(
|
| 242 |
+
type='YOLOv5MixUp',
|
| 243 |
+
prob=0.1,
|
| 244 |
+
pre_transform=[
|
| 245 |
+
*pre_transform,
|
| 246 |
+
*mosaic_transform
|
| 247 |
+
]),
|
| 248 |
+
...
|
| 249 |
+
]
|
| 250 |
+
```
|
| 251 |
+
|
| 252 |
+
### 1.2 网络结构
|
| 253 |
+
|
| 254 |
+
本小结由 RangeKing@github 撰写,非常感谢!!!
|
| 255 |
+
|
| 256 |
+
YOLOv5 网络结构是标准的 `CSPDarknet` + `PAFPN` + `非解耦 Head`。
|
| 257 |
+
|
| 258 |
+
YOLOv5 网络结构大小由 `deepen_factor` 和 `widen_factor` 两个参数决定。其中 `deepen_factor` 控制网络结构深度,即 `CSPLayer` 中 `DarknetBottleneck` 模块堆叠的数量;`widen_factor` 控制网络结构宽度,即模块输出特征图的通道数。以 YOLOv5-l 为例,其 `deepen_factor = widen_factor = 1.0` 。P5 和 P6 的模型整体结构分别如图 1 和图 2 所示。
|
| 259 |
+
|
| 260 |
+
图的上半部分为模型总览;下半部分为具体网络结构,其中的模块均标有序号,方便用户与 YOLOv5 官方仓库的配置文件对应;中间部分为各子模块的具体构成。
|
| 261 |
+
|
| 262 |
+
如果想使用 netron 可视化网络结构图细节,可以直接在 netron 中将 MMDeploy 导出的 ONNX 文件格式文件打开。
|
| 263 |
+
|
| 264 |
+
```{hint}
|
| 265 |
+
1.2 小节涉及的特征维度(shape)都为 (B, C, H, W)。
|
| 266 |
+
```
|
| 267 |
+
|
| 268 |
+
#### 1.2.1 Backbone
|
| 269 |
+
|
| 270 |
+
在 MMYOLO 中 `CSPDarknet` 继承自 `BaseBackbone`,整体结构和 `ResNet` 类似。P5 模型共 5 层结构,包含 1 个 `Stem Layer` 和 4 个 `Stage Layer`:
|
| 271 |
+
|
| 272 |
+
- `Stem Layer` 是 1 个 6x6 kernel 的 `ConvModule`,相较于 v6.1 版本之前的 `Focus` 模块更加高效。
|
| 273 |
+
- 除了最后一个 `Stage Layer`,其他均由 1 个 `ConvModule` 和 1 个 `CSPLayer` 组成。如上图 Details 部分所示。
|
| 274 |
+
其中 `ConvModule` 为 3x3的 `Conv2d` + `BatchNorm` + `SiLU 激活函数`。`CSPLayer` 即 YOLOv5 官方仓库中的 C3 模块,由 3 个 `ConvModule` + n 个 `DarknetBottleneck`(带残差连接) 组成。
|
| 275 |
+
- 最后一个 `Stage Layer` 在最后增加了 `SPPF` 模块。`SPPF` 模块是将输入串行通过多个 5x5 大小的 `MaxPool2d` 层,与 `SPP` 模块效果相同,但速度更快。
|
| 276 |
+
- P5 模型会在 `Stage Layer` 2-4 之后分别输出一个特征图进入 `Neck` 结构。以 640x640 输入图片为例,其输出特征为 (B,256,80,80)、(B,512,40,40) 和 (B,1024,20,20),对应的 stride 分别为 8/16/32。
|
| 277 |
+
- P6 模型会在 `Stage Layer` 2-5 之后分别输出一个特征图进入 `Neck` 结构。以 1280x1280 输入图片为例,其输出特征为 (B,256,160,160)、(B,512,80,80)、(B,768,40,40) 和 (B,1024,20,20),对应的 stride 分别为 8/16/32/64。
|
| 278 |
+
|
| 279 |
+
#### 1.2.2 Neck
|
| 280 |
+
|
| 281 |
+
YOLOv5 官方仓库的配置文件中并没有 Neck 部分,为方便用户与其他目标检测网络结构相对应,我们将官方仓库的 `Head` 拆分成 `PAFPN` 和 `Head` 两部分。
|
| 282 |
+
|
| 283 |
+
基于 `BaseYOLONeck` 结构,YOLOv5 `Neck` 也是遵循同一套构建流程,对于不存在的模块,我们采用 `nn.Identity` 代替。
|
| 284 |
+
|
| 285 |
+
Neck 模块输出的特征图和 Backbone 完全一致。即 P5 模型为 (B,256,80,80)、 (B,512,40,40) 和 (B,1024,20,20);P6 模型为 (B,256,160,160)、(B,512,80,80)、(B,768,40,40) 和 (B,1024,20,20)。
|
| 286 |
+
|
| 287 |
+
#### 1.2.3 Head
|
| 288 |
+
|
| 289 |
+
YOLOv5 Head 结构和 YOLOv3 完全一样,为 `非解耦 Head`。Head 模块只包括 3 个不共享权重的卷积,用于将输入特征图进行变换而已。
|
| 290 |
+
|
| 291 |
+
前面的 PAFPN 依然是输出 3 个不同尺度的特征图,shape 为 (B,256,80,80)、 (B,512,40,40) 和 (B,1024,20,20)。
|
| 292 |
+
由于 YOLOv5 是非解耦输出,即分类和 bbox 检测等都是在同一个卷积的不同通道中完成。以 COCO 80 类为例:
|
| 293 |
+
|
| 294 |
+
- P5 模型在输入为 640x640 分辨率情况下,其 Head 模块输出的 shape 分别为 `(B, 3x(4+1+80),80,80)`, `(B, 3x(4+1+80),40,40)` 和 `(B, 3x(4+1+80),20,20)`。
|
| 295 |
+
- P6 模型在输入为 1280x1280 分辨率情况下,其 Head 模块输出的 shape 分别为 `(B, 3x(4+1+80),160,160)`, `(B, 3x(4+1+80),80,80)`, `(B, 3x(4+1+80),40,40)` 和 `(B, 3x(4+1+80),20,20)`。
|
| 296 |
+
其中 3 表示 3 个 anchor,4 表示 bbox 预测分支,1 表示 obj 预测分支,80 表示 COCO 数据集类别预测分支。
|
| 297 |
+
|
| 298 |
+
### 1.3 正负样本匹配策略
|
| 299 |
+
|
| 300 |
+
正负样本匹配策略的核心是确定预测特征图的所有位置中哪些位置应该是正样本,哪些是负样本,甚至有些是忽略样本。
|
| 301 |
+
匹配策略是目标检测算法的核心,一个好的匹配策略可以显著提升算法性能。
|
| 302 |
+
|
| 303 |
+
YOLOV5 的匹配策略简单总结为:**采用了 anchor 和 gt_bbox 的 shape 匹配度作为划分规则,同时引入跨邻域网格策略来增加正样本**。
|
| 304 |
+
其主要包括如下两个核心步骤:
|
| 305 |
+
|
| 306 |
+
1. 对于任何一个输出层,抛弃了常用的基于 Max IoU 匹配的规则,而是直接采用 shape 规则匹配,也就是该 GT Bbox 和当前层的 Anchor 计算宽高比,如果宽高比例大于设定阈值,则说明该 GT Bbox 和 Anchor 匹配度不够,将该 GT Bbox 暂时丢掉,在该层预测中该 GT Bbox 对应的网格内的预测位置认为是负样本
|
| 307 |
+
2. 对于剩下的 GT Bbox(也就是匹配上的 GT Bbox),计算其落在哪个网格内,同时利用四舍五入规则,找出最近的两个网格,将这三个网格都认为是负责预测该 GT Bbox 的,可以粗略估计正样本数相比之前的 YOLO 系列,至少增加了三倍
|
| 308 |
+
|
| 309 |
+
下面会对每个部分进行详细说明,部分描述和图示直接或间接参考自官方 [Repo](https://github.com/ultralytics/YOLOv5/issues/6998#44)。
|
| 310 |
+
|
| 311 |
+
#### 1.3.1 Anchor 设置
|
| 312 |
+
|
| 313 |
+
YOLOv5 是 Anchor-based 的目标检测算法,其 Anchor size 的获取方式与 YOLOv3 类似,也是使用聚类获得,其不同之处在于聚类使用的标准不再是基于 IoU 的,而是使用形状上的宽高比作为聚类准则(即 shape-match )。
|
| 314 |
+
|
| 315 |
+
在用户更换了数据集后,可以使用 MMYOLO 里带有的 Anchor 分析工具,对自己的数据集进行分析,确定合适的 Anchor size。
|
| 316 |
+
|
| 317 |
+
```shell
|
| 318 |
+
python tools/analysis_tools/optimize_anchors.py ${CONFIG} --algorithm v5-k-means
|
| 319 |
+
--input-shape ${INPUT_SHAPE [WIDTH HEIGHT]} --output-dir ${OUTPUT_DIR}
|
| 320 |
+
```
|
| 321 |
+
|
| 322 |
+
然后在 [config 文件](https://github.com/open-mmlab/mmyolo/blob/main/configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py) 里修改默认 Anchor size:
|
| 323 |
+
|
| 324 |
+
```python
|
| 325 |
+
anchors = [[(10, 13), (16, 30), (33, 23)], [(30, 61), (62, 45), (59, 119)],
|
| 326 |
+
[(116, 90), (156, 198), (373, 326)]]
|
| 327 |
+
```
|
| 328 |
+
|
| 329 |
+
#### 1.3.2 Bbox 编解码过程
|
| 330 |
+
|
| 331 |
+
在 Anchor-based 算法中,预测框通常会基于 Anchor 进行变换,然后预测变换量,这对应 GT Bbox 编码过程,而在预测后需要进行 Pred Bbox 解码,还原为真实尺度的 Bbox,这对应 Pred Bbox 解码过程。
|
| 332 |
+
|
| 333 |
+
在 YOLOv3 中,回归公式为:
|
| 334 |
+
|
| 335 |
+
```{math}
|
| 336 |
+
b_x=\sigma(t_x)+c_x \\
|
| 337 |
+
b_y=\sigma(t_y)+c_y \\
|
| 338 |
+
b_w=a_w\cdot e^{t_w} \\
|
| 339 |
+
b_h=a_h\cdot e^{t_h} \\
|
| 340 |
+
```
|
| 341 |
+
|
| 342 |
+
公式中,
|
| 343 |
+
|
| 344 |
+
```{math}
|
| 345 |
+
a_w 代表 Anchor 的宽度 \\
|
| 346 |
+
c_x 代表 Grid 所处的坐标 \\
|
| 347 |
+
\sigma 代表 Sigmoid 公式。
|
| 348 |
+
```
|
| 349 |
+
|
| 350 |
+
而在 YOLOv5 中,回归公式为:
|
| 351 |
+
|
| 352 |
+
```{math}
|
| 353 |
+
b_x=(2\cdot\sigma(t_x)-0.5)+c_x \\
|
| 354 |
+
b_y=(2\cdot\sigma(t_y)-0.5)+c_y \\
|
| 355 |
+
b_w=a_w\cdot(2\cdot\sigma(t_w))^2 \\
|
| 356 |
+
b_h=a_h\cdot(2\cdot\sigma(t_h))^2
|
| 357 |
+
```
|
| 358 |
+
|
| 359 |
+
改进之处主要有以下两点:
|
| 360 |
+
|
| 361 |
+
- 中心点坐标范围从 (0, 1) 调整至 (-0.5, 1.5)
|
| 362 |
+
- 宽高范围从
|
| 363 |
+
|
| 364 |
+
```{math}
|
| 365 |
+
(0,+\infty)
|
| 366 |
+
```
|
| 367 |
+
|
| 368 |
+
调整至
|
| 369 |
+
|
| 370 |
+
```{math}
|
| 371 |
+
(0,4a_{wh})
|
| 372 |
+
```
|
| 373 |
+
|
| 374 |
+
这个改进具有以下好处:
|
| 375 |
+
|
| 376 |
+
- **新的中心点设置能更好的预测到 0 和 1**。这有助于更精准回归出 box 坐标。
|
| 377 |
+
|
| 378 |
+
<div align=center >
|
| 379 |
+
<img alt="image" src="https://user-images.githubusercontent.com/40284075/190546778-83001bac-4e71-4b9a-8de8-bd41146495af.png"/>
|
| 380 |
+
</div>
|
| 381 |
+
|
| 382 |
+
- 宽高回归公式中 exp(x) 是无界的,这会导致**梯度失去控制**,造成训练不稳定。YOLOv5 中改进后的宽高回归公式优化了此问题。
|
| 383 |
+
|
| 384 |
+
<div align=center >
|
| 385 |
+
<img alt="image" src="https://user-images.githubusercontent.com/40284075/190546793-5364d6d3-7891-4af3-98e3-9f06970f3163.png"/>
|
| 386 |
+
</div>
|
| 387 |
+
|
| 388 |
+
#### 1.3.3 匹配策略
|
| 389 |
+
|
| 390 |
+
在 MMYOLO 设计中,无论网络是 Anchor-based 还是 Anchor-free,**我们统一使用 prior 称呼 Anchor**。
|
| 391 |
+
|
| 392 |
+
正样本匹配包含以下两步:
|
| 393 |
+
|
| 394 |
+
**(1) “比例”比较**
|
| 395 |
+
|
| 396 |
+
将 GT Bbox 的 WH 与 Prior 的 WH 进行“比例”比较。
|
| 397 |
+
|
| 398 |
+
比较流程:
|
| 399 |
+
|
| 400 |
+
```{math}
|
| 401 |
+
r_w = w\_{gt} / w\_{pt} \\
|
| 402 |
+
r_h = h\_{gt} / h\_{pt} \\
|
| 403 |
+
r_w^{max}=max(r_w, 1/r_w) \\
|
| 404 |
+
r_h^{max}=max(r_h, 1/r_h) \\
|
| 405 |
+
r^{max}=max(r_w^{max}, r_h^{max}) \\
|
| 406 |
+
if\ \ r_{max} < prior\_match\_thr: match!
|
| 407 |
+
```
|
| 408 |
+
|
| 409 |
+
此处我们用一个 GT Bbox 与 P3 特征图的 Prior 进行匹配的案例进行讲解和图示:
|
| 410 |
+
|
| 411 |
+
<div align=center >
|
| 412 |
+
<img alt="image" src="https://user-images.githubusercontent.com/40284075/190547195-60d6cd7a-b12a-4c6f-9cc8-13f48c8ab1e0.png"/>
|
| 413 |
+
</div>
|
| 414 |
+
|
| 415 |
+
prior1 匹配失败的原因是
|
| 416 |
+
|
| 417 |
+
```{math}
|
| 418 |
+
h\_{gt}\ /\ h\_{prior}\ =\ 4.8\ >\ prior\_match\_thr
|
| 419 |
+
```
|
| 420 |
+
|
| 421 |
+
**(2) 为步骤 1 中 match 的 GT 分配对应的正样本**
|
| 422 |
+
|
| 423 |
+
依然沿用上面的例子:
|
| 424 |
+
|
| 425 |
+
GT Bbox (cx, cy, w, h) 值为 (26, 37, 36, 24),
|
| 426 |
+
|
| 427 |
+
Prior WH 值为 \[(15, 5), (24, 16), (16, 24)\],在 P3 特征图��,stride 为 8。通过计算,prior2 和 prior3 能够 match。
|
| 428 |
+
|
| 429 |
+
计算过程如下:
|
| 430 |
+
|
| 431 |
+
**(2.1) 将 GT Bbox 的中心点坐标对应到 P3 的 grid 上**
|
| 432 |
+
|
| 433 |
+
```{math}
|
| 434 |
+
GT_x^{center_grid}=26/8=3.25 \\
|
| 435 |
+
GT_y^{center_grid}=37/8=4.625
|
| 436 |
+
```
|
| 437 |
+
|
| 438 |
+
<div align=center >
|
| 439 |
+
<img alt="image" src="https://user-images.githubusercontent.com/40284075/190549304-020ec19e-6d54-4d40-8f43-f78b8d6948aa.png"/>
|
| 440 |
+
</div>
|
| 441 |
+
|
| 442 |
+
**(2.2)** 将 GT Bbox 中心点所在的 grid 分成四个象限,**由于中心点落在了左下角的象限当中,那么会将物体的左、下两个 grid 也认为是正样本**
|
| 443 |
+
|
| 444 |
+
<div align=center >
|
| 445 |
+
<img alt="image" src="https://user-images.githubusercontent.com/40284075/190549310-e5da53e3-eae3-4085-bd0a-1843ac8ca653.png"/>
|
| 446 |
+
</div>
|
| 447 |
+
|
| 448 |
+
下图展示中心点落到不同位置时的正样本分配情况:
|
| 449 |
+
|
| 450 |
+
<div align=center >
|
| 451 |
+
<img alt="image" src="https://user-images.githubusercontent.com/40284075/190549613-eb47e70a-a2c1-4729-9fb7-f5ce7007842b.png"/>
|
| 452 |
+
</div>
|
| 453 |
+
|
| 454 |
+
那么 YOLOv5 的 Assign 方式具体带来了哪些改进?
|
| 455 |
+
|
| 456 |
+
- 一个 GT Bbox 能够匹配多个 Prior
|
| 457 |
+
|
| 458 |
+
- 一个 GT Bbox 和一个Prior 匹配时,能分配 1-3 个正样本
|
| 459 |
+
|
| 460 |
+
- 以上策略能**适度缓解目标检测中常见的正负样本不均衡问题**。
|
| 461 |
+
|
| 462 |
+
而 YOLOv5 中的回归方式,和 Assign 方式是相互呼应的:
|
| 463 |
+
|
| 464 |
+
1. 中心点回归方式:
|
| 465 |
+
|
| 466 |
+
<div align=center >
|
| 467 |
+
<img alt="image" src="https://user-images.githubusercontent.com/40284075/190549684-21776c33-9ef8-4818-9530-14f750a18d63.png"/>
|
| 468 |
+
</div>
|
| 469 |
+
|
| 470 |
+
2. WH 回归方式:
|
| 471 |
+
|
| 472 |
+
<div align=center >
|
| 473 |
+
<img alt="image" src="https://user-images.githubusercontent.com/40284075/190549696-3da08c06-753a-4108-be47-64495ea480f2.png"/>
|
| 474 |
+
</div>
|
| 475 |
+
|
| 476 |
+
### 1.4 Loss 设计
|
| 477 |
+
|
| 478 |
+
YOLOv5 中总共包含 3 个 Loss,分别为:
|
| 479 |
+
|
| 480 |
+
- Classes loss:使用的是 BCE loss
|
| 481 |
+
- Objectness loss:使用的是 BCE loss
|
| 482 |
+
- Location loss:使用的是 CIoU loss
|
| 483 |
+
|
| 484 |
+
三个 loss 按照一定比例汇总:
|
| 485 |
+
|
| 486 |
+
```{math}
|
| 487 |
+
Loss=\lambda_1L_{cls}+\lambda_2L_{obj}+\lambda_3L_{loc}
|
| 488 |
+
```
|
| 489 |
+
|
| 490 |
+
P3、P4、P5 层对应的 Objectness loss 按照不同权重进行相加,默认的设置是
|
| 491 |
+
|
| 492 |
+
```python
|
| 493 |
+
obj_level_weights=[4., 1., 0.4]
|
| 494 |
+
```
|
| 495 |
+
|
| 496 |
+
```{math}
|
| 497 |
+
L_{obj}=4.0\cdot L_{obj}^{small}+1.0\cdot L_{obj}^{medium}+0.4\cdot L_{obj}^{large}
|
| 498 |
+
```
|
| 499 |
+
|
| 500 |
+
在复现中我们发现 YOLOv5 中使用的 CIoU 与目前最新官方 CIoU 存在一定的差距,差距体现在 alpha 参数的计算。
|
| 501 |
+
|
| 502 |
+
官方版本:
|
| 503 |
+
|
| 504 |
+
参考资料:https://github.com/Zzh-tju/CIoU/blob/master/layers/modules/multibox_loss.py#L53-L55
|
| 505 |
+
|
| 506 |
+
```python
|
| 507 |
+
alpha = (ious > 0.5).float() * v / (1 - ious + v)
|
| 508 |
+
```
|
| 509 |
+
|
| 510 |
+
YOLOv5 版本:
|
| 511 |
+
|
| 512 |
+
```python
|
| 513 |
+
alpha = v / (v - ious + (1 + eps))
|
| 514 |
+
```
|
| 515 |
+
|
| 516 |
+
这是一个有趣的细节,后续需要测试不同 alpha 计算方式情况下带来的精度差距。
|
| 517 |
+
|
| 518 |
+
### 1.5 优化策略和训练过程
|
| 519 |
+
|
| 520 |
+
YOLOv5 对每个优化器的参数组进行非常精细的控制,简单来说包括如下部分。
|
| 521 |
+
|
| 522 |
+
#### 1.5.1 优化器分组
|
| 523 |
+
|
| 524 |
+
将优化参数分成 Conv/Bias/BN 三组,在 WarmUp 阶段,不同组采用不同的 lr 以及 momentum 更新曲线。
|
| 525 |
+
同时在 WarmUp 阶段采用的是 iter-based 更新策略,而在非 WarmUp 阶段则变成 epoch-based 更新策略,可谓是 trick 十足。
|
| 526 |
+
|
| 527 |
+
MMYOLO 中是采用 YOLOv5OptimizerConstructor 优化器构造器实现优化器参数分组。优化器构造器的作用就是对一些特殊的参数组初始化过程进行精细化控制,因此可以很好的满足需求。
|
| 528 |
+
|
| 529 |
+
而不同的参数组采用不同的调度曲线功能则是通过 YOLOv5ParamSchedulerHook 实现。而不同的参数组采用不同的调度曲线功能则是通过 YOLOv5ParamSchedulerHook 实现。
|
| 530 |
+
|
| 531 |
+
#### 1.5.2 weight decay 参数自适应
|
| 532 |
+
|
| 533 |
+
作者针对不同的 batch size 采用了不同的 weight decay 策略,具体来说为:
|
| 534 |
+
|
| 535 |
+
1. 当训练 batch size \<= 64 时,weight decay 不变
|
| 536 |
+
2. 当训练 batch size > 64 时,weight decay 会根据总 batch size 进行线性缩放
|
| 537 |
+
|
| 538 |
+
MMYOLO 也是通过 YOLOv5OptimizerConstructor 实现。
|
| 539 |
+
|
| 540 |
+
#### 1.5.3 梯度累加
|
| 541 |
+
|
| 542 |
+
为了最大化不同 batch size 情况下的性能,作者设置总 batch size 小于 64 时候会自动开启梯度累加功能。
|
| 543 |
+
|
| 544 |
+
训练过程和大部分 YOLO 类似,包括如下策略:
|
| 545 |
+
|
| 546 |
+
1. 没有使用预训练权重
|
| 547 |
+
2. 没有采用多尺度训练策略,同时可以开启 cudnn.benchmark 进一步加速训练
|
| 548 |
+
3. 使用了 EMA 策略平滑模型
|
| 549 |
+
4. 默认采用 AMP 自动混合精度训练
|
| 550 |
+
|
| 551 |
+
需要特意说明的是:YOLOv5 官方对于 small 模型是采用单卡 v100 训练,bs 为 128,而 m/l/x 等是采用不同数目的多卡实现的,
|
| 552 |
+
这种训练策略不太规范,**为此在 MMYOLO 中全部采用了 8 卡,每卡 16 bs 的设置,同时为了避免性能差异,训练时候开启了 SyncBN**。
|
| 553 |
+
|
| 554 |
+
### 1.6 推理和后处理过程
|
| 555 |
+
|
| 556 |
+
YOLOv5 后处理过程和 YOLOv3 非常类似,实际上 YOLO 系列的后处理逻辑都是类似的。
|
| 557 |
+
|
| 558 |
+
#### 1.6.1 核心控制参数
|
| 559 |
+
|
| 560 |
+
1. **multi_label**
|
| 561 |
+
|
| 562 |
+
对于多类别预测来说需要考虑是否是多标签任务,也就是同一个预测位置会预测的多个类别概率,和是否当作单类处理。因为 YOLOv5 采用 sigmoid 预测模式,在考虑多标签情况下可能会出现一个物体检测出两个不同类别的框,这有助于评估指标 mAP,但是不利于实际应用。
|
| 563 |
+
因此在需要算评估指标时候 multi_label 是 True,而推理或者实际应用时候是 False
|
| 564 |
+
|
| 565 |
+
2. **score_thr 和 nms_thr**
|
| 566 |
+
|
| 567 |
+
score_thr 阈值用于过滤类别分值,低于分值的检测框当做背景处理,nms_thr 是 nms 时阈值。同样的,在计算评估指标 mAP 阶段可以将 score_thr 设置的非常低,这通常能够提高召回率,从而提升 mAP,但是对于实际应用来说没有意义,且会导致推理过程极慢。为此在测试和推理阶段会设置不同的阈值
|
| 568 |
+
|
| 569 |
+
3. **nms_pre 和 max_per_img**
|
| 570 |
+
|
| 571 |
+
nms_pre 表示 nms 前的最大保留检测框数目,这通常是为了防止 nms 运行时候输入框过多导致速度过慢问题,默认值是 30000。
|
| 572 |
+
max_per_img 表示最终保留的最大检测框数目,通常设置为 300。
|
| 573 |
+
|
| 574 |
+
以 COCO 80 类为例,假设输入图片大小为 640x640
|
| 575 |
+
|
| 576 |
+
<div align=center >
|
| 577 |
+
<img alt="image" src="https://user-images.githubusercontent.com/17425982/192942249-96b0fcfb-059f-48fe-862f-7d526a3a06d7.png"/>
|
| 578 |
+
</div>
|
| 579 |
+
|
| 580 |
+
其推理和后处理过程为:
|
| 581 |
+
|
| 582 |
+
**(1) 维度变换**
|
| 583 |
+
|
| 584 |
+
YOLOv5 输出特征图尺度为 80x80、40x40 和 20x20 的三个特征图,每个位置共 3 个 anchor,因此输出特征图通道为 3x(5+80)=255。
|
| 585 |
+
YOLOv5 是非解耦输出头,而其他大部分算法都是解耦输出头,为了统一后处理逻辑,我们提前将其进行解耦,分成了类别预测分支、bbox 预测分支和 obj 预测分支。
|
| 586 |
+
|
| 587 |
+
将三个不同尺度的类别预测分支、bbox 预测分支和 obj 预测分支进行拼接,并进行维度变换。为了后续方便处理,会将原先的通道维度置换到最后,类别预测分支、bbox 预测分支和 obj 预测分支的 shape 分别为 (b, 3x80x80+3x40x40+3x20x20, 80)=(b,25200,80),(b,25200,4),(b,25200,1)。
|
| 588 |
+
|
| 589 |
+
**(2) 解码还原到原图尺度**
|
| 590 |
+
|
| 591 |
+
分类预测分支和 obj 分支需要进行 sigmoid 计算,而 bbox 预测分支需要进行解码,还原为真实的原图解码后 xyxy 格式
|
| 592 |
+
|
| 593 |
+
**(3) 第一次阈值过滤**
|
| 594 |
+
|
| 595 |
+
遍历 batch 中的每张图,然后用 score_thr 对类别预测分值进行阈值过滤,去掉低于 score_thr 的预测结果
|
| 596 |
+
|
| 597 |
+
**(4) 第二次阈值过滤**
|
| 598 |
+
|
| 599 |
+
将 obj 预测分值和过滤后的类别预测分值相乘,然后依然采用 score_thr 进行阈值过滤。
|
| 600 |
+
在这过程中还需要考虑 **multi_label 和 nms_pre,确保过滤后的检测框数目不会多于 nms_pre**。
|
| 601 |
+
|
| 602 |
+
**(5) 还原到原图尺度和 nms**
|
| 603 |
+
|
| 604 |
+
基于前处理过程,将剩下的检测框还原到网络输出前的原图尺度,然后进行 nms 即可。最终输出的检测框不能多于 **max_per_img**。
|
| 605 |
+
|
| 606 |
+
#### 1.6.2 batch shape 策略
|
| 607 |
+
|
| 608 |
+
为了加速验证集的推理过程,作者提出了 batch shape 策略,其核心原则是:**确保在 batch 推理过程中同一个 batch 内的图片 pad 像素最少,不要求整个验证过程中所有 batch 的图片尺度一样**。
|
| 609 |
+
|
| 610 |
+
其大概流程是:将整个测试或者验证数据的宽高比进行排序,然后依据 batch 设置将排序后的图片组成一个 batch,
|
| 611 |
+
同时计算这个 batch 内最佳的 batch shape,防止 pad 像素过多。最佳 batch shape 计算原则为在保持宽高比的情况下进行 pad,不追求正方形图片输出。
|
| 612 |
+
|
| 613 |
+
```python
|
| 614 |
+
image_shapes = []
|
| 615 |
+
for data_info in data_list:
|
| 616 |
+
image_shapes.append((data_info['width'], data_info['height']))
|
| 617 |
+
|
| 618 |
+
image_shapes = np.array(image_shapes, dtype=np.float64)
|
| 619 |
+
|
| 620 |
+
n = len(image_shapes) # number of images
|
| 621 |
+
batch_index = np.floor(np.arange(n) / self.batch_size).astype(
|
| 622 |
+
np.int64) # batch index
|
| 623 |
+
number_of_batches = batch_index[-1] + 1 # number of batches
|
| 624 |
+
|
| 625 |
+
aspect_ratio = image_shapes[:, 1] / image_shapes[:, 0] # aspect ratio
|
| 626 |
+
irect = aspect_ratio.argsort()
|
| 627 |
+
|
| 628 |
+
data_list = [data_list[i] for i in irect]
|
| 629 |
+
|
| 630 |
+
aspect_ratio = aspect_ratio[irect]
|
| 631 |
+
# Set training image shapes
|
| 632 |
+
shapes = [[1, 1]] * number_of_batches
|
| 633 |
+
for i in range(number_of_batches):
|
| 634 |
+
aspect_ratio_index = aspect_ratio[batch_index == i]
|
| 635 |
+
min_index, max_index = aspect_ratio_index.min(
|
| 636 |
+
), aspect_ratio_index.max()
|
| 637 |
+
if max_index < 1:
|
| 638 |
+
shapes[i] = [max_index, 1]
|
| 639 |
+
elif min_index > 1:
|
| 640 |
+
shapes[i] = [1, 1 / min_index]
|
| 641 |
+
|
| 642 |
+
batch_shapes = np.ceil(
|
| 643 |
+
np.array(shapes) * self.img_size / self.size_divisor +
|
| 644 |
+
self.pad).astype(np.int64) * self.size_divisor
|
| 645 |
+
|
| 646 |
+
for i, data_info in enumerate(data_list):
|
| 647 |
+
data_info['batch_shape'] = batch_shapes[batch_index[i]]
|
| 648 |
+
```
|
| 649 |
+
|
| 650 |
+
## 2 总结
|
| 651 |
+
|
| 652 |
+
本文对 YOLOv5 原理和在 MMYOLO 实现进行了详细解析,希望能帮助用户理解算法实现过程。同时请注意:由于 YOLOv5 本身也在不���更新,本开源库也会不断迭代,请及时阅读和同步最新版本。
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/algorithm_descriptions/yolov6_description.md
ADDED
|
@@ -0,0 +1,452 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# YOLOv6 原理和实现全解析
|
| 2 |
+
|
| 3 |
+
## 0 简介
|
| 4 |
+
|
| 5 |
+
<div align=center >
|
| 6 |
+
<img alt="YOLOv6-S" src="https://user-images.githubusercontent.com/58845482/209790152-21c29d42-30cc-4c48-a723-39b198286c4d.png"/>
|
| 7 |
+
图 1:YOLOv6-S 模型结构
|
| 8 |
+
</div>
|
| 9 |
+
|
| 10 |
+
<div align=center >
|
| 11 |
+
<img alt="YOLOv6-L" src="https://user-images.githubusercontent.com/58845482/209787949-d57691c0-a2ea-4a0a-829f-e8a64ac29c7e.png"/>
|
| 12 |
+
图 2:YOLOv6-L 模型结构
|
| 13 |
+
</div>
|
| 14 |
+
|
| 15 |
+
以上结构图由 wzr-skn@github 绘制。
|
| 16 |
+
|
| 17 |
+
YOLOv6 提出了一系列适用于各种工业场景的模型,包括 N/T/S/M/L,考虑到模型的大小,其架构有所不同,以获得更好的精度-速度权衡。本算法专注于检测的精度和推理效率,并在网络结构、训练策略等算法层面进行了多项改进和优化。
|
| 18 |
+
|
| 19 |
+
简单来说 YOLOv6 开源库的主要特点为:
|
| 20 |
+
|
| 21 |
+
1. 统一设计了更高效的 Backbone 和 Neck:受到硬件感知神经网络设计思想的启发,基于 RepVGG style 设计了可重参数化、更高效的骨干网络 EfficientRep Backbone 和 Rep-PAN Neck。
|
| 22 |
+
2. 相比于 YOLOX 的 Decoupled Head,进一步优化设计了简洁有效的 Efficient Decoupled Head,在维持精度的同时,降低了一般解耦头带来的额外延时开销。
|
| 23 |
+
3. 在训练策略上,采用 Anchor-free 的策略,同时辅以 SimOTA 标签分配策略以及 SIoU 边界框回归损失来进一步提高检测精度。
|
| 24 |
+
|
| 25 |
+
本文将从 YOLOv6 算法本身原理讲起,然后重点分析 MMYOLO 中的实现。关于 YOLOv6 的使用指南和速度等对比请阅读本文的后续内容。
|
| 26 |
+
|
| 27 |
+
希望本文能够成为你入门和掌握 YOLOv6 的核心文档。由于 YOLOv6 本身也在不断迭代更新,我们也会不断的更新本文档。请注意阅读最新版本。
|
| 28 |
+
|
| 29 |
+
MMYOLO 实现配置:https://github.com/open-mmlab/mmyolo/blob/main/configs/yolov6/
|
| 30 |
+
|
| 31 |
+
YOLOv6 官方开源库地址:https://github.com/meituan/YOLOv6
|
| 32 |
+
|
| 33 |
+
## 1 YOLOv6 2.0 算法原理和 MMYOLO 实现解析
|
| 34 |
+
|
| 35 |
+
YOLOv6 2.0 官方 release 地址:https://github.com/meituan/YOLOv6/releases/tag/0.2.0
|
| 36 |
+
|
| 37 |
+
<div align=center >
|
| 38 |
+
<img alt="YOLOv6精度图" src="https://github.com/meituan/YOLOv6/blob/main/assets/speed_comparision_v2.png"/>
|
| 39 |
+
</div>
|
| 40 |
+
|
| 41 |
+
<div align=center >
|
| 42 |
+
<img alt="YOLOv6精度速度图" src="https://user-images.githubusercontent.com/25873202/201611723-0d3d02be-d778-4bdd-8010-fbcb9df8740e.png"/>
|
| 43 |
+
</div>
|
| 44 |
+
|
| 45 |
+
YOLOv6 和 YOLOv5 一样也可以分成数据增强、模型结构、loss 计算等组件,如下所示:
|
| 46 |
+
|
| 47 |
+
<div align=center >
|
| 48 |
+
<img alt="训练测试策略" src="https://user-images.githubusercontent.com/40284075/190542423-f6b20d8e-c82a-4a34-9065-c161c5e29e7c.png"/>
|
| 49 |
+
</div>
|
| 50 |
+
|
| 51 |
+
下面将从原理和结合 MMYOLO 的具体实现方面进行简要分析。
|
| 52 |
+
|
| 53 |
+
### 1.1 数据增强模块
|
| 54 |
+
|
| 55 |
+
YOLOv6 目标检测算法中使用的数据增强与 YOLOv5 基本一致,唯独不一样的是没有使用 Albu 的数据增强方式:
|
| 56 |
+
|
| 57 |
+
- **Mosaic 马赛克**
|
| 58 |
+
- **RandomAffine 随机仿射变换**
|
| 59 |
+
- **MixUp**
|
| 60 |
+
- ~~**图像模糊等采用 Albu 库实现的变换**~~
|
| 61 |
+
- **HSV 颜色空间增强**
|
| 62 |
+
- **随机水平翻转**
|
| 63 |
+
|
| 64 |
+
关于每一个增强的详细解释,详情请看 [YOLOv5 数据增强模块](yolov5_description.md)
|
| 65 |
+
|
| 66 |
+
另外,YOLOv6 参考了 YOLOX 的数据增强方式,分为 2 种增强方法组,一开始和 YOLOv5 一致,但是在最后 15 个 epoch 的时候将 `Mosaic` 使用 `YOLOv5KeepRatioResize` + `LetterResize` 替代了,个人感觉是为了拟合真实情况。
|
| 67 |
+
|
| 68 |
+
### 1.2 网络结构
|
| 69 |
+
|
| 70 |
+
YOLOv6 N/T/S 模型的网络结构由 `EfficientRep` + `Rep-PAN` + `Efficient decoupled Head` 构成,M/L 模型的网络结构则由 `CSPBep` + `CSPRepPAFPN` + `Efficient decoupled Head` 构成。其中,Backbone 和 Neck 部分的结构与 YOLOv5 较为相似,但不同的是其采用了重参数化结构 `RepVGG Block` 替换掉了原本的 `ConvModule`,在此基础上,将 `CSPLayer` 改进为了多个 `RepVGG` 堆叠的 `RepStageBlock`(N/T/S 模型)或 `BepC3StageBlock`(M/L 模型);Head 部分则参考了 FCOS 和 YOLOX 的检测头,将回归与分类分支解耦成两个分支进行预测。YOLOv6-S 和 YOLOv6-L 整体结构分别如图 1 和图 2 所示。
|
| 71 |
+
|
| 72 |
+
#### 1.2.1 Backbone
|
| 73 |
+
|
| 74 |
+
已有研究表明,多分支的网络结构通常比单分支网络性能更加优异,例如 YOLOv5 的 `CSPDarknet`,但是这种结构会导致并行度降低进而增加推理延时;相反,类似于 `VGG` 的单分支网络则具有并行度高、内存占用小的优点,因此推理效率更高。而 `RepVGG` 则同时具备上述两种结构的优点,在训练时可解耦成多分支拓扑结构提升模型精度,实际部署时可等效融合为单个 3×3 卷积提升推理速度,`RepVGG` 示意图如下。因此,YOLOv6 基于 `RepVGG` 重参数化结构设计了高效的骨干网络 `EfficientRep` 和 `CSPBep`,其可以充分利用硬件算力,提升模型表征能力的同时降低推理延时。
|
| 75 |
+
|
| 76 |
+
<img src="https://user-images.githubusercontent.com/58845482/209788313-05e3870b-9b25-4dbb-89c8-7c9502c84577.png" alt="image" style="zoom: 40%;" />
|
| 77 |
+
|
| 78 |
+
在 N/T/S 模型中,YOLOv6 使用了 `EfficientRep` 作为骨干网络,其包含 1 个 `Stem Layer` 和 4 个 `Stage Layer`,具体细节如下:
|
| 79 |
+
|
| 80 |
+
- `Stem Layer` 中采用 stride=2 的 `RepVGGBlock` 替换了 stride=2 的 6×6 `ConvModule`。
|
| 81 |
+
- `Stage Layer` 结构与 YOLOv5 基本相似,将每个 `Stage layer` 的 1 个 `ConvModule` 和 1 个 `CSPLayer` 分别替换为 1 个 `RepVGGBlock` 和 1 个 `RepStageBlock`,如上图 Details 部分所示。其中,第一个 `RepVGGBlock` 会做下采样和 `Channel` 维度变换,而每个 `RepStageBlock` 则由 n 个 `RepVGGBlock` 组成。此外,仍然在第 4 个 `Stage Layer` 最后增加 `SPPF` 模块后输出。
|
| 82 |
+
|
| 83 |
+
在 M/L 模型中,由于模型容量进一步增大,直接使用多个 `RepVGGBlock` 堆叠的 `RepStageBlock` 结构计算量和参数量呈现指数增长。因此,为了权衡计算负担和模型精度,在 M/L 模型中使用了 `CSPBep` 骨干网络,其采用 `BepC3StageBlock` 替换了小模型中的 `RepStageBlock` 。如下图所示,`BepC3StageBlock` 由 3 个 1×1 的 `ConvModule` 和多个子块(每个子块由两个 `RepVGGBlock` 残差连接)组成。
|
| 84 |
+
|
| 85 |
+
<img src="https://user-images.githubusercontent.com/58845482/208235469-a85865a5-5d15-435d-bb74-0be6f56dd03f.png" alt="image" style="zoom: 67%;" />
|
| 86 |
+
|
| 87 |
+
#### 1.2.2 Neck
|
| 88 |
+
|
| 89 |
+
Neck 部分结构仍然在 YOLOv5 基础上进行了模块的改动,同样采用 `RepStageBlock` 或 `BepC3StageBlock` 对原本的 `CSPLayer` 进行了替换,需要注意的是,Neck 中 `Down Sample` 部分仍然使用了 stride=2 的 3×3 `ConvModule`,而不是像 Backbone 一样替换为 `RepVGGBlock`。
|
| 90 |
+
|
| 91 |
+
#### 1.2.3 Head
|
| 92 |
+
|
| 93 |
+
不同于传统的 YOLO 系列检测头,YOLOv6 参考了 FCOS 和 YOLOX 中的做法,将分类和回归分支解耦成两个分支进行预测并且去掉了 obj 分支。同时,采用了 hybrid-channel 策略构建了更高效的解耦检测头,将中间 3×3 的 `ConvModule` 减少为 1 个,在维持精度的同时进一步减少了模型耗费,降低了推理延时。此外,需要说明的是,YOLOv6 在 Backobone 和 Neck 部分使用的激活函数是 `ReLU`,而在 Head 部分则使用的是 `SiLU`。
|
| 94 |
+
|
| 95 |
+
由于 YOLOv6 是解耦输出,分类和 bbox 检测通过不同卷积完成。以 COCO 80 类为例:
|
| 96 |
+
|
| 97 |
+
- P5 模型在输入为 640x640 分辨率情况下,其 Head 模块输出的 shape 分别为 `(B,4,80,80)`, `(B,80,80,80)`, `(B,4,40,40)`, `(B,80,40,40)`, `(B,4,20,20)`, `(B,80,20,20)`。
|
| 98 |
+
|
| 99 |
+
### 1.3 正负样本匹配策略
|
| 100 |
+
|
| 101 |
+
YOLOv6 采用的标签匹配策略与 [TOOD](https://arxiv.org/abs/2108.07755)
|
| 102 |
+
相同, 前 4 个 epoch 采用 `ATSSAssigner` 作为标签匹配策略的 `warm-up` ,
|
| 103 |
+
后续使用 `TaskAlignedAssigner` 算法选择正负样本, 基于官方开源代码, `MMYOLO` 中也对两个 assigner 算法进行了优化, 改进为 `Batch` 维度进行计算,
|
| 104 |
+
能够一定程度的加快速度。 下面会对每个部分进行详细说明。
|
| 105 |
+
|
| 106 |
+
#### 1.3.1 Anchor 设置
|
| 107 |
+
|
| 108 |
+
YOLOv6 采用与 YOLOX 一样的 Anchor-free 无锚范式,省略了聚类和繁琐的 Anchor 超参设定,泛化能力强,解码逻辑简单。在训练的过程中会根据 feature size 去自动生成先验框。
|
| 109 |
+
|
| 110 |
+
使用 `mmdet.MlvlPointGenerator` 生成 anchor points。
|
| 111 |
+
|
| 112 |
+
```python
|
| 113 |
+
prior_generator: ConfigType = dict(
|
| 114 |
+
type='mmdet.MlvlPointGenerator',
|
| 115 |
+
offset=0.5, # 网格中心点
|
| 116 |
+
strides=[8, 16, 32]) ,
|
| 117 |
+
|
| 118 |
+
# 调用生成多层 anchor points: list[torch.Tensor]
|
| 119 |
+
# 每一层都是 (featrue_h*feature_w,4), 4 表示 (x,y,stride_h,stride_w)
|
| 120 |
+
self.mlvl_priors = self.prior_generator.grid_priors(
|
| 121 |
+
self.featmap_sizes,
|
| 122 |
+
with_stride=True)
|
| 123 |
+
```
|
| 124 |
+
|
| 125 |
+
#### 1.3.2 Bbox 编解码过程
|
| 126 |
+
|
| 127 |
+
YOLOv6 的 BBox Coder 采用的是 `DistancePointBBoxCoder`。
|
| 128 |
+
|
| 129 |
+
网络 bbox 预测的值为 (top, bottom, left, right),解码器将 `anchor point` 通过四个距离解码到坐标 (x1,y1,x2,y2)。
|
| 130 |
+
|
| 131 |
+
MMYOLO 中解码的核心源码:
|
| 132 |
+
|
| 133 |
+
```python
|
| 134 |
+
def decode(points: torch.Tensor, pred_bboxes: torch.Tensor, stride: torch.Tensor) -> torch.Tensor:
|
| 135 |
+
"""
|
| 136 |
+
将预测值解码转化 bbox 的 xyxy
|
| 137 |
+
points (Tensor): 生成的 anchor point [x, y],Shape (B, N, 2) or (N, 2).
|
| 138 |
+
pred_bboxes (Tensor): 预测距离四边的距离。(left, top, right, bottom). Shape (B, N, 4) or (N, 4)
|
| 139 |
+
stride (Tensor): 特征图下采样倍率.
|
| 140 |
+
"""
|
| 141 |
+
# 首先将预测值转化为原图尺度
|
| 142 |
+
distance = pred_bboxes * stride[None, :, None]
|
| 143 |
+
# 根据点以及到四条边距离转为 bbox 的 x1y1x2y2
|
| 144 |
+
x1 = points[..., 0] - distance[..., 0]
|
| 145 |
+
y1 = points[..., 1] - distance[..., 1]
|
| 146 |
+
x2 = points[..., 0] + distance[..., 2]
|
| 147 |
+
y2 = points[..., 1] + distance[..., 3]
|
| 148 |
+
|
| 149 |
+
bboxes = torch.stack([x1, y1, x2, y2], -1)
|
| 150 |
+
|
| 151 |
+
return bboxes
|
| 152 |
+
```
|
| 153 |
+
|
| 154 |
+
#### 1.3.3 匹配策略
|
| 155 |
+
|
| 156 |
+
- 0 \<= epoch \< 4,使用 `BatchATSSAssigner`
|
| 157 |
+
- epoch >= 4,使用 `BatchTaskAlignedAssigner`
|
| 158 |
+
|
| 159 |
+
#### ATSSAssigner
|
| 160 |
+
|
| 161 |
+
ATSSAssigner 是 [ATSS](https://arxiv.org/abs/1912.02424) 中提出的标签匹配策略。
|
| 162 |
+
ATSS 的匹配策略简单总结为:**通过中心点距离先验对样本进行初筛,然后自适应生成 IoU 阈值筛选正样本。**
|
| 163 |
+
YOLOv6 的实现种主要包括如下三个核心步骤:
|
| 164 |
+
|
| 165 |
+
1. 因为 YOLOv6 是 Anchor-free,所以首先将 `anchor point` 转化为大小为 `5*strdie` 的 `anchor`。
|
| 166 |
+
2. 对于每一个 `GT`,在 `FPN` 的每一个特征层上, 计算与该层所有 `anchor` 中心点距离(位置先验),
|
| 167 |
+
然后优先选取距离 `topK` 近的样本,作为 **初筛样本**。
|
| 168 |
+
3. 对于每一个 `GT`,计算其 **初筛样本** 的 `IoU` 的均值 `mean`与标准差 `std`,将 `mean + std`
|
| 169 |
+
作为该 `GT` 的正样本的 **自适应 IoU 阈值** ,大于该 **自适应阈值** 且中心点在 `GT` 内部的 `anchor`
|
| 170 |
+
才作为正样本,使得样本能够被 `assign` 到合适的 `FPN` 特征层上。
|
| 171 |
+
|
| 172 |
+
下图中,(a) 所示中等大小物体被 assign 到 FPN 的中层,(b) 所示偏大的物体被 assign 到 FPN 中检测大物体和偏大物体的两个层。
|
| 173 |
+
|
| 174 |
+
<div align=center>
|
| 175 |
+
<img src="https://user-images.githubusercontent.com/52028100/204796892-6df4fb85-e2cf-4d84-b85f-d8e8efccc644.png" alt="image"/>
|
| 176 |
+
</div>
|
| 177 |
+
|
| 178 |
+
```python
|
| 179 |
+
# 1. 首先将anchor points 转化为 anchors
|
| 180 |
+
# priors为(point_x,point_y,stride_w,stride_h), shape 为(N,4)
|
| 181 |
+
cell_half_size = priors[:, 2:] * 2.5
|
| 182 |
+
priors_gen = torch.zeros_like(priors)
|
| 183 |
+
priors_gen[:, :2] = priors[:, :2] - cell_half_size
|
| 184 |
+
priors_gen[:, 2:] = priors[:, :2] + cell_half_size
|
| 185 |
+
priors = priors_gen
|
| 186 |
+
# 2. 计算 anchors 与 GT 的 IoU
|
| 187 |
+
overlaps = self.iou_calculator(gt_bboxes.reshape([-1, 4]), priors)
|
| 188 |
+
# 3. 计算 anchor 与 GT 的中心距离
|
| 189 |
+
distances, priors_points = bbox_center_distance(
|
| 190 |
+
gt_bboxes.reshape([-1, 4]), priors)
|
| 191 |
+
# 4. 根据中心点距离,在 FPN 的每一层选取 TopK 临近的样本作为初筛样本
|
| 192 |
+
is_in_candidate, candidate_idxs = self.select_topk_candidates(
|
| 193 |
+
distances, num_level_priors, pad_bbox_flag)
|
| 194 |
+
# 5. 对于每一个 GT 计算其对应初筛样本的均值与标准差的和, 作为该GT的样本阈值
|
| 195 |
+
overlaps_thr_per_gt, iou_candidates = self.threshold_calculator(
|
| 196 |
+
is_in_candidate, candidate_idxs, overlaps, num_priors, batch_size,
|
| 197 |
+
num_gt)
|
| 198 |
+
# 6. 筛选大于阈值的样本作为正样本
|
| 199 |
+
is_pos = torch.where(
|
| 200 |
+
iou_candidates > overlaps_thr_per_gt.repeat([1, 1, num_priors]),
|
| 201 |
+
is_in_candidate, torch.zeros_like(is_in_candidate))
|
| 202 |
+
# 6. 保证样本中心点在 GT 内部且不超图像边界
|
| 203 |
+
pos_mask = is_pos * is_in_gts * pad_bbox_flag
|
| 204 |
+
```
|
| 205 |
+
|
| 206 |
+
#### TaskAlignedAssigner
|
| 207 |
+
|
| 208 |
+
TaskAlignedAssigner 是 [TOOD](https://arxiv.org/abs/2108.07755) 中提出的一种动态样本匹配策略。
|
| 209 |
+
由于 `ATSSAssigner` 是属于静态标签匹配策略,其选取正样本的策略主要根据 `anchor` 的位置进行挑选,
|
| 210 |
+
并不会随着网络的优化而选取到更好的样本。在目标检测中,分类和回归的任务最终作用于同一个目标,所以
|
| 211 |
+
`TaskAlignedAssigner` 认为样本的选取应该更加关注到对分类以及回归都友好的样本点。
|
| 212 |
+
|
| 213 |
+
`TaskAlignedAssigner` 的匹配策略简单总结为: **根据分类与回归的分数加权的分数选择正样本**。
|
| 214 |
+
|
| 215 |
+
1. 对于每一个 `GT`,对所有的 `预测框` 基于 **GT类别对应分类分数** 与 **预测框与 GT 的 IoU** 的加权得到一个关联分类以及回归的对齐分数 `alignment_metrics`。
|
| 216 |
+
2. 对于每一个 `GT`,直接基于 `alignment_metrics` 对齐分数选取 `topK` 大的作为正样本。
|
| 217 |
+
|
| 218 |
+
因为在网络初期参数随机, `分类分数` 和 `预测框与 GT 的 IoU` 都不准确,所以需要经过前 4 个 `epoch` 的 `ATSSAssigner`
|
| 219 |
+
的 `warm-up`。经过预热之后的 `TaskAlignedAssigner` 标签匹配策略就不使用中心距离的先验,
|
| 220 |
+
而是直接对每一个`GT` 选取 `alignment_metrics` 中 `topK` 大的样本作为正样本。
|
| 221 |
+
|
| 222 |
+
```python
|
| 223 |
+
# 1. 基于分类分数与回归的 IoU 计算对齐分数 alignment_metrics
|
| 224 |
+
alignment_metrics = bbox_scores.pow(self.alpha) * overlaps.pow(
|
| 225 |
+
self.beta)
|
| 226 |
+
# 2. 保证中心点在 GT 内部的 mask
|
| 227 |
+
is_in_gts = select_candidates_in_gts(priors, gt_bboxes)
|
| 228 |
+
# 3. 选取 TopK 大的对齐分数的样本
|
| 229 |
+
topk_metric = self.select_topk_candidates(
|
| 230 |
+
alignment_metrics * is_in_gts,
|
| 231 |
+
topk_mask=pad_bbox_flag.repeat([1, 1, self.topk]).bool())
|
| 232 |
+
```
|
| 233 |
+
|
| 234 |
+
### 1.4 Loss 设计
|
| 235 |
+
|
| 236 |
+
参与 Loss 计算的共有两个值:loss_cls 和 loss_bbox,其各自使用的 Loss 方法如下:
|
| 237 |
+
|
| 238 |
+
- Classes loss:使用的是 `mmdet.VarifocalLoss`
|
| 239 |
+
- BBox loss:l/m/s使用的是 `GIoULoss`, t/n 用的是 `SIoULoss`
|
| 240 |
+
|
| 241 |
+
权重比例是:`loss_cls` : `loss_bbox` = `1 : 2.5`
|
| 242 |
+
|
| 243 |
+
#### 分类损失函数 VarifocalLoss
|
| 244 |
+
|
| 245 |
+
Varifocal Loss (VFL) 是 [VarifocalNet: An IoU-aware Dense Object Detector](https://arxiv.org/abs/2008.13367)
|
| 246 |
+
中的损失函数。
|
| 247 |
+
|
| 248 |
+
<div align=center>
|
| 249 |
+
<img src="https://user-images.githubusercontent.com/52028100/204796751-3b1ed7d7-9185-4894-9832-147f84220cdf.png" alt="image"/>
|
| 250 |
+
</div>
|
| 251 |
+
|
| 252 |
+
`VFL` 是在 `GFL` 的基础上做的改进,`GFL`详情请看 [GFL详解](rtmdet_description.md)
|
| 253 |
+
|
| 254 |
+
在上述标签匹配策略中提到过选择样本应该优先考虑分类回归都友好的样本点,
|
| 255 |
+
这是由于目标检测包含的分类与回归两个子任务都是作用于同一个物体。
|
| 256 |
+
与 `GFL` 思想相同,都是将 **预测框与 `GT` 的 `IoU` 软化作为分类的标签**,使得分类分数关联回归质量,
|
| 257 |
+
使其在后处理 NMS 阶段有**分类回归一致性很强的分值排序策略**,以达到选取优秀预测框的目的。
|
| 258 |
+
|
| 259 |
+
Varifocal Loss 原本的公式:
|
| 260 |
+
|
| 261 |
+
```{math}
|
| 262 |
+
{VFL}(p,q)= \begin{cases}
|
| 263 |
+
-q(qlog(p) +(1-q)log(1-p)), & q > 0 \\
|
| 264 |
+
-\alpha p^\gamma log(1-p), & q = 0
|
| 265 |
+
\end{cases}
|
| 266 |
+
```
|
| 267 |
+
|
| 268 |
+
其中 {math}`q` 是预测 `bboxes` 与 `GT` 的 `IoU`,使用软标签的形式作为分类的标签。
|
| 269 |
+
{math}`p\in[0,1]` 表示分类分数。
|
| 270 |
+
|
| 271 |
+
1. 对于负样本,即当 {math}`q = 0` 时,标准交叉熵部分为 {math}`-\log(p)`,负样本权重使用 {math}`\alpha p^\gamma` 作为 `focal weight`
|
| 272 |
+
使样本聚焦与困难样本上,这与 `Focal Loss` 基本一致。
|
| 273 |
+
2. 对于正样本,即当 {math}`q > 0` 时,首先计算标准二值交叉熵部分 {math}`-(qlog(p) +(1-q)log(1-p))`,
|
| 274 |
+
但是针对正样本的权重设置,`Varifocal Loss` 中并没有采用类似 {math}`\alpha p^\gamma`的方式降权,
|
| 275 |
+
而是认为在网络的学习过程中正样本相对于负样本的学习信号来说更为重要,所以使用了分类的标签 {math}`q`,
|
| 276 |
+
即 `IoU` 作为 `focal weight`, 使得聚焦到具有高质量的样本上。
|
| 277 |
+
|
| 278 |
+
但是 YOLOv6 中的 Varifocal Loss 公式采用 `TOOD` 中的 `Task ALignment Learning (TAL)`,
|
| 279 |
+
将预测的 `IoU` 根据之前标签匹配策略中的分类对齐度 `alignment_metrics` 进行了归一化,
|
| 280 |
+
得到归一化 {math}`\hat{t}`。
|
| 281 |
+
具体实现方式为:
|
| 282 |
+
|
| 283 |
+
对于每一个 `Gt`,找到所有样本中与 `Gt` 最大的 `IoU`,具有最大 `alignment_metrics` 的样本位置的 {math}`\hat{t} = max(Iou)`
|
| 284 |
+
|
| 285 |
+
```{math}
|
| 286 |
+
\hat{t} = AlignmentMetrics / max(AlignmentMetrics) * max(IoU)
|
| 287 |
+
```
|
| 288 |
+
|
| 289 |
+
最终 YOLOv6 分类损失损失函数为:
|
| 290 |
+
|
| 291 |
+
```{math}
|
| 292 |
+
{VFL}(p,\hat{t})= \begin{cases}
|
| 293 |
+
-\hat{t}(\hat{t}log(p) +(1-\hat{t})log(1-p)), & \hat{t} > 0 \\
|
| 294 |
+
-\alpha p^\gamma log(1-p), & \hat{t} = 0
|
| 295 |
+
\end{cases}
|
| 296 |
+
```
|
| 297 |
+
|
| 298 |
+
MMDetection 实现源码的核心部分:
|
| 299 |
+
|
| 300 |
+
```python
|
| 301 |
+
def varifocal_loss(pred, target, alpha=0.75, gamma=2.0, iou_weighted=True):
|
| 302 |
+
"""
|
| 303 |
+
pred (torch.Tensor): 预测的分类分数,形状为 (B,N,C) , N 表示 anchor 数量, C 表示类别数
|
| 304 |
+
target (torch.Tensor): 经过对齐度归一化后的 IoU 分数,形状为 (B,N,C),数值范围为 0~1
|
| 305 |
+
alpha (float, optional): 调节正负样本之间的平衡因子,默认 0.75.
|
| 306 |
+
gamma (float, optional): 负样本 focal 权重因子, 默认 2.0.
|
| 307 |
+
iou_weighted (bool, optional): 正样本是否用 IoU 加权
|
| 308 |
+
"""
|
| 309 |
+
pred_sigmoid = pred.sigmoid()
|
| 310 |
+
target = target.type_as(pred)
|
| 311 |
+
if iou_weighted:
|
| 312 |
+
# 计算权重,正样本(target > 0)中权重为 target,
|
| 313 |
+
# 负样本权重为 alpha*pred_simogid^2
|
| 314 |
+
focal_weight = target * (target > 0.0).float() + \
|
| 315 |
+
alpha * (pred_sigmoid - target).abs().pow(gamma) * \
|
| 316 |
+
(target <= 0.0).float()
|
| 317 |
+
else:
|
| 318 |
+
focal_weight = (target > 0.0).float() + \
|
| 319 |
+
alpha * (pred_sigmoid - target).abs().pow(gamma) * \
|
| 320 |
+
(target <= 0.0).float()
|
| 321 |
+
# 计算二值交叉熵后乘以权重
|
| 322 |
+
loss = F.binary_cross_entropy_with_logits(
|
| 323 |
+
pred, target, reduction='none') * focal_weight
|
| 324 |
+
loss = weight_reduce_loss(loss, weight, reduction, avg_factor)
|
| 325 |
+
return loss
|
| 326 |
+
```
|
| 327 |
+
|
| 328 |
+
#### 回归损失函数 GIoU Loss / SIoU Loss
|
| 329 |
+
|
| 330 |
+
在 YOLOv6 中,针对不同大小的模型采用了不同的回归损失函数,其中 l/m/s使用的是 `GIoULoss`, t/n 用的是 `SIoULoss`。
|
| 331 |
+
|
| 332 |
+
其中` GIoULoss` 详情请看 [GIoU详解](rtmdet_description.md)。
|
| 333 |
+
|
| 334 |
+
##### SIou Loss
|
| 335 |
+
|
| 336 |
+
SIoU 损失函数是 [SIoU Loss: More Powerful Learning for Bounding Box Regression](https://arxiv.org/pdf/2205.12740.pdf)
|
| 337 |
+
中提出的度量预测框与 `GT` 的匹配度的指标,由于之前的`GIoU`, `CIoU`, `DIoU` 都没有考虑预测框向 `GT`
|
| 338 |
+
框回归的角度,然而角度也确实是回归中一个重要的影响因素,因此提出了全新的`SIoU`。
|
| 339 |
+
|
| 340 |
+
SIoU 损失主要由四个度量方面组成:
|
| 341 |
+
|
| 342 |
+
- IoU成本
|
| 343 |
+
- 角度成本
|
| 344 |
+
- 距离成本
|
| 345 |
+
- 形状成本
|
| 346 |
+
|
| 347 |
+
如下图所示,**角度成本** 就是指图中预测框 {math}`B` 向 {math}`B^{GT}` 的回归过程中,
|
| 348 |
+
尽可能去使得优化过程中的不确定性因素减少,比如现将图中的角度 {math}`\alpha` 或者 {math}`\beta`
|
| 349 |
+
变为 0 ,再去沿着 `x` 轴或者 `y` 轴去回归边界。
|
| 350 |
+
|
| 351 |
+
<div align=center>
|
| 352 |
+
<img src="https://user-images.githubusercontent.com/52028100/207532021-6a4660bf-be94-4c21-a608-a44fc3b3ccb8.png" alt="image"/>
|
| 353 |
+
</div>
|
| 354 |
+
|
| 355 |
+
MMYOLO 实现源码的核心部分:
|
| 356 |
+
|
| 357 |
+
```python
|
| 358 |
+
|
| 359 |
+
def bbox_overlaps(bboxes1, bboxes2, mode='siou', is_aligned=False, eps=1e-6):
|
| 360 |
+
# 两个box的顶点x1,y1,x2,y2
|
| 361 |
+
bbox1_x1, bbox1_y1 = pred[:, 0], pred[:, 1]
|
| 362 |
+
bbox1_x2, bbox1_y2 = pred[:, 2], pred[:, 3]
|
| 363 |
+
bbox2_x1, bbox2_y1 = target[:, 0], target[:, 1]
|
| 364 |
+
bbox2_x2, bbox2_y2 = target[:, 2], target[:, 3]
|
| 365 |
+
# 交集
|
| 366 |
+
overlap = (torch.min(bbox1_x2, bbox2_x2) -
|
| 367 |
+
torch.max(bbox1_x1, bbox2_x1)).clamp(0) * \
|
| 368 |
+
(torch.min(bbox1_y2, bbox2_y2) -
|
| 369 |
+
torch.max(bbox1_y1, bbox2_y1)).clamp(0)
|
| 370 |
+
# 并集
|
| 371 |
+
w1, h1 = bbox1_x2 - bbox1_x1, bbox1_y2 - bbox1_y1
|
| 372 |
+
w2, h2 = bbox2_x2 - bbox2_x1, bbox2_y2 - bbox2_y1
|
| 373 |
+
union = (w1 * h1) + (w2 * h2) - overlap + eps
|
| 374 |
+
# IoU = 交集/并集
|
| 375 |
+
ious = overlap / union
|
| 376 |
+
# 最小外界矩的宽高
|
| 377 |
+
enclose_x1y1 = torch.min(pred[:, :2], target[:, :2])
|
| 378 |
+
enclose_x2y2 = torch.max(pred[:, 2:], target[:, 2:])
|
| 379 |
+
enclose_wh = (enclose_x2y2 - enclose_x1y1).clamp(min=0)
|
| 380 |
+
enclose_w = enclose_wh[:, 0] # enclose_w
|
| 381 |
+
enclose_h = enclose_wh[:, 1] # enclose_h
|
| 382 |
+
elif iou_mode == 'siou':
|
| 383 |
+
# 1.计算 σ (两个box中心点距离):
|
| 384 |
+
# sigma_cw,sigma_ch:上图中cw,ch
|
| 385 |
+
sigma_cw = (bbox2_x1 + bbox2_x2) / 2 - (bbox1_x1 + bbox1_x2) / 2 + eps
|
| 386 |
+
sigma_ch = (bbox2_y1 + bbox2_y2) / 2 - (bbox1_y1 + bbox1_y2) / 2 + eps
|
| 387 |
+
sigma = torch.pow(sigma_cw**2 + sigma_ch**2, 0.5)
|
| 388 |
+
|
| 389 |
+
# 2. 在 α 和 β 中选择一个小的角度(小于π/4)去优化
|
| 390 |
+
sin_alpha = torch.abs(sigma_ch) / sigma
|
| 391 |
+
sin_beta = torch.abs(sigma_cw) / sigma
|
| 392 |
+
sin_alpha = torch.where(sin_alpha <= math.sin(math.pi / 4), sin_alpha,
|
| 393 |
+
sin_beta)
|
| 394 |
+
|
| 395 |
+
# 角度损失 = 1 - 2 * ( sin^2 ( arcsin(x) - (π / 4) ) ) = cos(2α-π/2) = sin(2α)
|
| 396 |
+
# 这里就是角度损失,当 α=0 或者 α=90° 时损失为 0, 当 α=45° 损失为 1
|
| 397 |
+
angle_cost = torch.cos(torch.arcsin(sin_alpha) * 2 - math.pi / 2)
|
| 398 |
+
|
| 399 |
+
# 3.这里将角度损失与距离损失进行融合
|
| 400 |
+
# Distance cost = Σ_(t=x,y) (1 - e ^ (- γ ρ_t))
|
| 401 |
+
rho_x = (sigma_cw / enclose_w)**2 # ρ_x:x轴中心点距离距离损失
|
| 402 |
+
rho_y = (sigma_ch / enclose_h)**2 # ρ_y:y轴中心点距离距离损失
|
| 403 |
+
gamma = 2 - angle_cost # γ
|
| 404 |
+
# 当 α=0, angle_cost=0, gamma=2, dis_cost_x = 1 - e ^ (-2 p_x),因为 ρ_x>0, 主要优化距离
|
| 405 |
+
# 当 α=45°,angle_cost=1, gamma=1, dis_cost_x = 1 - e ^ (-1* p_x),因为 ρ_x<1, 主要优化角度
|
| 406 |
+
distance_cost = (1 - torch.exp(-1 * gamma * rho_x)) + (
|
| 407 |
+
1 - torch.exp(-1 * gamma * rho_y))
|
| 408 |
+
|
| 409 |
+
# 4.形状损失 就是两个box之间的宽高比
|
| 410 |
+
# Shape cost = Ω = Σ_(t=w,h) ( ( 1 - ( e ^ (-ω_t) ) ) ^ θ )
|
| 411 |
+
omiga_w = torch.abs(w1 - w2) / torch.max(w1, w2) # ω_w
|
| 412 |
+
omiga_h = torch.abs(h1 - h2) / torch.max(h1, h2) # ω_h
|
| 413 |
+
shape_cost = torch.pow(1 - torch.exp(-1 * omiga_w),
|
| 414 |
+
siou_theta) + torch.pow(
|
| 415 |
+
1 - torch.exp(-1 * omiga_h), siou_theta)
|
| 416 |
+
|
| 417 |
+
# 5.综合 IoU、角度、距离以及形状信息
|
| 418 |
+
# SIoU = IoU - ( (Distance Cost + Shape Cost) / 2 )
|
| 419 |
+
ious = ious - ((distance_cost + shape_cost) * 0.5)
|
| 420 |
+
|
| 421 |
+
return ious.clamp(min=-1.0, max=1.0)
|
| 422 |
+
|
| 423 |
+
@weighted_loss
|
| 424 |
+
def siou_loss(pred, target, eps=1e-7):
|
| 425 |
+
sious = bbox_overlaps(pred, target, mode='siou', is_aligned=True, eps=eps)
|
| 426 |
+
loss = 1 - sious
|
| 427 |
+
return loss
|
| 428 |
+
```
|
| 429 |
+
|
| 430 |
+
#### Object Loss
|
| 431 |
+
|
| 432 |
+
在 YOLOv6 中,由于额外的置信度预测头可能与 `Aligned Head` 有所冲突,经实验验证在不同大小的模型上也都有掉点,
|
| 433 |
+
所以最后选择弃用 `Objectness` 分支。
|
| 434 |
+
|
| 435 |
+
### 1.5 优化策略和训练过程
|
| 436 |
+
|
| 437 |
+
#### 1.5.1 优化器分组
|
| 438 |
+
|
| 439 |
+
与 YOLOv5 一致,详情请看 [YOLOv5 优化器分组](yolov5_description.md)
|
| 440 |
+
|
| 441 |
+
#### 1.5.2 weight decay 参数自适应
|
| 442 |
+
|
| 443 |
+
与 YOLOv5 一致,详情请看 [YOLOv5 weight decay 参数自适应](yolov5_description.md)
|
| 444 |
+
|
| 445 |
+
### 1.6 推理和后处理过程
|
| 446 |
+
|
| 447 |
+
YOLOv6 后处理过程和 YOLOv5 高度类似,实际上 YOLO 系列的后处理逻辑都是类似的。
|
| 448 |
+
详情请看 [YOLOv5 推理和后处理过程](yolov5_description.md)
|
| 449 |
+
|
| 450 |
+
## 2 总结
|
| 451 |
+
|
| 452 |
+
本文对 YOLOv6 原理和在 MMYOLO 实现进行了详细解析,希望能帮助用户理解算法实现过程。同时请注意:由于 YOLOv6 本身也在不断更新,本开源库也会不断迭代,请及时阅读和同步最新版本。
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/algorithm_descriptions/yolov8_description.md
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
| 1 |
+
# YOLOv8 原理和实现全解析
|
| 2 |
+
|
| 3 |
+
## 0 简介
|
| 4 |
+
|
| 5 |
+
<div align=center >
|
| 6 |
+
<img alt="YOLOv8-P5_structure" src="https://user-images.githubusercontent.com/27466624/222869864-1955f054-aa6d-4a80-aed3-92f30af28849.jpg"/>
|
| 7 |
+
图 1:YOLOv8-P5 模型结构
|
| 8 |
+
</div>
|
| 9 |
+
|
| 10 |
+
以上结构图由 RangeKing@github 绘制。
|
| 11 |
+
|
| 12 |
+
YOLOv8 是 Ultralytics 公司在 2023 年 1月 10 号开源的 YOLOv5 的下一个重大更新版本,目前支持图像分类、物体检测和实例分割任务,在还没有开源时就收到了用户的广泛关注。
|
| 13 |
+
|
| 14 |
+
按照官方描述,YOLOv8 是一个 SOTA 模型,它建立在以前 YOLO 版本的成功基础上,并引入了新的功能和改进,以进一步提升性能和灵活性。具体创新包括一个新的骨干网络、一个新的 Ancher-Free 检测头和一个新的损失函数,可以在从 CPU 到 GPU 的各种硬件平台上运行。
|
| 15 |
+
不过 Ultralytics 并没有直接将开源库命名为 YOLOv8,而是直接使用 Ultralytics 这个词,原因是 Ultralytics 将这个库定位为算法框架,而非某一个特定算法,一个主要特点是可扩展性。其希望这个库不仅仅能够用于 YOLO 系列模型,而是能够支持非 YOLO 模型以及分类分割姿态估计等各类任务。
|
| 16 |
+
总而言之,Ultralytics 开源库的两个主要优点是:
|
| 17 |
+
|
| 18 |
+
- **融合众多当前 SOTA 技术于一体**
|
| 19 |
+
- **未来将支持其他 YOLO 系列以及 YOLO 之外的更多算法**
|
| 20 |
+
|
| 21 |
+
<div align=center >
|
| 22 |
+
<img alt="YOLOv8-table" src="https://user-images.githubusercontent.com/17425982/212007736-f592bc70-3959-4ff6-baf7-a93c7ad1d882.png"/>
|
| 23 |
+
图 2:YOLOv8 性能曲线
|
| 24 |
+
</div>
|
| 25 |
+
|
| 26 |
+
下表为官方在 COCO Val 2017 数据集上测试的 mAP、参数量和 FLOPs 结果。可以看出 YOLOv8 相比 YOLOv5 精度提升非常多,但是 N/S/M 模型相应的参数量和 FLOPs 都增加了不少,从上图也可以看出相比 YOLOV5 大部分模型推理速度变慢了。
|
| 27 |
+
|
| 28 |
+
| **模型** | **YOLOv5** | **params(M)** | **FLOPs@640 (B)** | **YOLOv8** | **params(M)** | **FLOPs@640 (B)** |
|
| 29 |
+
| -------- | ----------- | ------------- | ----------------- | ----------- | ------------- | ----------------- |
|
| 30 |
+
| n | 28.0(300e) | 1.9 | 4.5 | 37.3 (500e) | 3.2 | 8.7 |
|
| 31 |
+
| s | 37.4 (300e) | 7.2 | 16.5 | 44.9 (500e) | 11.2 | 28.6 |
|
| 32 |
+
| m | 45.4 (300e) | 21.2 | 49.0 | 50.2 (500e) | 25.9 | 78.9 |
|
| 33 |
+
| l | 49.0 (300e) | 46.5 | 109.1 | 52.9 (500e) | 43.7 | 165.2 |
|
| 34 |
+
| x | 50.7 (300e) | 86.7 | 205.7 | 53.9 (500e) | 68.2 | 257.8 |
|
| 35 |
+
|
| 36 |
+
额外提一句,现在各个 YOLO 系列改进算法都在 COCO 上面有明显性能提升,但是在自定义数据集上面的泛化性还没有得到广泛验证,至今依然听到不少关于 YOLOv5 泛化性能较优异的说法。**对各系列 YOLO 泛化性验证也是 MMYOLO 中一个特别关心和重点发力的方向。**
|
| 37 |
+
|
| 38 |
+
阅读本文前,如果你对 YOLOv5、YOLOv6 和 RTMDet 不熟悉,可以先看下如下文档:
|
| 39 |
+
|
| 40 |
+
1. [YOLOv5 原理和实现全解析](https://mmyolo.readthedocs.io/zh_CN/latest/algorithm_descriptions/yolov5_description.html)
|
| 41 |
+
2. [YOLOv6 原理和实现全解析](https://mmyolo.readthedocs.io/zh_CN/latest/algorithm_descriptions/yolov6_description.html)
|
| 42 |
+
3. [RTMDet 原理和实现全解析](https://mmyolo.readthedocs.io/zh_CN/latest/algorithm_descriptions/rtmdet_description.html)
|
| 43 |
+
|
| 44 |
+
## 1 YOLOv8 概述
|
| 45 |
+
|
| 46 |
+
YOLOv8 算法的核心特性和改动可以归结为如下:
|
| 47 |
+
|
| 48 |
+
1. **提供了一个全新的 SOTA 模型,包括 P5 640 和 P6 1280 分辨率的目标检测网络和基于 YOLACT 的实例分割模型。和 YOLOv5 一样,基于缩放系数也提供了 N/S/M/L/X 尺度的不同大小模型,用于满足不同场景需求**
|
| 49 |
+
2. **骨干网络和 Neck 部分可能参考了 YOLOv7 ELAN 设计思想,将 YOLOv5 的 C3 结构换成了梯度流更丰富的 C2f 结构,并对不同尺度模型调整了不同的通道数,属于对模型结构精心微调,不再是无脑一套参数应用所有模型,大幅提升了模型性能。不过这个 C2f 模块中存在 Split 等操作对特定硬件部署没有之前那么友好了**
|
| 50 |
+
3. **Head 部分相比 YOLOv5 改动较大,换成了目前主流的解耦头结构,将分类和检测头分离,同时也从 Anchor-Based 换成了 Anchor-Free**
|
| 51 |
+
4. **Loss 计算方面采用了 TaskAlignedAssigner 正样本分配策略,并引入了 Distribution Focal Loss**
|
| 52 |
+
5. **训练的数据增强部分引入了 YOLOX 中的最后 10 epoch 关闭 Mosiac 增强的操作,可以有效地提升精度**
|
| 53 |
+
|
| 54 |
+
从上面可以看出,YOLOv8 主要参考了最近提出的诸如 YOLOX、YOLOv6、YOLOv7 和 PPYOLOE 等算法的相关设计,本身的创新点不多,偏向工程实践,主推的还是 ultralytics 这个框架本身。
|
| 55 |
+
|
| 56 |
+
下面将按照模型结构设计、Loss 计算、训练数据增强、训练策略和模型推理过程共 5 个部分详细介绍 YOLOv8 目标检测的各种改进,实例分割部分暂时不进行描述。
|
| 57 |
+
|
| 58 |
+
## 2 模型结构设计
|
| 59 |
+
|
| 60 |
+
模型完整图示可以看图 1。
|
| 61 |
+
|
| 62 |
+
在暂时不考虑 Head 情况下,对比 YOLOv5 和 YOLOv8 的 yaml 配置文件可以发现改动较小。
|
| 63 |
+
|
| 64 |
+
<div align=center >
|
| 65 |
+
<img alt="yaml" src="https://user-images.githubusercontent.com/17425982/212008977-28c3fc7b-ee00-4d56-b912-d77ded585d78.png"/>
|
| 66 |
+
图 3:YOLOv5 和 YOLOv8 YAML 文件对比
|
| 67 |
+
</div>
|
| 68 |
+
|
| 69 |
+
左侧为 YOLOv5-s,右侧为 YOLOv8-s
|
| 70 |
+
|
| 71 |
+
骨干网络和 Neck 的具体变化为:
|
| 72 |
+
|
| 73 |
+
- 第一个卷积层的 kernel 从 6x6 变成了 3x3
|
| 74 |
+
- 所有的 C3 模块换成 C2f,结构如下所示,可以发现多了更多的跳层连接和额外的 Split 操作
|
| 75 |
+
|
| 76 |
+
<div align=center >
|
| 77 |
+
<img alt="module" src="https://user-images.githubusercontent.com/17425982/212009208-92f45c23-a024-49bb-a2ee-bb6f87adcc92.png"/>
|
| 78 |
+
图 4:YOLOv5 和 YOLOv8 模块对比
|
| 79 |
+
</div>
|
| 80 |
+
|
| 81 |
+
- 去掉了 Neck 模块中的 2 个卷积连接层
|
| 82 |
+
- Backbone 中 C2f 的 block 数从 3-6-9-3 改成了 3-6-6-3
|
| 83 |
+
- 查看 N/S/M/L/X 等不同大小模型,可以发现 N/S 和 L/X 两组模型只是改了缩放系数,但是 S/M/L 等骨干网络的通道数设置不一样,没有遵循同一套缩放系数。如此设计的原因应该是同一套缩放系数下的通道设置不是最优设计,YOLOv7 网络设计时也没有遵循一套缩放系数作用于所有模型
|
| 84 |
+
|
| 85 |
+
Head 部分变化最大,从原先的耦合头变成了解耦头,并且从 YOLOv5 的 Anchor-Based 变成了 Anchor-Free。其结构如下所示:
|
| 86 |
+
|
| 87 |
+
<div align=center >
|
| 88 |
+
<img alt="head" src="https://user-images.githubusercontent.com/17425982/212009547-189e14aa-6f93-4af0-8446-adf604a46b95.png"/>
|
| 89 |
+
图 5:YOLOv8 Head 结构
|
| 90 |
+
</div>
|
| 91 |
+
|
| 92 |
+
可以看出,不再有之前的 objectness 分支,只有解耦的分类和回归分支,并且其回归分支使用了 Distribution Focal Loss 中提出的积分形式表示法。
|
| 93 |
+
|
| 94 |
+
## 3 Loss 计算
|
| 95 |
+
|
| 96 |
+
Loss 计算过程包括 2 个部分: 正负样本分配策略和 Loss 计算。
|
| 97 |
+
现代目标检测器大部分都会在正负样本分配策略上面做文章,典型的如 YOLOX 的 simOTA、TOOD 的 TaskAlignedAssigner 和 RTMDet 的 DynamicSoftLabelAssigner,这类 Assigner 大都是动态分配策略,而 YOLOv5 采用的依然是静态分配策略。考虑到动态分配策略的优异性,YOLOv8 算法中则直接引用了 TOOD 的 TaskAlignedAssigner。
|
| 98 |
+
TaskAlignedAssigner 的匹配策略简单总结为: 根据分类与回归的分数加权的分数选择正样本。
|
| 99 |
+
|
| 100 |
+
```{math}
|
| 101 |
+
t=s^\alpha+u^\beta
|
| 102 |
+
```
|
| 103 |
+
|
| 104 |
+
`s` 是标注类别对应的预测分值,`u` 是预测框和 gt 框的 iou,两者相乘就可以衡量对齐程度。
|
| 105 |
+
|
| 106 |
+
1. 对于每一个 GT,对所有的预测框基于 GT 类别对应分类分数,预测框与 GT 的 IoU 的加权得到一个关联分类以及回归的对齐分数 `alignment_metrics`
|
| 107 |
+
2. 对于每一个 GT,直接基于 `alignment_metrics` 对齐分数选取 topK 大的作为正样本
|
| 108 |
+
|
| 109 |
+
Loss 计算包括 2 个分支: **分类和回归分支,没有了之前的 objectness 分支**。
|
| 110 |
+
|
| 111 |
+
- 分类分支依然采用 BCE Loss
|
| 112 |
+
- 回归分支需要和 Distribution Focal Loss 中提出的积分形式表示法绑定,因此使用了 Distribution Focal Loss, 同时还使用了 CIoU Loss
|
| 113 |
+
|
| 114 |
+
3 个 Loss 采用一定权重比例加权即可。
|
| 115 |
+
|
| 116 |
+
## 4 训练数据增强
|
| 117 |
+
|
| 118 |
+
数据增强方面和 YOLOv5 差距不大,只不过引入了 YOLOX 中提出的最后 10 个 epoch 关闭 Mosaic 的操作。假设训练 epoch 是 500,其示意图如下所示:
|
| 119 |
+
|
| 120 |
+
<div align=center >
|
| 121 |
+
<img alt="head" src="https://user-images.githubusercontent.com/17425982/212815248-38384da9-b289-468e-8414-ab3c27ee2026.png"/>
|
| 122 |
+
图 6:pipeline
|
| 123 |
+
</div>
|
| 124 |
+
|
| 125 |
+
考虑到不同模型应该采用的数据增强强度不一样,因此对于不同大小模型,有部分超参会进行修改,典型的如大模型会开启 MixUp 和 CopyPaste。数据增强后典型效果如下所示:
|
| 126 |
+
|
| 127 |
+
<div align=center >
|
| 128 |
+
<img alt="head" src="https://user-images.githubusercontent.com/17425982/212815840-063524e1-d754-46b1-9efc-61d17c03fd0e.png"/>
|
| 129 |
+
图 7:results
|
| 130 |
+
</div>
|
| 131 |
+
|
| 132 |
+
上述效果可以运行 [browse_dataset](https://github.com/open-mmlab/mmyolo/blob/dev/tools/analysis_tools/browse_dataset.py) 脚本得到。由于每个 pipeline 都是比较常规的操作,本文不再赘述。如果想了解每个 pipeline 的细节,可以查看 MMYOLO 中 [YOLOv5 的算法解析文档](https://mmyolo.readthedocs.io/zh_CN/latest/algorithm_descriptions/yolov5_description.html#id2) 。
|
| 133 |
+
|
| 134 |
+
## 5 训练策略
|
| 135 |
+
|
| 136 |
+
YOLOv8 的训练策略和 YOLOv5 没有啥区别,最大区别就是**模型的训练总 epoch 数从 300 提升到了 500**,这也导致训练时间急剧增加。以 YOLOv8-S 为例,其训练策略汇总如下:
|
| 137 |
+
|
| 138 |
+
| 配置 | YOLOv8-s P5 参数 |
|
| 139 |
+
| ---------------------- | ------------------------------- |
|
| 140 |
+
| optimizer | SGD |
|
| 141 |
+
| base learning rate | 0.01 |
|
| 142 |
+
| Base weight decay | 0.0005 |
|
| 143 |
+
| optimizer momentum | 0.937 |
|
| 144 |
+
| batch size | 128 |
|
| 145 |
+
| learning rate schedule | linear |
|
| 146 |
+
| training epochs | **500** |
|
| 147 |
+
| warmup iterations | max(1000,3 * iters_per_epochs) |
|
| 148 |
+
| input size | 640x640 |
|
| 149 |
+
| EMA decay | 0.9999 |
|
| 150 |
+
|
| 151 |
+
## 6 模型推理过程
|
| 152 |
+
|
| 153 |
+
YOLOv8 的推理过程和 YOLOv5 几乎一样,唯一差别在于前面需要对 Distribution Focal Loss 中的积分表示 bbox 形式进行解码,变成常规的 4 维度 bbox,后续计算过程就和 YOLOv5 一样了。
|
| 154 |
+
|
| 155 |
+
以 COCO 80 类为例,假设输入图片大小为 640x640,MMYOLO 中实现的推理过程示意图如下所示:
|
| 156 |
+
|
| 157 |
+
<div align=center >
|
| 158 |
+
<img alt="head" src="https://user-images.githubusercontent.com/17425982/212816206-33815716-3c12-49a2-9c37-0bd85f941bec.png"/>
|
| 159 |
+
图 8:results
|
| 160 |
+
</div>
|
| 161 |
+
|
| 162 |
+
其推理和后处理过程为:
|
| 163 |
+
|
| 164 |
+
**(1) bbox 积分形式转换为 4d bbox 格式**
|
| 165 |
+
|
| 166 |
+
对 Head 输出的 bbox 分支进行转换,利用 Softmax 和 Conv 计算将积分形式转换为 4 维 bbox 格式
|
| 167 |
+
|
| 168 |
+
**(2) 维度变换**
|
| 169 |
+
|
| 170 |
+
YOLOv8 输出特征图尺度为 `80x80`、`40x40` 和 `20x20` 的三个特征图。Head 部分输出分类和回归共 6 个尺度的特征图。
|
| 171 |
+
将 3 个不同尺度的类别预测分支、bbox 预测分支进行拼接,并进行维度变换。为了后续方便处理,会将原先的通道维度置换到最后,类别预测分支 和 bbox 预测分支 shape 分别为 (b, 80x80+40x40+20x20, 80)=(b,8400,80),(b,8400,4)。
|
| 172 |
+
|
| 173 |
+
**(3) 解码还原到原图尺度**
|
| 174 |
+
|
| 175 |
+
分类预测分支进行 Sigmoid 计算,而 bbox 预测分支需要进行解码,还原为真实的原图解码后 xyxy 格式。
|
| 176 |
+
|
| 177 |
+
**(4) 阈值过滤**
|
| 178 |
+
|
| 179 |
+
遍历 batch 中的每张图,采用 `score_thr` 进行阈值过滤。在这过程中还需要考虑 **multi_label 和 nms_pre,确保过滤后的检测框数目不会多于 nms_pre。**
|
| 180 |
+
|
| 181 |
+
**(5) 还原到原图尺度和 nms**
|
| 182 |
+
|
| 183 |
+
基于前处理过程,将剩下的检测框还原到网络输出前的原图尺度,然后进行 nms 即可。最终输出的检测框不能多于 **max_per_img。**
|
| 184 |
+
|
| 185 |
+
有一个特别注意的点:**YOLOv5 中采用的 Batch shape 推理策略,在 YOLOv8 推理中暂时没有开启,不清楚后面是否会开启,在 MMYOLO 中快速测试了下,如果开启 Batch shape 会涨大概 0.1~0.2。**
|
| 186 |
+
|
| 187 |
+
## 7 特征图可视化
|
| 188 |
+
|
| 189 |
+
MMYOLO 中提供了一套完善的特征图可视化工具,可以帮助用户可视化特征的分布情况。 为了和官方性能对齐,此处依然采用官方权重进行可视化。
|
| 190 |
+
|
| 191 |
+
以 YOLOv8-s 模型为例,第一步需要下载官方权重,然后将该权重通过 [yolov8_to_mmyolo](https://github.com/open-mmlab/mmyolo/blob/dev/tools/model_converters/yolov8_to_mmyolo.py) 脚本将去转换到 MMYOLO 中,注意必须要将脚本置于官方仓库下才能正确运行,假设得到的权重名字为 mmyolov8s.pth。
|
| 192 |
+
|
| 193 |
+
假设想可视化 backbone 输出的 3 个特征图效果,则只需要
|
| 194 |
+
|
| 195 |
+
```bash
|
| 196 |
+
cd mmyolo
|
| 197 |
+
python demo/featmap_vis_demo.py demo/demo.jpg configs/yolov8/yolov8_s_syncbn_fast_8xb16-500e_coco.py mmyolov8s.pth --channel-reductio squeeze_mean
|
| 198 |
+
```
|
| 199 |
+
|
| 200 |
+
需要特别注意,为了确保特征图和图片叠加显示能对齐效果,需要先将原先的 `test_pipeline` 替换为如下:
|
| 201 |
+
|
| 202 |
+
```Python
|
| 203 |
+
test_pipeline = [
|
| 204 |
+
dict(
|
| 205 |
+
type='LoadImageFromFile',
|
| 206 |
+
backend_args=_base_.backend_args),
|
| 207 |
+
dict(type='mmdet.Resize', scale=img_scale, keep_ratio=False), # 这里将 LetterResize 修改成 mmdet.Resize
|
| 208 |
+
dict(type='LoadAnnotations', with_bbox=True, _scope_='mmdet'),
|
| 209 |
+
dict(
|
| 210 |
+
type='mmdet.PackDetInputs',
|
| 211 |
+
meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',
|
| 212 |
+
'scale_factor'))
|
| 213 |
+
]
|
| 214 |
+
```
|
| 215 |
+
|
| 216 |
+
<div align=center >
|
| 217 |
+
<img alt="head" src="https://user-images.githubusercontent.com/17425982/212816319-9ac19484-987a-40ac-a0fe-2c13a7048df7.png"/>
|
| 218 |
+
图 9:featmap
|
| 219 |
+
</div>
|
| 220 |
+
|
| 221 |
+
从上图可以看出**不同输出特征图层主要负责预测不同尺度的物体**。
|
| 222 |
+
|
| 223 |
+
我们也可以可视化 Neck 层的 3 个输出层特征图:
|
| 224 |
+
|
| 225 |
+
```bash
|
| 226 |
+
cd mmyolo
|
| 227 |
+
python demo/featmap_vis_demo.py demo/demo.jpg configs/yolov8/yolov8_s_syncbn_fast_8xb16-500e_coco.py mmyolov8s.pth --channel-reductio squeeze_mean --target-layers neck
|
| 228 |
+
```
|
| 229 |
+
|
| 230 |
+
<div align=center >
|
| 231 |
+
<img alt="head" src="https://user-images.githubusercontent.com/17425982/212816458-a4e4600a-5f50-49c6-864b-0254a2720f3c.png"/>
|
| 232 |
+
图 10:featmap
|
| 233 |
+
</div>
|
| 234 |
+
|
| 235 |
+
**从上图可以发现物体处的特征更加聚焦。**
|
| 236 |
+
|
| 237 |
+
## 总结
|
| 238 |
+
|
| 239 |
+
本文详细分析和总结了最新的 YOLOv8 算法,从整体设计到模型结构、Loss 计算、训练数据增强、训练策略和推理过程进行了详细的说明,并提供了大量的示意图供大家方便理解。
|
| 240 |
+
简单来说 YOLOv8 是一个包括了图像分类、Anchor-Free 物体检测和实例分割的高效算法,检测部分设计参考了目前大量优异的最新的 YOLO 改进算法,实现了新的 SOTA。不仅如此还推出了一个全新的框架。不过这个框架还处于早期阶段,还需要不断完善。
|
| 241 |
+
|
| 242 |
+
MMYOLO 开源地址: https://github.com/open-mmlab/mmyolo/blob/dev/configs/yolov8/README.md
|
| 243 |
+
|
| 244 |
+
MMYOLO 算法解析教程:https://mmyolo.readthedocs.io/zh_CN/latest/algorithm_descriptions/index.html#id2
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/application_examples/index.rst
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MMYOLO 应用范例介绍
|
| 2 |
+
********************
|
| 3 |
+
|
| 4 |
+
.. toctree::
|
| 5 |
+
:maxdepth: 1
|
| 6 |
+
|
| 7 |
+
ionogram_detection.md
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/application_examples/ionogram_detection.md
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 基于 MMYOLO 的频高图实时目标检测 benchmark
|
| 2 |
+
|
| 3 |
+
## 数据集构建
|
| 4 |
+
|
| 5 |
+
数字频高图是获取电离层实时信息最重要的途径。电离层结构检测对精准提取电离层关键参数,具有非常重要的研究意义。
|
| 6 |
+
|
| 7 |
+
利用中国科学院在海南、武汉、怀来获取的不同季节的 4311 张频高图建立数据集,使用 [labelme](https://github.com/wkentaro/labelme) 人工标注出 E 层、Es-c 层、Es-l 层、F1 层、F2 层、Spread F 层共 6 种结构。[数据集下载](https://github.com/VoyagerXvoyagerx/Ionogram_detection/releases/download/Dataset/Iono4311.zip)
|
| 8 |
+
|
| 9 |
+
<div align=center>
|
| 10 |
+
<img width="40%" src="https://user-images.githubusercontent.com/67947949/223638535-c4583d88-aa5a-4f21-b35a-e6e8328c9bd4.jpg"/>
|
| 11 |
+
|
| 12 |
+
使用 labelme 标注的图像预览
|
| 13 |
+
|
| 14 |
+
</div>
|
| 15 |
+
|
| 16 |
+
1. 数据集准备
|
| 17 |
+
|
| 18 |
+
下载数据后,放置在 MMYOLO 仓库的根目录下,使用 `unzip test.zip` 命令(linux)解压至当前文件夹。解压后的文件夹结构为:
|
| 19 |
+
|
| 20 |
+
```shell
|
| 21 |
+
Iono4311/
|
| 22 |
+
├── images
|
| 23 |
+
| ├── 20130401005200.png
|
| 24 |
+
| └── ...
|
| 25 |
+
└── labels
|
| 26 |
+
├── 20130401005200.json
|
| 27 |
+
└── ...
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
其中,`images` 目录下存放输入图片,`labels` 目录下存放使用 labelme 标注得到的 json 文件。
|
| 31 |
+
|
| 32 |
+
2. 数据集格式转换
|
| 33 |
+
|
| 34 |
+
使用MMYOLO提供的 `tools/dataset_converters/labelme2coco.py` 脚本将 labelme 格式的标注文件转换为 COCO 格式的标注文件。
|
| 35 |
+
|
| 36 |
+
```shell
|
| 37 |
+
python tools/dataset_converters/labelme2coco.py --img-dir ./Iono4311/images \
|
| 38 |
+
--labels-dir ./Iono4311/labels \
|
| 39 |
+
--out ./Iono4311/annotations/annotations_all.json
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
3. 浏览数据集
|
| 43 |
+
|
| 44 |
+
使用下面的命令可以将 COCO 的 label 在图片上进行显示,这一步可以验证刚刚转换是否有问题。
|
| 45 |
+
|
| 46 |
+
```shell
|
| 47 |
+
python tools/analysis_tools/browse_coco_json.py --img-dir ./Iono4311/images \
|
| 48 |
+
--ann-file ./Iono4311/annotations/annotations_all.json
|
| 49 |
+
```
|
| 50 |
+
|
| 51 |
+
4. 划分训练集、验证集、测试集
|
| 52 |
+
|
| 53 |
+
设置 70% 的图片为训练集,15% 作为验证集,15% 为测试集。
|
| 54 |
+
|
| 55 |
+
```shell
|
| 56 |
+
python tools/misc/coco_split.py --json ./Iono4311/annotations/annotations_all.json \
|
| 57 |
+
--out-dir ./Iono4311/annotations \
|
| 58 |
+
--ratios 0.7 0.15 0.15 \
|
| 59 |
+
--shuffle \
|
| 60 |
+
--seed 14
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
划分后的文件夹结构:
|
| 64 |
+
|
| 65 |
+
```shell
|
| 66 |
+
Iono4311/
|
| 67 |
+
├── annotations
|
| 68 |
+
│ ├── annotations_all.json
|
| 69 |
+
│ ├── class_with_id.txt
|
| 70 |
+
│ ├── test.json
|
| 71 |
+
│ ├── train.json
|
| 72 |
+
│ └── val.json
|
| 73 |
+
├── classes_with_id.txt
|
| 74 |
+
├── images
|
| 75 |
+
├── labels
|
| 76 |
+
├── test_images
|
| 77 |
+
├── train_images
|
| 78 |
+
└── val_images
|
| 79 |
+
```
|
| 80 |
+
|
| 81 |
+
## 配置文件
|
| 82 |
+
|
| 83 |
+
配置文件存放在目录 `/projects/misc/ionogram_detection/` 下。
|
| 84 |
+
|
| 85 |
+
1. 数据集分析
|
| 86 |
+
|
| 87 |
+
使用 `tools/analysis_tools/dataset_analysis.py` 从数据集中采样 200 张图片进行可视化分析:
|
| 88 |
+
|
| 89 |
+
```shell
|
| 90 |
+
python tools/analysis_tools/dataset_analysis.py projects/misc/ionogram_detection/yolov5/yolov5_s-v61_fast_1xb96-100e_ionogram.py \
|
| 91 |
+
--out-dir output
|
| 92 |
+
```
|
| 93 |
+
|
| 94 |
+
得到以下输出:
|
| 95 |
+
|
| 96 |
+
```shell
|
| 97 |
+
The information obtained is as follows:
|
| 98 |
+
+------------------------------+
|
| 99 |
+
| Information of dataset class |
|
| 100 |
+
+---------------+--------------+
|
| 101 |
+
| Class name | Bbox num |
|
| 102 |
+
+---------------+--------------+
|
| 103 |
+
| E | 98 |
|
| 104 |
+
| Es-l | 27 |
|
| 105 |
+
| Es-c | 46 |
|
| 106 |
+
| F1 | 100 |
|
| 107 |
+
| F2 | 194 |
|
| 108 |
+
| Spread-F | 6 |
|
| 109 |
+
+---------------+--------------+
|
| 110 |
+
```
|
| 111 |
+
|
| 112 |
+
说明本数据集存在样本不均衡的现象。
|
| 113 |
+
|
| 114 |
+
<div align=center>
|
| 115 |
+
<img width="100%" src="https://user-images.githubusercontent.com/67947949/223640412-4008a0a1-0626-419d-90bf-fb7ce6f26fc9.jpg"/>
|
| 116 |
+
|
| 117 |
+
各类别目标大小统计
|
| 118 |
+
|
| 119 |
+
</div>
|
| 120 |
+
|
| 121 |
+
根据统计结果,E、Es-l、Esc、F1 类别以小目标居多,F2、Spread F 类主要是中等大小目标。
|
| 122 |
+
|
| 123 |
+
2. 可视化 config 中的数据处理部分
|
| 124 |
+
|
| 125 |
+
以 YOLOv5-s 为例,根据配置文件中的 `train_pipeline`,训练时采用的数据增强策略包括:
|
| 126 |
+
|
| 127 |
+
- 马赛克增强
|
| 128 |
+
- 随机仿射变换
|
| 129 |
+
- Albumentations 数据增强工具包(包括多种数字图像处理方法)
|
| 130 |
+
- HSV 随机增强图像
|
| 131 |
+
- 随机水平翻转
|
| 132 |
+
|
| 133 |
+
使用 `tools/analysis_tools/browse_dataset.py` 脚本的 **'pipeline'** 模式,可以可视化每个 pipeline 的输出效果:
|
| 134 |
+
|
| 135 |
+
```shell
|
| 136 |
+
python tools/analysis_tools/browse_dataset.py projects/misc/ionogram_detection/yolov5/yolov5_s-v61_fast_1xb96-100e_ionogram.py \
|
| 137 |
+
-m pipeline \
|
| 138 |
+
--out-dir output
|
| 139 |
+
```
|
| 140 |
+
|
| 141 |
+
<div align=center>
|
| 142 |
+
<img src="https://user-images.githubusercontent.com/67947949/223914228-abcd017d-a068-4dcd-9d91-e6b546540060.png"/>
|
| 143 |
+
|
| 144 |
+
pipeline 输出可视化
|
| 145 |
+
|
| 146 |
+
</div>
|
| 147 |
+
|
| 148 |
+
3. 优化 Anchor 尺寸
|
| 149 |
+
|
| 150 |
+
使用分析工具中的 `tools/analysis_tools/optimize_anchors.py` 脚本得到适用于本数据集的先验锚框尺寸。
|
| 151 |
+
|
| 152 |
+
```shell
|
| 153 |
+
python tools/analysis_tools/optimize_anchors.py projects/misc/ionogram_detection/yolov5/yolov5_s-v61_fast_1xb96-100e_ionogram.py \
|
| 154 |
+
--algorithm v5-k-means \
|
| 155 |
+
--input-shape 640 640 \
|
| 156 |
+
--prior-match-thr 4.0 \
|
| 157 |
+
--out-dir work_dirs/dataset_analysis_5_s
|
| 158 |
+
```
|
| 159 |
+
|
| 160 |
+
4. 模型复杂度分析
|
| 161 |
+
|
| 162 |
+
根据配置文件,使用分析工具中的 `tools/analysis_tools/get_flops.py` 脚本可以得到模型的参数量、浮点计算量等信息。以 YOLOv5-s 为例:
|
| 163 |
+
|
| 164 |
+
```shell
|
| 165 |
+
python tools/analysis_tools/get_flops.py projects/misc/ionogram_detection/yolov5/yolov5_s-v61_fast_1xb96-100e_ionogram.py
|
| 166 |
+
```
|
| 167 |
+
|
| 168 |
+
得到如下输出,表示模型的浮点运算量为 7.947G,一共有 7.036M 个可学习参数。
|
| 169 |
+
|
| 170 |
+
```shell
|
| 171 |
+
==============================
|
| 172 |
+
Input shape: torch.Size([640, 640])
|
| 173 |
+
Model Flops: 7.947G
|
| 174 |
+
Model Parameters: 7.036M
|
| 175 |
+
==============================
|
| 176 |
+
```
|
| 177 |
+
|
| 178 |
+
## 训练和测试
|
| 179 |
+
|
| 180 |
+
1. 训练
|
| 181 |
+
|
| 182 |
+
训练可视化:本范例按照[标注+训练+测试+部署全流程](https://mmyolo.readthedocs.io/zh_CN/dev/recommended_topics/labeling_to_deployment_tutorials.html#id11)中的步骤安装和配置 [wandb](https://wandb.ai/site)。
|
| 183 |
+
|
| 184 |
+
调试技巧:在调试代码的过程中,有时需要训练几个 epoch,例如调试验证过程或者权重的保存是否符合期望。对于继承自 `BaseDataset` 的数据集(如本范例中的 `YOLOv5CocoDataset`),在 `train_dataloader` 中的 `dataset` 字段增加 `indices` 参数,即可指定每个 epoch 迭代的样本数,减少迭代时间。
|
| 185 |
+
|
| 186 |
+
```python
|
| 187 |
+
train_dataloader = dict(
|
| 188 |
+
batch_size=train_batch_size_per_gpu,
|
| 189 |
+
num_workers=train_num_workers,
|
| 190 |
+
dataset=dict(
|
| 191 |
+
_delete_=True,
|
| 192 |
+
type='RepeatDataset',
|
| 193 |
+
times=1,
|
| 194 |
+
dataset=dict(
|
| 195 |
+
type=_base_.dataset_type,
|
| 196 |
+
indices=200, # 设置 indices=200,表示每个 epoch 只迭代 200 个样本
|
| 197 |
+
data_root=data_root,
|
| 198 |
+
metainfo=metainfo,
|
| 199 |
+
ann_file=train_ann_file,
|
| 200 |
+
data_prefix=dict(img=train_data_prefix),
|
| 201 |
+
filter_cfg=dict(filter_empty_gt=False, min_size=32),
|
| 202 |
+
pipeline=_base_.train_pipeline)))
|
| 203 |
+
```
|
| 204 |
+
|
| 205 |
+
启动训练:
|
| 206 |
+
|
| 207 |
+
```shell
|
| 208 |
+
python tools/train.py projects/misc/ionogram_detection/yolov5/yolov5_s-v61_fast_1xb96-100e_ionogram.py
|
| 209 |
+
```
|
| 210 |
+
|
| 211 |
+
2. 测试
|
| 212 |
+
|
| 213 |
+
指定配置文件和模型的路径以启动测试:
|
| 214 |
+
|
| 215 |
+
```shell
|
| 216 |
+
python tools/test.py projects/misc/ionogram_detection/yolov5/yolov5_s-v61_fast_1xb96-100e_ionogram.py \
|
| 217 |
+
work_dirs/yolov5_s-v61_fast_1xb96-100e_ionogram/xxx
|
| 218 |
+
```
|
| 219 |
+
|
| 220 |
+
## 实验与结果分析
|
| 221 |
+
|
| 222 |
+
### 选择合适的 batch size
|
| 223 |
+
|
| 224 |
+
- Batch size 主导了训练速度。通常,理想的 batch size 是是硬件能支持的最大 batch size。
|
| 225 |
+
- 当显存占用没有达到饱和时,如果 batch size 翻倍,训练吞吐量也应该翻倍(或接近翻倍),训练时间应该减半或接近减半。
|
| 226 |
+
- 使用**混合精度训练**可以加快训练速度、减小显存。在执行 `train.py` 脚本时添加 `--amp` 参数即可开启。
|
| 227 |
+
|
| 228 |
+
硬件信息:
|
| 229 |
+
|
| 230 |
+
- GPU:V100,显存 32G
|
| 231 |
+
- CPU:10核,内存 40G
|
| 232 |
+
|
| 233 |
+
实验结果:
|
| 234 |
+
|
| 235 |
+
| Model | Epoch(best) | AMP | Batchsize | Num workers | Memory Allocated | Training Time | Val mAP |
|
| 236 |
+
| -------- | ----------- | ----- | --------- | ----------- | ---------------- | ------------- | ------- |
|
| 237 |
+
| YOLOv5-s | 100(82) | False | 32 | 6 | 35.07% | 54 min | 0.575 |
|
| 238 |
+
| YOLOv5-s | 100(96) | True | 32 | 6 | 24.93% | 49 min | 0.578 |
|
| 239 |
+
| YOLOv5-s | 100(100) | False | 96 | 6 | 96.64% | 48 min | 0.571 |
|
| 240 |
+
| YOLOv5-s | 100(100) | True | 96 | 6 | 54.66% | **37** min | 0.575 |
|
| 241 |
+
| YOLOv5-s | 100(90) | True | 144 | 6 | 77.06% | 39 min | 0.573 |
|
| 242 |
+
| YOLOv5-s | 200(148) | True | 96 | 6 | 54.66% | 72 min | 0.575 |
|
| 243 |
+
| YOLOv5-s | 200(188) | True | 96 | **8** | 54.66% | 67 min | 0.576 |
|
| 244 |
+
|
| 245 |
+
<div align=center>
|
| 246 |
+
<img width="60%" src="https://user-images.githubusercontent.com/67947949/223635966-948c8424-1ba8-4df0-92d7-079029dc1231.png">
|
| 247 |
+
|
| 248 |
+
不同 batch size 的训练过程中,数据加载时间 `data_time` 占每步总时长的比例
|
| 249 |
+
|
| 250 |
+
</div>
|
| 251 |
+
|
| 252 |
+
分析结果,可以得出以下结论:
|
| 253 |
+
|
| 254 |
+
- 混合精度训练对模型的精度几乎没有影响,并且可以明显减少显存占用。
|
| 255 |
+
- Batch size 增加 3 倍,和训练时长并没有相应地减小 3 倍。根据训练过程中 `data_time` 的记录,batch size 越大,`data_time` 也越大,说明数据加载成为了限制��练速度的瓶颈。增大加载数据的进程数 `num_workers` 可以加快数据加载。
|
| 256 |
+
|
| 257 |
+
### 消融实验
|
| 258 |
+
|
| 259 |
+
为了得到适用于本数据集的训练流水线,以 YOLOv5-s 模型为例,进行以下消融实验。
|
| 260 |
+
|
| 261 |
+
#### 不同数据增强方法
|
| 262 |
+
|
| 263 |
+
| Aug Method | [config](/projects/misc/ionogram_detection/yolov5/yolov5_s-v61_fast_1xb96-100e_ionogram_aug0.py) | [config](/projects/misc/ionogram_detection/yolov5/yolov5_s-v61_fast_1xb32-100e_ionogram_mosaic.py) | [config](/projects/misc/ionogram_detection/yolov5/yolov5_s-v61_fast_1xb96-100e_ionogram_mosaic_affine.py) | [config](/projects/misc/ionogram_detection/yolov5/yolov5_s-v61_fast_1xb96-100e_ionogram_mosaic_affine_albu_hsv.py) | [config](/projects/misc/ionogram_detection/yolov5/yolov5_s-v61_fast_1xb96-100e_ionogram.py) |
|
| 264 |
+
| ---------- | ------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------- |
|
| 265 |
+
| Mosaic | | √ | √ | √ | √ |
|
| 266 |
+
| Affine | | | √ | √ | √ |
|
| 267 |
+
| Albu | | | | √ | √ |
|
| 268 |
+
| HSV | | | | √ | √ |
|
| 269 |
+
| Flip | | | | | √ |
|
| 270 |
+
| Val mAP | 0.507 | 0.550 | 0.572 | 0.567 | 0.575 |
|
| 271 |
+
|
| 272 |
+
结果表明,马赛克增强和随机仿射变换可以对验证集表现带来明显的提升。
|
| 273 |
+
|
| 274 |
+
#### 是否使用预训练权重
|
| 275 |
+
|
| 276 |
+
在配置文件中,修改 `load_from = None` 即可不使用预训练权重。对不使用预训练权重的实验,将基础学习率增大四倍,训练轮数增加至 200 轮,使模型得到较为充分的训练。
|
| 277 |
+
|
| 278 |
+
| Model | Epoch(best) | FLOPs(G) | Params(M) | Pretrain | Val mAP | Config |
|
| 279 |
+
| -------- | ----------- | -------- | --------- | -------- | ------- | ------------------------------------------------------------------------------------------------ |
|
| 280 |
+
| YOLOv5-s | 100(82) | 7.95 | 7.04 | Coco | 0.575 | [config](/projects/misc/ionogram_detection/yolov5/yolov5_s-v61_fast_1xb96-100e_ionogram.py) |
|
| 281 |
+
| YOLOv5-s | 200(145) | 7.95 | 7.04 | None | 0.565 | [config](/projects/misc/ionogram_detection/yolov5/yolov5_s-v61_fast_1xb96-200e_ionogram_pre0.py) |
|
| 282 |
+
| YOLOv6-s | 100(54) | 24.2 | 18.84 | Coco | 0.584 | [config](/projects/misc/ionogram_detection/yolov6/yolov6_s_fast_1xb32-100e_ionogram.py) |
|
| 283 |
+
| YOLOv6-s | 200(188) | 24.2 | 18.84 | None | 0.557 | [config](/projects/misc/ionogram_detection/yolov6/yolov6_s_fast_1xb32-200e_ionogram_pre0.py) |
|
| 284 |
+
|
| 285 |
+
<div align=center>
|
| 286 |
+
<img width="60%" src="https://user-images.githubusercontent.com/67947949/223641016-9ded0d11-62b8-45f4-be5b-bd4ffae3ec21.png">
|
| 287 |
+
|
| 288 |
+
训练过程中的损失下降对比图
|
| 289 |
+
|
| 290 |
+
</div>
|
| 291 |
+
|
| 292 |
+
损失下降曲线表明,使用预训练权重时,loss 下降得更快。可见即使是自然图像数据集上预训练的模型,在雷达图像数据集上微调时,也可以加快模型收敛。
|
| 293 |
+
|
| 294 |
+
### 频高图结构检测 benchmark
|
| 295 |
+
|
| 296 |
+
| Model | epoch(best) | FLOPs(G) | Params(M) | pretrain | val mAP | test mAP | Config | Log |
|
| 297 |
+
| ----------- | ----------- | -------- | --------- | -------- | ------- | -------- | ------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------- |
|
| 298 |
+
| YOLOv5-s | 100(82) | 7.95 | 7.04 | Coco | 0.575 | 0.584 | [config](/projects/misc/ionogram_detection/yolov5/yolov5_s-v61_fast_1xb96-100e_ionogram.py) | [log](https://github.com/VoyagerXvoyagerx/Ionogram_detection/blob/main/logs/yolov5_s_20230105_213510.json) |
|
| 299 |
+
| YOLOv5-m | 100(70) | 24.05 | 20.89 | Coco | 0.587 | 0.586 | [config](/projects/misc/ionogram_detection/yolov5/yolov5_m-v61_fast_1xb32-100e_ionogram.py) | [log](https://github.com/VoyagerXvoyagerx/Ionogram_detection/blob/main/logs/yolov5_m_20230106_004642.json) |
|
| 300 |
+
| YOLOv6-s | 100(54) | 24.2 | 18.84 | Coco | 0.584 | 0.594 | [config](/projects/misc/ionogram_detection/yolov6/yolov6_s_fast_1xb32-100e_ionogram.py) | [log](https://github.com/VoyagerXvoyagerx/Ionogram_detection/blob/main/logs/yolov6_s_20230107_003207.json) |
|
| 301 |
+
| YOLOv6-m | 100(76) | 37.08 | 44.42 | Coco | 0.590 | 0.590 | [config](/projects/misc/ionogram_detection/yolov6/yolov6_m_fast_1xb32-100e_ionogram.py) | [log](https://github.com/VoyagerXvoyagerx/Ionogram_detection/blob/main/logs/yolov6_m_20230107_201029.json) |
|
| 302 |
+
| YOLOv6-l | 100(76) | 71.33 | 58.47 | Coco | 0.605 | 0.597 | [config](/projects/misc/ionogram_detection/yolov6/yolov6_l_fast_1xb32-100e_ionogram.py) | [log](https://github.com/VoyagerXvoyagerx/Ionogram_detection/blob/main/logs/yolov6_l_20230108_005634.json) |
|
| 303 |
+
| YOLOv7-tiny | 100(78) | 6.57 | 6.02 | Coco | 0.549 | 0.568 | [config](/projects/misc/ionogram_detection/yolov7/yolov7_tiny_fast_1xb16-100e_ionogram.py) | [log](https://github.com/VoyagerXvoyagerx/Ionogram_detection/blob/main/logs/yolov7_tiny_20230215_202837.json) |
|
| 304 |
+
| YOLOv7-x | 100(58) | 94.27 | 70.85 | Coco | 0.602 | 0.595 | [config](/projects/misc/ionogram_detection/yolov7/yolov7_x_fast_1xb16-100e_ionogram.py) | [log](https://github.com/VoyagerXvoyagerx/Ionogram_detection/blob/main/logs/yolov7_x_20230110_165832.json) |
|
| 305 |
+
| rtmdet-tiny | 100(100) | 8.03 | 4.88 | Coco | 0.582 | 0.589 | [config](/projects/misc/ionogram_detection/rtmdet/rtmdet_tiny_fast_1xb32-100e_ionogram.py) | [log](https://github.com/VoyagerXvoyagerx/Ionogram_detection/blob/main/logs/rtmdet_tiny_20230310_125440.json) |
|
| 306 |
+
| rtmdet-s | 100(92) | 14.76 | 8.86 | Coco | 0.588 | 0.585 | [config](/projects/misc/ionogram_detection/rtmdet/rtmdet_s_fast_1xb32-100e_ionogram.py) | [log](https://github.com/VoyagerXvoyagerx/Ionogram_detection/blob/main/logs/rtmdet_s_20230310_163853.json) |
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/complexity_analysis.md
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 模型复杂度分析
|
| 2 |
+
|
| 3 |
+
我们提供了 `tools/analysis_tools/get_flops.py` 脚本来帮助进行 MMYOLO 系列中所有模型的复杂度分析。目前支持计算并输出给定模型的 parameters, activation 以及 flops;同时支持以网络结构或表格的形式打印输出每一层网络的复杂度信息。
|
| 4 |
+
|
| 5 |
+
调用命令如下:
|
| 6 |
+
|
| 7 |
+
```shell
|
| 8 |
+
python tools/analysis_tools/get_flops.py
|
| 9 |
+
${CONFIG_FILE} \ # 配置文件路径
|
| 10 |
+
[--shape ${IMAGE_SIZE}] \ # 输入图像大小(int),默认取 640*640
|
| 11 |
+
[--show-arch ${ARCH_DISPLAY}] \ # 以网络结构形式逐层展示复杂度信息
|
| 12 |
+
[--not-show-table ${TABLE_DISPLAY}] \ # 以表格形式逐层展示复杂度信息
|
| 13 |
+
[--cfg-options ${CFG_OPTIONS}] # 配置文件参数修改选项
|
| 14 |
+
# [] 代表可选参数,实际输入命令行时,不用输入 []
|
| 15 |
+
```
|
| 16 |
+
|
| 17 |
+
接下来以 RTMDet 中的 `rtmdet_s_syncbn_fast_8xb32-300e_coco.py` 配置文件为例,详细展示该脚本的几种使用情形:
|
| 18 |
+
|
| 19 |
+
## 样例 1: 打印模型的 Flops 和 Parameters,并以表格形式展示每层网络复杂度
|
| 20 |
+
|
| 21 |
+
```shell
|
| 22 |
+
python tools/analysis_tools/get_flops.py configs/rtmdet/rtmdet_s_syncbn_fast_8xb32-300e_coco.py
|
| 23 |
+
```
|
| 24 |
+
|
| 25 |
+
输出如下:
|
| 26 |
+
|
| 27 |
+
```python
|
| 28 |
+
==============================
|
| 29 |
+
Input shape: torch.Size([640, 640])
|
| 30 |
+
Model Flops: 14.835G
|
| 31 |
+
Model Parameters: 8.887M
|
| 32 |
+
==============================
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
| module | #parameters or shape | #flops | #activations |
|
| 36 |
+
| :-------------------------------- | :------------------- | :------ | :----------: |
|
| 37 |
+
| model | 8.887M | 14.835G | 35.676M |
|
| 38 |
+
| backbone | 4.378M | 5.416G | 22.529M |
|
| 39 |
+
| backbone.stem | 7.472K | 0.765G | 6.554M |
|
| 40 |
+
| backbone.stem.0 | 0.464K | 47.514M | 1.638M |
|
| 41 |
+
| backbone.stem.1 | 2.336K | 0.239G | 1.638M |
|
| 42 |
+
| backbone.stem.2 | 4.672K | 0.478G | 3.277M |
|
| 43 |
+
| backbone.stage1 | 42.4K | 0.981G | 7.373M |
|
| 44 |
+
| backbone.stage1.0 | 18.56K | 0.475G | 1.638M |
|
| 45 |
+
| backbone.stage1.1 | 23.84K | 0.505G | 5.734M |
|
| 46 |
+
| backbone.stage2 | 0.21M | 1.237G | 4.915M |
|
| 47 |
+
| backbone.stage2.0 | 73.984K | 0.473G | 0.819M |
|
| 48 |
+
| backbone.stage2.1 | 0.136M | 0.764G | 4.096M |
|
| 49 |
+
| backbone.stage3 | 0.829M | 1.221G | 2.458M |
|
| 50 |
+
| backbone.stage3.0 | 0.295M | 0.473G | 0.41M |
|
| 51 |
+
| backbone.stage3.1 | 0.534M | 0.749G | 2.048M |
|
| 52 |
+
| backbone.stage4 | 3.29M | 1.211G | 1.229M |
|
| 53 |
+
| backbone.stage4.0 | 1.181M | 0.472G | 0.205M |
|
| 54 |
+
| backbone.stage4.1 | 0.657M | 0.263G | 0.307M |
|
| 55 |
+
| backbone.stage4.2 | 1.452M | 0.476G | 0.717M |
|
| 56 |
+
| neck | 3.883M | 4.366G | 8.141M |
|
| 57 |
+
| neck.reduce_layers.2 | 0.132M | 52.634M | 0.102M |
|
| 58 |
+
| neck.reduce_layers.2.conv | 0.131M | 52.429M | 0.102M |
|
| 59 |
+
| neck.reduce_layers.2.bn | 0.512K | 0.205M | 0 |
|
| 60 |
+
| neck.top_down_layers | 0.491M | 1.23G | 4.506M |
|
| 61 |
+
| neck.top_down_layers.0 | 0.398M | 0.638G | 1.638M |
|
| 62 |
+
| neck.top_down_layers.1 | 92.608K | 0.593G | 2.867M |
|
| 63 |
+
| neck.downsample_layers | 0.738M | 0.472G | 0.307M |
|
| 64 |
+
| neck.downsample_layers.0 | 0.148M | 0.236G | 0.205M |
|
| 65 |
+
| neck.downsample_layers.1 | 0.59M | 0.236G | 0.102M |
|
| 66 |
+
| neck.bottom_up_layers | 1.49M | 0.956G | 2.15M |
|
| 67 |
+
| neck.bottom_up_layers.0 | 0.3M | 0.48G | 1.434M |
|
| 68 |
+
| neck.bottom_up_layers.1 | 1.19M | 0.476G | 0.717M |
|
| 69 |
+
| neck.out_layers | 1.033M | 1.654G | 1.075M |
|
| 70 |
+
| neck.out_layers.0 | 0.148M | 0.945G | 0.819M |
|
| 71 |
+
| neck.out_layers.1 | 0.295M | 0.472G | 0.205M |
|
| 72 |
+
| neck.out_layers.2 | 0.59M | 0.236G | 51.2K |
|
| 73 |
+
| neck.upsample_layers | | 1.229M | 0 |
|
| 74 |
+
| neck.upsample_layers.0 | | 0.41M | 0 |
|
| 75 |
+
| neck.upsample_layers.1 | | 0.819M | 0 |
|
| 76 |
+
| bbox_head.head_module | 0.625M | 5.053G | 5.006M |
|
| 77 |
+
| bbox_head.head_module.cls_convs | 0.296M | 2.482G | 2.15M |
|
| 78 |
+
| bbox_head.head_module.cls_convs.0 | 0.295M | 2.481G | 2.15M |
|
| 79 |
+
| bbox_head.head_module.cls_convs.1 | 0.512K | 0.819M | 0 |
|
| 80 |
+
| bbox_head.head_module.cls_convs.2 | 0.512K | 0.205M | 0 |
|
| 81 |
+
| bbox_head.head_module.reg_convs | 0.296M | 2.482G | 2.15M |
|
| 82 |
+
| bbox_head.head_module.reg_convs.0 | 0.295M | 2.481G | 2.15M |
|
| 83 |
+
| bbox_head.head_module.reg_convs.1 | 0.512K | 0.819M | 0 |
|
| 84 |
+
| bbox_head.head_module.reg_convs.2 | 0.512K | 0.205M | 0 |
|
| 85 |
+
| bbox_head.head_module.rtm_cls | 30.96K | 86.016M | 0.672M |
|
| 86 |
+
| bbox_head.head_module.rtm_cls.0 | 10.32K | 65.536M | 0.512M |
|
| 87 |
+
| bbox_head.head_module.rtm_cls.1 | 10.32K | 16.384M | 0.128M |
|
| 88 |
+
| bbox_head.head_module.rtm_cls.2 | 10.32K | 4.096M | 32K |
|
| 89 |
+
| bbox_head.head_module.rtm_reg | 1.548K | 4.301M | 33.6K |
|
| 90 |
+
| bbox_head.head_module.rtm_reg.0 | 0.516K | 3.277M | 25.6K |
|
| 91 |
+
| bbox_head.head_module.rtm_reg.1 | 0.516K | 0.819M | 6.4K |
|
| 92 |
+
| bbox_head.head_module.rtm_reg.2 | 0.516K | 0.205M | 1.6K |
|
| 93 |
+
|
| 94 |
+
## 样例 2:以网络结构形式逐层展示模型复杂度信息
|
| 95 |
+
|
| 96 |
+
```shell
|
| 97 |
+
python tools/analysis_tools/get_flops.py configs/rtmdet/rtmdet_s_syncbn_fast_8xb32-300e_coco.py --show-arch
|
| 98 |
+
```
|
| 99 |
+
|
| 100 |
+
由于该网络结构复杂,输出较长。以下仅展示 bbox_head.head_module.rtm_reg 部分的输出:
|
| 101 |
+
|
| 102 |
+
```python
|
| 103 |
+
(rtm_reg): ModuleList(
|
| 104 |
+
#params: 1.55K, #flops: 4.3M, #acts: 33.6K
|
| 105 |
+
(0): Conv2d(
|
| 106 |
+
128, 4, kernel_size=(1, 1), stride=(1, 1)
|
| 107 |
+
#params: 0.52K, #flops: 3.28M, #acts: 25.6K
|
| 108 |
+
)
|
| 109 |
+
(1): Conv2d(
|
| 110 |
+
128, 4, kernel_size=(1, 1), stride=(1, 1)
|
| 111 |
+
#params: 0.52K, #flops: 0.82M, #acts: 6.4K
|
| 112 |
+
)
|
| 113 |
+
(2): Conv2d(
|
| 114 |
+
128, 4, kernel_size=(1, 1), stride=(1, 1)
|
| 115 |
+
#params: 0.52K, #flops: 0.2M, #acts: 1.6K
|
| 116 |
+
)
|
| 117 |
+
```
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/contributing.md
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 如何给 MMYOLO 贡献代码
|
| 2 |
+
|
| 3 |
+
欢迎加入 MMYOLO 社区,我们致力于打造最前沿的计算机视觉基础库,我们欢迎任何类型的贡献,包括但不限于
|
| 4 |
+
|
| 5 |
+
**修复错误**
|
| 6 |
+
|
| 7 |
+
修复代码实现错误的步骤如下:
|
| 8 |
+
|
| 9 |
+
1. 如果提交的代码改动较大,建议先提交 issue,并正确描述 issue 的现象、原因和复现方式,讨论后确认修复方案。
|
| 10 |
+
2. 修复错误并补充相应的单元测试,提交拉取请求。
|
| 11 |
+
|
| 12 |
+
**新增功能或组件**
|
| 13 |
+
|
| 14 |
+
1. 如果新功能或模块涉及较大的代码改动,建议先提交 issue,确认功能的必要性。
|
| 15 |
+
2. 实现新增功能并添单元测试,提交拉取请求。
|
| 16 |
+
|
| 17 |
+
**文档补充**
|
| 18 |
+
|
| 19 |
+
修复文档可以直接提交拉取请求
|
| 20 |
+
|
| 21 |
+
添加文档或将文档翻译成其他语言步骤如下
|
| 22 |
+
|
| 23 |
+
1. 提交 issue,确认添加文档的必要性。
|
| 24 |
+
2. 添加文档,提交拉取请求。
|
| 25 |
+
|
| 26 |
+
## 准备工作
|
| 27 |
+
|
| 28 |
+
拉取请求工作的命令都是用 Git 去实现的,该章节详细描述 `Git 配置` 以及与 `GitHub 绑定`
|
| 29 |
+
|
| 30 |
+
### 1. Git 配置
|
| 31 |
+
|
| 32 |
+
首先,确认电脑是否安装了 Git。Linux 系统和 macOS 系统,一般默认安装 Git,如未安装可在 [Git-Downloads](https://git-scm.com/downloads) 下载。
|
| 33 |
+
|
| 34 |
+
```shell
|
| 35 |
+
# 在命令提示符(cmd)或终端下输入以下命令,查看 Git 版本
|
| 36 |
+
git --version
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
其次,检测自己 `Git Config` 是否配置
|
| 40 |
+
|
| 41 |
+
```shell
|
| 42 |
+
# 在命令提示符(cmd)或终端下输入以下命令,查看 Git Config 是否配置
|
| 43 |
+
git config --global --list
|
| 44 |
+
```
|
| 45 |
+
|
| 46 |
+
若 `user.name` 和 `user.email` 为空,则输入以下命令进行配置。
|
| 47 |
+
|
| 48 |
+
```shell
|
| 49 |
+
git config --global user.name "这里换上你的用户名"
|
| 50 |
+
git config --global user.email "这里换上你的邮箱"
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
最后,在 `git bash` 或者 `终端` 中,输入以下命令生成密钥文件。生成成功后,会在用户目录下出现 `.ssh` 文件,其中 `id_rsa.pub` 是公钥文件。
|
| 54 |
+
|
| 55 |
+
```shell
|
| 56 |
+
# useremail 是 GitHub 的邮箱
|
| 57 |
+
ssh-keygen -t rsa -C "useremail"
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
### 2. GitHub 绑定
|
| 61 |
+
|
| 62 |
+
首先,用记事本打开 `id_rsa.pub` 公钥文件,并复制里面全部内容。
|
| 63 |
+
|
| 64 |
+
其次,登录 GitHub 账户找到下图位置进行设置。
|
| 65 |
+
|
| 66 |
+
<img src="https://user-images.githubusercontent.com/90811472/221778382-a075167d-b028-4f68-a1c7-49a8f6f3d97b.png" width="1200">
|
| 67 |
+
|
| 68 |
+
点击 `New SSH key` 新增一个 SSH keys,将刚才复制的内容粘贴到下图所示的 Key 中,Title 可以写设备名称,最后确认即可。
|
| 69 |
+
|
| 70 |
+
<img src="https://user-images.githubusercontent.com/90811472/221549754-53670c19-5efe-48b2-9ac5-bafb43891903.png" width="1200">
|
| 71 |
+
|
| 72 |
+
最后,在 `git bash` 或者 `终端` 中输入以下命令,验证 SSH 是否与 GitHub 账户匹配。如果匹配,输入 `yes` 就成功啦~
|
| 73 |
+
|
| 74 |
+
```shell
|
| 75 |
+
ssh -T git@github.com
|
| 76 |
+
```
|
| 77 |
+
|
| 78 |
+
<img src="https://user-images.githubusercontent.com/90811472/221573637-30e5d04d-955c-4c8f-86ab-ed6608644fc8.png" width="1200">
|
| 79 |
+
|
| 80 |
+
## 拉取请求工作流
|
| 81 |
+
|
| 82 |
+
如果你对拉取请求不了解,没关系,接下来的内容将会从零开始,一步一步地指引你如何创建一个拉取请求。如果你想深入了解拉取请求的开发模式,可以参考 github [官方文档](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests)
|
| 83 |
+
|
| 84 |
+
### 1. 复刻仓库
|
| 85 |
+
|
| 86 |
+
当你第一次提交拉取请求时,先复刻 OpenMMLab 原代码库,点击 GitHub 页面右上角的 **Fork** 按钮,复刻后的代码库将会出现在你的 GitHub 个人主页下。
|
| 87 |
+
|
| 88 |
+
<img src="https://user-images.githubusercontent.com/27466624/204301143-2d262d2c-28b3-4060-8576-21d9f4237f2f.png" width="1200">
|
| 89 |
+
|
| 90 |
+
将代码克隆到本地
|
| 91 |
+
|
| 92 |
+
```shell
|
| 93 |
+
git clone git@github.com:{username}/mmyolo.git
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
进入项目并添加原代码库为上游代码库
|
| 97 |
+
|
| 98 |
+
```bash
|
| 99 |
+
cd mmyolo
|
| 100 |
+
git remote add upstream git@github.com:open-mmlab/mmyolo
|
| 101 |
+
```
|
| 102 |
+
|
| 103 |
+
检查 remote 是否添加成功,在终端输入 `git remote -v`
|
| 104 |
+
|
| 105 |
+
```bash
|
| 106 |
+
origin git@github.com:{username}/mmyolo.git (fetch)
|
| 107 |
+
origin git@github.com:{username}/mmyolo.git (push)
|
| 108 |
+
upstream git@github.com:open-mmlab/mmyolo (fetch)
|
| 109 |
+
upstream git@github.com:open-mmlab/mmyolo (push)
|
| 110 |
+
```
|
| 111 |
+
|
| 112 |
+
```{note}
|
| 113 |
+
这里对 origin 和 upstream 进行一个简单的介绍,当我们使用 git clone 来克隆代码时,会默认创建一个 origin 的 remote,它指向我们克隆的代码库地址,而 upstream 则是我们自己添加的,用来指向原始代码库地址。当然如果你不喜欢他叫 upstream,也可以自己修改,比如叫 open-mmlab。我们通常向 origin 提交代码(即 fork 下来的远程仓库),然后向 upstream 提交一个 pull request。如果提交的代码和最新的代码发生冲突,再从 upstream 拉取最新的代码,和本地分支解决冲突,再提交到 origin。
|
| 114 |
+
```
|
| 115 |
+
|
| 116 |
+
### 2. 配置 pre-commit
|
| 117 |
+
|
| 118 |
+
在本地开发环境中,我们使用 [pre-commit](https://pre-commit.com/#intro) 来检查代码风格,以确保代码风格的统一。在提交代码,需要先安装 pre-commit(需要在 MMYOLO 目录下执行):
|
| 119 |
+
|
| 120 |
+
```shell
|
| 121 |
+
pip install -U pre-commit
|
| 122 |
+
pre-commit install
|
| 123 |
+
```
|
| 124 |
+
|
| 125 |
+
检查 pre-commit 是否配置成功,并安装 `.pre-commit-config.yaml` 中的钩子:
|
| 126 |
+
|
| 127 |
+
```shell
|
| 128 |
+
pre-commit run --all-files
|
| 129 |
+
```
|
| 130 |
+
|
| 131 |
+
<img src="https://user-images.githubusercontent.com/57566630/173660750-3df20a63-cb66-4d33-a986-1f643f1d8aaf.png" width="1200">
|
| 132 |
+
|
| 133 |
+
<img src="https://user-images.githubusercontent.com/57566630/202368856-0465a90d-8fce-4345-918e-67b8b9c82614.png" width="1200">
|
| 134 |
+
|
| 135 |
+
```{note}
|
| 136 |
+
如果你是中国用户,由于网络原因,可能会出现安装失败的情况,这时可以使用国内源
|
| 137 |
+
|
| 138 |
+
pre-commit install -c .pre-commit-config-zh-cn.yaml
|
| 139 |
+
|
| 140 |
+
pre-commit run --all-files -c .pre-commit-config-zh-cn.yaml
|
| 141 |
+
```
|
| 142 |
+
|
| 143 |
+
如果安装过程被中断,可以重复执行 `pre-commit run ...` 继续安装。
|
| 144 |
+
|
| 145 |
+
如果提交的代码不符合代码风格规范,pre-commit 会发出警告,并自动修复部分错误。
|
| 146 |
+
|
| 147 |
+
<img src="https://user-images.githubusercontent.com/57566630/202369176-67642454-0025-4023-a095-263529107aa3.png" width="1200">
|
| 148 |
+
|
| 149 |
+
如果我们想临时绕开 pre-commit 的检查提交一次代码,可以在 `git commit` 时加上 `--no-verify`(需要保证最后推送至远程仓库的代码能够通过 pre-commit 检查)。
|
| 150 |
+
|
| 151 |
+
```shell
|
| 152 |
+
git commit -m "xxx" --no-verify
|
| 153 |
+
```
|
| 154 |
+
|
| 155 |
+
### 3. 创建开发分支
|
| 156 |
+
|
| 157 |
+
安装完 pre-commit 之后,我们需要基于 dev 创建开发分支,建议的分支命名规则为 `username/pr_name`。
|
| 158 |
+
|
| 159 |
+
```shell
|
| 160 |
+
git checkout -b yhc/refactor_contributing_doc
|
| 161 |
+
```
|
| 162 |
+
|
| 163 |
+
在后续的开发中,如果本地仓库的 dev 分支落后于 upstream 的 dev 分支,我们需要先拉取 upstream 的代码进行同步,再执行上面的命令
|
| 164 |
+
|
| 165 |
+
```shell
|
| 166 |
+
git pull upstream dev
|
| 167 |
+
```
|
| 168 |
+
|
| 169 |
+
### 4. 提交代码并在本地通过单元测试
|
| 170 |
+
|
| 171 |
+
- MMYOLO 引入了 mypy 来做静态类型检查,以增加代码的鲁棒性。因此我们在提交代码时,需要补充 Type Hints。具体规则可以参考[教程](https://zhuanlan.zhihu.com/p/519335398)。
|
| 172 |
+
|
| 173 |
+
- 提交的代码同样需要通过单元测试
|
| 174 |
+
|
| 175 |
+
```shell
|
| 176 |
+
# 通过全量单元测试
|
| 177 |
+
pytest tests
|
| 178 |
+
|
| 179 |
+
# 我们需要保证提交的代码能够通过修改模块的单元测试,以 yolov5_coco dataset 为例
|
| 180 |
+
pytest tests/test_datasets/test_yolov5_coco.py
|
| 181 |
+
```
|
| 182 |
+
|
| 183 |
+
如果你由于缺少依赖无法运行修改模块的单元测试,可以参考[指引-单元测试](#单元测试)
|
| 184 |
+
|
| 185 |
+
- 如果修改/添加了文档,参考[指引](#文档渲染)确认文档渲染正常。
|
| 186 |
+
|
| 187 |
+
### 5. 推送代码到远程
|
| 188 |
+
|
| 189 |
+
代码通过单元测试和 pre-commit 检查后,将代码推送到远程仓库,如果是第一次推送,可以在 `git push` 后加上 `-u` 参数以关联远程分支
|
| 190 |
+
|
| 191 |
+
```shell
|
| 192 |
+
git push -u origin {branch_name}
|
| 193 |
+
```
|
| 194 |
+
|
| 195 |
+
这样下次就可以直接使用 `git push` 命令推送代码了,而无需指定分支和远程仓库。
|
| 196 |
+
|
| 197 |
+
### 6. 提交拉取请求(PR)
|
| 198 |
+
|
| 199 |
+
(1) 在 GitHub 的 Pull request 界面创建拉取请求
|
| 200 |
+
<img src="https://user-images.githubusercontent.com/27466624/204302289-d1e54901-8f27-4934-923f-fda800ff9851.png" width="1200">
|
| 201 |
+
|
| 202 |
+
(2) 根据指引修改 PR 描述,以便于其他开发者更好地理解你的修改
|
| 203 |
+
|
| 204 |
+
```{note}
|
| 205 |
+
注意在 PR branch 左侧的 base 需要修改为 dev 分支
|
| 206 |
+
```
|
| 207 |
+
|
| 208 |
+
<img src="https://user-images.githubusercontent.com/62822224/216594960-a2292b9d-2b7c-4861-b4c5-362a9458b194.png" width="1200">
|
| 209 |
+
|
| 210 |
+
描述规范详见[拉取请求规范](#拉取请求规范)
|
| 211 |
+
|
| 212 |
+
 
|
| 213 |
+
|
| 214 |
+
**注意事项**
|
| 215 |
+
|
| 216 |
+
(a) PR 描述应该包含修改理由、修改内容以及修改后带来的影响,并关联相关 Issue(具体方式见[文档](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue))
|
| 217 |
+
|
| 218 |
+
(b) 如果是第一次为 OpenMMLab 做贡献,需要签署 CLA
|
| 219 |
+
|
| 220 |
+
<img src="https://user-images.githubusercontent.com/57566630/167307569-a794b967-6e28-4eac-a942-00deb657815f.png" width="1200">
|
| 221 |
+
|
| 222 |
+
(c) 检查提交的 PR 是否通过 CI(集成测试)
|
| 223 |
+
|
| 224 |
+
<img src="https://user-images.githubusercontent.com/27466624/204303753-900de590-ddd1-4be2-8e43-8dc09f127f5d.png" width="1200">
|
| 225 |
+
|
| 226 |
+
MMYOLO 会在 Linux 上,基于不同版本的 Python、PyTorch 对提交的代码进行单元测试,以保证代码的正确性,如果有任何一个没有通过,我们可点击上图中的 `Details` 来查看具体的测试信息,以便于我们修改代码。
|
| 227 |
+
|
| 228 |
+
(3) 如果 PR 通过了 CI,那么就可以等待其他开发者的 review,并根据 reviewer 的意见,修改代码,并重复 [4](#4-提交代码并本地通过单元测试)-[5](#5-推送代码到远程) 步骤,直到 reviewer 同意合入 PR。
|
| 229 |
+
|
| 230 |
+
<img src="https://user-images.githubusercontent.com/57566630/202145400-cc2cd8c4-10b0-472f-ba37-07e6f50acc67.png" width="1200">
|
| 231 |
+
|
| 232 |
+
所有 reviewer 同意合入 PR 后,我们会尽快将 PR 合并到 dev 分支。
|
| 233 |
+
|
| 234 |
+
### 7. 解决冲突
|
| 235 |
+
|
| 236 |
+
随着时间的推移,我们的代码库会不断更新,这时候,如果你的 PR 与 dev 分支存在冲突,你需要解决冲突,解决冲突的方式有两种:
|
| 237 |
+
|
| 238 |
+
```shell
|
| 239 |
+
git fetch --all --prune
|
| 240 |
+
git rebase upstream/dev
|
| 241 |
+
```
|
| 242 |
+
|
| 243 |
+
或者
|
| 244 |
+
|
| 245 |
+
```shell
|
| 246 |
+
git fetch --all --prune
|
| 247 |
+
git merge upstream/dev
|
| 248 |
+
```
|
| 249 |
+
|
| 250 |
+
如果你非常善于处理冲突,那么可以使用 rebase 的方式来解决冲突,因为这能够保证你的 commit log 的整洁。如果你不太熟悉 `rebase` 的使用,那么可以使用 `merge` 的方式来解决冲突。
|
| 251 |
+
|
| 252 |
+
## 指引
|
| 253 |
+
|
| 254 |
+
### 单元测试
|
| 255 |
+
|
| 256 |
+
在提交修复代码错误或新增特性的拉取请求时,我们应该尽可能的让单元测试覆盖所有提交的代码,计算单元测试覆盖率的方法如下
|
| 257 |
+
|
| 258 |
+
```shell
|
| 259 |
+
python -m coverage run -m pytest /path/to/test_file
|
| 260 |
+
python -m coverage html
|
| 261 |
+
# check file in htmlcov/index.html
|
| 262 |
+
```
|
| 263 |
+
|
| 264 |
+
### 文档渲染
|
| 265 |
+
|
| 266 |
+
在提交修复代码错误或新增特性的拉取请求时,可能会需要修改/新增模块的 docstring。我们需要确认渲染后的文档样式是正确的。
|
| 267 |
+
本地生成渲染后的文档的方法如下
|
| 268 |
+
|
| 269 |
+
```shell
|
| 270 |
+
pip install -r requirements/docs.txt
|
| 271 |
+
cd docs/zh_cn/
|
| 272 |
+
# or docs/en
|
| 273 |
+
make html
|
| 274 |
+
# check file in ./docs/zh_cn/_build/html/index.html
|
| 275 |
+
```
|
| 276 |
+
|
| 277 |
+
## 代码风格
|
| 278 |
+
|
| 279 |
+
### Python
|
| 280 |
+
|
| 281 |
+
[PEP8](https://www.python.org/dev/peps/pep-0008/) 作为 OpenMMLab 算法库首选的代码规范,我们使用以下工具检查和格式化代码
|
| 282 |
+
|
| 283 |
+
- [flake8](https://github.com/PyCQA/flake8):Python 官方发布的代码规范检查工具,是多个检查工具的封装
|
| 284 |
+
- [isort](https://github.com/timothycrosley/isort):自动调整模块导入顺序的工具
|
| 285 |
+
- [yapf](https://github.com/google/yapf):Google 发布的代码规范检查工具
|
| 286 |
+
- [codespell](https://github.com/codespell-project/codespell):检查单词拼写是否有误
|
| 287 |
+
- [mdformat](https://github.com/executablebooks/mdformat):检查 markdown 文件的工具
|
| 288 |
+
- [docformatter](https://github.com/myint/docformatter):格式化 docstring 的工具
|
| 289 |
+
|
| 290 |
+
yapf 和 isort 的配置可以在 [setup.cfg](../../../setup.cfg) 找到
|
| 291 |
+
|
| 292 |
+
通过配置 [pre-commit hook](https://pre-commit.com/) ,我们可以在提交代码时自动检查和格式化 `flake8`、`yapf`、`isort`、`trailing whitespaces`、`markdown files`,
|
| 293 |
+
修复 `end-of-files`、`double-quoted-strings`、`python-encoding-pragma`、`mixed-line-ending`,调整 `requirments.txt` 的包顺序。
|
| 294 |
+
pre-commit 钩子的配置可以在 [.pre-commit-config](../../../.pre-commit-config.yaml) 找到。
|
| 295 |
+
|
| 296 |
+
pre-commit 具体的安装使用方式见[拉取请求](#2-配置-pre-commit)。
|
| 297 |
+
|
| 298 |
+
更具体的规范请参考 [OpenMMLab 代码规范](../notes/code_style.md)。
|
| 299 |
+
|
| 300 |
+
### C++ and CUDA
|
| 301 |
+
|
| 302 |
+
C++ 和 CUDA 的代码规范遵从 [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html)
|
| 303 |
+
|
| 304 |
+
## 拉取请求规范
|
| 305 |
+
|
| 306 |
+
1. 使用 [pre-commit hook](https://pre-commit.com),尽量减少代码风格相关问题
|
| 307 |
+
|
| 308 |
+
2. 一个`拉取请求`对应一个短期分支
|
| 309 |
+
|
| 310 |
+
3. 粒度要细,一个`拉取请求`只做一件事情,避免超大的`拉取请求`
|
| 311 |
+
|
| 312 |
+
- Bad:实现 Faster R-CNN
|
| 313 |
+
- Acceptable:给 Faster R-CNN 添加一个 box head
|
| 314 |
+
- Good:给 box head 增加一个参数来支持自定义的 conv 层数
|
| 315 |
+
|
| 316 |
+
4. 每次 Commit 时需要提供清晰且有意义 commit 信息
|
| 317 |
+
|
| 318 |
+
5. 提供清晰且有意义的`拉取请求`描述
|
| 319 |
+
|
| 320 |
+
- 标题写明白任务名称,一般格式:\[Prefix\] Short description of the pull request (Suffix)
|
| 321 |
+
- prefix:新增功能 \[Feature\], 修 bug \[Fix\], 文档相关 \[Docs\], 开发中 \[WIP\] (暂时不会被 review)
|
| 322 |
+
- 描述里介绍`拉取请求`的主要修改内容,结果,以及对其他部分的影响, 参考`拉取请求`模板
|
| 323 |
+
- 关联相关的`议题` (issue) 和其他`拉取请求`
|
| 324 |
+
|
| 325 |
+
6. 如果引入了其他三方库,或借鉴了三方库的代码,请确认他们的许可证和 mmyolo 兼容,并在借鉴的代码上补充 `This code is inspired from http://`
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/dataset_preparation.md
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 数据集格式准备和说明
|
| 2 |
+
|
| 3 |
+
## DOTA 数据集
|
| 4 |
+
|
| 5 |
+
### 下载 DOTA 数据集
|
| 6 |
+
|
| 7 |
+
数据集可以从 DOTA 数据集的主页 [DOTA](https://captain-whu.github.io/DOTA/dataset.html)
|
| 8 |
+
或 [OpenDataLab](https://opendatalab.org.cn/DOTA_V1.0) 下载。
|
| 9 |
+
|
| 10 |
+
我们推荐使用 [OpenDataLab](https://opendatalab.org.cn/DOTA_V1.0) 下载,其中的文件夹结构已经按照需要排列好了,只需要解压即可,不需要费心去调整文件夹结构。
|
| 11 |
+
|
| 12 |
+
下载后解压数据集,并按如下文件夹结构存放。
|
| 13 |
+
|
| 14 |
+
```none
|
| 15 |
+
${DATA_ROOT}
|
| 16 |
+
├── train
|
| 17 |
+
│ ├── images
|
| 18 |
+
│ │ ├── P0000.png
|
| 19 |
+
│ │ ├── ...
|
| 20 |
+
│ ├── labelTxt-v1.0
|
| 21 |
+
│ │ ├── labelTxt
|
| 22 |
+
│ │ │ ├── P0000.txt
|
| 23 |
+
│ │ │ ├── ...
|
| 24 |
+
│ │ ├── trainset_reclabelTxt
|
| 25 |
+
│ │ │ ├── P0000.txt
|
| 26 |
+
│ │ │ ├── ...
|
| 27 |
+
├── val
|
| 28 |
+
│ ├── images
|
| 29 |
+
│ │ ├── P0003.png
|
| 30 |
+
│ │ ├── ...
|
| 31 |
+
│ ├── labelTxt-v1.0
|
| 32 |
+
│ │ ├── labelTxt
|
| 33 |
+
│ │ │ ├── P0003.txt
|
| 34 |
+
│ │ │ ├── ...
|
| 35 |
+
│ │ ├── valset_reclabelTxt
|
| 36 |
+
│ │ │ ├── P0003.txt
|
| 37 |
+
│ │ │ ├── ...
|
| 38 |
+
├── test
|
| 39 |
+
│ ├── images
|
| 40 |
+
│ │ ├── P0006.png
|
| 41 |
+
│ │ ├── ...
|
| 42 |
+
|
| 43 |
+
```
|
| 44 |
+
|
| 45 |
+
其中,以 `reclabelTxt` 为结尾的文件夹存放了水平检测框的标注,目前仅使用了 `labelTxt-v1.0` 中旋转框的标注。
|
| 46 |
+
|
| 47 |
+
### 数据集切片
|
| 48 |
+
|
| 49 |
+
我们提供了 `tools/dataset_converters/dota/dota_split.py` 脚本用于 DOTA 数据集的准备和切片。
|
| 50 |
+
|
| 51 |
+
```shell
|
| 52 |
+
python tools/dataset_converters/dota/dota_split.py \
|
| 53 |
+
[--splt-config ${SPLIT_CONFIG}] \
|
| 54 |
+
[--data-root ${DATA_ROOT}] \
|
| 55 |
+
[--out-dir ${OUT_DIR}] \
|
| 56 |
+
[--ann-subdir ${ANN_SUBDIR}] \
|
| 57 |
+
[--phase ${DATASET_PHASE}] \
|
| 58 |
+
[--nproc ${NPROC}] \
|
| 59 |
+
[--save-ext ${SAVE_EXT}] \
|
| 60 |
+
[--overwrite]
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
脚本依赖于 shapely 包,请先通过 `pip install shapely` 安装 shapely。
|
| 64 |
+
|
| 65 |
+
**参数说明**:
|
| 66 |
+
|
| 67 |
+
- `--splt-config` : 切片参数的配置文件。
|
| 68 |
+
- `--data-root`: DOTA 数据集的存放位置。
|
| 69 |
+
- `--out-dir`: 切片后的输出位置。
|
| 70 |
+
- `--ann-subdir`: 标注文件夹的名字。 默认为 `labelTxt-v1.0` 。
|
| 71 |
+
- `--phase`: 数据集的阶段。默认为 `trainval test` 。
|
| 72 |
+
- `--nproc`: 进程数量。 默认为 8 。
|
| 73 |
+
- `--save-ext`: 输出图像的扩展名,如置空则与原图保持一致。 默认为 `None` 。
|
| 74 |
+
- `--overwrite`: 如果目标文件夹已存在,是否允许覆盖。
|
| 75 |
+
|
| 76 |
+
基于 DOTA 数据集论文中提供的配置,我们提供了两种切片配置。
|
| 77 |
+
|
| 78 |
+
`./split_config/single_scale.json` 用于单尺度 `single-scale` 切片
|
| 79 |
+
`./split_config/multi_scale.json` 用于多尺度 `multi-scale` 切片
|
| 80 |
+
|
| 81 |
+
DOTA 数据集通常使用 `trainval` 集进行训练,然后使用 `test` 集进行在线验证,大多数论文提供的也是在线验证的精度。
|
| 82 |
+
如果你需要进行本地验证,可以准备 `train` 集和 `val` 集进行训练和测试。
|
| 83 |
+
|
| 84 |
+
示例:
|
| 85 |
+
|
| 86 |
+
使用单尺度切片配置准备 `trainval` 和 `test` 集
|
| 87 |
+
|
| 88 |
+
```shell
|
| 89 |
+
python tools/dataset_converters/dota/dota_split.py
|
| 90 |
+
--split-config 'tools/dataset_converters/dota/split_config/single_scale.json'
|
| 91 |
+
--data-root ${DATA_ROOT} \
|
| 92 |
+
--out-dir ${OUT_DIR}
|
| 93 |
+
```
|
| 94 |
+
|
| 95 |
+
准备 DOTA-v1.5 数据集,它的标注文件夹名字是 `labelTxt-v1.5`
|
| 96 |
+
|
| 97 |
+
```shell
|
| 98 |
+
python tools/dataset_converters/dota/dota_split.py
|
| 99 |
+
--split-config 'tools/dataset_converters/dota/split_config/single_scale.json'
|
| 100 |
+
--data-root ${DATA_ROOT} \
|
| 101 |
+
--out-dir ${OUT_DIR} \
|
| 102 |
+
--ann-subdir 'labelTxt-v1.5'
|
| 103 |
+
```
|
| 104 |
+
|
| 105 |
+
使用单尺度切片配置准备 `train` 和 `val` 集
|
| 106 |
+
|
| 107 |
+
```shell
|
| 108 |
+
python tools/dataset_converters/dota/dota_split.py
|
| 109 |
+
--split-config 'tools/dataset_converters/dota/split_config/single_scale.json'
|
| 110 |
+
--data-root ${DATA_ROOT} \
|
| 111 |
+
--phase train val \
|
| 112 |
+
--out-dir ${OUT_DIR}
|
| 113 |
+
```
|
| 114 |
+
|
| 115 |
+
使用多尺度切片配置准备 `trainval` 和 `test` 集
|
| 116 |
+
|
| 117 |
+
```shell
|
| 118 |
+
python tools/dataset_converters/dota/dota_split.py
|
| 119 |
+
--split-config 'tools/dataset_converters/dota/split_config/multi_scale.json'
|
| 120 |
+
--data-root ${DATA_ROOT} \
|
| 121 |
+
--out-dir ${OUT_DIR}
|
| 122 |
+
```
|
| 123 |
+
|
| 124 |
+
在运行完成后,输出的结构如下:
|
| 125 |
+
|
| 126 |
+
```none
|
| 127 |
+
${OUT_DIR}
|
| 128 |
+
├── trainval
|
| 129 |
+
│ ├── images
|
| 130 |
+
│ │ ├── P0000__1024__0___0.png
|
| 131 |
+
│ │ ├── ...
|
| 132 |
+
│ ├── annfiles
|
| 133 |
+
│ │ ├── P0000__1024__0___0.txt
|
| 134 |
+
│ │ ├── ...
|
| 135 |
+
├── test
|
| 136 |
+
│ ├── images
|
| 137 |
+
│ │ ├── P0006__1024__0___0.png
|
| 138 |
+
│ │ ├── ...
|
| 139 |
+
│ ├── annfiles
|
| 140 |
+
│ │ ├── P0006__1024__0___0.txt
|
| 141 |
+
│ │ ├── ...
|
| 142 |
+
```
|
| 143 |
+
|
| 144 |
+
此时将配置文件中的 `data_root` 修改为 ${OUT_DIR} 即可开始使用 DOTA 数据集训练。
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/deploy/easydeploy_guide.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# EasyDeploy 部署
|
| 2 |
+
|
| 3 |
+
本项目作为 MMYOLO 的部署 project 单独存在,意图剥离 MMDeploy 当前的体系,独自支持用户完成模型训练后的转换和部署功能,使用户的学习和工程成本下降。
|
| 4 |
+
|
| 5 |
+
当前支持对 ONNX 格式和 TensorRT 格式的转换,后续对其他推理平台也会支持起来。
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/deploy/index.rst
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MMDeploy 部署必备教程
|
| 2 |
+
************************
|
| 3 |
+
|
| 4 |
+
.. toctree::
|
| 5 |
+
:maxdepth: 1
|
| 6 |
+
|
| 7 |
+
mmdeploy_guide.md
|
| 8 |
+
mmdeploy_yolov5.md
|
| 9 |
+
|
| 10 |
+
EasyDeploy 部署必备教程
|
| 11 |
+
************************
|
| 12 |
+
|
| 13 |
+
.. toctree::
|
| 14 |
+
:maxdepth: 1
|
| 15 |
+
|
| 16 |
+
easydeploy_guide.md
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/deploy/mmdeploy_guide.md
ADDED
|
@@ -0,0 +1,415 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# MMDeploy 部署
|
| 2 |
+
|
| 3 |
+
## MMDeploy 介绍
|
| 4 |
+
|
| 5 |
+
MMDeploy 是 [OpenMMLab](https://openmmlab.com/) 模型部署工具箱,**为各算法库提供统一的部署体验**。基于 MMDeploy,开发者可以轻松从训练 repo 生成指定硬件所需 SDK,省去大量适配时间。
|
| 6 |
+
|
| 7 |
+
更多介绍和使用指南见 https://mmdeploy.readthedocs.io/zh_CN/latest/get_started.html
|
| 8 |
+
|
| 9 |
+
## 算法支持列表
|
| 10 |
+
|
| 11 |
+
目前支持的 model-backend 组合:
|
| 12 |
+
|
| 13 |
+
| Model | Task | OnnxRuntime | TensorRT | Model config |
|
| 14 |
+
| :----- | :-------------- | :---------: | :------: | :---------------------------------------------------------------------: |
|
| 15 |
+
| YOLOv5 | ObjectDetection | Y | Y | [config](https://github.com/open-mmlab/mmyolo/tree/main/configs/yolov5) |
|
| 16 |
+
| YOLOv6 | ObjectDetection | Y | Y | [config](https://github.com/open-mmlab/mmyolo/tree/main/configs/yolov6) |
|
| 17 |
+
| YOLOX | ObjectDetection | Y | Y | [config](https://github.com/open-mmlab/mmyolo/tree/main/configs/yolox) |
|
| 18 |
+
| RTMDet | ObjectDetection | Y | Y | [config](https://github.com/open-mmlab/mmyolo/tree/main/configs/rtmdet) |
|
| 19 |
+
|
| 20 |
+
ncnn 和其他后端的支持会在后续支持。
|
| 21 |
+
|
| 22 |
+
## 安装
|
| 23 |
+
|
| 24 |
+
按照[说明](https://mmdeploy.readthedocs.io/zh_CN/latest/get_started.html)安装 mmdeploy。
|
| 25 |
+
|
| 26 |
+
```{note}
|
| 27 |
+
如果安装的是 mmdeploy 预编译包,那么也请通过 ‘git clone https://github.com/open-mmlab/mmdeploy.git –depth=1’ 下载 mmdeploy 源码。因为它包含了部署时所需的 tools 文件夹。
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
## MMYOLO 中部署相关配置说明
|
| 31 |
+
|
| 32 |
+
所有部署配置文件在 [`configs/deploy`](../../../configs/deploy/) 目录下。
|
| 33 |
+
|
| 34 |
+
您可以部署静态输入或者动态输入的模型,因此您需要修改模型配置文件中与此相关的数据处理流程。
|
| 35 |
+
|
| 36 |
+
MMDeploy 将后处理整合到自定义的算子中,因此您可以修改 `codebase_config` 中的 `post_processing` 参数来调整后处理策略,参数描述如下:
|
| 37 |
+
|
| 38 |
+
```python
|
| 39 |
+
codebase_config = dict(
|
| 40 |
+
type='mmyolo',
|
| 41 |
+
task='ObjectDetection',
|
| 42 |
+
model_type='end2end',
|
| 43 |
+
post_processing=dict(
|
| 44 |
+
score_threshold=0.05,
|
| 45 |
+
confidence_threshold=0.005,
|
| 46 |
+
iou_threshold=0.5,
|
| 47 |
+
max_output_boxes_per_class=200,
|
| 48 |
+
pre_top_k=5000,
|
| 49 |
+
keep_top_k=100,
|
| 50 |
+
background_label_id=-1),
|
| 51 |
+
module=['mmyolo.deploy'])
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
- `score_threshold`:在 `nms` 之前筛选候选框的类别分数阈值。
|
| 55 |
+
- `confidence_threshold`:在 `nms` 之前筛选候选框的置信度分数阈值。
|
| 56 |
+
- `iou_threshold`:在 `nms` 中去除重复框的 `iou` 阈值。
|
| 57 |
+
- `max_output_boxes_per_class`:每个类别最大的输出框数量。
|
| 58 |
+
- `pre_top_k`:在 `nms` 之前对候选框分数排序然后固定候选框的个数。
|
| 59 |
+
- `keep_top_k`:`nms` 算法最终输出的候选框个数。
|
| 60 |
+
- `background_label_id`:MMYOLO 算法中没有背景类别信息,置为 `-1` 即可。
|
| 61 |
+
|
| 62 |
+
### 静态输入配置
|
| 63 |
+
|
| 64 |
+
#### (1) 模型配置文件介绍
|
| 65 |
+
|
| 66 |
+
以 MMYOLO 中的 `YOLOv5` 模型配置为例,下面是对部署时使用的模型配置文件参数说明介绍。
|
| 67 |
+
|
| 68 |
+
```python
|
| 69 |
+
_base_ = '../../yolov5/yolov5_s-v61_syncbn_8xb16-300e_coco.py'
|
| 70 |
+
|
| 71 |
+
test_pipeline = [
|
| 72 |
+
dict(type='LoadImageFromFile', backend_args=_base_.backend_args),
|
| 73 |
+
dict(
|
| 74 |
+
type='LetterResize',
|
| 75 |
+
scale=_base_.img_scale,
|
| 76 |
+
allow_scale_up=False,
|
| 77 |
+
use_mini_pad=False,
|
| 78 |
+
),
|
| 79 |
+
dict(type='LoadAnnotations', with_bbox=True, _scope_='mmdet'),
|
| 80 |
+
dict(
|
| 81 |
+
type='mmdet.PackDetInputs',
|
| 82 |
+
meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',
|
| 83 |
+
'scale_factor', 'pad_param'))
|
| 84 |
+
]
|
| 85 |
+
|
| 86 |
+
test_dataloader = dict(
|
| 87 |
+
dataset=dict(pipeline=test_pipeline, batch_shapes_cfg=None))
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
`_base_ = '../../yolov5/yolov5_s-v61_syncbn_8xb16-300e_coco.py'` 继承了训练时构建模型的配置。
|
| 91 |
+
|
| 92 |
+
`test_pipeline` 为部署时对输入图像进行处理的流程,`LetterResize` 控制了输入图像的尺寸,同时限制了导出模型所能接受的输入尺寸。
|
| 93 |
+
|
| 94 |
+
`test_dataloader` 为部署时构建数据加载器配置,`batch_shapes_cfg` 控制了是否启用 `batch_shapes` 策略,详细内容可以参考 [yolov5 配置文件说明](../../tutorials/config.md) 。
|
| 95 |
+
|
| 96 |
+
#### (2) 部署配置文件介绍
|
| 97 |
+
|
| 98 |
+
以 `MMYOLO` 中的 `YOLOv5` 部署配置为例,下面是对配置文件参数说明介绍。
|
| 99 |
+
|
| 100 |
+
`ONNXRuntime` 部署 `YOLOv5` 可以使用 [`detection_onnxruntime_static.py`](https://github.com/open-mmlab/mmyolo/blob/main/configs/deploy/detection_onnxruntime_static.py) 配置。
|
| 101 |
+
|
| 102 |
+
```python
|
| 103 |
+
_base_ = ['./base_static.py']
|
| 104 |
+
codebase_config = dict(
|
| 105 |
+
type='mmyolo',
|
| 106 |
+
task='ObjectDetection',
|
| 107 |
+
model_type='end2end',
|
| 108 |
+
post_processing=dict(
|
| 109 |
+
score_threshold=0.05,
|
| 110 |
+
confidence_threshold=0.005,
|
| 111 |
+
iou_threshold=0.5,
|
| 112 |
+
max_output_boxes_per_class=200,
|
| 113 |
+
pre_top_k=5000,
|
| 114 |
+
keep_top_k=100,
|
| 115 |
+
background_label_id=-1),
|
| 116 |
+
module=['mmyolo.deploy'])
|
| 117 |
+
backend_config = dict(type='onnxruntime')
|
| 118 |
+
```
|
| 119 |
+
|
| 120 |
+
`backend_config` 中指定了部署后端 `type='onnxruntime'`,其他信息可参考第三小节。
|
| 121 |
+
|
| 122 |
+
`TensorRT` 部署 `YOLOv5` 可以使用 [`detection_tensorrt_static-640x640.py`](https://github.com/open-mmlab/mmyolo/blob/main/configs/deploy/detection_tensorrt_static-640x640.py) 配置。
|
| 123 |
+
|
| 124 |
+
```python
|
| 125 |
+
_base_ = ['./base_static.py']
|
| 126 |
+
onnx_config = dict(input_shape=(640, 640))
|
| 127 |
+
backend_config = dict(
|
| 128 |
+
type='tensorrt',
|
| 129 |
+
common_config=dict(fp16_mode=False, max_workspace_size=1 << 30),
|
| 130 |
+
model_inputs=[
|
| 131 |
+
dict(
|
| 132 |
+
input_shapes=dict(
|
| 133 |
+
input=dict(
|
| 134 |
+
min_shape=[1, 3, 640, 640],
|
| 135 |
+
opt_shape=[1, 3, 640, 640],
|
| 136 |
+
max_shape=[1, 3, 640, 640])))
|
| 137 |
+
])
|
| 138 |
+
use_efficientnms = False
|
| 139 |
+
```
|
| 140 |
+
|
| 141 |
+
`backend_config` 中指定了后端 `type=‘tensorrt’`。
|
| 142 |
+
|
| 143 |
+
与 `ONNXRuntime` 部署配置不同的是,`TensorRT` 需要指定输入图片尺寸和构建引擎文件需要的参数,包括:
|
| 144 |
+
|
| 145 |
+
- `onnx_config` 中指定 `input_shape=(640, 640)`
|
| 146 |
+
- `backend_config['common_config']` 中包括 `fp16_mode=False` 和 `max_workspace_size=1 << 30`, `fp16_mode` 表示是否以 `fp16` 的参数格式构建引擎,`max_workspace_size` 表示当前 `gpu` 设备最大显存, 单位为 `GB`。`fp16` 的详细配置可以参考 [`detection_tensorrt-fp16_static-640x640.py`](https://github.com/open-mmlab/mmyolo/blob/main/configs/deploy/detection_tensorrt-fp16_static-640x640.py)
|
| 147 |
+
- `backend_config['model_inputs']['input_shapes']['input']` 中 `min_shape` /`opt_shape`/`max_shape` 对应的值在静态输入下应该保持相同,即默认均为 `[1, 3, 640, 640]`。
|
| 148 |
+
|
| 149 |
+
`use_efficientnms` 是 `MMYOLO` 系列新引入的配置,表示在导出 `onnx` 时是否启用`Efficient NMS Plugin`来替换 `MMDeploy` 中的 `TRTBatchedNMS plugin` 。
|
| 150 |
+
|
| 151 |
+
可以参考 `TensorRT` 官方实现的 [Efficient NMS Plugin](https://github.com/NVIDIA/TensorRT/blob/main/plugin/efficientNMSPlugin/README.md) 获取更多详细信息。
|
| 152 |
+
|
| 153 |
+
**注意**,这个功能仅仅在 TensorRT >= 8.0 版本才能使用,无需编译开箱即用。
|
| 154 |
+
|
| 155 |
+
### 动态输入配置
|
| 156 |
+
|
| 157 |
+
#### (1) 模型配置文件介绍
|
| 158 |
+
|
| 159 |
+
当您部署动态输入模型时,您无需修改任何模型配置文件,仅需要修改部署配置文件即可。
|
| 160 |
+
|
| 161 |
+
#### (2) 部署配置文件介绍
|
| 162 |
+
|
| 163 |
+
`ONNXRuntime` 部署 `YOLOv5` 可以使用 [`detection_onnxruntime_dynamic.py`](https://github.com/open-mmlab/mmyolo/blob/main/configs/deploy/detection_onnxruntime_dynamic.py) 配置。
|
| 164 |
+
|
| 165 |
+
```python
|
| 166 |
+
_base_ = ['./base_dynamic.py']
|
| 167 |
+
codebase_config = dict(
|
| 168 |
+
type='mmyolo',
|
| 169 |
+
task='ObjectDetection',
|
| 170 |
+
model_type='end2end',
|
| 171 |
+
post_processing=dict(
|
| 172 |
+
score_threshold=0.05,
|
| 173 |
+
confidence_threshold=0.005,
|
| 174 |
+
iou_threshold=0.5,
|
| 175 |
+
max_output_boxes_per_class=200,
|
| 176 |
+
pre_top_k=5000,
|
| 177 |
+
keep_top_k=100,
|
| 178 |
+
background_label_id=-1),
|
| 179 |
+
module=['mmyolo.deploy'])
|
| 180 |
+
backend_config = dict(type='onnxruntime')
|
| 181 |
+
```
|
| 182 |
+
|
| 183 |
+
`backend_config` 中指定了后端 `type='onnxruntime'`,其他配置与上一节在 ONNXRuntime 部署静态输入模型相同。
|
| 184 |
+
|
| 185 |
+
`TensorRT` 部署 `YOLOv5` 可以使用 [`detection_tensorrt_dynamic-192x192-960x960.py`](https://github.com/open-mmlab/mmyolo/blob/main/configs/deploy/detection_tensorrt_dynamic-192x192-960x960.py) 配置。
|
| 186 |
+
|
| 187 |
+
```python
|
| 188 |
+
_base_ = ['./base_dynamic.py']
|
| 189 |
+
backend_config = dict(
|
| 190 |
+
type='tensorrt',
|
| 191 |
+
common_config=dict(fp16_mode=False, max_workspace_size=1 << 30),
|
| 192 |
+
model_inputs=[
|
| 193 |
+
dict(
|
| 194 |
+
input_shapes=dict(
|
| 195 |
+
input=dict(
|
| 196 |
+
min_shape=[1, 3, 192, 192],
|
| 197 |
+
opt_shape=[1, 3, 640, 640],
|
| 198 |
+
max_shape=[1, 3, 960, 960])))
|
| 199 |
+
])
|
| 200 |
+
use_efficientnms = False
|
| 201 |
+
```
|
| 202 |
+
|
| 203 |
+
`backend_config` 中指定了后端 `type='tensorrt'`,由于 `TensorRT` 动态输入与静态输入有所不同,您可以了解更多动态输入相关信息通过访问 [TensorRT dynamic input official introduction](https://docs.nvidia.com/deeplearning/tensorrt/archives/tensorrt-843/developer-guide/index.html#work_dynamic_shapes)。
|
| 204 |
+
|
| 205 |
+
`TensorRT` 部署需要配置 `min_shape`, `opt_shape`, `max_shape` ,`TensorRT` 限制输入图片的尺寸在 `min_shape` 和 ` max_shape` 之间。
|
| 206 |
+
|
| 207 |
+
`min_shape` 为输入图片的最小尺寸,`opt_shape` 为输入图片常见尺寸, 在这个尺寸下推理性能最好,`max_shape` 为输入图片的最大尺寸。
|
| 208 |
+
|
| 209 |
+
`use_efficientnms` 配置与上节 `TensorRT` 静态输入配置相同。
|
| 210 |
+
|
| 211 |
+
### INT8 量化配置
|
| 212 |
+
|
| 213 |
+
!!! 部署 TensorRT INT8 模型教程即将发布 !!!
|
| 214 |
+
|
| 215 |
+
## 模型转换
|
| 216 |
+
|
| 217 |
+
### 使用方法
|
| 218 |
+
|
| 219 |
+
#### 从源码安装的 MMDeploy
|
| 220 |
+
|
| 221 |
+
设置 `MMDeploy` 根目录为环境变量 `MMDEPLOY_DIR` ,例如 `export MMDEPLOY_DIR=/the/root/path/of/MMDeploy`
|
| 222 |
+
|
| 223 |
+
```shell
|
| 224 |
+
python3 ${MMDEPLOY_DIR}/tools/deploy.py \
|
| 225 |
+
${DEPLOY_CFG_PATH} \
|
| 226 |
+
${MODEL_CFG_PATH} \
|
| 227 |
+
${MODEL_CHECKPOINT_PATH} \
|
| 228 |
+
${INPUT_IMG} \
|
| 229 |
+
--test-img ${TEST_IMG} \
|
| 230 |
+
--work-dir ${WORK_DIR} \
|
| 231 |
+
--calib-dataset-cfg ${CALIB_DATA_CFG} \
|
| 232 |
+
--device ${DEVICE} \
|
| 233 |
+
--log-level INFO \
|
| 234 |
+
--show \
|
| 235 |
+
--dump-info
|
| 236 |
+
```
|
| 237 |
+
|
| 238 |
+
### 参数描述
|
| 239 |
+
|
| 240 |
+
- `deploy_cfg` : mmdeploy 针对此模型的部署配置,包含推理框架类型、是否量化、输入 shape 是否动态等。配置文件之间可能有引用关系,`configs/deploy/detection_onnxruntime_static.py` 是一个示例。
|
| 241 |
+
- `model_cfg` : MMYOLO 算法库的模型配置,例如 `configs/deploy/model/yolov5_s-deploy.py`, 与 mmdeploy 的路径无关。
|
| 242 |
+
- `checkpoint` : torch 模型路径。可以 http/https 开头,详见 `mmengine.fileio` 的实现。
|
| 243 |
+
- `img` : 模型转换时,用做测试的图像文件路径。
|
| 244 |
+
- `--test-img` : 用于测试模型的图像文件路径。默认设置成`None`。
|
| 245 |
+
- `--work-dir` : 工作目录,用来保存日志和模型文件。
|
| 246 |
+
- `--calib-dataset-cfg` : 此参数只有int8模式下生效,用于校准数据集配置文件。若在int8模式下未传入参数,则会自动使用模型配置文件中的'val'数据集进行校准。
|
| 247 |
+
- `--device` : 用于模型转换的设备。 默认是`cpu`,对于 trt 可使用 `cuda:0` 这种形式。
|
| 248 |
+
- `--log-level` : 设置日记的等级,选项包括`'CRITICAL', 'FATAL', 'ERROR', 'WARN', 'WARNING', 'INFO', 'DEBUG', 'NOTSET'`。 默认是`INFO`。
|
| 249 |
+
- `--show` : 是否显示检测的结果。
|
| 250 |
+
- `--dump-info` : 是否输出 SDK 信息。
|
| 251 |
+
|
| 252 |
+
#### 通过 pip install 安装的 MMDeploy
|
| 253 |
+
|
| 254 |
+
假设当前的工作目录为 mmyolo 的根目录, 那么以 [YoloV5](https://github.com/open-mmlab/mmyolo/blob/main/configs/yolov5/yolov5_s-v61_syncbn_8xb16-300e_coco.py) 模型为例,你可以从[此处](https://download.openmmlab.com/mmyolo/v0/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco/yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth)下载对应的 checkpoint,并使用以下代码将之转换为 onnx 模型:
|
| 255 |
+
|
| 256 |
+
```python
|
| 257 |
+
from mmdeploy.apis import torch2onnx
|
| 258 |
+
from mmdeploy.backend.sdk.export_info import export2SDK
|
| 259 |
+
|
| 260 |
+
img = 'demo/demo.jpg'
|
| 261 |
+
work_dir = 'mmdeploy_models/mmyolo/onnx'
|
| 262 |
+
save_file = 'end2end.onnx'
|
| 263 |
+
deploy_cfg = 'configs/deploy/detection_onnxruntime_dynamic.py'
|
| 264 |
+
model_cfg = 'configs/yolov5/yolov5_s-v61_syncbn_8xb16-300e_coco.py'
|
| 265 |
+
model_checkpoint = 'checkpoints/yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth'
|
| 266 |
+
device = 'cpu'
|
| 267 |
+
|
| 268 |
+
# 1. convert model to onnx
|
| 269 |
+
torch2onnx(img, work_dir, save_file, deploy_cfg, model_cfg,
|
| 270 |
+
model_checkpoint, device)
|
| 271 |
+
|
| 272 |
+
# 2. extract pipeline info for inference by MMDeploy SDK
|
| 273 |
+
export2SDK(deploy_cfg, model_cfg, work_dir, pth=model_checkpoint,
|
| 274 |
+
device=device)
|
| 275 |
+
```
|
| 276 |
+
|
| 277 |
+
## 模型规范
|
| 278 |
+
|
| 279 |
+
在使用转换后的模型进行推理之前,有必要了解转换结果的结构。 它存放在 `--work-dir` 指定的路路径下。
|
| 280 |
+
|
| 281 |
+
上例中的`mmdeploy_models/mmyolo/onnx`,结构如下:
|
| 282 |
+
|
| 283 |
+
```
|
| 284 |
+
mmdeploy_models/mmyolo/onnx
|
| 285 |
+
├── deploy.json
|
| 286 |
+
├── detail.json
|
| 287 |
+
├── end2end.onnx
|
| 288 |
+
└── pipeline.json
|
| 289 |
+
```
|
| 290 |
+
|
| 291 |
+
重要的是:
|
| 292 |
+
|
| 293 |
+
- **end2end.onnx**: 推理引擎文件。可用 ONNX Runtime 推理
|
| 294 |
+
- ***xxx*.json**: mmdeploy SDK 推理所需的 meta 信息
|
| 295 |
+
|
| 296 |
+
整个文件夹被定义为**mmdeploy SDK model**。换言之,**mmdeploy SDK model**既包括推理引擎,也包括推理 meta 信息。
|
| 297 |
+
|
| 298 |
+
## 模型推理
|
| 299 |
+
|
| 300 |
+
### 后端模型推理
|
| 301 |
+
|
| 302 |
+
以上述模型转换后的 `end2end.onnx` 为例,你可以使用如下代码进行推理:
|
| 303 |
+
|
| 304 |
+
```python
|
| 305 |
+
from mmdeploy.apis.utils import build_task_processor
|
| 306 |
+
from mmdeploy.utils import get_input_shape, load_config
|
| 307 |
+
import torch
|
| 308 |
+
|
| 309 |
+
deploy_cfg = 'configs/deploy/detection_onnxruntime_dynamic.py'
|
| 310 |
+
model_cfg = 'configs/yolov5/yolov5_s-v61_syncbn_8xb16-300e_coco.py'
|
| 311 |
+
device = 'cpu'
|
| 312 |
+
backend_model = ['mmdeploy_models/mmyolo/onnx/end2end.onnx']
|
| 313 |
+
image = 'demo/demo.jpg'
|
| 314 |
+
|
| 315 |
+
# read deploy_cfg and model_cfg
|
| 316 |
+
deploy_cfg, model_cfg = load_config(deploy_cfg, model_cfg)
|
| 317 |
+
|
| 318 |
+
# build task and backend model
|
| 319 |
+
task_processor = build_task_processor(model_cfg, deploy_cfg, device)
|
| 320 |
+
model = task_processor.build_backend_model(backend_model)
|
| 321 |
+
|
| 322 |
+
# process input image
|
| 323 |
+
input_shape = get_input_shape(deploy_cfg)
|
| 324 |
+
model_inputs, _ = task_processor.create_input(image, input_shape)
|
| 325 |
+
|
| 326 |
+
# do model inference
|
| 327 |
+
with torch.no_grad():
|
| 328 |
+
result = model.test_step(model_inputs)
|
| 329 |
+
|
| 330 |
+
# visualize results
|
| 331 |
+
task_processor.visualize(
|
| 332 |
+
image=image,
|
| 333 |
+
model=model,
|
| 334 |
+
result=result[0],
|
| 335 |
+
window_name='visualize',
|
| 336 |
+
output_file='work_dir/output_detection.png')
|
| 337 |
+
```
|
| 338 |
+
|
| 339 |
+
运行上述代码后,你可以在 `work_dir` 中看到推理的结果图片 `output_detection.png`。
|
| 340 |
+
|
| 341 |
+
### SDK模型推理
|
| 342 |
+
|
| 343 |
+
你也可以参考如下代码,对 SDK model 进行推理:
|
| 344 |
+
|
| 345 |
+
```python
|
| 346 |
+
from mmdeploy_runtime import Detector
|
| 347 |
+
import cv2
|
| 348 |
+
|
| 349 |
+
img = cv2.imread('demo/demo.jpg')
|
| 350 |
+
|
| 351 |
+
# create a detector
|
| 352 |
+
detector = Detector(model_path='mmdeploy_models/mmyolo/onnx',
|
| 353 |
+
device_name='cpu', device_id=0)
|
| 354 |
+
# perform inference
|
| 355 |
+
bboxes, labels, masks = detector(img)
|
| 356 |
+
|
| 357 |
+
# visualize inference result
|
| 358 |
+
indices = [i for i in range(len(bboxes))]
|
| 359 |
+
for index, bbox, label_id in zip(indices, bboxes, labels):
|
| 360 |
+
[left, top, right, bottom], score = bbox[0:4].astype(int), bbox[4]
|
| 361 |
+
if score < 0.3:
|
| 362 |
+
continue
|
| 363 |
+
|
| 364 |
+
cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0))
|
| 365 |
+
|
| 366 |
+
cv2.imwrite('work_dir/output_detection.png', img)
|
| 367 |
+
```
|
| 368 |
+
|
| 369 |
+
除了python API,mmdeploy SDK 还提供了诸如 C、C++、C#、Java等多语言接口。
|
| 370 |
+
你可以参考[样例](https://github.com/open-mmlab/mmdeploy/tree/main/demo)学习其他语言接口的使用方法。
|
| 371 |
+
|
| 372 |
+
## 模型评测
|
| 373 |
+
|
| 374 |
+
当您将 PyTorch 模型转换为后端支持的模型后,您可能需要验证模型的精度,使用 `${MMDEPLOY_DIR}/tools/test.py`
|
| 375 |
+
|
| 376 |
+
```shell
|
| 377 |
+
python3 ${MMDEPLOY_DIR}/tools/test.py \
|
| 378 |
+
${DEPLOY_CFG} \
|
| 379 |
+
${MODEL_CFG} \
|
| 380 |
+
--model ${BACKEND_MODEL_FILES} \
|
| 381 |
+
--device ${DEVICE} \
|
| 382 |
+
--work-dir ${WORK_DIR} \
|
| 383 |
+
[--cfg-options ${CFG_OPTIONS}] \
|
| 384 |
+
[--show] \
|
| 385 |
+
[--show-dir ${OUTPUT_IMAGE_DIR}] \
|
| 386 |
+
[--interval ${INTERVAL}] \
|
| 387 |
+
[--wait-time ${WAIT_TIME}] \
|
| 388 |
+
[--log2file work_dirs/output.txt]
|
| 389 |
+
[--speed-test] \
|
| 390 |
+
[--warmup ${WARM_UP}] \
|
| 391 |
+
[--log-interval ${LOG_INTERVERL}] \
|
| 392 |
+
[--batch-size ${BATCH_SIZE}] \
|
| 393 |
+
[--uri ${URI}]
|
| 394 |
+
```
|
| 395 |
+
|
| 396 |
+
### 参数描述
|
| 397 |
+
|
| 398 |
+
- `deploy_cfg`: 部署配置文件。
|
| 399 |
+
- `model_cfg`: MMYOLO 模型配置文件。
|
| 400 |
+
- `--model`: 导出的后端模型。 例如, 如果我们导出了 TensorRT 模型,我们需要传入后缀为 ".engine" 文件路径。
|
| 401 |
+
- `--device`: 运行模型的设备。请注意,某些后端会限制设备。例如,TensorRT 必须在 cuda 上运行。
|
| 402 |
+
- `--work-dir`: 模型转换、报告生成的路径。
|
| 403 |
+
- `--cfg-options`: 传入额外的配置,将会覆盖当前部署配置。
|
| 404 |
+
- `--show`: 是否在屏幕上显示评估结果。
|
| 405 |
+
- `--show-dir`: 保存评估结果的目录。(只有给出这个参数才会保存结果)。
|
| 406 |
+
- `--interval`: 屏幕上显示评估结果的间隔。
|
| 407 |
+
- `--wait-time`: 每个窗口的显示时间
|
| 408 |
+
- `--log2file`: 将评估结果(和速度)记录到文件中。
|
| 409 |
+
- `--speed-test`: 是否开启速度测试。
|
| 410 |
+
- `--warmup`: 在计算推理时间之前进行预热,需要先开启 `speed-test`。
|
| 411 |
+
- `--log-interval`: 每个日志之间的间隔,需要先设置 `speed-test`。
|
| 412 |
+
- `--batch-size`: 推理的批量大小,它将覆盖数据配置中的 `samples_per_gpu`。默认为 `1`。请注意,并非所有模型都支持 `batch_size > 1`。
|
| 413 |
+
- `--uri`: 在边缘设备上推理时的 ipv4 或 ipv6 端口号。
|
| 414 |
+
|
| 415 |
+
注意:`${MMDEPLOY_DIR}/tools/test.py` 中的其他参数用于速度测试。他们不影响评估。
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# YOLOv5 部署全流程说明
|
| 2 |
+
|
| 3 |
+
请先参考 [`部署必备指南`](./mmdeploy_guide.md) 了解部署配置文件等相关信息。
|
| 4 |
+
|
| 5 |
+
## 模型训练和测试
|
| 6 |
+
|
| 7 |
+
模型训练和测试请参考 [YOLOv5 从入门到部署全流程](./mmdeploy_yolov5.md) 。
|
| 8 |
+
|
| 9 |
+
## 准备 MMDeploy 运行环境
|
| 10 |
+
|
| 11 |
+
安装 `MMDeploy` 请参考 [`源码手动安装`](https://github.com/open-mmlab/mmdeploy/blob/dev-1.x/docs/zh_cn/01-how-to-build/build_from_source.md) ,选择您所使用的平台编译 `MMDeploy` 和自定义算子。
|
| 12 |
+
|
| 13 |
+
*注意!* 如果环境安装有问题,可以查看 [`MMDeploy FAQ`](https://github.com/open-mmlab/mmdeploy/blob/dev-1.x/docs/zh_cn/faq.md) 或者在 `issuse` 中提出您的问题。
|
| 14 |
+
|
| 15 |
+
## 准备模型配置文件
|
| 16 |
+
|
| 17 |
+
本例将以基于 `coco` 数据集预训练的 `YOLOv5` 配置和权重进行部署的全流程讲解,包括静态/动态输入模型导出和推理,`TensorRT` / `ONNXRuntime` 两种后端部署和测试。
|
| 18 |
+
|
| 19 |
+
### 静态输入配置
|
| 20 |
+
|
| 21 |
+
#### (1) 模型配置文件
|
| 22 |
+
|
| 23 |
+
当您需要部署静态输入模型时,您应该确保模型的输入尺寸是固定的,比如在测试流程或测试数据集加载时输入尺寸为 `640x640`。
|
| 24 |
+
|
| 25 |
+
您可以查看 [`yolov5_s-static.py`](https://github.com/open-mmlab/mmyolo/tree/main/configs/deploy/model/yolov5_s-static.py) 中测试流程或测试数据集加载部分,如下所示:
|
| 26 |
+
|
| 27 |
+
```python
|
| 28 |
+
_base_ = '../../yolov5/yolov5_s-v61_syncbn_8xb16-300e_coco.py'
|
| 29 |
+
|
| 30 |
+
test_pipeline = [
|
| 31 |
+
dict(type='LoadImageFromFile', backend_args=_base_.backend_args),
|
| 32 |
+
dict(
|
| 33 |
+
type='LetterResize',
|
| 34 |
+
scale=_base_.img_scale,
|
| 35 |
+
allow_scale_up=False,
|
| 36 |
+
use_mini_pad=False,
|
| 37 |
+
),
|
| 38 |
+
dict(type='LoadAnnotations', with_bbox=True, _scope_='mmdet'),
|
| 39 |
+
dict(
|
| 40 |
+
type='mmdet.PackDetInputs',
|
| 41 |
+
meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',
|
| 42 |
+
'scale_factor', 'pad_param'))
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
test_dataloader = dict(
|
| 46 |
+
dataset=dict(pipeline=test_pipeline, batch_shapes_cfg=None))
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
+
由于 `yolov5` 在测试时会开启 `allow_scale_up` 和 `use_mini_pad` 改变输入图像的尺寸来取得更高的精度,但是会给部署静态输入模型造成输入尺寸不匹配的问题。
|
| 50 |
+
|
| 51 |
+
该配置相比与原始配置文件进行了如下修改:
|
| 52 |
+
|
| 53 |
+
- 关闭 `test_pipline` 中改变尺寸相关的配置,如 `LetterResize` 中 `allow_scale_up=False` 和 `use_mini_pad=False` 。
|
| 54 |
+
- 关闭 `test_dataloader` 中 `batch shapes` 策略,即 `batch_shapes_cfg=None` 。
|
| 55 |
+
|
| 56 |
+
#### (2) 部署配置文件
|
| 57 |
+
|
| 58 |
+
当您部署在 `ONNXRuntime` 时,您可以查看 [`detection_onnxruntime_static.py`](https://github.com/open-mmlab/mmyolo/tree/main/configs/deploy/detection_onnxruntime_static.py) ,如下所示:
|
| 59 |
+
|
| 60 |
+
```python
|
| 61 |
+
_base_ = ['./base_static.py']
|
| 62 |
+
codebase_config = dict(
|
| 63 |
+
type='mmyolo',
|
| 64 |
+
task='ObjectDetection',
|
| 65 |
+
model_type='end2end',
|
| 66 |
+
post_processing=dict(
|
| 67 |
+
score_threshold=0.05,
|
| 68 |
+
confidence_threshold=0.005,
|
| 69 |
+
iou_threshold=0.5,
|
| 70 |
+
max_output_boxes_per_class=200,
|
| 71 |
+
pre_top_k=5000,
|
| 72 |
+
keep_top_k=100,
|
| 73 |
+
background_label_id=-1),
|
| 74 |
+
module=['mmyolo.deploy'])
|
| 75 |
+
backend_config = dict(type='onnxruntime')
|
| 76 |
+
```
|
| 77 |
+
|
| 78 |
+
默认配置中的 `post_processing` 后处理参数是当前模型与 `pytorch` 模型精度对齐的配置,若您需要修改相关参数,可以参考 [`部署必备指南`](./mmdeploy_guide.md) 的详细介绍。
|
| 79 |
+
|
| 80 |
+
当您部署在 `TensorRT` 时,您可以查看 [`detection_tensorrt_static-640x640.py`](https://github.com/open-mmlab/mmyolo/tree/main/configs/deploy/detection_tensorrt_static-640x640.py) ,如下所示:
|
| 81 |
+
|
| 82 |
+
```python
|
| 83 |
+
_base_ = ['./base_static.py']
|
| 84 |
+
onnx_config = dict(input_shape=(640, 640))
|
| 85 |
+
backend_config = dict(
|
| 86 |
+
type='tensorrt',
|
| 87 |
+
common_config=dict(fp16_mode=False, max_workspace_size=1 << 30),
|
| 88 |
+
model_inputs=[
|
| 89 |
+
dict(
|
| 90 |
+
input_shapes=dict(
|
| 91 |
+
input=dict(
|
| 92 |
+
min_shape=[1, 3, 640, 640],
|
| 93 |
+
opt_shape=[1, 3, 640, 640],
|
| 94 |
+
max_shape=[1, 3, 640, 640])))
|
| 95 |
+
])
|
| 96 |
+
use_efficientnms = False
|
| 97 |
+
```
|
| 98 |
+
|
| 99 |
+
本例使用了默认的输入尺寸 `input_shape=(640, 640)` ,构建网络以 `fp32` 模式即 `fp16_mode=False`,并且默认构建 `TensorRT` 构建引擎所使用的显存 `max_workspace_size=1 << 30` 即最大为 `1GB` 显存。
|
| 100 |
+
|
| 101 |
+
### 动态输入配置
|
| 102 |
+
|
| 103 |
+
#### (1) 模型配置文件
|
| 104 |
+
|
| 105 |
+
当您需要部署动态输入模型时,模型的输入可以为任意尺寸(`TensorRT` 会限制最小和最大输入尺寸),因此使用默认的 [`yolov5_s-v61_syncbn_8xb16-300e_coco.py`](https://github.com/open-mmlab/mmyolo/tree/main/configs/yolov5/yolov5_s-v61_syncbn_8xb16-300e_coco.py) 模型配置文件即可,其中数据处理和数据集加载器部分如下所示:
|
| 106 |
+
|
| 107 |
+
```python
|
| 108 |
+
batch_shapes_cfg = dict(
|
| 109 |
+
type='BatchShapePolicy',
|
| 110 |
+
batch_size=val_batch_size_per_gpu,
|
| 111 |
+
img_size=img_scale[0],
|
| 112 |
+
size_divisor=32,
|
| 113 |
+
extra_pad_ratio=0.5)
|
| 114 |
+
|
| 115 |
+
test_pipeline = [
|
| 116 |
+
dict(type='LoadImageFromFile', backend_args=_base_.backend_args),
|
| 117 |
+
dict(type='YOLOv5KeepRatioResize', scale=img_scale),
|
| 118 |
+
dict(
|
| 119 |
+
type='LetterResize',
|
| 120 |
+
scale=img_scale,
|
| 121 |
+
allow_scale_up=False,
|
| 122 |
+
pad_val=dict(img=114)),
|
| 123 |
+
dict(type='LoadAnnotations', with_bbox=True, _scope_='mmdet'),
|
| 124 |
+
dict(
|
| 125 |
+
type='mmdet.PackDetInputs',
|
| 126 |
+
meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',
|
| 127 |
+
'scale_factor', 'pad_param'))
|
| 128 |
+
]
|
| 129 |
+
|
| 130 |
+
val_dataloader = dict(
|
| 131 |
+
batch_size=val_batch_size_per_gpu,
|
| 132 |
+
num_workers=val_num_workers,
|
| 133 |
+
persistent_workers=persistent_workers,
|
| 134 |
+
pin_memory=True,
|
| 135 |
+
drop_last=False,
|
| 136 |
+
sampler=dict(type='DefaultSampler', shuffle=False),
|
| 137 |
+
dataset=dict(
|
| 138 |
+
type=dataset_type,
|
| 139 |
+
data_root=data_root,
|
| 140 |
+
test_mode=True,
|
| 141 |
+
data_prefix=dict(img='val2017/'),
|
| 142 |
+
ann_file='annotations/instances_val2017.json',
|
| 143 |
+
pipeline=test_pipeline,
|
| 144 |
+
batch_shapes_cfg=batch_shapes_cfg))
|
| 145 |
+
```
|
| 146 |
+
|
| 147 |
+
其中 `LetterResize` 类初始化传入了 `allow_scale_up=False` 控制输入的小图像是否上采样,同时默认 `use_mini_pad=False` 关闭了图片最小填充策略,`val_dataloader['dataset']`中传入了 `batch_shapes_cfg=batch_shapes_cfg`,即按照 `batch` 内的输入尺寸进行最小填充。上述策略会改变输入图像的尺寸,因此动态输入模型在测试时会按照上述数据集加载器动态输入。
|
| 148 |
+
|
| 149 |
+
#### (2) 部署配置文件
|
| 150 |
+
|
| 151 |
+
当您部署在 `ONNXRuntime` 时,您可以查看 [`detection_onnxruntime_dynamic.py`](https://github.com/open-mmlab/mmyolo/blob/main/configs/deploy/detection_onnxruntime_dynamic.py) ,如下所示:
|
| 152 |
+
|
| 153 |
+
```python
|
| 154 |
+
_base_ = ['./base_dynamic.py']
|
| 155 |
+
codebase_config = dict(
|
| 156 |
+
type='mmyolo',
|
| 157 |
+
task='ObjectDetection',
|
| 158 |
+
model_type='end2end',
|
| 159 |
+
post_processing=dict(
|
| 160 |
+
score_threshold=0.05,
|
| 161 |
+
confidence_threshold=0.005,
|
| 162 |
+
iou_threshold=0.5,
|
| 163 |
+
max_output_boxes_per_class=200,
|
| 164 |
+
pre_top_k=5000,
|
| 165 |
+
keep_top_k=100,
|
| 166 |
+
background_label_id=-1),
|
| 167 |
+
module=['mmyolo.deploy'])
|
| 168 |
+
backend_config = dict(type='onnxruntime')
|
| 169 |
+
```
|
| 170 |
+
|
| 171 |
+
与静态输入配置仅有 `_base_ = ['./base_dynamic.py']` 不同,动态输入会额外继承 `dynamic_axes` 属性。其他配置与静态输入配置相同。
|
| 172 |
+
|
| 173 |
+
当您部署在 `TensorRT` 时,您可以查看 [`detection_tensorrt_dynamic-192x192-960x960.py`](https://github.com/open-mmlab/mmyolo/tree/main/configs/deploy/detection_tensorrt_dynamic-192x192-960x960.py) ,如下所示:
|
| 174 |
+
|
| 175 |
+
```python
|
| 176 |
+
_base_ = ['./base_dynamic.py']
|
| 177 |
+
backend_config = dict(
|
| 178 |
+
type='tensorrt',
|
| 179 |
+
common_config=dict(fp16_mode=False, max_workspace_size=1 << 30),
|
| 180 |
+
model_inputs=[
|
| 181 |
+
dict(
|
| 182 |
+
input_shapes=dict(
|
| 183 |
+
input=dict(
|
| 184 |
+
min_shape=[1, 3, 192, 192],
|
| 185 |
+
opt_shape=[1, 3, 640, 640],
|
| 186 |
+
max_shape=[1, 3, 960, 960])))
|
| 187 |
+
])
|
| 188 |
+
use_efficientnms = False
|
| 189 |
+
```
|
| 190 |
+
|
| 191 |
+
本例构建网络以 `fp32` 模式即 `fp16_mode=False`,构建 `TensorRT` 构建引擎所使用的显存 `max_workspace_size=1 << 30` 即最大为 `1GB` 显存。
|
| 192 |
+
|
| 193 |
+
同时默认配置 `min_shape=[1, 3, 192, 192]`,`opt_shape=[1, 3, 640, 640]` ,`max_shape=[1, 3, 960, 960]` ,意为该模型所能接受的输入尺寸最小为 `192x192` ,最大为 `960x960`,最常见尺寸为 `640x640`。
|
| 194 |
+
|
| 195 |
+
当您部署自己的模型时,需要根据您的输入图像尺寸进行调整。
|
| 196 |
+
|
| 197 |
+
## 模型转换
|
| 198 |
+
|
| 199 |
+
本教程所使用的 `MMDeploy` 根目录为 `/home/openmmlab/dev/mmdeploy`,请注意修改为您的 `MMDeploy` 目录。
|
| 200 |
+
预训练权重下载于 [yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth](https://download.openmmlab.com/mmyolo/v0/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco/yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth) ,保存在本地的 `/home/openmmlab/dev/mmdeploy/yolov5s.pth`。
|
| 201 |
+
|
| 202 |
+
```shell
|
| 203 |
+
wget https://download.openmmlab.com/mmyolo/v0/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco/yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth -O /home/openmmlab/dev/mmdeploy/yolov5s.pth
|
| 204 |
+
```
|
| 205 |
+
|
| 206 |
+
命令行执行以下命令配置相关路径:
|
| 207 |
+
|
| 208 |
+
```shell
|
| 209 |
+
export MMDEPLOY_DIR=/home/openmmlab/dev/mmdeploy
|
| 210 |
+
export PATH_TO_CHECKPOINTS=/home/openmmlab/dev/mmdeploy/yolov5s.pth
|
| 211 |
+
```
|
| 212 |
+
|
| 213 |
+
### YOLOv5 静态输入模型导出
|
| 214 |
+
|
| 215 |
+
#### ONNXRuntime
|
| 216 |
+
|
| 217 |
+
```shell
|
| 218 |
+
python3 ${MMDEPLOY_DIR}/tools/deploy.py \
|
| 219 |
+
configs/deploy/detection_onnxruntime_static.py \
|
| 220 |
+
configs/deploy/model/yolov5_s-static.py \
|
| 221 |
+
${PATH_TO_CHECKPOINTS} \
|
| 222 |
+
demo/demo.jpg \
|
| 223 |
+
--work-dir work_dir \
|
| 224 |
+
--show \
|
| 225 |
+
--device cpu
|
| 226 |
+
```
|
| 227 |
+
|
| 228 |
+
#### TensorRT
|
| 229 |
+
|
| 230 |
+
```shell
|
| 231 |
+
python3 ${MMDEPLOY_DIR}/tools/deploy.py \
|
| 232 |
+
configs/deploy/detection_tensorrt_static-640x640.py \
|
| 233 |
+
configs/deploy/model/yolov5_s-static.py \
|
| 234 |
+
${PATH_TO_CHECKPOINTS} \
|
| 235 |
+
demo/demo.jpg \
|
| 236 |
+
--work-dir work_dir \
|
| 237 |
+
--show \
|
| 238 |
+
--device cuda:0
|
| 239 |
+
```
|
| 240 |
+
|
| 241 |
+
### YOLOv5 动态输入模型导出
|
| 242 |
+
|
| 243 |
+
#### ONNXRuntime
|
| 244 |
+
|
| 245 |
+
```shell
|
| 246 |
+
python3 ${MMDEPLOY_DIR}/tools/deploy.py \
|
| 247 |
+
configs/deploy/detection_onnxruntime_dynamic.py \
|
| 248 |
+
configs/yolov5/yolov5_s-v61_syncbn_8xb16-300e_coco.py \
|
| 249 |
+
${PATH_TO_CHECKPOINTS} \
|
| 250 |
+
demo/demo.jpg \
|
| 251 |
+
--work-dir work_dir \
|
| 252 |
+
--show \
|
| 253 |
+
--device cpu
|
| 254 |
+
--dump-info
|
| 255 |
+
```
|
| 256 |
+
|
| 257 |
+
#### TensorRT
|
| 258 |
+
|
| 259 |
+
```shell
|
| 260 |
+
python3 ${MMDEPLOY_DIR}/tools/deploy.py \
|
| 261 |
+
configs/deploy/detection_tensorrt_dynamic-192x192-960x960.py \
|
| 262 |
+
configs/yolov5/yolov5_s-v61_syncbn_8xb16-300e_coco.py \
|
| 263 |
+
${PATH_TO_CHECKPOINTS} \
|
| 264 |
+
demo/demo.jpg \
|
| 265 |
+
--work-dir work_dir \
|
| 266 |
+
--show \
|
| 267 |
+
--device cuda:0
|
| 268 |
+
--dump-info
|
| 269 |
+
```
|
| 270 |
+
|
| 271 |
+
当您使用上述命令转换模型时,您将会在 `work_dir` 文件夹下发现以下文件:
|
| 272 |
+
|
| 273 |
+

|
| 274 |
+
|
| 275 |
+
或者
|
| 276 |
+
|
| 277 |
+

|
| 278 |
+
|
| 279 |
+
在导出 `onnxruntime`模型后,您将得到图1的六个文件,其中 `end2end.onnx` 表示导出的`onnxruntime`模型,`xxx.json` 表示 `MMDeploy SDK` 推理所需要的 meta 信息。
|
| 280 |
+
|
| 281 |
+
在导出 `TensorRT`模型后,您将得到图2的七个文件,其中 `end2end.onnx` 表示导出的中间模型,`MMDeploy`利用该模型自动继续转换获得 `end2end.engine` 模型用于 `TensorRT `部署,`xxx.json` 表示 `MMDeploy SDK` 推理所需要的 meta 信息。
|
| 282 |
+
|
| 283 |
+
## 模型评测
|
| 284 |
+
|
| 285 |
+
当您转换模型成功后,可以使用 `${MMDEPLOY_DIR}/tools/test.py` 工具对转换后的模型进行评测。下面是对 `ONNXRuntime` 和 `TensorRT` 静态模型的评测,动态模型评测修改传入模型配置即可。
|
| 286 |
+
|
| 287 |
+
### ONNXRuntime
|
| 288 |
+
|
| 289 |
+
```shell
|
| 290 |
+
python3 ${MMDEPLOY_DIR}/tools/test.py \
|
| 291 |
+
configs/deploy/detection_onnxruntime_static.py \
|
| 292 |
+
configs/deploy/model/yolov5_s-static.py \
|
| 293 |
+
--model work_dir/end2end.onnx \
|
| 294 |
+
--device cpu \
|
| 295 |
+
--work-dir work_dir
|
| 296 |
+
```
|
| 297 |
+
|
| 298 |
+
执行完成您将看到命令行输出检测结果指标如下:
|
| 299 |
+
|
| 300 |
+

|
| 301 |
+
|
| 302 |
+
### TensorRT
|
| 303 |
+
|
| 304 |
+
**注意**: TensorRT 需要执行设备是 `cuda`
|
| 305 |
+
|
| 306 |
+
```shell
|
| 307 |
+
python3 ${MMDEPLOY_DIR}/tools/test.py \
|
| 308 |
+
configs/deploy/detection_tensorrt_static-640x640.py \
|
| 309 |
+
configs/deploy/model/yolov5_s-static.py \
|
| 310 |
+
--model work_dir/end2end.engine \
|
| 311 |
+
--device cuda:0 \
|
| 312 |
+
--work-dir work_dir
|
| 313 |
+
```
|
| 314 |
+
|
| 315 |
+
执行完成您将看到命令行输出检测结果指标如下:
|
| 316 |
+
|
| 317 |
+

|
| 318 |
+
|
| 319 |
+
**未来我们将会支持模型测速等更加实用的脚本**
|
| 320 |
+
|
| 321 |
+
# 使用 Docker 部署测试
|
| 322 |
+
|
| 323 |
+
`MMYOLO` 提供了一个 [`Dockerfile`](https://github.com/open-mmlab/mmyolo/blob/main/docker/Dockerfile_deployment) 用于构建镜像。请确保您的 `docker` 版本大于等于 `19.03`。
|
| 324 |
+
|
| 325 |
+
温馨提示;国内用户建议取消掉 [`Dockerfile`](https://github.com/open-mmlab/mmyolo/blob/main/docker/Dockerfile_deployment) 里面 `Optional` 后两行的注释,可以获得火箭一般的下载提速:
|
| 326 |
+
|
| 327 |
+
```dockerfile
|
| 328 |
+
# (Optional)
|
| 329 |
+
RUN sed -i 's/http:\/\/archive.ubuntu.com\/ubuntu\//http:\/\/mirrors.aliyun.com\/ubuntu\//g' /etc/apt/sources.list && \
|
| 330 |
+
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
|
| 331 |
+
```
|
| 332 |
+
|
| 333 |
+
构建命令:
|
| 334 |
+
|
| 335 |
+
```bash
|
| 336 |
+
# build an image with PyTorch 1.12, CUDA 11.6, TensorRT 8.2.4 ONNXRuntime 1.8.1
|
| 337 |
+
docker build -f docker/Dockerfile_deployment -t mmyolo:v1 .
|
| 338 |
+
```
|
| 339 |
+
|
| 340 |
+
用以下命令运行 Docker 镜像:
|
| 341 |
+
|
| 342 |
+
```bash
|
| 343 |
+
export DATA_DIR=/path/to/your/dataset
|
| 344 |
+
docker run --gpus all --shm-size=8g -it --name mmyolo -v ${DATA_DIR}:/openmmlab/mmyolo/data/coco mmyolo:v1
|
| 345 |
+
```
|
| 346 |
+
|
| 347 |
+
`DATA_DIR` 是 COCO 数据的路径。
|
| 348 |
+
|
| 349 |
+
复制以下脚本到 `docker` 容器 `/openmmlab/mmyolo/script.sh`:
|
| 350 |
+
|
| 351 |
+
```bash
|
| 352 |
+
#!/bin/bash
|
| 353 |
+
wget -q https://download.openmmlab.com/mmyolo/v0/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco/yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth \
|
| 354 |
+
-O yolov5s.pth
|
| 355 |
+
export MMDEPLOY_DIR=/openmmlab/mmdeploy
|
| 356 |
+
export PATH_TO_CHECKPOINTS=/openmmlab/mmyolo/yolov5s.pth
|
| 357 |
+
|
| 358 |
+
python3 ${MMDEPLOY_DIR}/tools/deploy.py \
|
| 359 |
+
configs/deploy/detection_tensorrt_static-640x640.py \
|
| 360 |
+
configs/deploy/model/yolov5_s-static.py \
|
| 361 |
+
${PATH_TO_CHECKPOINTS} \
|
| 362 |
+
demo/demo.jpg \
|
| 363 |
+
--work-dir work_dir_trt \
|
| 364 |
+
--device cuda:0
|
| 365 |
+
|
| 366 |
+
python3 ${MMDEPLOY_DIR}/tools/test.py \
|
| 367 |
+
configs/deploy/detection_tensorrt_static-640x640.py \
|
| 368 |
+
configs/deploy/model/yolov5_s-static.py \
|
| 369 |
+
--model work_dir_trt/end2end.engine \
|
| 370 |
+
--device cuda:0 \
|
| 371 |
+
--work-dir work_dir_trt
|
| 372 |
+
|
| 373 |
+
python3 ${MMDEPLOY_DIR}/tools/deploy.py \
|
| 374 |
+
configs/deploy/detection_onnxruntime_static.py \
|
| 375 |
+
configs/deploy/model/yolov5_s-static.py \
|
| 376 |
+
${PATH_TO_CHECKPOINTS} \
|
| 377 |
+
demo/demo.jpg \
|
| 378 |
+
--work-dir work_dir_ort \
|
| 379 |
+
--device cpu
|
| 380 |
+
|
| 381 |
+
python3 ${MMDEPLOY_DIR}/tools/test.py \
|
| 382 |
+
configs/deploy/detection_onnxruntime_static.py \
|
| 383 |
+
configs/deploy/model/yolov5_s-static.py \
|
| 384 |
+
--model work_dir_ort/end2end.onnx \
|
| 385 |
+
--device cpu \
|
| 386 |
+
--work-dir work_dir_ort
|
| 387 |
+
```
|
| 388 |
+
|
| 389 |
+
在 `/openmmlab/mmyolo` 下运行:
|
| 390 |
+
|
| 391 |
+
```bash
|
| 392 |
+
sh script.sh
|
| 393 |
+
```
|
| 394 |
+
|
| 395 |
+
脚本会自动下载 `MMYOLO` 的 `YOLOv5` 预训练权重并使用 `MMDeploy` 进行模型转换和测试。您将会看到以下输出:
|
| 396 |
+
|
| 397 |
+
- TensorRT:
|
| 398 |
+
|
| 399 |
+

|
| 400 |
+
|
| 401 |
+
- ONNXRuntime:
|
| 402 |
+
|
| 403 |
+

|
| 404 |
+
|
| 405 |
+
可以看到,经过 `MMDeploy` 部署的模型与 [MMYOLO-YOLOv5](https://github.com/open-mmlab/mmyolo/tree/main/configs/yolov5#results-and-models) 的 mAP-37.7 差距在 1% 以内。
|
| 406 |
+
|
| 407 |
+
如果您需要测试您的模型推理速度,可以使用以下命令:
|
| 408 |
+
|
| 409 |
+
- TensorRT
|
| 410 |
+
|
| 411 |
+
```shell
|
| 412 |
+
python3 ${MMDEPLOY_DIR}/tools/profiler.py \
|
| 413 |
+
configs/deploy/detection_tensorrt_static-640x640.py \
|
| 414 |
+
configs/deploy/model/yolov5_s-static.py \
|
| 415 |
+
data/coco/val2017 \
|
| 416 |
+
--model work_dir_trt/end2end.engine \
|
| 417 |
+
--device cuda:0
|
| 418 |
+
```
|
| 419 |
+
|
| 420 |
+
- ONNXRuntime
|
| 421 |
+
|
| 422 |
+
```shell
|
| 423 |
+
python3 ${MMDEPLOY_DIR}/tools/profiler.py \
|
| 424 |
+
configs/deploy/detection_onnxruntime_static.py \
|
| 425 |
+
configs/deploy/model/yolov5_s-static.py \
|
| 426 |
+
data/coco/val2017 \
|
| 427 |
+
--model work_dir_ort/end2end.onnx \
|
| 428 |
+
--device cpu
|
| 429 |
+
```
|
| 430 |
+
|
| 431 |
+
## 模型推理
|
| 432 |
+
|
| 433 |
+
### 后端模型推理
|
| 434 |
+
|
| 435 |
+
#### ONNXRuntime
|
| 436 |
+
|
| 437 |
+
以上述模型转换后的 `end2end.onnx` 为例,您可以使用如下代码进行推理:
|
| 438 |
+
|
| 439 |
+
```python
|
| 440 |
+
from mmdeploy.apis.utils import build_task_processor
|
| 441 |
+
from mmdeploy.utils import get_input_shape, load_config
|
| 442 |
+
import torch
|
| 443 |
+
|
| 444 |
+
deploy_cfg = './configs/deploy/detection_onnxruntime_dynamic.py'
|
| 445 |
+
model_cfg = '../mmyolo/configs/yolov5/yolov5_s-v61_syncbn_8xb16-300e_coco.py'
|
| 446 |
+
device = 'cpu'
|
| 447 |
+
backend_model = ['./work_dir/end2end.onnx']
|
| 448 |
+
image = '../mmyolo/demo/demo.jpg'
|
| 449 |
+
|
| 450 |
+
# read deploy_cfg and model_cfg
|
| 451 |
+
deploy_cfg, model_cfg = load_config(deploy_cfg, model_cfg)
|
| 452 |
+
|
| 453 |
+
# build task and backend model
|
| 454 |
+
task_processor = build_task_processor(model_cfg, deploy_cfg, device)
|
| 455 |
+
model = task_processor.build_backend_model(backend_model)
|
| 456 |
+
|
| 457 |
+
# process input image
|
| 458 |
+
input_shape = get_input_shape(deploy_cfg)
|
| 459 |
+
model_inputs, _ = task_processor.create_input(image, input_shape)
|
| 460 |
+
|
| 461 |
+
# do model inference
|
| 462 |
+
with torch.no_grad():
|
| 463 |
+
result = model.test_step(model_inputs)
|
| 464 |
+
|
| 465 |
+
# visualize results
|
| 466 |
+
task_processor.visualize(
|
| 467 |
+
image=image,
|
| 468 |
+
model=model,
|
| 469 |
+
result=result[0],
|
| 470 |
+
window_name='visualize',
|
| 471 |
+
output_file='work_dir/output_detection.png')
|
| 472 |
+
```
|
| 473 |
+
|
| 474 |
+
#### TensorRT
|
| 475 |
+
|
| 476 |
+
以上述模型转换后的 `end2end.engine` 为例,您可以使用如下代码进行推理:
|
| 477 |
+
|
| 478 |
+
```python
|
| 479 |
+
from mmdeploy.apis.utils import build_task_processor
|
| 480 |
+
from mmdeploy.utils import get_input_shape, load_config
|
| 481 |
+
import torch
|
| 482 |
+
|
| 483 |
+
deploy_cfg = './configs/deploy/detection_tensorrt_dynamic-192x192-960x960.py'
|
| 484 |
+
model_cfg = '../mmyolo/configs/yolov5/yolov5_s-v61_syncbn_8xb16-300e_coco.py'
|
| 485 |
+
device = 'cuda:0'
|
| 486 |
+
backend_model = ['./work_dir/end2end.engine']
|
| 487 |
+
image = '../mmyolo/demo/demo.jpg'
|
| 488 |
+
|
| 489 |
+
# read deploy_cfg and model_cfg
|
| 490 |
+
deploy_cfg, model_cfg = load_config(deploy_cfg, model_cfg)
|
| 491 |
+
|
| 492 |
+
# build task and backend model
|
| 493 |
+
task_processor = build_task_processor(model_cfg, deploy_cfg, device)
|
| 494 |
+
model = task_processor.build_backend_model(backend_model)
|
| 495 |
+
|
| 496 |
+
# process input image
|
| 497 |
+
input_shape = get_input_shape(deploy_cfg)
|
| 498 |
+
model_inputs, _ = task_processor.create_input(image, input_shape)
|
| 499 |
+
|
| 500 |
+
# do model inference
|
| 501 |
+
with torch.no_grad():
|
| 502 |
+
result = model.test_step(model_inputs)
|
| 503 |
+
|
| 504 |
+
# visualize results
|
| 505 |
+
task_processor.visualize(
|
| 506 |
+
image=image,
|
| 507 |
+
model=model,
|
| 508 |
+
result=result[0],
|
| 509 |
+
window_name='visualize',
|
| 510 |
+
output_file='work_dir/output_detection.png')
|
| 511 |
+
```
|
| 512 |
+
|
| 513 |
+
### SDK 模型推理
|
| 514 |
+
|
| 515 |
+
#### ONNXRuntime
|
| 516 |
+
|
| 517 |
+
以上述模型转换后的 `end2end.onnx` 为例,您可以使用如下代码进行 `SDK` 推理:
|
| 518 |
+
|
| 519 |
+
```python
|
| 520 |
+
from mmdeploy_runtime import Detector
|
| 521 |
+
import cv2
|
| 522 |
+
|
| 523 |
+
img = cv2.imread('../mmyolo/demo/demo.jpg')
|
| 524 |
+
|
| 525 |
+
# create a detector
|
| 526 |
+
detector = Detector(model_path='work_dir',
|
| 527 |
+
device_name='cpu', device_id=0)
|
| 528 |
+
# perform inference
|
| 529 |
+
bboxes, labels, masks = detector(img)
|
| 530 |
+
|
| 531 |
+
# visualize inference result
|
| 532 |
+
indices = [i for i in range(len(bboxes))]
|
| 533 |
+
for index, bbox, label_id in zip(indices, bboxes, labels):
|
| 534 |
+
[left, top, right, bottom], score = bbox[0:4].astype(int), bbox[4]
|
| 535 |
+
if score < 0.3:
|
| 536 |
+
continue
|
| 537 |
+
|
| 538 |
+
cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0))
|
| 539 |
+
|
| 540 |
+
cv2.imwrite('work_dir/output_detection.png', img)
|
| 541 |
+
```
|
| 542 |
+
|
| 543 |
+
#### TensorRT
|
| 544 |
+
|
| 545 |
+
以上述模型转换后的 `end2end.engine` 为例,您可以使用如下代码进行 `SDK` 推理:
|
| 546 |
+
|
| 547 |
+
```python
|
| 548 |
+
from mmdeploy_runtime import Detector
|
| 549 |
+
import cv2
|
| 550 |
+
|
| 551 |
+
img = cv2.imread('../mmyolo/demo/demo.jpg')
|
| 552 |
+
|
| 553 |
+
# create a detector
|
| 554 |
+
detector = Detector(model_path='work_dir',
|
| 555 |
+
device_name='cuda', device_id=0)
|
| 556 |
+
# perform inference
|
| 557 |
+
bboxes, labels, masks = detector(img)
|
| 558 |
+
|
| 559 |
+
# visualize inference result
|
| 560 |
+
indices = [i for i in range(len(bboxes))]
|
| 561 |
+
for index, bbox, label_id in zip(indices, bboxes, labels):
|
| 562 |
+
[left, top, right, bottom], score = bbox[0:4].astype(int), bbox[4]
|
| 563 |
+
if score < 0.3:
|
| 564 |
+
continue
|
| 565 |
+
|
| 566 |
+
cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0))
|
| 567 |
+
|
| 568 |
+
cv2.imwrite('work_dir/output_detection.png', img)
|
| 569 |
+
```
|
| 570 |
+
|
| 571 |
+
除了python API,mmdeploy SDK 还提供了诸如 C、C++、C#、Java等多语言接口。
|
| 572 |
+
你可以参考[样例](https://github.com/open-mmlab/mmdeploy/tree/main/demo)学习其他语言接口的使用方法。
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/labeling_to_deployment_tutorials.md
ADDED
|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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 标注+训练+测试+部署全流程
|
| 2 |
+
|
| 3 |
+
在平时的工作学习中,我们经常会遇到一些任务需要训练自定义的私有数据集,开源数据集去作为上线模型的场景比较少,这就需要我们对自己的私有数据集进行一系列的操作,以确保模型能够上线生产服务于客户。
|
| 4 |
+
|
| 5 |
+
```{SeeAlso}
|
| 6 |
+
本文档配套的视频已发布在 B 站,可前去查看: [自定义数据集从标注到部署保姆级教程](https://www.bilibili.com/video/BV1RG4y137i5)
|
| 7 |
+
```
|
| 8 |
+
|
| 9 |
+
```{Note}
|
| 10 |
+
本教程所有指令是在 Linux 上面完成,Windows 也是完全可用的,但是命令和操作稍有不同。
|
| 11 |
+
```
|
| 12 |
+
|
| 13 |
+
本教程默认您已经完成 MMYOLO 的安装,如果未安装,请参考文档 [开始你的第一步](https://mmyolo.readthedocs.io/zh_CN/latest/get_started.html#id1) 进行安装。
|
| 14 |
+
|
| 15 |
+
本教程涵盖从 用户自定义图片数据集标注 到 最终进行训练和部署 的整体流程。步骤概览如下:
|
| 16 |
+
|
| 17 |
+
01. 数据集准备:`tools/misc/download_dataset.py`
|
| 18 |
+
02. 使用 [labelme](https://github.com/wkentaro/labelme) 和算法进行辅助和优化数据集标注:`demo/image_demo.py` + labelme
|
| 19 |
+
03. 使用脚本转换成 COCO 数据集格式:`tools/dataset_converters/labelme2coco.py`
|
| 20 |
+
04. 数据集划分为训练集、验证集和测试集:`tools/misc/coco_split.py`
|
| 21 |
+
05. 根据数据集内容新建 config 文件
|
| 22 |
+
06. 数据集可视化分析:`tools/analysis_tools/dataset_analysis.py`
|
| 23 |
+
07. 优化 Anchor 尺寸:`tools/analysis_tools/optimize_anchors.py`
|
| 24 |
+
08. 可视化 config 配置中数据处理部分: `tools/analysis_tools/browse_dataset.py`
|
| 25 |
+
09. 训练:`tools/train.py`
|
| 26 |
+
10. 推理:`demo/image_demo.py`
|
| 27 |
+
11. 部署
|
| 28 |
+
|
| 29 |
+
```{Note}
|
| 30 |
+
在训练得到模型权重和验证集的 mAP 后,用户需要对预测错误的 bad case 进行深入分析,以便优化模型,MMYOLO 在后续会增加这个功能,敬请期待。
|
| 31 |
+
```
|
| 32 |
+
|
| 33 |
+
下面详细介绍每一步。
|
| 34 |
+
|
| 35 |
+
## 1. 数据集准备
|
| 36 |
+
|
| 37 |
+
- 如果您现在暂时没有自己的数据集,亦或者想尝试用一个小型数据集来跑通我们的整体流程,可以使用本教程提供的一个 144 张图片的 `cat` 数据集(本 `cat` 数据集由 @RangeKing 提供原始图片,由 @PeterH0323 进行数据清洗)。本教程的剩余部分都将以此 `cat` 数据集为例进行讲解。
|
| 38 |
+
|
| 39 |
+
<div align=center>
|
| 40 |
+
<img src="https://user-images.githubusercontent.com/25873202/205423220-c4b8f2fd-22ba-4937-8e47-1b3f6a8facd8.png" alt="cat dataset"/>
|
| 41 |
+
</div>
|
| 42 |
+
|
| 43 |
+
下载也非常简单,只需要一条命令即可完成(数据集压缩包大小 `217 MB`):
|
| 44 |
+
|
| 45 |
+
```shell
|
| 46 |
+
python tools/misc/download_dataset.py --dataset-name cat --save-dir ./data/cat --unzip --delete
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
+
该命令会自动下载数据集到 `./data/cat` 文件夹中,该文件的目录结构是:
|
| 50 |
+
|
| 51 |
+
```shell
|
| 52 |
+
.
|
| 53 |
+
└── ./data/cat
|
| 54 |
+
├── images # 图片文件
|
| 55 |
+
│ ├── image1.jpg
|
| 56 |
+
│ ├── image2.png
|
| 57 |
+
│ └── ...
|
| 58 |
+
├── labels # labelme 标注文件
|
| 59 |
+
│ ├── image1.json
|
| 60 |
+
│ ├── image2.json
|
| 61 |
+
│ └── ...
|
| 62 |
+
├── annotations # 数据集划分的 COCO 文件
|
| 63 |
+
│ ├── annotations_all.json # 全量数据的 COCO label 文件
|
| 64 |
+
│ ├── trainval.json # 划分比例 80% 的数据
|
| 65 |
+
│ └── test.json # 划分比例 20% 的数据
|
| 66 |
+
└── class_with_id.txt # id + class_name 文件
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
这个数据集可以直接训练,如果您想体验整个流程的话,可以将 `images` 文件夹**以外的**其余文件都删除。
|
| 70 |
+
|
| 71 |
+
- 如您已经有数据,可以将其组成下面的结构:
|
| 72 |
+
|
| 73 |
+
```shell
|
| 74 |
+
.
|
| 75 |
+
└── $DATA_ROOT
|
| 76 |
+
└── images
|
| 77 |
+
├── image1.jpg
|
| 78 |
+
├── image2.png
|
| 79 |
+
└── ...
|
| 80 |
+
```
|
| 81 |
+
|
| 82 |
+
## 2. 使用 labelme 和算法进行辅助和优化数据集标注
|
| 83 |
+
|
| 84 |
+
通常,标注有 2 种方法:
|
| 85 |
+
|
| 86 |
+
- 软件或者算法辅助 + 人工修正 label(推荐,降本提速)
|
| 87 |
+
- 仅人工标注
|
| 88 |
+
|
| 89 |
+
```{Note}
|
| 90 |
+
目前我们也在考虑接入第三方库来支持通过 GUI 界面调用 MMYOLO 推理接口实现算法辅助标注和人工优化标注一体功能。
|
| 91 |
+
如果您有兴趣或者想法可以在 issue 留言或直接联系我们!
|
| 92 |
+
```
|
| 93 |
+
|
| 94 |
+
### 2.1 软件或者算法辅助 + 人工修正 label
|
| 95 |
+
|
| 96 |
+
辅助标注的原理是用已有模型进行推理,将得出的推理信息保存为标注软件 label 文件格式。然后人工操作标注软件加载生成好的 label 文件,只需要检查每张图片的目标是否标准,以及是否有漏掉、错标的目标。【软件或者算法辅助 + 人工修正 label】这种方式可以节省很多时间和精力,达到**降本提速**的目的。
|
| 97 |
+
|
| 98 |
+
```{Note}
|
| 99 |
+
如果已有模型(典型的如 COCO 预训练模型)没有您自定义新数据集的类别,建议先人工打 100 张左右的图片 label,训练个初始模型,然后再进行辅助标注。
|
| 100 |
+
```
|
| 101 |
+
|
| 102 |
+
下面会分别介绍其过程:
|
| 103 |
+
|
| 104 |
+
#### 2.1.1 软件或者算法辅助
|
| 105 |
+
|
| 106 |
+
使用 MMYOLO 提供的模型推理脚本 `demo/image_demo.py`,并设置 `--to-labelme` 则可以将推理结果生成 labelme 格式的 label 文件,具体用法如下:
|
| 107 |
+
|
| 108 |
+
```shell
|
| 109 |
+
python demo/image_demo.py img \
|
| 110 |
+
config \
|
| 111 |
+
checkpoint
|
| 112 |
+
[--out-dir OUT_DIR] \
|
| 113 |
+
[--device DEVICE] \
|
| 114 |
+
[--show] \
|
| 115 |
+
[--deploy] \
|
| 116 |
+
[--score-thr SCORE_THR] \
|
| 117 |
+
[--class-name CLASS_NAME]
|
| 118 |
+
[--to-labelme]
|
| 119 |
+
```
|
| 120 |
+
|
| 121 |
+
其中:
|
| 122 |
+
|
| 123 |
+
- `img`: 图片的路径,支持文件夹、文件、URL;
|
| 124 |
+
- `config`:用到的模型 config 文件路径;
|
| 125 |
+
- `checkpoint`:用到的模型权重文件路径;
|
| 126 |
+
- `--out-dir`:推理结果输出到指定目录下,默认为 `./output`,当 `--show` 参数存在时,不保存检测结果;
|
| 127 |
+
- `--device`:使用的计算资源,包括 `CUDA`, `CPU` 等,默认为 `cuda:0`;
|
| 128 |
+
- `--show`:使用该参数表示在屏幕上显示检测结果,默认为 `False`;
|
| 129 |
+
- `--deploy`:是否切换成 deploy 模式;
|
| 130 |
+
- `--score-thr`:置信度阈值,默认为 `0.3`;
|
| 131 |
+
- `--to-labelme`:是否导出 `labelme` 格式的 label 文件,不可以与 `--show` 参数同时存在
|
| 132 |
+
|
| 133 |
+
例子:
|
| 134 |
+
|
| 135 |
+
这里使用 YOLOv5-s 作为例子来进行辅助标注刚刚下载的 `cat` 数据集,先下载 YOLOv5-s 的权重:
|
| 136 |
+
|
| 137 |
+
```shell
|
| 138 |
+
mkdir work_dirs
|
| 139 |
+
wget https://download.openmmlab.com/mmyolo/v0/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco/yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth -P ./work_dirs
|
| 140 |
+
```
|
| 141 |
+
|
| 142 |
+
由于 COCO 80 类数据集中已经包括了 `cat` 这一类,因此我们可以直接加载 COCO 预训练权重进行辅助标注。
|
| 143 |
+
|
| 144 |
+
```shell
|
| 145 |
+
python demo/image_demo.py ./data/cat/images \
|
| 146 |
+
./configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py \
|
| 147 |
+
./work_dirs/yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth \
|
| 148 |
+
--out-dir ./data/cat/labels \
|
| 149 |
+
--class-name cat \
|
| 150 |
+
--to-labelme
|
| 151 |
+
```
|
| 152 |
+
|
| 153 |
+
```{Tip}
|
| 154 |
+
- 如果您的数据集需要标注多类,可以采用类似 `--class-name class1 class2` 格式输入;
|
| 155 |
+
- 如果全部输出,则删掉 `--class-name` 这个 flag 即可全部类都输出。
|
| 156 |
+
```
|
| 157 |
+
|
| 158 |
+
生成的 label 文件会在 `--out-dir` 中:
|
| 159 |
+
|
| 160 |
+
```shell
|
| 161 |
+
.
|
| 162 |
+
└── $OUT_DIR
|
| 163 |
+
├── image1.json
|
| 164 |
+
├── image1.json
|
| 165 |
+
└── ...
|
| 166 |
+
```
|
| 167 |
+
|
| 168 |
+
这是一张原图及其生成的 json 例子:
|
| 169 |
+
|
| 170 |
+
<div align=center>
|
| 171 |
+
<img src="https://user-images.githubusercontent.com/25873202/205471430-dcc882dd-16bb-45e4-938f-6b62ab3dff19.jpg" alt="图片" width="45%"/>
|
| 172 |
+
<img src="https://user-images.githubusercontent.com/25873202/205471559-643aecc8-7fa3-4fff-be51-2fb0a570fdd3.png" alt="图片" width="45%"/>
|
| 173 |
+
</div>
|
| 174 |
+
|
| 175 |
+
#### 2.1.2 人工标注
|
| 176 |
+
|
| 177 |
+
本教程使用的标注软件是 [labelme](https://github.com/wkentaro/labelme)
|
| 178 |
+
|
| 179 |
+
- 安装 labelme
|
| 180 |
+
|
| 181 |
+
```shell
|
| 182 |
+
conda create -n labelme python=3.8
|
| 183 |
+
conda activate labelme
|
| 184 |
+
pip install labelme==5.1.1
|
| 185 |
+
```
|
| 186 |
+
|
| 187 |
+
- 启动 labelme
|
| 188 |
+
|
| 189 |
+
```shell
|
| 190 |
+
labelme ${图片文件夹路径(即上一步的图片文件夹)} \
|
| 191 |
+
--output ${label文件所处的文件夹路径(即上一步的 --out-dir)} \
|
| 192 |
+
--autosave \
|
| 193 |
+
--nodata
|
| 194 |
+
```
|
| 195 |
+
|
| 196 |
+
其中:
|
| 197 |
+
|
| 198 |
+
- `--output`:labelme 标注文件保存路径,如果该路径下已经存在部分图片的标注文件,则会进行加载;
|
| 199 |
+
- `--autosave`:标注文件自动保存,会略去一些繁琐的保存步骤;
|
| 200 |
+
- `--nodata`:每张图片的标注文件中不保存图片的 base64 编码,设置了这个 flag 会大大减少标注文件的大小。
|
| 201 |
+
|
| 202 |
+
例子:
|
| 203 |
+
|
| 204 |
+
```shell
|
| 205 |
+
cd /path/to/mmyolo
|
| 206 |
+
labelme ./data/cat/images --output ./data/cat/labels --autosave --nodata
|
| 207 |
+
```
|
| 208 |
+
|
| 209 |
+
输入命令之后 labelme 就会启动,然后进行 label 检查即可。如果 labelme 启动失败,命令行输入 `export QT_DEBUG_PLUGINS=1` 查看具体缺少什么库,安装一下即可。
|
| 210 |
+
|
| 211 |
+
<div align=center>
|
| 212 |
+
<img src="https://user-images.githubusercontent.com/25873202/205432185-54407d83-3cee-473f-8743-656da157cf80.png" alt="label UI"/>
|
| 213 |
+
</div>
|
| 214 |
+
|
| 215 |
+
```{warning}
|
| 216 |
+
标注的时候务必使用 `rectangle`,快捷键 `Ctrl + R`(如下图)
|
| 217 |
+
|
| 218 |
+
<div align=center>
|
| 219 |
+
<img src="https://user-images.githubusercontent.com/25873202/204076212-86dab4fa-13dd-42cd-93d8-46b04b864449.png" alt="rectangle"/>
|
| 220 |
+
</div>
|
| 221 |
+
```
|
| 222 |
+
|
| 223 |
+
### 2.2 仅人工标注
|
| 224 |
+
|
| 225 |
+
步骤和 【2.1.2 人工标注】 相同,只是这里是直接标注,没有预先生成的 label 。
|
| 226 |
+
|
| 227 |
+
## 3. 使用脚本转换成 COCO 数据集格式
|
| 228 |
+
|
| 229 |
+
### 3.1 使用脚本转换
|
| 230 |
+
|
| 231 |
+
MMYOLO 提供脚本将 labelme 的 label 转换为 COCO label
|
| 232 |
+
|
| 233 |
+
```shell
|
| 234 |
+
python tools/dataset_converters/labelme2coco.py --img-dir ${图片文件夹路径} \
|
| 235 |
+
--labels-dir ${label 文件夹位置} \
|
| 236 |
+
--out ${输出 COCO label json 路径} \
|
| 237 |
+
[--class-id-txt ${class_with_id.txt 路径}]
|
| 238 |
+
```
|
| 239 |
+
|
| 240 |
+
其中:
|
| 241 |
+
`--class-id-txt`:是数据集 `id class_name` 的 `.txt` 文件:
|
| 242 |
+
|
| 243 |
+
- 如果不指定,则脚本会自动生成,生成在 `--out` 同级的目录中,保存文件名为 `class_with_id.txt`;
|
| 244 |
+
- 如果指定,脚本仅会进行读取但不会新增或者覆盖,同时,脚本里面还会判断是否存在 `.txt` 中其他的类,如果出现了会报错提示,届时,请用户检查 `.txt` 文件并加入新的类及其 `id`。
|
| 245 |
+
|
| 246 |
+
`.txt` 文件的例子如下( `id` 可以和 COCO 一样,从 `1` 开始):
|
| 247 |
+
|
| 248 |
+
```text
|
| 249 |
+
1 cat
|
| 250 |
+
2 dog
|
| 251 |
+
3 bicycle
|
| 252 |
+
4 motorcycle
|
| 253 |
+
|
| 254 |
+
```
|
| 255 |
+
|
| 256 |
+
例子:
|
| 257 |
+
|
| 258 |
+
以本教程的 `cat` 数据集为例:
|
| 259 |
+
|
| 260 |
+
```shell
|
| 261 |
+
python tools/dataset_converters/labelme2coco.py --img-dir ./data/cat/images \
|
| 262 |
+
--labels-dir ./data/cat/labels \
|
| 263 |
+
--out ./data/cat/annotations/annotations_all.json
|
| 264 |
+
```
|
| 265 |
+
|
| 266 |
+
本次演示的 `cat` 数据集(注意不需要包括背景类),可以看到生成的 `class_with_id.txt` 中只有 `1` 类:
|
| 267 |
+
|
| 268 |
+
```text
|
| 269 |
+
1 cat
|
| 270 |
+
|
| 271 |
+
```
|
| 272 |
+
|
| 273 |
+
### 3.2 检查转换的 COCO label
|
| 274 |
+
|
| 275 |
+
使用下面的命令可以将 COCO 的 label 在图片上进行显示,这一步可以验证刚刚转换是否有问题:
|
| 276 |
+
|
| 277 |
+
```shell
|
| 278 |
+
python tools/analysis_tools/browse_coco_json.py --img-dir ${图片文件夹路径} \
|
| 279 |
+
--ann-file ${COCO label json 路径}
|
| 280 |
+
```
|
| 281 |
+
|
| 282 |
+
例子:
|
| 283 |
+
|
| 284 |
+
```shell
|
| 285 |
+
python tools/analysis_tools/browse_coco_json.py --img-dir ./data/cat/images \
|
| 286 |
+
--ann-file ./data/cat/annotations/annotations_all.json
|
| 287 |
+
```
|
| 288 |
+
|
| 289 |
+
<div align=center>
|
| 290 |
+
<img alt="Image" src="https://user-images.githubusercontent.com/25873202/205429166-a6e48d20-c60b-4571-b00e-54439003ad3b.png">
|
| 291 |
+
</div>
|
| 292 |
+
|
| 293 |
+
```{SeeAlso}
|
| 294 |
+
关于 `tools/analysis_tools/browse_coco_json.py` 的更多用法请参考 [可视化 COCO label](https://mmyolo.readthedocs.io/zh_CN/latest/user_guides/useful_tools.html#coco)。
|
| 295 |
+
```
|
| 296 |
+
|
| 297 |
+
## 4. 数据集划分为训练集、验证集和测试集
|
| 298 |
+
|
| 299 |
+
通常,自定义图片都是一个大文件夹,里面全部都是图片,需要我们自己去对图片进行训练集、验证集、测试集的划分,如果数据量比较少,可以不划分验证集。下面是划分脚本的具体用法:
|
| 300 |
+
|
| 301 |
+
```shell
|
| 302 |
+
python tools/misc/coco_split.py --json ${COCO label json 路径} \
|
| 303 |
+
--out-dir ${划分 label json 保存根路径} \
|
| 304 |
+
--ratios ${划分比例} \
|
| 305 |
+
[--shuffle] \
|
| 306 |
+
[--seed ${划分的随机种子}]
|
| 307 |
+
```
|
| 308 |
+
|
| 309 |
+
其中:
|
| 310 |
+
|
| 311 |
+
- `--ratios`:划分的比例,如果只设置了 2 个,则划分为 `trainval + test`,如果设置为 3 个,则划分为 `train + val + test`。支持两种格式 —— 整数、小数:
|
| 312 |
+
- 整数:按比例进行划分,代码中会进行归一化之后划分数据集。例子: `--ratio 2 1 1`(代码里面会转换成 `0.5 0.25 0.25`) or `--ratio 3 1`(代码里面会转换成 `0.75 0.25`)
|
| 313 |
+
- 小数:划分为比例。**如果加起来不为 1 ,则脚本会进行自动归一化修正**。例子: `--ratio 0.8 0.1 0.1` or `--ratio 0.8 0.2`
|
| 314 |
+
- `--shuffle`: 是否打乱数据集再进行划分;
|
| 315 |
+
- `--seed`:设定划分的随机种子,不设置的话自动生成随机种子。
|
| 316 |
+
|
| 317 |
+
例子:
|
| 318 |
+
|
| 319 |
+
```shell
|
| 320 |
+
python tools/misc/coco_split.py --json ./data/cat/annotations/annotations_all.json \
|
| 321 |
+
--out-dir ./data/cat/annotations \
|
| 322 |
+
--ratios 0.8 0.2 \
|
| 323 |
+
--shuffle \
|
| 324 |
+
--seed 10
|
| 325 |
+
```
|
| 326 |
+
|
| 327 |
+
<div align=center>
|
| 328 |
+
<img alt="Image" src="https://user-images.githubusercontent.com/25873202/205428346-5fdfbfca-0682-47aa-b0be-fa467cd0c5f8.png">
|
| 329 |
+
</div>
|
| 330 |
+
|
| 331 |
+
## 5. 根据数据集内容新建 config 文件
|
| 332 |
+
|
| 333 |
+
确保数据集目录是这样的:
|
| 334 |
+
|
| 335 |
+
```shell
|
| 336 |
+
.
|
| 337 |
+
└── $DATA_ROOT
|
| 338 |
+
├── annotations
|
| 339 |
+
│ ├── trainval.json # 根据上面的指令只划分 trainval + test,如果您使用 3 组划分比例的话,这里是 train.json、val.json、test.json
|
| 340 |
+
│ └── test.json
|
| 341 |
+
├── images
|
| 342 |
+
│ ├── image1.jpg
|
| 343 |
+
│ ├── image1.png
|
| 344 |
+
│ └── ...
|
| 345 |
+
└── ...
|
| 346 |
+
```
|
| 347 |
+
|
| 348 |
+
因为是我们自定义的数据集,所以我们需要自己新建一个 config 并加入需要修改的部分信息。
|
| 349 |
+
|
| 350 |
+
关于新的 config 的命名:
|
| 351 |
+
|
| 352 |
+
- 这个 config 继承的是 `yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py`;
|
| 353 |
+
- 训练的类以本教程提供的数据集中的类 `cat` 为例(如果是自己的数据集,可以自定义类型的总称);
|
| 354 |
+
- 本教程测试的显卡型号是 1 x 3080Ti 12G 显存,电脑内存 32G,可以训练 YOLOv5-s 最大批次是 `batch size = 32`(详细机器资料可见附录);
|
| 355 |
+
- 训练轮次是 `100 epoch`。
|
| 356 |
+
|
| 357 |
+
综上所述:可以将其命名为 `yolov5_s-v61_syncbn_fast_1xb32-100e_cat.py`,并将其放置在文件夹 `configs/custom_dataset` 中。
|
| 358 |
+
|
| 359 |
+
我们可以在 configs 目录下新建一个新的目录 `custom_dataset`,同时在里面新建该 config 文件,并添加以下内容:
|
| 360 |
+
|
| 361 |
+
<div align=center>
|
| 362 |
+
<img alt="Image" src="https://user-images.githubusercontent.com/25873202/205428358-e32fb455-480a-4f14-9613-e4cc3193fb4d.png">
|
| 363 |
+
</div>
|
| 364 |
+
|
| 365 |
+
```python
|
| 366 |
+
_base_ = '../yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py'
|
| 367 |
+
|
| 368 |
+
max_epochs = 100 # 训练的最大 epoch
|
| 369 |
+
data_root = './data/cat/' # 数据集目录的绝对路径
|
| 370 |
+
# data_root = '/root/workspace/mmyolo/data/cat/' # Docker 容器里面数据集目录的绝对路径
|
| 371 |
+
|
| 372 |
+
# 结果保存的路径,可以省略,省略保存的文件名位于 work_dirs 下 config 同名的文件夹中
|
| 373 |
+
# 如果某个 config 只是修改了部分参数,修改这个变量就可以将新的训练文件保存到其他地方
|
| 374 |
+
work_dir = './work_dirs/yolov5_s-v61_syncbn_fast_1xb32-100e_cat'
|
| 375 |
+
|
| 376 |
+
# load_from 可以指定本地路径或者 URL,设置了 URL 会自动进行下载,因为上面已经下载过,我们这里设置本地路径
|
| 377 |
+
# 因为本教程是在 cat 数据集上微调,故这里需要使用 `load_from` 来加载 MMYOLO 中的预训练模型,这样可以在加快收敛速度的同时保证精度
|
| 378 |
+
load_from = './work_dirs/yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth' # noqa
|
| 379 |
+
|
| 380 |
+
# 根据自己的 GPU 情况,修改 batch size,YOLOv5-s 默认为 8卡 x 16bs
|
| 381 |
+
train_batch_size_per_gpu = 32
|
| 382 |
+
train_num_workers = 4 # 推荐使用 train_num_workers = nGPU x 4
|
| 383 |
+
|
| 384 |
+
save_epoch_intervals = 2 # 每 interval 轮迭代进行一次保存一次权重
|
| 385 |
+
|
| 386 |
+
# 根据自己的 GPU 情况,修改 base_lr,修改的比例是 base_lr_default * (your_bs / default_bs)
|
| 387 |
+
base_lr = _base_.base_lr / 4
|
| 388 |
+
|
| 389 |
+
anchors = [ # 此处已经根据数据集特点更新了 anchor,关于 anchor 的生成,后面小节会讲解
|
| 390 |
+
[(68, 69), (154, 91), (143, 162)], # P3/8
|
| 391 |
+
[(242, 160), (189, 287), (391, 207)], # P4/16
|
| 392 |
+
[(353, 337), (539, 341), (443, 432)] # P5/32
|
| 393 |
+
]
|
| 394 |
+
|
| 395 |
+
class_name = ('cat', ) # 根据 class_with_id.txt 类别信息,设置 class_name
|
| 396 |
+
num_classes = len(class_name)
|
| 397 |
+
metainfo = dict(
|
| 398 |
+
classes=class_name,
|
| 399 |
+
palette=[(220, 20, 60)] # 画图时候的颜色,随便设置即可
|
| 400 |
+
)
|
| 401 |
+
|
| 402 |
+
train_cfg = dict(
|
| 403 |
+
max_epochs=max_epochs,
|
| 404 |
+
val_begin=20, # 第几个 epoch 后验证,这里设置 20 是因为前 20 个 epoch 精度不高,测试意义不大,故跳过
|
| 405 |
+
val_interval=save_epoch_intervals # 每 val_interval 轮迭代进行一次测试评估
|
| 406 |
+
)
|
| 407 |
+
|
| 408 |
+
model = dict(
|
| 409 |
+
bbox_head=dict(
|
| 410 |
+
head_module=dict(num_classes=num_classes),
|
| 411 |
+
prior_generator=dict(base_sizes=anchors),
|
| 412 |
+
|
| 413 |
+
# loss_cls 会根据 num_classes 动态调整,但是 num_classes = 1 的时候,loss_cls 恒为 0
|
| 414 |
+
loss_cls=dict(loss_weight=0.5 *
|
| 415 |
+
(num_classes / 80 * 3 / _base_.num_det_layers))))
|
| 416 |
+
|
| 417 |
+
train_dataloader = dict(
|
| 418 |
+
batch_size=train_batch_size_per_gpu,
|
| 419 |
+
num_workers=train_num_workers,
|
| 420 |
+
dataset=dict(
|
| 421 |
+
_delete_=True,
|
| 422 |
+
type='RepeatDataset',
|
| 423 |
+
# 数据量太少的话,可以使用 RepeatDataset ,在每个 epoch 内重复当前数据集 n 次,这里设置 5 是重复 5 次
|
| 424 |
+
times=5,
|
| 425 |
+
dataset=dict(
|
| 426 |
+
type=_base_.dataset_type,
|
| 427 |
+
data_root=data_root,
|
| 428 |
+
metainfo=metainfo,
|
| 429 |
+
ann_file='annotations/trainval.json',
|
| 430 |
+
data_prefix=dict(img='images/'),
|
| 431 |
+
filter_cfg=dict(filter_empty_gt=False, min_size=32),
|
| 432 |
+
pipeline=_base_.train_pipeline)))
|
| 433 |
+
|
| 434 |
+
val_dataloader = dict(
|
| 435 |
+
dataset=dict(
|
| 436 |
+
metainfo=metainfo,
|
| 437 |
+
data_root=data_root,
|
| 438 |
+
ann_file='annotations/trainval.json',
|
| 439 |
+
data_prefix=dict(img='images/')))
|
| 440 |
+
|
| 441 |
+
test_dataloader = val_dataloader
|
| 442 |
+
|
| 443 |
+
val_evaluator = dict(ann_file=data_root + 'annotations/trainval.json')
|
| 444 |
+
test_evaluator = val_evaluator
|
| 445 |
+
|
| 446 |
+
optim_wrapper = dict(optimizer=dict(lr=base_lr))
|
| 447 |
+
|
| 448 |
+
default_hooks = dict(
|
| 449 |
+
# 设置间隔多少个 epoch 保存模型,以及保存模型最多几个,`save_best` 是另外保存最佳模型(推荐)
|
| 450 |
+
checkpoint=dict(
|
| 451 |
+
type='CheckpointHook',
|
| 452 |
+
interval=save_epoch_intervals,
|
| 453 |
+
max_keep_ckpts=5,
|
| 454 |
+
save_best='auto'),
|
| 455 |
+
param_scheduler=dict(max_epochs=max_epochs),
|
| 456 |
+
# logger 输出的间隔
|
| 457 |
+
logger=dict(type='LoggerHook', interval=10))
|
| 458 |
+
|
| 459 |
+
```
|
| 460 |
+
|
| 461 |
+
```{Note}
|
| 462 |
+
我们在 `projects/misc/custom_dataset/yolov5_s-v61_syncbn_fast_1xb32-100e_cat.py` 放了一份相同的 config 文件,用户可以选择复制到 `configs/custom_dataset/yolov5_s-v61_syncbn_fast_1xb32-100e_cat.py` 路径直接开始训练。
|
| 463 |
+
```
|
| 464 |
+
|
| 465 |
+
## 6. 数据集可视化分析
|
| 466 |
+
|
| 467 |
+
脚本 `tools/analysis_tools/dataset_analysis.py` 能够帮助用户得到数据集的分析图。该脚本可以生成 4 种分析图:
|
| 468 |
+
|
| 469 |
+
- 显示类别和 bbox 实例个数的分布图:`show_bbox_num`
|
| 470 |
+
- 显示类别和 bbox 实例宽、高的分布图:`show_bbox_wh`
|
| 471 |
+
- 显示类别和 bbox 实例宽/高比例的分布图:`show_bbox_wh_ratio`
|
| 472 |
+
- 基于面积规则下,显示类别和 bbox 实例面积的分布图:`show_bbox_area`
|
| 473 |
+
|
| 474 |
+
脚本使用方式如下:
|
| 475 |
+
|
| 476 |
+
```shell
|
| 477 |
+
python tools/analysis_tools/dataset_analysis.py ${CONFIG} \
|
| 478 |
+
[--val-dataset ${TYPE}] \
|
| 479 |
+
[--class-name ${CLASS_NAME}] \
|
| 480 |
+
[--area-rule ${AREA_RULE}] \
|
| 481 |
+
[--func ${FUNC}] \
|
| 482 |
+
[--out-dir ${OUT_DIR}]
|
| 483 |
+
```
|
| 484 |
+
|
| 485 |
+
例子:
|
| 486 |
+
|
| 487 |
+
以本教程 `cat` 数据集 的 config 为例:
|
| 488 |
+
|
| 489 |
+
查看训练集数据分布情况:
|
| 490 |
+
|
| 491 |
+
```shell
|
| 492 |
+
python tools/analysis_tools/dataset_analysis.py configs/custom_dataset/yolov5_s-v61_syncbn_fast_1xb32-100e_cat.py \
|
| 493 |
+
--out-dir work_dirs/dataset_analysis_cat/train_dataset
|
| 494 |
+
```
|
| 495 |
+
|
| 496 |
+
查看验证集数据分布情况:
|
| 497 |
+
|
| 498 |
+
```shell
|
| 499 |
+
python tools/analysis_tools/dataset_analysis.py configs/custom_dataset/yolov5_s-v61_syncbn_fast_1xb32-100e_cat.py \
|
| 500 |
+
--out-dir work_dirs/dataset_analysis_cat/val_dataset \
|
| 501 |
+
--val-dataset
|
| 502 |
+
```
|
| 503 |
+
|
| 504 |
+
效果(点击图片可查看大图):
|
| 505 |
+
|
| 506 |
+
<table align="center">
|
| 507 |
+
<tbody>
|
| 508 |
+
<tr align="center" valign="center">
|
| 509 |
+
<td>
|
| 510 |
+
<b>基于面积规则下,显示类别和 bbox 实例面积的分布图</b>
|
| 511 |
+
</td>
|
| 512 |
+
<td>
|
| 513 |
+
<b>显示类别和 bbox 实例宽、高的分布图</b>
|
| 514 |
+
</td>
|
| 515 |
+
</tr>
|
| 516 |
+
<tr align="center" valign="center">
|
| 517 |
+
<td>
|
| 518 |
+
<img alt="YOLOv5CocoDataset_bbox_area" src="https://user-images.githubusercontent.com/25873202/206709093-1ed40f4e-cae3-4383-b120-79ad44c12312.jpg" width="60%">
|
| 519 |
+
</td>
|
| 520 |
+
<td>
|
| 521 |
+
<img alt="YOLOv5CocoDataset_bbox_wh" src="https://user-images.githubusercontent.com/25873202/206709127-aebbb238-4af8-46c8-b71e-8540ed5f5de1.jpg" width="60%">
|
| 522 |
+
</td>
|
| 523 |
+
</tr>
|
| 524 |
+
<tr align="center" valign="center">
|
| 525 |
+
<td>
|
| 526 |
+
<b>显示类别和 bbox 实例个数的分布图</b>
|
| 527 |
+
</td>
|
| 528 |
+
<td>
|
| 529 |
+
<b>显示类别和 bbox 实例宽/高比例的分布图</b>
|
| 530 |
+
</td>
|
| 531 |
+
</tr>
|
| 532 |
+
<tr align="center" valign="center">
|
| 533 |
+
<td>
|
| 534 |
+
<img alt="YOLOv5CocoDataset_bbox_num" src="https://user-images.githubusercontent.com/25873202/206709108-8cee54f3-3102-4ca2-a10a-e4adb760881b.jpg" width="60%">
|
| 535 |
+
</td>
|
| 536 |
+
<td>
|
| 537 |
+
<img alt="YOLOv5CocoDataset_bbox_ratio" src="https://user-images.githubusercontent.com/25873202/206709115-17aeba09-4ff1-4697-8842-94fbada6c428.jpg" width="60%">
|
| 538 |
+
</td>
|
| 539 |
+
</tr>
|
| 540 |
+
</tbody>
|
| 541 |
+
</table>
|
| 542 |
+
|
| 543 |
+
```{Note}
|
| 544 |
+
因为本教程使用的 cat 数据集数量比较少,故 config 里面用了 RepeatDataset,显示的数目实际上都是重复了 5 次。如果您想得到无重复的分析结果,可以暂时将 RepeatDataset 下面的 `times` 参数从 `5` 改成 `1`。
|
| 545 |
+
```
|
| 546 |
+
|
| 547 |
+
经过输出的图片分析可以得出,本教程使用的 `cat` 数据集的训练集具有以下情况:
|
| 548 |
+
|
| 549 |
+
- 图片全部是 `large object`;
|
| 550 |
+
- 类别 cat 的数量是 `655`;
|
| 551 |
+
- bbox 的宽高比例大部分集中在 `1.0 ~ 1.11`,比例最小值是 `0.36`,最大值是 `2.9`;
|
| 552 |
+
- bbox 的宽大部分是 `500 ~ 600` 左右,高大部分是 `500 ~ 600` 左右。
|
| 553 |
+
|
| 554 |
+
```{SeeAlso}
|
| 555 |
+
关于 `tools/analysis_tools/dataset_analysis.py` 的更多用法请参考 [可视化数据集分析](https://mmyolo.readthedocs.io/zh_CN/latest/user_guides/useful_tools.html#id4)。
|
| 556 |
+
```
|
| 557 |
+
|
| 558 |
+
## 7. 优化 Anchor 尺寸
|
| 559 |
+
|
| 560 |
+
```{Warning}
|
| 561 |
+
该步骤仅适用于 anchor-base 的模型,例如 YOLOv5;
|
| 562 |
+
|
| 563 |
+
Anchor-free 的模型可以跳过此步骤,例如 YOLOv6、YOLOX。
|
| 564 |
+
```
|
| 565 |
+
|
| 566 |
+
脚本 `tools/analysis_tools/optimize_anchors.py` 支持 YOLO 系列中三种锚框生成方式,分别是 `k-means`、`Differential Evolution`、`v5-k-means`.
|
| 567 |
+
|
| 568 |
+
本示例使用的是 YOLOv5 进行训练,使用的是 `640 x 640` 的输入大小,使用 `v5-k-means` 进行描框的优化:
|
| 569 |
+
|
| 570 |
+
```shell
|
| 571 |
+
python tools/analysis_tools/optimize_anchors.py configs/custom_dataset/yolov5_s-v61_syncbn_fast_1xb32-100e_cat.py \
|
| 572 |
+
--algorithm v5-k-means \
|
| 573 |
+
--input-shape 640 640 \
|
| 574 |
+
--prior-match-thr 4.0 \
|
| 575 |
+
--out-dir work_dirs/dataset_analysis_cat
|
| 576 |
+
```
|
| 577 |
+
|
| 578 |
+
```{Note}
|
| 579 |
+
因为该命令使用的是 k-means 聚类算法,存在一定的随机性,这与初始化有关。故每次执行得到的 Anchor 都会有些不一样,但是都是基于传递进去的数据集来进行生成的,故不会有什么不良影响。
|
| 580 |
+
```
|
| 581 |
+
|
| 582 |
+
经过计算的 Anchor 如下:
|
| 583 |
+
|
| 584 |
+
<div align=center>
|
| 585 |
+
<img alt="Anchor" src="https://user-images.githubusercontent.com/25873202/205422434-1a68cded-b055-42e9-b01c-3e51f8f5ef81.png">
|
| 586 |
+
</div>
|
| 587 |
+
|
| 588 |
+
修改 config 文件里面的 `anchors` 变量:
|
| 589 |
+
|
| 590 |
+
```python
|
| 591 |
+
anchors = [
|
| 592 |
+
[(68, 69), (154, 91), (143, 162)], # P3/8
|
| 593 |
+
[(242, 160), (189, 287), (391, 207)], # P4/16
|
| 594 |
+
[(353, 337), (539, 341), (443, 432)] # P5/32
|
| 595 |
+
]
|
| 596 |
+
```
|
| 597 |
+
|
| 598 |
+
```{SeeAlso}
|
| 599 |
+
关于 `tools/analysis_tools/optimize_anchors.py` 的更多用法请参考 [优化锚框尺寸](https://mmyolo.readthedocs.io/zh_CN/latest/user_guides/useful_tools.html#id8)。
|
| 600 |
+
```
|
| 601 |
+
|
| 602 |
+
## 8. 可视化 config 配置中数据处理部分
|
| 603 |
+
|
| 604 |
+
脚本 `tools/analysis_tools/browse_dataset.py` 能够帮助用户去直接窗口可视化 config 配置中数据处理部分,同时可以选择保存可视化图片到指定文件夹内。
|
| 605 |
+
|
| 606 |
+
下面演示使用我们刚刚新建的 config 文件 `configs/custom_dataset/yolov5_s-v61_syncbn_fast_1xb32-100e_cat.py` 来可视化图片,该命令会使得图片直接弹出显示,每张图片持续 `3` 秒,图片不进行保存:
|
| 607 |
+
|
| 608 |
+
```shell
|
| 609 |
+
python tools/analysis_tools/browse_dataset.py configs/custom_dataset/yolov5_s-v61_syncbn_fast_1xb32-100e_cat.py \
|
| 610 |
+
--show-interval 3
|
| 611 |
+
```
|
| 612 |
+
|
| 613 |
+
<div align=center>
|
| 614 |
+
<img src="https://user-images.githubusercontent.com/25873202/205472078-c958e90d-8204-4c01-821a-8b6a006f05b2.png" alt="image" width="60%"/>
|
| 615 |
+
</div>
|
| 616 |
+
|
| 617 |
+
<div align=center>
|
| 618 |
+
<img src="https://user-images.githubusercontent.com/25873202/205472197-8228c75e-6046-404a-89b4-ed55eeb2cb95.png" alt="image" width="60%"/>
|
| 619 |
+
</div>
|
| 620 |
+
|
| 621 |
+
```{SeeAlso}
|
| 622 |
+
关于 `tools/analysis_tools/browse_dataset.py` 的更多用法请参考 [可视化数据集](https://mmyolo.readthedocs.io/zh_CN/latest/user_guides/useful_tools.html#id3)。
|
| 623 |
+
```
|
| 624 |
+
|
| 625 |
+
## 9. 训练
|
| 626 |
+
|
| 627 |
+
下面会从以下 3 点来进行讲解:
|
| 628 |
+
|
| 629 |
+
1. 训练可视化
|
| 630 |
+
2. YOLOv5 模型训练
|
| 631 |
+
3. 切换 YOLO 模型训练
|
| 632 |
+
|
| 633 |
+
### 9.1 训练可视化
|
| 634 |
+
|
| 635 |
+
如果需要采用浏览器对训练过程可视化,MMYOLO 目前提供 2 种方式 [wandb](https://wandb.ai/site) 和 [TensorBoard](https://tensorflow.google.cn/tensorboard),根据自己的情况选择其一即可(后续会扩展更多可视化后端支持)。
|
| 636 |
+
|
| 637 |
+
#### 9.1.1 wandb
|
| 638 |
+
|
| 639 |
+
wandb 可视化需要在[官网](https://wandb.ai/site)注册,并在 https://wandb.ai/settings 获取到 wandb 的 API Keys。
|
| 640 |
+
|
| 641 |
+
<div align=center>
|
| 642 |
+
<img src="https://cdn.vansin.top/img/20220913212628.png" alt="image"/>
|
| 643 |
+
</div>
|
| 644 |
+
|
| 645 |
+
然后在命令行进行安装
|
| 646 |
+
|
| 647 |
+
```shell
|
| 648 |
+
pip install wandb
|
| 649 |
+
# 运行了 wandb login 后输入上文中获取到的 API Keys ,便登录成功。
|
| 650 |
+
wandb login
|
| 651 |
+
```
|
| 652 |
+
|
| 653 |
+
<div align=center>
|
| 654 |
+
<img src="https://user-images.githubusercontent.com/25873202/206070473-201795e0-c81f-4247-842a-16d6acae0474.png" alt="推理图片"/>
|
| 655 |
+
</div>
|
| 656 |
+
|
| 657 |
+
在我们刚刚新建的 config 文件 `configs/custom_dataset/yolov5_s-v61_syncbn_fast_1xb32-100e_cat.py` 的最后添加 wandb 配置:
|
| 658 |
+
|
| 659 |
+
```python
|
| 660 |
+
visualizer = dict(vis_backends=[dict(type='LocalVisBackend'), dict(type='WandbVisBackend')])
|
| 661 |
+
```
|
| 662 |
+
|
| 663 |
+
#### 9.1.2 TensorBoard
|
| 664 |
+
|
| 665 |
+
安装 Tensorboard 环境
|
| 666 |
+
|
| 667 |
+
```shell
|
| 668 |
+
pip install tensorboard
|
| 669 |
+
```
|
| 670 |
+
|
| 671 |
+
在我们刚刚新建的 config 文件 `configs/custom_dataset/yolov5_s-v61_syncbn_fast_1xb32-100e_cat.py` 中添加 `tensorboard` 配置
|
| 672 |
+
|
| 673 |
+
```python
|
| 674 |
+
visualizer = dict(vis_backends=[dict(type='LocalVisBackend'),dict(type='TensorboardVisBackend')])
|
| 675 |
+
```
|
| 676 |
+
|
| 677 |
+
运行训练命令后,Tensorboard 文件会生成在可视化文件夹 `work_dirs/yolov5_s-v61_syncbn_fast_1xb32-100e_cat/${TIMESTAMP}/vis_data` 下,
|
| 678 |
+
运行下面的命令便可以在网页链接使用 Tensorboard 查看 loss、学习率和 coco/bbox_mAP 等可视化数据了:
|
| 679 |
+
|
| 680 |
+
```shell
|
| 681 |
+
tensorboard --logdir=work_dirs/yolov5_s-v61_syncbn_fast_1xb32-100e_cat
|
| 682 |
+
```
|
| 683 |
+
|
| 684 |
+
### 9.2 执行训练
|
| 685 |
+
|
| 686 |
+
使用下面命令进行启动训练(训练大约需要 2.5 个小时):
|
| 687 |
+
|
| 688 |
+
```shell
|
| 689 |
+
python tools/train.py configs/custom_dataset/yolov5_s-v61_syncbn_fast_1xb32-100e_cat.py
|
| 690 |
+
```
|
| 691 |
+
|
| 692 |
+
如果您开启了 wandb 的话,可以登录到自己的账户,在 wandb 中查看本次训练的详细信息了:
|
| 693 |
+
|
| 694 |
+
<div align=center>
|
| 695 |
+
<img src="https://user-images.githubusercontent.com/25873202/206097557-7b10cf0f-8a16-4ba6-8563-b0a3cb149537.png" alt="Image"/>
|
| 696 |
+
</div>
|
| 697 |
+
|
| 698 |
+
<div align=center>
|
| 699 |
+
<img src="https://user-images.githubusercontent.com/25873202/206097706-7e131bf7-f3bf-43fb-9fe5-5589a324de69.png" alt="Image"/>
|
| 700 |
+
</div>
|
| 701 |
+
|
| 702 |
+
下面是 `1 x 3080Ti`、`batch size = 32`,训练 `100 epoch` 最佳精度权重 `work_dirs/yolov5_s-v61_syncbn_fast_1xb32-100e_cat/best_coco/bbox_mAP_epoch_98.pth` 得出来的精度(详细机器资料可见附录):
|
| 703 |
+
|
| 704 |
+
```shell
|
| 705 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.968
|
| 706 |
+
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 1.000
|
| 707 |
+
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 1.000
|
| 708 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000
|
| 709 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000
|
| 710 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.968
|
| 711 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.886
|
| 712 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.977
|
| 713 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.977
|
| 714 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000
|
| 715 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000
|
| 716 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.977
|
| 717 |
+
|
| 718 |
+
bbox_mAP_copypaste: 0.968 1.000 1.000 -1.000 -1.000 0.968
|
| 719 |
+
Epoch(val) [98][116/116] coco/bbox_mAP: 0.9680 coco/bbox_mAP_50: 1.0000 coco/bbox_mAP_75: 1.0000 coco/bbox_mAP_s: -1.0000 coco/bbox_mAP_m: -1.0000 coco/bbox_mAP_l: 0.9680
|
| 720 |
+
```
|
| 721 |
+
|
| 722 |
+
```{Tip}
|
| 723 |
+
在一般的 finetune 最佳实践中都会推荐将 backbone 固定不参与训练,并且学习率 lr 也进行相应缩放,但是在本教程中发现这种做法会出现一定程度掉点。猜测可能原因是 cat 类别已经在 COCO 数据集中,而本教程使用的 cat 数据集数量比较小导致的。
|
| 724 |
+
```
|
| 725 |
+
|
| 726 |
+
下表是采用 MMYOLO YOLOv5 预训练模型 `yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth` 在没对 cat 数据集进行 finetune 的测试精度,可以看到 `cat` 类别的 mAP 只有 `0.866`,经过我们 finetune `mAP` 提升到了 `0.968`,提升了 `10.2 %`,可以证明训练是非常成功的:
|
| 727 |
+
|
| 728 |
+
```shell
|
| 729 |
+
+---------------+-------+--------------+-----+----------------+------+
|
| 730 |
+
| category | AP | category | AP | category | AP |
|
| 731 |
+
+---------------+-------+--------------+-----+----------------+------+
|
| 732 |
+
| person | nan | bicycle | nan | car | nan |
|
| 733 |
+
| motorcycle | nan | airplane | nan | bus | nan |
|
| 734 |
+
| train | nan | truck | nan | boat | nan |
|
| 735 |
+
| traffic light | nan | fire hydrant | nan | stop sign | nan |
|
| 736 |
+
| parking meter | nan | bench | nan | bird | nan |
|
| 737 |
+
| cat | 0.866 | dog | nan | horse | nan |
|
| 738 |
+
| sheep | nan | cow | nan | elephant | nan |
|
| 739 |
+
| bear | nan | zebra | nan | giraffe | nan |
|
| 740 |
+
| backpack | nan | umbrella | nan | handbag | nan |
|
| 741 |
+
| tie | nan | suitcase | nan | frisbee | nan |
|
| 742 |
+
| skis | nan | snowboard | nan | sports ball | nan |
|
| 743 |
+
| kite | nan | baseball bat | nan | baseball glove | nan |
|
| 744 |
+
| skateboard | nan | surfboard | nan | tennis racket | nan |
|
| 745 |
+
| bottle | nan | wine glass | nan | cup | nan |
|
| 746 |
+
| fork | nan | knife | nan | spoon | nan |
|
| 747 |
+
| bowl | nan | banana | nan | apple | nan |
|
| 748 |
+
| sandwich | nan | orange | nan | broccoli | nan |
|
| 749 |
+
| carrot | nan | hot dog | nan | pizza | nan |
|
| 750 |
+
| donut | nan | cake | nan | chair | nan |
|
| 751 |
+
| couch | nan | potted plant | nan | bed | nan |
|
| 752 |
+
| dining table | nan | toilet | nan | tv | nan |
|
| 753 |
+
| laptop | nan | mouse | nan | remote | nan |
|
| 754 |
+
| keyboard | nan | cell phone | nan | microwave | nan |
|
| 755 |
+
| oven | nan | toaster | nan | sink | nan |
|
| 756 |
+
| refrigerator | nan | book | nan | clock | nan |
|
| 757 |
+
| vase | nan | scissors | nan | teddy bear | nan |
|
| 758 |
+
| hair drier | nan | toothbrush | nan | None | None |
|
| 759 |
+
+---------------+-------+--------------+-----+----------------+------+
|
| 760 |
+
```
|
| 761 |
+
|
| 762 |
+
```{SeeAlso}
|
| 763 |
+
关于如何得到预训练权重的精度,可以详见附录【2. 如何测试数据集在预训练权重的精度】
|
| 764 |
+
```
|
| 765 |
+
|
| 766 |
+
### 9.3 尝试 MMYOLO 其他模型
|
| 767 |
+
|
| 768 |
+
MMYOLO 集成了多种 YOLO 算法,切换非常方便,无需重新熟悉一个新的 repo,直接切换 config 文件就可以轻松切换 YOLO 模型,只需简单 3 步即可切换模型:
|
| 769 |
+
|
| 770 |
+
1. 新建 config 文件
|
| 771 |
+
2. 下载预训练权重
|
| 772 |
+
3. 启动训练
|
| 773 |
+
|
| 774 |
+
下面以 YOLOv6-s 为例,进行讲解。
|
| 775 |
+
|
| 776 |
+
1. 搭建一个新的 config:
|
| 777 |
+
|
| 778 |
+
```python
|
| 779 |
+
_base_ = '../yolov6/yolov6_s_syncbn_fast_8xb32-400e_coco.py'
|
| 780 |
+
|
| 781 |
+
max_epochs = 100 # 训练的最大 epoch
|
| 782 |
+
data_root = './data/cat/' # 数据集目录的绝对路径
|
| 783 |
+
|
| 784 |
+
# 结果保存的路径,可以省略,省略保存的文件名位于 work_dirs 下 config 同名的文件夹中
|
| 785 |
+
# 如果某个 config 只是修改了部分参数,修改这个变量就可以将新的训练文件保存到其他地方
|
| 786 |
+
work_dir = './work_dirs/yolov6_s_syncbn_fast_1xb32-100e_cat'
|
| 787 |
+
|
| 788 |
+
# load_from 可以指定本地路径或者 URL,设置了 URL 会自动进行下载,因为上面已经下载过,我们这里设置本地路径
|
| 789 |
+
# 因为本教程是在 cat 数据集上微调,故这里需要使用 `load_from` 来加载 MMYOLO 中的预训练模型,这样可以在加快收敛速度的同时保证精度
|
| 790 |
+
load_from = './work_dirs/yolov6_s_syncbn_fast_8xb32-400e_coco_20221102_203035-932e1d91.pth' # noqa
|
| 791 |
+
|
| 792 |
+
# 根据自己的 GPU 情况,修改 batch size,YOLOv6-s 默认为 8卡 x 32bs
|
| 793 |
+
train_batch_size_per_gpu = 32
|
| 794 |
+
train_num_workers = 4 # 推荐使用 train_num_workers = nGPU x 4
|
| 795 |
+
|
| 796 |
+
save_epoch_intervals = 2 # 每 interval 轮迭代进行一次保存一次权重
|
| 797 |
+
|
| 798 |
+
# 根据自己的 GPU 情况,修改 base_lr,修改的比例是 base_lr_default * (your_bs / default_bs)
|
| 799 |
+
base_lr = _base_.base_lr / 8
|
| 800 |
+
|
| 801 |
+
class_name = ('cat', ) # 根据 class_with_id.txt 类别信息,设置 class_name
|
| 802 |
+
num_classes = len(class_name)
|
| 803 |
+
metainfo = dict(
|
| 804 |
+
classes=class_name,
|
| 805 |
+
palette=[(220, 20, 60)] # 画图时候的颜色,随便设置即可
|
| 806 |
+
)
|
| 807 |
+
|
| 808 |
+
train_cfg = dict(
|
| 809 |
+
max_epochs=max_epochs,
|
| 810 |
+
val_begin=20, # 第几个 epoch 后验证,这里设置 20 是因为前 20 个 epoch 精度不高,测试意义不大,故跳过
|
| 811 |
+
val_interval=save_epoch_intervals, # 每 val_interval 轮迭代进行一次测试评估
|
| 812 |
+
dynamic_intervals=[(max_epochs - _base_.num_last_epochs, 1)]
|
| 813 |
+
)
|
| 814 |
+
|
| 815 |
+
model = dict(
|
| 816 |
+
bbox_head=dict(
|
| 817 |
+
head_module=dict(num_classes=num_classes)),
|
| 818 |
+
train_cfg=dict(
|
| 819 |
+
initial_assigner=dict(num_classes=num_classes),
|
| 820 |
+
assigner=dict(num_classes=num_classes))
|
| 821 |
+
)
|
| 822 |
+
|
| 823 |
+
train_dataloader = dict(
|
| 824 |
+
batch_size=train_batch_size_per_gpu,
|
| 825 |
+
num_workers=train_num_workers,
|
| 826 |
+
dataset=dict(
|
| 827 |
+
_delete_=True,
|
| 828 |
+
type='RepeatDataset',
|
| 829 |
+
# 数据量太少的话,可以使用 RepeatDataset ,在每个 epoch 内重复当前数据集 n 次,这里设置 5 是重复 5 次
|
| 830 |
+
times=5,
|
| 831 |
+
dataset=dict(
|
| 832 |
+
type=_base_.dataset_type,
|
| 833 |
+
data_root=data_root,
|
| 834 |
+
metainfo=metainfo,
|
| 835 |
+
ann_file='annotations/trainval.json',
|
| 836 |
+
data_prefix=dict(img='images/'),
|
| 837 |
+
filter_cfg=dict(filter_empty_gt=False, min_size=32),
|
| 838 |
+
pipeline=_base_.train_pipeline)))
|
| 839 |
+
|
| 840 |
+
val_dataloader = dict(
|
| 841 |
+
dataset=dict(
|
| 842 |
+
metainfo=metainfo,
|
| 843 |
+
data_root=data_root,
|
| 844 |
+
ann_file='annotations/trainval.json',
|
| 845 |
+
data_prefix=dict(img='images/')))
|
| 846 |
+
|
| 847 |
+
test_dataloader = val_dataloader
|
| 848 |
+
|
| 849 |
+
val_evaluator = dict(ann_file=data_root + 'annotations/trainval.json')
|
| 850 |
+
test_evaluator = val_evaluator
|
| 851 |
+
|
| 852 |
+
optim_wrapper = dict(optimizer=dict(lr=base_lr))
|
| 853 |
+
|
| 854 |
+
default_hooks = dict(
|
| 855 |
+
# 设置间隔多少个 epoch 保存模型,以及保存模型最多几个,`save_best` 是另外保存最佳模型(推荐)
|
| 856 |
+
checkpoint=dict(
|
| 857 |
+
type='CheckpointHook',
|
| 858 |
+
interval=save_epoch_intervals,
|
| 859 |
+
max_keep_ckpts=5,
|
| 860 |
+
save_best='auto'),
|
| 861 |
+
param_scheduler=dict(max_epochs=max_epochs),
|
| 862 |
+
# logger 输出的间隔
|
| 863 |
+
logger=dict(type='LoggerHook', interval=10))
|
| 864 |
+
|
| 865 |
+
custom_hooks = [
|
| 866 |
+
dict(
|
| 867 |
+
type='EMAHook',
|
| 868 |
+
ema_type='ExpMomentumEMA',
|
| 869 |
+
momentum=0.0001,
|
| 870 |
+
update_buffers=True,
|
| 871 |
+
strict_load=False,
|
| 872 |
+
priority=49),
|
| 873 |
+
dict(
|
| 874 |
+
type='mmdet.PipelineSwitchHook',
|
| 875 |
+
switch_epoch=max_epochs - _base_.num_last_epochs,
|
| 876 |
+
switch_pipeline=_base_.train_pipeline_stage2)
|
| 877 |
+
]
|
| 878 |
+
|
| 879 |
+
```
|
| 880 |
+
|
| 881 |
+
```{Note}
|
| 882 |
+
同样,我们在 `projects/misc/custom_dataset/yolov6_s_syncbn_fast_1xb32-100e_cat.py` 放了一份相同的 config 文件,用户可以选择复制到 `configs/custom_dataset/yolov6_s_syncbn_fast_1xb32-100e_cat.py` 路径直接开始训练。
|
| 883 |
+
|
| 884 |
+
虽然新的 config 看上去好像很多东西,其实很多都是重复的,用户可以用对比软件对比一下即可看出大部分的配置都是和 `yolov5_s-v61_syncbn_fast_1xb32-100e_cat.py` 相同的。因为这 2 个 config 文件需要继承不同的 config,所以还是要添加一些必要的配置。
|
| 885 |
+
```
|
| 886 |
+
|
| 887 |
+
2. 下载 YOLOv6-s 的预训练权重
|
| 888 |
+
|
| 889 |
+
```bash
|
| 890 |
+
wget https://download.openmmlab.com/mmyolo/v0/yolov6/yolov6_s_syncbn_fast_8xb32-400e_coco/yolov6_s_syncbn_fast_8xb32-400e_coco_20221102_203035-932e1d91.pth -P work_dirs/
|
| 891 |
+
```
|
| 892 |
+
|
| 893 |
+
3. 训练
|
| 894 |
+
|
| 895 |
+
```shell
|
| 896 |
+
python tools/train.py configs/custom_dataset/yolov6_s_syncbn_fast_1xb32-100e_cat.py
|
| 897 |
+
```
|
| 898 |
+
|
| 899 |
+
在我的实验中,最佳模型是 `work_dirs/yolov6_s_syncbn_fast_1xb32-100e_cat/best_coco/bbox_mAP_epoch_96.pth`,其精度如下:
|
| 900 |
+
|
| 901 |
+
```bash
|
| 902 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.987
|
| 903 |
+
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 1.000
|
| 904 |
+
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 1.000
|
| 905 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000
|
| 906 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000
|
| 907 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.987
|
| 908 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.895
|
| 909 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.989
|
| 910 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.989
|
| 911 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000
|
| 912 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000
|
| 913 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.989
|
| 914 |
+
|
| 915 |
+
bbox_mAP_copypaste: 0.987 1.000 1.000 -1.000 -1.000 0.987
|
| 916 |
+
Epoch(val) [96][116/116] coco/bbox_mAP: 0.9870 coco/bbox_mAP_50: 1.0000 coco/bbox_mAP_75: 1.0000 coco/bbox_mAP_s: -1.0000 coco/bbox_mAP_m: -1.0000 coco/bbox_mAP_l: 0.9870
|
| 917 |
+
```
|
| 918 |
+
|
| 919 |
+
以上演示的是如何在 MMYOLO 中切换模型,可以快速对不同模型进行精度对比,精度高的模型可以上线生产。在我的实验中,YOLOv6 最佳精度 `0.9870` 比 YOLOv5 最佳精度 `0.9680` 高出 `1.9 %`,故后续我们使用 YOLOv6 来进行讲解。
|
| 920 |
+
|
| 921 |
+
## 10. 推理
|
| 922 |
+
|
| 923 |
+
使用最佳的模型进行推理,下面命令中的最佳模型路径是 `./work_dirs/yolov6_s_syncbn_fast_1xb32-100e_cat/best_coco/bbox_mAP_epoch_96.pth`,请用户自行修改为自己训练的最佳模型路径。
|
| 924 |
+
|
| 925 |
+
```shell
|
| 926 |
+
python demo/image_demo.py ./data/cat/images \
|
| 927 |
+
./configs/custom_dataset/yolov6_s_syncbn_fast_1xb32-100e_cat.py \
|
| 928 |
+
./work_dirs/yolov6_s_syncbn_fast_1xb32-100e_cat/best_coco/bbox_mAP_epoch_96.pth \
|
| 929 |
+
--out-dir ./data/cat/pred_images
|
| 930 |
+
```
|
| 931 |
+
|
| 932 |
+
<div align=center>
|
| 933 |
+
<img src="https://user-images.githubusercontent.com/25873202/204773727-5d3cbbad-1265-45a0-822a-887713555049.jpg" alt="推理图片"/>
|
| 934 |
+
</div>
|
| 935 |
+
|
| 936 |
+
```{Tip}
|
| 937 |
+
如果推理结果不理想,这里举例 2 种情况:
|
| 938 |
+
|
| 939 |
+
1. 模型欠拟合:
|
| 940 |
+
需要先判断是不是训练 epoch 不够导致的欠拟合,如果是训练不够,则修改 config 文件里面的 `max_epochs` 和 `work_dir` 参数,或者根据上面的命名方式新建一个 config 文件,重新进行训练。
|
| 941 |
+
|
| 942 |
+
2. 数据集需优化:
|
| 943 |
+
如果 epoch 加上去了还是不行,可以增加数据集数量,同时可以重新检查并优化数据集的标注,然后重新进行训练。
|
| 944 |
+
```
|
| 945 |
+
|
| 946 |
+
## 11. 部署
|
| 947 |
+
|
| 948 |
+
MMYOLO 提供两种部署方式:
|
| 949 |
+
|
| 950 |
+
1. [MMDeploy](https://github.com/open-mmlab/mmdeploy) 框架进行部署
|
| 951 |
+
2. 使用 `projects/easydeploy` 进行部署
|
| 952 |
+
|
| 953 |
+
### 11.1 MMDeploy 框架进行部署
|
| 954 |
+
|
| 955 |
+
考虑到部署的机器环境千差万别,很多时候在本地机器可以,但是在生产环境则不一定,这里推荐使用 Docker,做到环境一次部署,终身使用,节省运维搭建环境和部署生产的时间。
|
| 956 |
+
|
| 957 |
+
本小节会从一下几个小点进行展开讲解:
|
| 958 |
+
|
| 959 |
+
1. 构建 Docker 镜像
|
| 960 |
+
2. 创建 Docker 容器
|
| 961 |
+
3. 转换 TensorRT 模型
|
| 962 |
+
4. 部署模型执行推理
|
| 963 |
+
|
| 964 |
+
```{SeeAlso}
|
| 965 |
+
如果是对 Docker 不熟悉的用户,可以参考 MMDeploy 的 [源码手动安装](https://mmdeploy.readthedocs.io/zh_CN/latest/01-how-to-build/build_from_source.html) 文档直接在本地编译。安装完之后,可以直接跳到 【11.1.3 转换 TensorRT 模型】 小节。
|
| 966 |
+
```
|
| 967 |
+
|
| 968 |
+
#### 11.1.1 构建 Docker 镜像
|
| 969 |
+
|
| 970 |
+
```shell
|
| 971 |
+
git clone -b dev-1.x https://github.com/open-mmlab/mmdeploy.git
|
| 972 |
+
cd mmdeploy
|
| 973 |
+
docker build docker/GPU/ -t mmdeploy:gpu --build-arg USE_SRC_INSIDE=true
|
| 974 |
+
```
|
| 975 |
+
|
| 976 |
+
其中 `USE_SRC_INSIDE=true` 是拉取基础进行之后在内部切换国内源,构建速度会快一些。
|
| 977 |
+
|
| 978 |
+
执行脚本后,会进行构建,此刻需要等一段时间:
|
| 979 |
+
|
| 980 |
+
<div align=center>
|
| 981 |
+
<img src="https://user-images.githubusercontent.com/25873202/205482447-329186c8-eba3-443f-b1fa-b33c2ab3d5da.png" alt="Image"/>
|
| 982 |
+
</div>
|
| 983 |
+
|
| 984 |
+
#### 11.1.2 创建 Docker 容器
|
| 985 |
+
|
| 986 |
+
```shell
|
| 987 |
+
export MMYOLO_PATH=/path/to/local/mmyolo # 先将您机器上 MMYOLO 的路径写入环境变量
|
| 988 |
+
docker run --gpus all --name mmyolo-deploy -v ${MMYOLO_PATH}:/root/workspace/mmyolo -it mmdeploy:gpu /bin/bash
|
| 989 |
+
```
|
| 990 |
+
|
| 991 |
+
<div align=center>
|
| 992 |
+
<img src="https://user-images.githubusercontent.com/25873202/205536974-1eeb2901-9b14-4851-9c96-5046cd05f171.png" alt="Image"/>
|
| 993 |
+
</div>
|
| 994 |
+
|
| 995 |
+
可以看到本地的 MMYOLO 环境已经挂载到容器里面了
|
| 996 |
+
|
| 997 |
+
<div align=center>
|
| 998 |
+
<img src="https://user-images.githubusercontent.com/25873202/205537473-0afc16c3-c6d4-451a-96d7-1a2388341b60.png" alt="Image"/>
|
| 999 |
+
</div>
|
| 1000 |
+
|
| 1001 |
+
```{SeeAlso}
|
| 1002 |
+
有关这部分的详细介绍可以看 MMDeploy 官方文档 [使用 Docker 镜像](https://mmdeploy.readthedocs.io/zh_CN/latest/01-how-to-build/build_from_docker.html#docker)
|
| 1003 |
+
```
|
| 1004 |
+
|
| 1005 |
+
#### 11.1.3 转换 TensorRT 模型
|
| 1006 |
+
|
| 1007 |
+
首先需要在 Docker 容器里面安装 MMYOLO 和 `pycuda`:
|
| 1008 |
+
|
| 1009 |
+
```shell
|
| 1010 |
+
export MMYOLO_PATH=/root/workspace/mmyolo # 镜像中的路径,这里不需要修改
|
| 1011 |
+
cd ${MMYOLO_PATH}
|
| 1012 |
+
export MMYOLO_VERSION=$(python -c "import mmyolo.version as v; print(v.__version__)") # 查看训练使用的 MMYOLO 版本号
|
| 1013 |
+
echo "Using MMYOLO ${MMYOLO_VERSION}"
|
| 1014 |
+
mim install --no-cache-dir mmyolo==${MMYOLO_VERSION}
|
| 1015 |
+
pip install --no-cache-dir pycuda==2022.2
|
| 1016 |
+
```
|
| 1017 |
+
|
| 1018 |
+
进行模型转换
|
| 1019 |
+
|
| 1020 |
+
```shell
|
| 1021 |
+
cd /root/workspace/mmdeploy
|
| 1022 |
+
python ./tools/deploy.py \
|
| 1023 |
+
${MMYOLO_PATH}/configs/deploy/detection_tensorrt-fp16_dynamic-192x192-960x960.py \
|
| 1024 |
+
${MMYOLO_PATH}/configs/custom_dataset/yolov6_s_syncbn_fast_1xb32-100e_cat.py \
|
| 1025 |
+
${MMYOLO_PATH}/work_dirs/yolov6_s_syncbn_fast_1xb32-100e_cat/best_coco/bbox_mAP_epoch_96.pth \
|
| 1026 |
+
${MMYOLO_PATH}/data/cat/images/mmexport1633684751291.jpg \
|
| 1027 |
+
--test-img ${MMYOLO_PATH}/data/cat/images/mmexport1633684751291.jpg \
|
| 1028 |
+
--work-dir ./work_dir/yolov6_s_syncbn_fast_1xb32-100e_cat_deploy_dynamic_fp16 \
|
| 1029 |
+
--device cuda:0 \
|
| 1030 |
+
--log-level INFO \
|
| 1031 |
+
--show \
|
| 1032 |
+
--dump-info
|
| 1033 |
+
```
|
| 1034 |
+
|
| 1035 |
+
<div align=center>
|
| 1036 |
+
<img src="https://user-images.githubusercontent.com/25873202/206736259-72b76698-cba4-4472-909d-0fd866b45d55.png" alt="Image"/>
|
| 1037 |
+
</div>
|
| 1038 |
+
|
| 1039 |
+
等待一段时间,出现了 `All process success.` 即为成功:
|
| 1040 |
+
|
| 1041 |
+
<div align=center>
|
| 1042 |
+
<img src="https://user-images.githubusercontent.com/25873202/206736030-3b702929-4fcb-4cec-a6ce-f22a94777f6c.png" alt="Image"/>
|
| 1043 |
+
</div>
|
| 1044 |
+
|
| 1045 |
+
查看导出的路径,可以看到如下图所示的文件结构:
|
| 1046 |
+
|
| 1047 |
+
```shell
|
| 1048 |
+
$WORK_DIR
|
| 1049 |
+
├── deploy.json
|
| 1050 |
+
├── detail.json
|
| 1051 |
+
├── end2end.engine
|
| 1052 |
+
├── end2end.onnx
|
| 1053 |
+
└── pipeline.json
|
| 1054 |
+
```
|
| 1055 |
+
|
| 1056 |
+
```{SeeAlso}
|
| 1057 |
+
关于转换模型的详细介绍,请参考 [如何转换模型](https://mmdeploy.readthedocs.io/zh_CN/latest/02-how-to-run/convert_model.html)
|
| 1058 |
+
```
|
| 1059 |
+
|
| 1060 |
+
#### 11.1.4 部署模型执行推理
|
| 1061 |
+
|
| 1062 |
+
需要将 `${MMYOLO_PATH}/configs/custom_dataset/yolov6_s_syncbn_fast_1xb32-100e_cat.py` 里面的 `data_root` 修改为 Docker 容器里面的路径:
|
| 1063 |
+
|
| 1064 |
+
```python
|
| 1065 |
+
data_root = '/root/workspace/mmyolo/data/cat/' # Docker 容器里面数据集目录的绝对路径
|
| 1066 |
+
```
|
| 1067 |
+
|
| 1068 |
+
执行速度和精度测试:
|
| 1069 |
+
|
| 1070 |
+
```shell
|
| 1071 |
+
python tools/test.py \
|
| 1072 |
+
${MMYOLO_PATH}/configs/deploy/detection_tensorrt-fp16_dynamic-192x192-960x960.py \
|
| 1073 |
+
${MMYOLO_PATH}/configs/custom_dataset/yolov6_s_syncbn_fast_1xb32-100e_cat.py \
|
| 1074 |
+
--model ./work_dir/yolov6_s_syncbn_fast_1xb32-100e_cat_deploy_dynamic_fp16/end2end.engine \
|
| 1075 |
+
--speed-test \
|
| 1076 |
+
--device cuda
|
| 1077 |
+
```
|
| 1078 |
+
|
| 1079 |
+
速度测试如下,可见平均推理速度是 `24.10 ms`,对比 PyTorch 推理有速度提升,同时显存也下降了很多:
|
| 1080 |
+
|
| 1081 |
+
```shell
|
| 1082 |
+
Epoch(test) [ 10/116] eta: 0:00:20 time: 0.1919 data_time: 0.1330 memory: 12
|
| 1083 |
+
Epoch(test) [ 20/116] eta: 0:00:15 time: 0.1220 data_time: 0.0939 memory: 12
|
| 1084 |
+
Epoch(test) [ 30/116] eta: 0:00:12 time: 0.1168 data_time: 0.0850 memory: 12
|
| 1085 |
+
Epoch(test) [ 40/116] eta: 0:00:10 time: 0.1241 data_time: 0.0940 memory: 12
|
| 1086 |
+
Epoch(test) [ 50/116] eta: 0:00:08 time: 0.0974 data_time: 0.0696 memory: 12
|
| 1087 |
+
Epoch(test) [ 60/116] eta: 0:00:06 time: 0.0865 data_time: 0.0547 memory: 16
|
| 1088 |
+
Epoch(test) [ 70/116] eta: 0:00:05 time: 0.1521 data_time: 0.1226 memory: 16
|
| 1089 |
+
Epoch(test) [ 80/116] eta: 0:00:04 time: 0.1364 data_time: 0.1056 memory: 12
|
| 1090 |
+
Epoch(test) [ 90/116] eta: 0:00:03 time: 0.0923 data_time: 0.0627 memory: 12
|
| 1091 |
+
Epoch(test) [100/116] eta: 0:00:01 time: 0.0844 data_time: 0.0583 memory: 12
|
| 1092 |
+
[tensorrt]-110 times per count: 24.10 ms, 41.50 FPS
|
| 1093 |
+
Epoch(test) [110/116] eta: 0:00:00 time: 0.1085 data_time: 0.0832 memory: 12
|
| 1094 |
+
```
|
| 1095 |
+
|
| 1096 |
+
精度测试如下。此配置采用 FP16 格式推理,会有一定程度掉点,但是推理速度更快、显存占比更小:
|
| 1097 |
+
|
| 1098 |
+
```shell
|
| 1099 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.954
|
| 1100 |
+
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 1.000
|
| 1101 |
+
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.975
|
| 1102 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000
|
| 1103 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000
|
| 1104 |
+
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.954
|
| 1105 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.860
|
| 1106 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.965
|
| 1107 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.965
|
| 1108 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = -1.000
|
| 1109 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000
|
| 1110 |
+
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.965
|
| 1111 |
+
|
| 1112 |
+
INFO - bbox_mAP_copypaste: 0.954 1.000 0.975 -1.000 -1.000 0.954
|
| 1113 |
+
INFO - Epoch(test) [116/116] coco/bbox_mAP: 0.9540 coco/bbox_mAP_50: 1.0000 coco/bbox_mAP_75: 0.9750 coco/bbox_mAP_s: -1.0000 coco/bbox_mAP_m: -1.0000 coco/bbox_mAP_l: 0.9540
|
| 1114 |
+
```
|
| 1115 |
+
|
| 1116 |
+
部署模型图片推理演示:
|
| 1117 |
+
|
| 1118 |
+
```{Note}
|
| 1119 |
+
用户可以参考 MMDeploy 的 SDK 部署方式,使用 C++ 来进行部署,进而进一步提升推理速度。
|
| 1120 |
+
```
|
| 1121 |
+
|
| 1122 |
+
```shell
|
| 1123 |
+
cd ${MMYOLO_PATH}/demo
|
| 1124 |
+
python deploy_demo.py \
|
| 1125 |
+
${MMYOLO_PATH}/data/cat/images/mmexport1633684900217.jpg \
|
| 1126 |
+
${MMYOLO_PATH}/configs/custom_dataset/yolov6_s_syncbn_fast_1xb32-100e_cat.py \
|
| 1127 |
+
/root/workspace/mmdeploy/work_dir/yolov6_s_syncbn_fast_1xb32-100e_cat_deploy_dynamic_fp16/end2end.engine \
|
| 1128 |
+
--deploy-cfg ${MMYOLO_PATH}/configs/deploy/detection_tensorrt-fp16_dynamic-192x192-960x960.py \
|
| 1129 |
+
--out-dir ${MMYOLO_PATH}/work_dirs/deploy_predict_out \
|
| 1130 |
+
--device cuda:0 \
|
| 1131 |
+
--score-thr 0.5
|
| 1132 |
+
```
|
| 1133 |
+
|
| 1134 |
+
```{Warning}
|
| 1135 |
+
该脚本 `deploy_demo.py` 暂时没有做批量推理的处理,而且代码前处理还需要完善,暂时不能完全展现出推理的速度,只能演示推理的结果,后续会优化,敬请期待。
|
| 1136 |
+
```
|
| 1137 |
+
|
| 1138 |
+
执行之后,可以看到在 `--out-dir` 下面的推理图片结果:
|
| 1139 |
+
|
| 1140 |
+
<div align=center>
|
| 1141 |
+
<img src="https://user-images.githubusercontent.com/25873202/205815829-6f85e655-722a-47c8-9e23-2a74437c0923.jpg" alt="Image"/>
|
| 1142 |
+
</div>
|
| 1143 |
+
|
| 1144 |
+
```{Note}
|
| 1145 |
+
您也可以做其他优化调整,例如增大 batch,量化 int8 等等。
|
| 1146 |
+
```
|
| 1147 |
+
|
| 1148 |
+
#### 11.1.4 保存和加载 Docker 容器
|
| 1149 |
+
|
| 1150 |
+
因为如果每次都进行 docker 镜像的构建,特别费时间,此时您可以考虑使用 docker 自带的打包 api 进行打包和加载。
|
| 1151 |
+
|
| 1152 |
+
```shell
|
| 1153 |
+
# 保存,得到的 tar 包可以放到移动硬盘
|
| 1154 |
+
docker save mmyolo-deploy > mmyolo-deploy.tar
|
| 1155 |
+
|
| 1156 |
+
# 加载镜像到系统
|
| 1157 |
+
docker load < /path/to/mmyolo-deploy.tar
|
| 1158 |
+
```
|
| 1159 |
+
|
| 1160 |
+
### 11.2 使用 `projects/easydeploy` 进行部署
|
| 1161 |
+
|
| 1162 |
+
```{SeeAlso}
|
| 1163 |
+
详见[部署文档](https://github.com/open-mmlab/mmyolo/blob/dev/projects/easydeploy/README_zh-CN.md)
|
| 1164 |
+
```
|
| 1165 |
+
|
| 1166 |
+
TODO: 下个版本会完善这个部分...
|
| 1167 |
+
|
| 1168 |
+
## 附录
|
| 1169 |
+
|
| 1170 |
+
### 1. 本教程训练机器的详细环境的资料如下:
|
| 1171 |
+
|
| 1172 |
+
```shell
|
| 1173 |
+
sys.platform: linux
|
| 1174 |
+
Python: 3.9.13 | packaged by conda-forge | (main, May 27 2022, 16:58:50) [GCC 10.3.0]
|
| 1175 |
+
CUDA available: True
|
| 1176 |
+
numpy_random_seed: 2147483648
|
| 1177 |
+
GPU 0: NVIDIA GeForce RTX 3080 Ti
|
| 1178 |
+
CUDA_HOME: /usr/local/cuda
|
| 1179 |
+
NVCC: Cuda compilation tools, release 11.5, V11.5.119
|
| 1180 |
+
GCC: gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
|
| 1181 |
+
PyTorch: 1.10.0
|
| 1182 |
+
PyTorch compiling details: PyTorch built with:
|
| 1183 |
+
- GCC 7.3
|
| 1184 |
+
- C++ Version: 201402
|
| 1185 |
+
- Intel(R) oneAPI Math Kernel Library Version 2021.4-Product Build 20210904 for Intel(R) 64 architecture applications
|
| 1186 |
+
- Intel(R) MKL-DNN v2.2.3 (Git Hash 7336ca9f055cf1bfa13efb658fe15dc9b41f0740)
|
| 1187 |
+
- OpenMP 201511 (a.k.a. OpenMP 4.5)
|
| 1188 |
+
- LAPACK is enabled (usually provided by MKL)
|
| 1189 |
+
- NNPACK is enabled
|
| 1190 |
+
- CPU capability usage: AVX2
|
| 1191 |
+
- CUDA Runtime 11.3
|
| 1192 |
+
- NVCC architecture flags: -gencode;arch=compute_37,code=sm_37;-gencode;arch=compute_50,code=sm_50;-gencode;
|
| 1193 |
+
arch=compute_60,code=sm_60;-gencode;arch=compute_61,code=sm_61;-gencode;arch=compute_70,code=sm_70;
|
| 1194 |
+
-gencode;arch=compute_75,code=sm_75;-gencode;arch=compute_80,code=sm_80;-gencode;
|
| 1195 |
+
arch=compute_86,code=sm_86;-gencode;arch=compute_37,code=compute_37
|
| 1196 |
+
- CuDNN 8.2
|
| 1197 |
+
- Magma 2.5.2
|
| 1198 |
+
- Build settings: BLAS_INFO=mkl, BUILD_TYPE=Release, CUDA_VERSION=11.3, CUDNN_VERSION=8.2.0,
|
| 1199 |
+
CXX_COMPILER=/opt/rh/devtoolset-7/root/usr/bin/c++, CXX_FLAGS= -Wno-deprecated -fvisibility-inlines-hidden
|
| 1200 |
+
-DUSE_PTHREADPOOL -fopenmp -DNDEBUG -DUSE_KINETO -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -DUSE_XNNPACK
|
| 1201 |
+
-DSYMBOLICATE_MOBILE_DEBUG_HANDLE -DEDGE_PROFILER_USE_KINETO -O2 -fPIC -Wno-narrowing -Wall -Wextra
|
| 1202 |
+
-Werror=return-type -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas
|
| 1203 |
+
-Wno-sign-compare -Wno-error=deprecated-declarations -Wno-stringop-overflow -Wno-psabi -Wno-error=pedantic
|
| 1204 |
+
-Wno-error=redundant-decls -Wno-error=old-style-cast -fdiagnostics-color=always -faligned-new
|
| 1205 |
+
-Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -Werror=format
|
| 1206 |
+
-Wno-stringop-overflow, LAPACK_INFO=mkl, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1,
|
| 1207 |
+
TORCH_VERSION=1.10.0, USE_CUDA=ON, USE_CUDNN=ON, USE_EXCEPTION_PTR=1, USE_GFLAGS=OFF, USE_GLOG=OFF, USE_MKL=ON,
|
| 1208 |
+
USE_MKLDNN=ON, USE_MPI=OFF, USE_NCCL=ON, USE_NNPACK=ON, USE_OPENMP=ON,
|
| 1209 |
+
|
| 1210 |
+
TorchVision: 0.11.0
|
| 1211 |
+
OpenCV: 4.6.0
|
| 1212 |
+
MMEngine: 0.3.1
|
| 1213 |
+
MMCV: 2.0.0rc3
|
| 1214 |
+
MMDetection: 3.0.0rc3
|
| 1215 |
+
MMYOLO: 0.2.0+cf279a5
|
| 1216 |
+
```
|
| 1217 |
+
|
| 1218 |
+
### 2. 如何测试数据集在预训练权重的精度:
|
| 1219 |
+
|
| 1220 |
+
```{Warning}
|
| 1221 |
+
前提:该类在 COCO 80 类中!
|
| 1222 |
+
```
|
| 1223 |
+
|
| 1224 |
+
本小节以 `cat` 数据集为例进行讲解,使用的是:
|
| 1225 |
+
|
| 1226 |
+
- config 文件:`configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py`
|
| 1227 |
+
- 权重 `yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth`
|
| 1228 |
+
|
| 1229 |
+
1. 修改 config 文件中的路径
|
| 1230 |
+
|
| 1231 |
+
因为 `configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py` 是继承于 `configs/yolov5/yolov5_s-v61_syncbn_8xb16-300e_coco.py`,故主要修改 `configs/yolov5/yolov5_s-v61_syncbn_8xb16-300e_coco.py` 文件即可。
|
| 1232 |
+
|
| 1233 |
+
| 修改前 | 修改后 |
|
| 1234 |
+
| --------------------------------------------------------------------------------- | ------------------------------------------------------------------------------ |
|
| 1235 |
+
| `data_root = 'data/coco/'` | `data_root = './data/cat/'` |
|
| 1236 |
+
| `ann_file='annotations/instances_train2017.json'` | `ann_file='annotations/trainval.json'` |
|
| 1237 |
+
| data_prefix=dict(img='train2017/')\` | `data_prefix=dict(img='images/')` |
|
| 1238 |
+
| `val_evaluator` 中的 `ann_file=data_root + 'annotations/instances_val2017.json'` | `val_evaluator` 中的 `dict(ann_file=data_root + 'annotations/trainval.json')` |
|
| 1239 |
+
|
| 1240 |
+
2. 修改标签
|
| 1241 |
+
|
| 1242 |
+
```{note}
|
| 1243 |
+
建议直接复制一份标签,防止弄坏好的标签
|
| 1244 |
+
```
|
| 1245 |
+
|
| 1246 |
+
将 `trainval.json` 里面的 "categories" 字段改为 COCO 原本的:
|
| 1247 |
+
|
| 1248 |
+
```json
|
| 1249 |
+
"categories": [{"supercategory": "person","id": 1,"name": "person"},{"supercategory": "vehicle","id": 2,"name": "bicycle"},{"supercategory": "vehicle","id": 3,"name": "car"},{"supercategory": "vehicle","id": 4,"name": "motorcycle"},{"supercategory": "vehicle","id": 5,"name": "airplane"},{"supercategory": "vehicle","id": 6,"name": "bus"},{"supercategory": "vehicle","id": 7,"name": "train"},{"supercategory": "vehicle","id": 8,"name": "truck"},{"supercategory": "vehicle","id": 9,"name": "boat"},{"supercategory": "outdoor","id": 10,"name": "traffic light"},{"supercategory": "outdoor","id": 11,"name": "fire hydrant"},{"supercategory": "outdoor","id": 13,"name": "stop sign"},{"supercategory": "outdoor","id": 14,"name": "parking meter"},{"supercategory": "outdoor","id": 15,"name": "bench"},{"supercategory": "animal","id": 16,"name": "bird"},{"supercategory": "animal","id": 17,"name": "cat"},{"supercategory": "animal","id": 18,"name": "dog"},{"supercategory": "animal","id": 19,"name": "horse"},{"supercategory": "animal","id": 20,"name": "sheep"},{"supercategory": "animal","id": 21,"name": "cow"},{"supercategory": "animal","id": 22,"name": "elephant"},{"supercategory": "animal","id": 23,"name": "bear"},{"supercategory": "animal","id": 24,"name": "zebra"},{"supercategory": "animal","id": 25,"name": "giraffe"},{"supercategory": "accessory","id": 27,"name": "backpack"},{"supercategory": "accessory","id": 28,"name": "umbrella"},{"supercategory": "accessory","id": 31,"name": "handbag"},{"supercategory": "accessory","id": 32,"name": "tie"},{"supercategory": "accessory","id": 33,"name": "suitcase"},{"supercategory": "sports","id": 34,"name": "frisbee"},{"supercategory": "sports","id": 35,"name": "skis"},{"supercategory": "sports","id": 36,"name": "snowboard"},{"supercategory": "sports","id": 37,"name": "sports ball"},{"supercategory": "sports","id": 38,"name": "kite"},{"supercategory": "sports","id": 39,"name": "baseball bat"},{"supercategory": "sports","id": 40,"name": "baseball glove"},{"supercategory": "sports","id": 41,"name": "skateboard"},{"supercategory": "sports","id": 42,"name": "surfboard"},{"supercategory": "sports","id": 43,"name": "tennis racket"},{"supercategory": "kitchen","id": 44,"name": "bottle"},{"supercategory": "kitchen","id": 46,"name": "wine glass"},{"supercategory": "kitchen","id": 47,"name": "cup"},{"supercategory": "kitchen","id": 48,"name": "fork"},{"supercategory": "kitchen","id": 49,"name": "knife"},{"supercategory": "kitchen","id": 50,"name": "spoon"},{"supercategory": "kitchen","id": 51,"name": "bowl"},{"supercategory": "food","id": 52,"name": "banana"},{"supercategory": "food","id": 53,"name": "apple"},{"supercategory": "food","id": 54,"name": "sandwich"},{"supercategory": "food","id": 55,"name": "orange"},{"supercategory": "food","id": 56,"name": "broccoli"},{"supercategory": "food","id": 57,"name": "carrot"},{"supercategory": "food","id": 58,"name": "hot dog"},{"supercategory": "food","id": 59,"name": "pizza"},{"supercategory": "food","id": 60,"name": "donut"},{"supercategory": "food","id": 61,"name": "cake"},{"supercategory": "furniture","id": 62,"name": "chair"},{"supercategory": "furniture","id": 63,"name": "couch"},{"supercategory": "furniture","id": 64,"name": "potted plant"},{"supercategory": "furniture","id": 65,"name": "bed"},{"supercategory": "furniture","id": 67,"name": "dining table"},{"supercategory": "furniture","id": 70,"name": "toilet"},{"supercategory": "electronic","id": 72,"name": "tv"},{"supercategory": "electronic","id": 73,"name": "laptop"},{"supercategory": "electronic","id": 74,"name": "mouse"},{"supercategory": "electronic","id": 75,"name": "remote"},{"supercategory": "electronic","id": 76,"name": "keyboard"},{"supercategory": "electronic","id": 77,"name": "cell phone"},{"supercategory": "appliance","id": 78,"name": "microwave"},{"supercategory": "appliance","id": 79,"name": "oven"},{"supercategory": "appliance","id": 80,"name": "toaster"},{"supercategory": "appliance","id": 81,"name": "sink"},{"supercategory": "appliance","id": 82,"name": "refrigerator"},{"supercategory": "indoor","id": 84,"name": "book"},{"supercategory": "indoor","id": 85,"name": "clock"},{"supercategory": "indoor","id": 86,"name": "vase"},{"supercategory": "indoor","id": 87,"name": "scissors"},{"supercategory": "indoor","id": 88,"name": "teddy bear"},{"supercategory": "indoor","id": 89,"name": "hair drier"},{"supercategory": "indoor","id": 90,"name": "toothbrush"}],
|
| 1250 |
+
```
|
| 1251 |
+
|
| 1252 |
+
同时,将 `"annotations"` 字段里面的 `"category_id"` 改为 COCO 对应的 `id` ,例如本例子的 `cat` 是 `17`,下面展示部分修改结果:
|
| 1253 |
+
|
| 1254 |
+
```json
|
| 1255 |
+
"annotations": [
|
| 1256 |
+
{
|
| 1257 |
+
"iscrowd": 0,
|
| 1258 |
+
"category_id": 17, # 这个 "category_id" 改为 COCO 对应的 id,例如本例子的 cat 是 17
|
| 1259 |
+
"id": 32,
|
| 1260 |
+
"image_id": 32,
|
| 1261 |
+
"bbox": [
|
| 1262 |
+
822.49072265625,
|
| 1263 |
+
958.3897094726562,
|
| 1264 |
+
1513.693115234375,
|
| 1265 |
+
988.3231811523438
|
| 1266 |
+
],
|
| 1267 |
+
"area": 1496017.9949368387,
|
| 1268 |
+
"segmentation": [
|
| 1269 |
+
[
|
| 1270 |
+
822.49072265625,
|
| 1271 |
+
958.3897094726562,
|
| 1272 |
+
822.49072265625,
|
| 1273 |
+
1946.712890625,
|
| 1274 |
+
2336.183837890625,
|
| 1275 |
+
1946.712890625,
|
| 1276 |
+
2336.183837890625,
|
| 1277 |
+
958.3897094726562
|
| 1278 |
+
]
|
| 1279 |
+
]
|
| 1280 |
+
}
|
| 1281 |
+
]
|
| 1282 |
+
```
|
| 1283 |
+
|
| 1284 |
+
3. 执行命令
|
| 1285 |
+
|
| 1286 |
+
```shell
|
| 1287 |
+
python tools\test.py configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py \
|
| 1288 |
+
work_dirs/yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth \
|
| 1289 |
+
--cfg-options test_evaluator.classwise=True
|
| 1290 |
+
```
|
| 1291 |
+
|
| 1292 |
+
执行之后就可以看到测试后的指标了:
|
| 1293 |
+
|
| 1294 |
+
```shell
|
| 1295 |
+
+---------------+-------+--------------+-----+----------------+------+
|
| 1296 |
+
| category | AP | category | AP | category | AP |
|
| 1297 |
+
+---------------+-------+--------------+-----+----------------+------+
|
| 1298 |
+
| person | nan | bicycle | nan | car | nan |
|
| 1299 |
+
| motorcycle | nan | airplane | nan | bus | nan |
|
| 1300 |
+
| train | nan | truck | nan | boat | nan |
|
| 1301 |
+
| traffic light | nan | fire hydrant | nan | stop sign | nan |
|
| 1302 |
+
| parking meter | nan | bench | nan | bird | nan |
|
| 1303 |
+
| cat | 0.866 | dog | nan | horse | nan |
|
| 1304 |
+
| sheep | nan | cow | nan | elephant | nan |
|
| 1305 |
+
| bear | nan | zebra | nan | giraffe | nan |
|
| 1306 |
+
| backpack | nan | umbrella | nan | handbag | nan |
|
| 1307 |
+
| tie | nan | suitcase | nan | frisbee | nan |
|
| 1308 |
+
| skis | nan | snowboard | nan | sports ball | nan |
|
| 1309 |
+
| kite | nan | baseball bat | nan | baseball glove | nan |
|
| 1310 |
+
| skateboard | nan | surfboard | nan | tennis racket | nan |
|
| 1311 |
+
| bottle | nan | wine glass | nan | cup | nan |
|
| 1312 |
+
| fork | nan | knife | nan | spoon | nan |
|
| 1313 |
+
| bowl | nan | banana | nan | apple | nan |
|
| 1314 |
+
| sandwich | nan | orange | nan | broccoli | nan |
|
| 1315 |
+
| carrot | nan | hot dog | nan | pizza | nan |
|
| 1316 |
+
| donut | nan | cake | nan | chair | nan |
|
| 1317 |
+
| couch | nan | potted plant | nan | bed | nan |
|
| 1318 |
+
| dining table | nan | toilet | nan | tv | nan |
|
| 1319 |
+
| laptop | nan | mouse | nan | remote | nan |
|
| 1320 |
+
| keyboard | nan | cell phone | nan | microwave | nan |
|
| 1321 |
+
| oven | nan | toaster | nan | sink | nan |
|
| 1322 |
+
| refrigerator | nan | book | nan | clock | nan |
|
| 1323 |
+
| vase | nan | scissors | nan | teddy bear | nan |
|
| 1324 |
+
| hair drier | nan | toothbrush | nan | None | None |
|
| 1325 |
+
+---------------+-------+--------------+-----+----------------+------+
|
| 1326 |
+
```
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/mm_basics.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# MM 系列仓库必备基础
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/model_design.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# MMYOLO 模型设计相关说明
|
| 2 |
+
|
| 3 |
+
## YOLO 系列模型基类
|
| 4 |
+
|
| 5 |
+
下图为 RangeKing@GitHub 提供,非常感谢!
|
| 6 |
+
|
| 7 |
+
<div align=center>
|
| 8 |
+
<img src="https://user-images.githubusercontent.com/27466624/199999337-0544a4cb-3cbd-4f3e-be26-bcd9e74db7ff.jpg" alt="基类 P5">
|
| 9 |
+
图 1:P5 模型结构
|
| 10 |
+
</div>
|
| 11 |
+
|
| 12 |
+
<div align=center>
|
| 13 |
+
<img src="https://user-images.githubusercontent.com/27466624/200850066-0c434173-2d40-4c12-8de3-eda473ff172f.jpg" alt="基类 P6">
|
| 14 |
+
图 2:P6 模型结构
|
| 15 |
+
</div>
|
| 16 |
+
|
| 17 |
+
YOLO 系列算法大部分采用了统一的算法搭建结构,典型的如 Darknet + PAFPN。为了让用户快速理解 YOLO 系列算法架构,我们特意设计了如上图中的 BaseBackbone + BaseYOLONeck 结构。
|
| 18 |
+
|
| 19 |
+
抽象 BaseBackbone 的好处包括:
|
| 20 |
+
|
| 21 |
+
1. 子类不需要关心 forward 过程,只要类似建造者模式一样构建模型即可。
|
| 22 |
+
2. 可以通过配置实现定制插件功能,用户可以很方便的插入一些类似注意力模块。
|
| 23 |
+
3. 所有子类自动支持 frozen 某些 stage 和 frozen bn 功能。
|
| 24 |
+
|
| 25 |
+
抽象 BaseYOLONeck 也有同样好处。
|
| 26 |
+
|
| 27 |
+
### BaseBackbone
|
| 28 |
+
|
| 29 |
+
- 如图 1 所示,对于 P5 而言,BaseBackbone 为包含 1 个 stem 层 + 4 个 stage 层的类似 ResNet 的基础结构。
|
| 30 |
+
- 如图 2 所示,对于 P6 而言,BaseBackbone 为包含 1 个 stem 层 + 5 个 stage 层的结构。
|
| 31 |
+
|
| 32 |
+
不同算法的主干网络继承 BaseBackbone,用户可以通过实现内部的 `build_xx` 方法,使用自定义的基础模块来构建每一层的内部结构。
|
| 33 |
+
|
| 34 |
+
### BaseYOLONeck
|
| 35 |
+
|
| 36 |
+
与 `BaseBackbone` 的设计类似,我们为 MMYOLO 系列的 Neck 层进行了重构,主要分为 `Reduce 层`, `UpSample 层`,`TopDown 层`,`DownSample 层`,`BottomUP 层`以及`输出卷积层`,每一层结构都可以通过继承重写 `build_xx` 方法来实现自定义的内部结构。
|
| 37 |
+
|
| 38 |
+
### BaseDenseHead
|
| 39 |
+
|
| 40 |
+
MMYOLO 系列沿用 MMDetection 中设计的 `BaseDenseHead` 作为其 Head 结构的基类,但是进一步拆分了 HeadModule. 以 YOLOv5 为例,其 [HeadModule](https://github.com/open-mmlab/mmyolo/blob/main/mmyolo/models/dense_heads/yolov5_head.py#L24) 中的 forward 实现代替了原有的 forward 实现。
|
| 41 |
+
|
| 42 |
+
## HeadModule 说明
|
| 43 |
+
|
| 44 |
+
<div align=center>
|
| 45 |
+
<img src="https://user-images.githubusercontent.com/33799979/190985845-ed303ad4-3a77-447b-83f9-1feba38d5e24.png" width=800 alt="HeadModule">
|
| 46 |
+
</div>
|
| 47 |
+
|
| 48 |
+
如上图所示,虚线部分为 [MMDetection](https://github.com/open-mmlab/mmdetection/blob/3.x/mmdet/models/dense_heads/base_dense_head.py) 中的实现,实线部分为 [MMYOLO](https://github.com/open-mmlab/mmyolo/blob/main/mmyolo/models/dense_heads/yolov5_head.py) 中的实现。MMYOLO版本与原实现相比具备具有以下优势:
|
| 49 |
+
|
| 50 |
+
1. MMDetection 中将 `bbox_head` 拆分为 `assigner` + `box coder` + `sampler` 三个大的组件,但由于 3 个组件之间的传递为了通用性,需要封装额外的对象来处理,统一之后用户可以不用进行拆分。不刻意强求划分三大组件的好处为:不再需要对内部数据进行数据封装,简化了代码逻辑,减轻了社区使用难度和算法复现难度。
|
| 51 |
+
2. 速度更快,用户在自定义实现算法时候,可以不依赖于原有框架,对部分代码进行深度优化。
|
| 52 |
+
|
| 53 |
+
总的来说,在 MMYOLO 中只需要做到将 `model` + `loss_by_feat` 部分解耦,用户就可以通过修改配置实现任意模型配合任意的 `loss_by_feat` 计算过程。例如将 YOLOv5 模型应用 YOLOX 的 `loss_by_feat` 等。
|
| 54 |
+
|
| 55 |
+
以 MMDetection 中 YOLOX 配置为例,其 Head 模块配置写法为:
|
| 56 |
+
|
| 57 |
+
```python
|
| 58 |
+
bbox_head=dict(
|
| 59 |
+
type='YOLOXHead',
|
| 60 |
+
num_classes=80,
|
| 61 |
+
in_channels=128,
|
| 62 |
+
feat_channels=128,
|
| 63 |
+
stacked_convs=2,
|
| 64 |
+
strides=(8, 16, 32),
|
| 65 |
+
use_depthwise=False,
|
| 66 |
+
norm_cfg=dict(type='BN', momentum=0.03, eps=0.001),
|
| 67 |
+
act_cfg=dict(type='Swish'),
|
| 68 |
+
...
|
| 69 |
+
loss_obj=dict(
|
| 70 |
+
type='CrossEntropyLoss',
|
| 71 |
+
use_sigmoid=True,
|
| 72 |
+
reduction='sum',
|
| 73 |
+
loss_weight=1.0),
|
| 74 |
+
loss_l1=dict(type='L1Loss', reduction='sum', loss_weight=1.0)),
|
| 75 |
+
train_cfg=dict(assigner=dict(type='SimOTAAssigner', center_radius=2.5)),
|
| 76 |
+
```
|
| 77 |
+
|
| 78 |
+
在 MMYOLO 中抽取 `head_module` 后,新的配置写法为:
|
| 79 |
+
|
| 80 |
+
```python
|
| 81 |
+
bbox_head=dict(
|
| 82 |
+
type='YOLOXHead',
|
| 83 |
+
head_module=dict(
|
| 84 |
+
type='YOLOXHeadModule',
|
| 85 |
+
num_classes=80,
|
| 86 |
+
in_channels=256,
|
| 87 |
+
feat_channels=256,
|
| 88 |
+
widen_factor=widen_factor,
|
| 89 |
+
stacked_convs=2,
|
| 90 |
+
featmap_strides=(8, 16, 32),
|
| 91 |
+
use_depthwise=False,
|
| 92 |
+
norm_cfg=dict(type='BN', momentum=0.03, eps=0.001),
|
| 93 |
+
act_cfg=dict(type='SiLU', inplace=True),
|
| 94 |
+
),
|
| 95 |
+
...
|
| 96 |
+
loss_obj=dict(
|
| 97 |
+
type='mmdet.CrossEntropyLoss',
|
| 98 |
+
use_sigmoid=True,
|
| 99 |
+
reduction='sum',
|
| 100 |
+
loss_weight=1.0),
|
| 101 |
+
loss_bbox_aux=dict(type='mmdet.L1Loss', reduction='sum', loss_weight=1.0)),
|
| 102 |
+
train_cfg=dict(
|
| 103 |
+
assigner=dict(
|
| 104 |
+
type='mmdet.SimOTAAssigner',
|
| 105 |
+
center_radius=2.5,
|
| 106 |
+
iou_calculator=dict(type='mmdet.BboxOverlaps2D'))),
|
| 107 |
+
```
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/replace_backbone.md
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 轻松更换主干网络
|
| 2 |
+
|
| 3 |
+
```{note}
|
| 4 |
+
1. 使用其他主干网络时,你需要保证主干网络的输出通道与 Neck 的输入通道相匹配。
|
| 5 |
+
2. 下面给出的配置文件,仅能确保训练可以正确运行,直接训练性能可能不是最优的。因为某些 backbone 需要配套特定的学习率、优化器等超参数。后续会在“训练技巧章节”补充训练调优相关内容。
|
| 6 |
+
```
|
| 7 |
+
|
| 8 |
+
## 使用 MMYOLO 中注册的主干网络
|
| 9 |
+
|
| 10 |
+
假设想将 `YOLOv6EfficientRep` 作为 `YOLOv5` 的主干网络,则配置文件如下:
|
| 11 |
+
|
| 12 |
+
```python
|
| 13 |
+
_base_ = './yolov5_s-v61_syncbn_8xb16-300e_coco.py'
|
| 14 |
+
|
| 15 |
+
model = dict(
|
| 16 |
+
backbone=dict(
|
| 17 |
+
type='YOLOv6EfficientRep',
|
| 18 |
+
norm_cfg=dict(type='BN', momentum=0.03, eps=0.001),
|
| 19 |
+
act_cfg=dict(type='ReLU', inplace=True))
|
| 20 |
+
)
|
| 21 |
+
```
|
| 22 |
+
|
| 23 |
+
## 跨库使用主干网络
|
| 24 |
+
|
| 25 |
+
OpenMMLab 2.0 体系中 MMYOLO、MMDetection、MMClassification、MMSelfsup 中的模型注册表都继承自 MMEngine 中的根注册表,允许这些 OpenMMLab 开源库直接使用彼此已经实现的模块。 因此用户可以在 MMYOLO 中使用来自 MMDetection、MMClassification、MMSelfsup 的主干网络,而无需重新实现。
|
| 26 |
+
|
| 27 |
+
### 使用在 MMDetection 中实现的主干网络
|
| 28 |
+
|
| 29 |
+
1. 假设想将 `ResNet-50` 作为 `YOLOv5` 的主干网络,则配置文件如下:
|
| 30 |
+
|
| 31 |
+
```python
|
| 32 |
+
_base_ = './yolov5_s-v61_syncbn_8xb16-300e_coco.py'
|
| 33 |
+
|
| 34 |
+
deepen_factor = _base_.deepen_factor
|
| 35 |
+
widen_factor = 1.0
|
| 36 |
+
channels = [512, 1024, 2048]
|
| 37 |
+
|
| 38 |
+
model = dict(
|
| 39 |
+
backbone=dict(
|
| 40 |
+
_delete_=True, # 将 _base_ 中关于 backbone 的字段删除
|
| 41 |
+
type='mmdet.ResNet', # 使用 mmdet 中的 ResNet
|
| 42 |
+
depth=50,
|
| 43 |
+
num_stages=4,
|
| 44 |
+
out_indices=(1, 2, 3),
|
| 45 |
+
frozen_stages=1,
|
| 46 |
+
norm_cfg=dict(type='BN', requires_grad=True),
|
| 47 |
+
norm_eval=True,
|
| 48 |
+
style='pytorch',
|
| 49 |
+
init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50')),
|
| 50 |
+
neck=dict(
|
| 51 |
+
type='YOLOv5PAFPN',
|
| 52 |
+
widen_factor=widen_factor,
|
| 53 |
+
in_channels=channels, # 注意:ResNet-50 输出的3个通道是 [512, 1024, 2048],和原先的 yolov5-s neck 不匹配,需要更改
|
| 54 |
+
out_channels=channels),
|
| 55 |
+
bbox_head=dict(
|
| 56 |
+
type='YOLOv5Head',
|
| 57 |
+
head_module=dict(
|
| 58 |
+
type='YOLOv5HeadModule',
|
| 59 |
+
in_channels=channels, # head 部分输入通道也要做相应更改
|
| 60 |
+
widen_factor=widen_factor))
|
| 61 |
+
)
|
| 62 |
+
```
|
| 63 |
+
|
| 64 |
+
2. 假设想将 `SwinTransformer-Tiny` 作为 `YOLOv5` 的主干网络,则配置文件如下:
|
| 65 |
+
|
| 66 |
+
```python
|
| 67 |
+
_base_ = './yolov5_s-v61_syncbn_8xb16-300e_coco.py'
|
| 68 |
+
|
| 69 |
+
deepen_factor = _base_.deepen_factor
|
| 70 |
+
widen_factor = 1.0
|
| 71 |
+
channels = [192, 384, 768]
|
| 72 |
+
checkpoint_file = 'https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_tiny_patch4_window7_224.pth' # noqa
|
| 73 |
+
|
| 74 |
+
model = dict(
|
| 75 |
+
backbone=dict(
|
| 76 |
+
_delete_=True, # 将 _base_ 中关于 backbone 的字段删除
|
| 77 |
+
type='mmdet.SwinTransformer', # 使用 mmdet 中的 SwinTransformer
|
| 78 |
+
embed_dims=96,
|
| 79 |
+
depths=[2, 2, 6, 2],
|
| 80 |
+
num_heads=[3, 6, 12, 24],
|
| 81 |
+
window_size=7,
|
| 82 |
+
mlp_ratio=4,
|
| 83 |
+
qkv_bias=True,
|
| 84 |
+
qk_scale=None,
|
| 85 |
+
drop_rate=0.,
|
| 86 |
+
attn_drop_rate=0.,
|
| 87 |
+
drop_path_rate=0.2,
|
| 88 |
+
patch_norm=True,
|
| 89 |
+
out_indices=(1, 2, 3),
|
| 90 |
+
with_cp=False,
|
| 91 |
+
convert_weights=True,
|
| 92 |
+
init_cfg=dict(type='Pretrained', checkpoint=checkpoint_file)),
|
| 93 |
+
neck=dict(
|
| 94 |
+
type='YOLOv5PAFPN',
|
| 95 |
+
deepen_factor=deepen_factor,
|
| 96 |
+
widen_factor=widen_factor,
|
| 97 |
+
in_channels=channels, # 注意:SwinTransformer-Tiny 输出的3个通道是 [192, 384, 768],和原先的 yolov5-s neck 不匹配,需要更改
|
| 98 |
+
out_channels=channels),
|
| 99 |
+
bbox_head=dict(
|
| 100 |
+
type='YOLOv5Head',
|
| 101 |
+
head_module=dict(
|
| 102 |
+
type='YOLOv5HeadModule',
|
| 103 |
+
in_channels=channels, # head 部分输入通道也要做相应更改
|
| 104 |
+
widen_factor=widen_factor))
|
| 105 |
+
)
|
| 106 |
+
```
|
| 107 |
+
|
| 108 |
+
### 使用在 MMClassification 中实现的主干网络
|
| 109 |
+
|
| 110 |
+
1. 假设想将 `ConvNeXt-Tiny` 作为 `YOLOv5` 的主干网络,则配置文件如下:
|
| 111 |
+
|
| 112 |
+
```python
|
| 113 |
+
_base_ = './yolov5_s-v61_syncbn_8xb16-300e_coco.py'
|
| 114 |
+
|
| 115 |
+
# 请先使用命令: mim install "mmcls>=1.0.0rc2",安装 mmcls
|
| 116 |
+
# 导入 mmcls.models 使得可以调用 mmcls 中注册的模块
|
| 117 |
+
custom_imports = dict(imports=['mmcls.models'], allow_failed_imports=False)
|
| 118 |
+
checkpoint_file = 'https://download.openmmlab.com/mmclassification/v0/convnext/downstream/convnext-tiny_3rdparty_32xb128-noema_in1k_20220301-795e9634.pth' # noqa
|
| 119 |
+
deepen_factor = _base_.deepen_factor
|
| 120 |
+
widen_factor = 1.0
|
| 121 |
+
channels = [192, 384, 768]
|
| 122 |
+
|
| 123 |
+
model = dict(
|
| 124 |
+
backbone=dict(
|
| 125 |
+
_delete_=True, # 将 _base_ 中关于 backbone 的字段删除
|
| 126 |
+
type='mmcls.ConvNeXt', # 使用 mmcls 中的 ConvNeXt
|
| 127 |
+
arch='tiny',
|
| 128 |
+
out_indices=(1, 2, 3),
|
| 129 |
+
drop_path_rate=0.4,
|
| 130 |
+
layer_scale_init_value=1.0,
|
| 131 |
+
gap_before_final_norm=False,
|
| 132 |
+
init_cfg=dict(
|
| 133 |
+
type='Pretrained', checkpoint=checkpoint_file,
|
| 134 |
+
prefix='backbone.')), # MMCls 中主干网络的预训练权重含义 prefix='backbone.',为了正常加载权重,需要把这个 prefix 去掉。
|
| 135 |
+
neck=dict(
|
| 136 |
+
type='YOLOv5PAFPN',
|
| 137 |
+
deepen_factor=deepen_factor,
|
| 138 |
+
widen_factor=widen_factor,
|
| 139 |
+
in_channels=channels, # 注意:ConvNeXt-Tiny 输出的3个通道是 [192, 384, 768],和原先的 yolov5-s neck 不匹配,需要更改
|
| 140 |
+
out_channels=channels),
|
| 141 |
+
bbox_head=dict(
|
| 142 |
+
type='YOLOv5Head',
|
| 143 |
+
head_module=dict(
|
| 144 |
+
type='YOLOv5HeadModule',
|
| 145 |
+
in_channels=channels, # head 部分输入通道也要做相应更改
|
| 146 |
+
widen_factor=widen_factor))
|
| 147 |
+
)
|
| 148 |
+
```
|
| 149 |
+
|
| 150 |
+
2. 假设想将 `MobileNetV3-small` 作为 `YOLOv5` 的主干网络,则配置文件如下:
|
| 151 |
+
|
| 152 |
+
```python
|
| 153 |
+
_base_ = './yolov5_s-v61_syncbn_8xb16-300e_coco.py'
|
| 154 |
+
|
| 155 |
+
# 请先使用命令: mim install "mmcls>=1.0.0rc2",安装 mmcls
|
| 156 |
+
# 导入 mmcls.models 使得可以调用 mmcls 中注册的模块
|
| 157 |
+
custom_imports = dict(imports=['mmcls.models'], allow_failed_imports=False)
|
| 158 |
+
checkpoint_file = 'https://download.openmmlab.com/mmclassification/v0/mobilenet_v3/convert/mobilenet_v3_small-8427ecf0.pth' # noqa
|
| 159 |
+
deepen_factor = _base_.deepen_factor
|
| 160 |
+
widen_factor = 1.0
|
| 161 |
+
channels = [24, 48, 96]
|
| 162 |
+
|
| 163 |
+
model = dict(
|
| 164 |
+
backbone=dict(
|
| 165 |
+
_delete_=True, # 将 _base_ 中关于 backbone 的字段删除
|
| 166 |
+
type='mmcls.MobileNetV3', # 使用 mmcls 中的 MobileNetV3
|
| 167 |
+
arch='small',
|
| 168 |
+
out_indices=(3, 8, 11), # 修改 out_indices
|
| 169 |
+
init_cfg=dict(
|
| 170 |
+
type='Pretrained',
|
| 171 |
+
checkpoint=checkpoint_file,
|
| 172 |
+
prefix='backbone.')), # MMCls 中主干网络的预训练权重含义 prefix='backbone.',为了正常加载权重,需要把这个 prefix 去掉。
|
| 173 |
+
neck=dict(
|
| 174 |
+
type='YOLOv5PAFPN',
|
| 175 |
+
deepen_factor=deepen_factor,
|
| 176 |
+
widen_factor=widen_factor,
|
| 177 |
+
in_channels=channels, # 注意:MobileNetV3-small 输出的3个通道是 [24, 48, 96],和原先的 yolov5-s neck 不匹配,需要更改
|
| 178 |
+
out_channels=channels),
|
| 179 |
+
bbox_head=dict(
|
| 180 |
+
type='YOLOv5Head',
|
| 181 |
+
head_module=dict(
|
| 182 |
+
type='YOLOv5HeadModule',
|
| 183 |
+
in_channels=channels, # head 部分输入通道也要做相应更改
|
| 184 |
+
widen_factor=widen_factor))
|
| 185 |
+
)
|
| 186 |
+
```
|
| 187 |
+
|
| 188 |
+
### 通过 MMClassification 使用 `timm` 中实现的主干网络
|
| 189 |
+
|
| 190 |
+
由于 MMClassification 提供了 Py**T**orch **Im**age **M**odels (`timm`) 主干网络的封装,用户也可以通过 MMClassification 直接使用 `timm` 中的主干网络。假设想将 `EfficientNet-B1`作为 `YOLOv5` 的主干网络,则配置文件如下:
|
| 191 |
+
|
| 192 |
+
```python
|
| 193 |
+
_base_ = './yolov5_s-v61_syncbn_8xb16-300e_coco.py'
|
| 194 |
+
|
| 195 |
+
# 请先使用命令: mim install "mmcls>=1.0.0rc2",安装 mmcls
|
| 196 |
+
# 以及: pip install timm,安装 timm
|
| 197 |
+
# 导入 mmcls.models 使得可以调用 mmcls 中注册的模块
|
| 198 |
+
custom_imports = dict(imports=['mmcls.models'], allow_failed_imports=False)
|
| 199 |
+
|
| 200 |
+
deepen_factor = _base_.deepen_factor
|
| 201 |
+
widen_factor = 1.0
|
| 202 |
+
channels = [40, 112, 320]
|
| 203 |
+
|
| 204 |
+
model = dict(
|
| 205 |
+
backbone=dict(
|
| 206 |
+
_delete_=True, # 将 _base_ 中关于 backbone 的字段删除
|
| 207 |
+
type='mmcls.TIMMBackbone', # 使用 mmcls 中的 timm 主干网络
|
| 208 |
+
model_name='efficientnet_b1', # 使用 TIMM 中的 efficientnet_b1
|
| 209 |
+
features_only=True,
|
| 210 |
+
pretrained=True,
|
| 211 |
+
out_indices=(2, 3, 4)),
|
| 212 |
+
neck=dict(
|
| 213 |
+
type='YOLOv5PAFPN',
|
| 214 |
+
deepen_factor=deepen_factor,
|
| 215 |
+
widen_factor=widen_factor,
|
| 216 |
+
in_channels=channels, # 注意:EfficientNet-B1 输出的3个通道是 [40, 112, 320],和原先的 yolov5-s neck 不匹配,需要更改
|
| 217 |
+
out_channels=channels),
|
| 218 |
+
bbox_head=dict(
|
| 219 |
+
type='YOLOv5Head',
|
| 220 |
+
head_module=dict(
|
| 221 |
+
type='YOLOv5HeadModule',
|
| 222 |
+
in_channels=channels, # head 部分输入通道也要做相应更改
|
| 223 |
+
widen_factor=widen_factor))
|
| 224 |
+
)
|
| 225 |
+
```
|
| 226 |
+
|
| 227 |
+
### 使用在 MMSelfSup 中实现的主干网络
|
| 228 |
+
|
| 229 |
+
假设想将 MMSelfSup 中 `MoCo v3` 自监督训练的 `ResNet-50` 作为 `YOLOv5` 的主干网络,则配置文件如下:
|
| 230 |
+
|
| 231 |
+
```python
|
| 232 |
+
_base_ = './yolov5_s-v61_syncbn_8xb16-300e_coco.py'
|
| 233 |
+
|
| 234 |
+
# 请先使用命令: mim install "mmselfsup>=1.0.0rc3",安装 mmselfsup
|
| 235 |
+
# 导入 mmselfsup.models 使得可以调用 mmselfsup 中注册的模块
|
| 236 |
+
custom_imports = dict(imports=['mmselfsup.models'], allow_failed_imports=False)
|
| 237 |
+
checkpoint_file = 'https://download.openmmlab.com/mmselfsup/1.x/mocov3/mocov3_resnet50_8xb512-amp-coslr-800e_in1k/mocov3_resnet50_8xb512-amp-coslr-800e_in1k_20220927-e043f51a.pth' # noqa
|
| 238 |
+
deepen_factor = _base_.deepen_factor
|
| 239 |
+
widen_factor = 1.0
|
| 240 |
+
channels = [512, 1024, 2048]
|
| 241 |
+
|
| 242 |
+
model = dict(
|
| 243 |
+
backbone=dict(
|
| 244 |
+
_delete_=True, # 将 _base_ 中关于 backbone 的字段删除
|
| 245 |
+
type='mmselfsup.ResNet',
|
| 246 |
+
depth=50,
|
| 247 |
+
num_stages=4,
|
| 248 |
+
out_indices=(2, 3, 4), # 注意:MMSelfSup 中 ResNet 的 out_indices 比 MMdet 和 MMCls 的要大 1
|
| 249 |
+
frozen_stages=1,
|
| 250 |
+
norm_cfg=dict(type='BN', requires_grad=True),
|
| 251 |
+
norm_eval=True,
|
| 252 |
+
style='pytorch',
|
| 253 |
+
init_cfg=dict(type='Pretrained', checkpoint=checkpoint_file)),
|
| 254 |
+
neck=dict(
|
| 255 |
+
type='YOLOv5PAFPN',
|
| 256 |
+
deepen_factor=deepen_factor,
|
| 257 |
+
widen_factor=widen_factor,
|
| 258 |
+
in_channels=channels, # 注意:ResNet-50 输出的3个通道是 [512, 1024, 2048],和原先的 yolov5-s neck 不匹配,需要更改
|
| 259 |
+
out_channels=channels),
|
| 260 |
+
bbox_head=dict(
|
| 261 |
+
type='YOLOv5Head',
|
| 262 |
+
head_module=dict(
|
| 263 |
+
type='YOLOv5HeadModule',
|
| 264 |
+
in_channels=channels, # head 部分输入通道也要做相应更改
|
| 265 |
+
widen_factor=widen_factor))
|
| 266 |
+
)
|
| 267 |
+
```
|
| 268 |
+
|
| 269 |
+
### 不使用预训练权重
|
| 270 |
+
|
| 271 |
+
通常情况下,骨干网络初始化都是优先选择预训练权重。如果你不想使用预训练权重,而是想从头开始训练时模型时,
|
| 272 |
+
我们可以将 `backbone` 中的 `init_cfg` 设置为 `None`,此时骨干网络将会以默认的初始化方法进行初始化,
|
| 273 |
+
而不会使用训练好的预训练权重进行初始。以下是以 `YOLOv5` 使用 resnet 作为主干网络为例子,其余算法也是同样的处理:
|
| 274 |
+
|
| 275 |
+
```python
|
| 276 |
+
_base_ = './yolov5_s-v61_syncbn_8xb16-300e_coco.py'
|
| 277 |
+
|
| 278 |
+
deepen_factor = _base_.deepen_factor
|
| 279 |
+
widen_factor = 1.0
|
| 280 |
+
channels = [512, 1024, 2048]
|
| 281 |
+
|
| 282 |
+
model = dict(
|
| 283 |
+
backbone=dict(
|
| 284 |
+
_delete_=True, # 将 _base_ 中关于 backbone 的字段删除
|
| 285 |
+
type='mmdet.ResNet', # 使用 mmdet 中的 ResNet
|
| 286 |
+
depth=50,
|
| 287 |
+
num_stages=4,
|
| 288 |
+
out_indices=(1, 2, 3),
|
| 289 |
+
frozen_stages=1,
|
| 290 |
+
norm_cfg=dict(type='BN', requires_grad=True),
|
| 291 |
+
norm_eval=True,
|
| 292 |
+
style='pytorch',
|
| 293 |
+
init_cfg=None # init_cfg 设置为 None,则 backbone 将不会使用预训练好的权重进行初始化了
|
| 294 |
+
),
|
| 295 |
+
neck=dict(
|
| 296 |
+
type='YOLOv5PAFPN',
|
| 297 |
+
widen_factor=widen_factor,
|
| 298 |
+
in_channels=channels, # 注意:ResNet-50 输出的 3 个通道是 [512, 1024, 2048],和原先的 yolov5-s neck 不匹配,需要更改
|
| 299 |
+
out_channels=channels),
|
| 300 |
+
bbox_head=dict(
|
| 301 |
+
type='YOLOv5Head',
|
| 302 |
+
head_module=dict(
|
| 303 |
+
type='YOLOv5HeadModule',
|
| 304 |
+
in_channels=channels, # head 部分输入通道也要做相应更改
|
| 305 |
+
widen_factor=widen_factor))
|
| 306 |
+
)
|
| 307 |
+
```
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/training_testing_tricks.md
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 训练和测试技巧
|
| 2 |
+
|
| 3 |
+
MMYOLO 中已经支持了大部分 YOLO 系列目标检测相关算法。不同算法可能涉及到一些实用技巧。本章节将基于所实现的目标检测算法,详细描述 MMYOLO 中已经支持的常用的训练和测试技巧。
|
| 4 |
+
|
| 5 |
+
## 训练技巧
|
| 6 |
+
|
| 7 |
+
### 提升检测性能
|
| 8 |
+
|
| 9 |
+
#### 1 开启多尺度训练
|
| 10 |
+
|
| 11 |
+
在目标检测领域,多尺度训练是一个非常常用的技巧,但是在 YOLO 中大部分模型的训练输入都是单尺度的 640x640,原因有两个方面:
|
| 12 |
+
|
| 13 |
+
1. 单尺度训练速度快。当训练 epoch 在 300 或者 500 的时候训练效率是用户非常关注的,多尺度训练会比较慢
|
| 14 |
+
2. 训练 pipeline 中隐含了多尺度增强,等价于应用了多尺度训练,典型的如 `Mosaic`、`RandomAffine` 和 `Resize` 等,故没有必要再次引入模型输入的多尺度训练
|
| 15 |
+
|
| 16 |
+
在 COCO 数据集上进行了简单实验,如果直接在 YOLOv5 的 DataLoader 输出后再次引入多尺度训练增强实际性能提升非常小,但是这不代表用户自定义数据集微调模式下没有明显增益。如果想在 MMYOLO 中对 YOLO 系列算法开启多尺度训练,可以参考 [多尺度训练文档](../common_usage/ms_training_testing.md)
|
| 17 |
+
|
| 18 |
+
#### 2 使用 Mask 标注优化目标检测性能
|
| 19 |
+
|
| 20 |
+
在数据集标注完备例如同时存在边界框和实例分割标注但任务只需要其中部分标注情况下,可以借助完备的数据标注训练单一任务从而提升性能。在目标检测中同样可以借鉴实例分割标注来提升目标检测性能。 以下是 YOLOv8 额外引入实例分割标注优化目标检测结果。 性能增益如下所示:
|
| 21 |
+
|
| 22 |
+
<div align="center">
|
| 23 |
+
<img src="https://user-images.githubusercontent.com/17425982/224920799-597ee962-5997-48b3-9499-25f885d7a421.png" width="1000"/>
|
| 24 |
+
</div>
|
| 25 |
+
|
| 26 |
+
从上述曲线图可以看出,不同尺度模型都有了不同程度性能提升。需要注意的是 `Mask Refine` 仅仅的是作用在数据增强阶段,对模型其他训练部分不需要任何改动,且不会影响训练速度。具体如下所示:
|
| 27 |
+
|
| 28 |
+
<div align="center">
|
| 29 |
+
<img src="https://user-images.githubusercontent.com/17425982/224922191-a52cb410-d08d-455a-bd38-08b83266cc5f.png" width="1000"/>
|
| 30 |
+
</div>
|
| 31 |
+
|
| 32 |
+
上述的 Mask 表示实例分割标注发挥关键作用的数据增强变换,将该技巧应用到其他 YOLO 系列中均有不同程度涨点。
|
| 33 |
+
|
| 34 |
+
#### 3 训练后期关闭强增强提升检测性能
|
| 35 |
+
|
| 36 |
+
该策略是在 YOLOX 算法中第一次被提出可以极大的提升检测性能。 论文中指出虽然 Mosaic+MixUp 可以极大的提升目标检测性能,但是它生成的训练图片远远脱离自然图片的真实分布,并且 Mosaic 大量的裁剪操作会带来很多不准确的标注框,所以 YOLOX 提出在最后 15 个 epoch 关掉强增强,转而使用较弱的增强,从而为了让检测器避开不准确标注框的影响,在自然图片的数据分布下完成最终的收敛。
|
| 37 |
+
|
| 38 |
+
该策略已经被应用到了大部分 YOLO 算法中,以 YOLOv8 为例其数据增强 pipeline 如下所示:
|
| 39 |
+
|
| 40 |
+
<div align="center">
|
| 41 |
+
<img src="https://user-images.githubusercontent.com/17425982/224923285-dd23e419-1d9d-4ee6-bec6-af0a6ef5fed0.png" width="400"/>
|
| 42 |
+
</div>
|
| 43 |
+
|
| 44 |
+
不过在何时关闭强增强是一个超参,如果关闭太早则可能没有充分发挥 Mosaic 等强增强效果,如果关闭太晚则由于之前已经过拟合,此时再关闭则没有任何增益。 在 YOLOv8 实验中可以观察到该现象
|
| 45 |
+
|
| 46 |
+
| Backbone | Mask Refine | box AP | Epoch of best mAP |
|
| 47 |
+
| :------: | :---------: | :---------: | :---------------: |
|
| 48 |
+
| YOLOv8-n | No | 37.2 | 500 |
|
| 49 |
+
| YOLOv8-n | Yes | 37.4 (+0.2) | 499 |
|
| 50 |
+
| YOLOv8-s | No | 44.2 | 430 |
|
| 51 |
+
| YOLOv8-s | Yes | 45.1 (+0.9) | 460 |
|
| 52 |
+
| YOLOv8-m | No | 49.8 | 460 |
|
| 53 |
+
| YOLOv8-m | Yes | 50.6 (+0.8) | 480 |
|
| 54 |
+
| YOLOv8-l | No | 52.1 | 460 |
|
| 55 |
+
| YOLOv8-l | Yes | 53.0 (+0.9) | 491 |
|
| 56 |
+
| YOLOv8-x | No | 52.7 | 450 |
|
| 57 |
+
| YOLOv8-x | Yes | 54.0 (+1.3) | 460 |
|
| 58 |
+
|
| 59 |
+
从上表可以看出:
|
| 60 |
+
|
| 61 |
+
- 大模型在 COCO 数据集训练 500 epoch 会过拟合,在过拟合情况下再关闭 Mosaic 等强增强效果没有效果
|
| 62 |
+
- 使用 Mask 标注可以缓解过拟合,并且提升性能
|
| 63 |
+
|
| 64 |
+
#### 4 加入纯背景图片抑制误报率
|
| 65 |
+
|
| 66 |
+
对于非开放世界数据集目标检测而言,训练和测试都是在固定类别上进行,一旦应用到没有训练过的类别图片上有可能会产生误报,一个常见的缓解策略是加入一定比例的纯背景图片。 在大部分 YOLO 系列中都是默认开启了加入纯背景图片抑制误报率功能,用户只需要设置 `train_dataloader.dataset.filter_cfg.filter_empty_gt` 为 False 即可,表示将纯背景图片不过滤掉加入训练。
|
| 67 |
+
|
| 68 |
+
#### 5 试试 AdamW 也许效果显著
|
| 69 |
+
|
| 70 |
+
YOLOv5,YOLOv6,YOLOv7 和 YOLOv8 等都是采用了 SGD 优化器,该参数器对参数的设置比较严格,而 AdamW 则正好相反,其对学习率设置等没有那么敏感。因此如果用户在自定义数据集微调可以尝试选择 AdamW 优化器。我们在 YOLOX 中进行了简单尝试,发现在 tiny、s 和 m 尺度模型上将其优化器替换为 AdamW 均有一定程度涨点。
|
| 71 |
+
|
| 72 |
+
| Backbone | Size | Batch Size | RTMDet-Hyp | Box AP |
|
| 73 |
+
| :--------: | :--: | :--------: | :--------: | :---------: |
|
| 74 |
+
| YOLOX-tiny | 416 | 8xb8 | No | 32.7 |
|
| 75 |
+
| YOLOX-tiny | 416 | 8xb32 | Yes | 34.3 (+1.6) |
|
| 76 |
+
| YOLOX-s | 640 | 8xb8 | No | 40.7 |
|
| 77 |
+
| YOLOX-s | 640 | 8xb32 | Yes | 41.9 (+1.2) |
|
| 78 |
+
| YOLOX-m | 640 | 8xb8 | No | 46.9 |
|
| 79 |
+
| YOLOX-m | 640 | 8xb32 | Yes | 47.5 (+0.6) |
|
| 80 |
+
|
| 81 |
+
具体见 [configs/yolox/README.md](https://github.com/open-mmlab/mmyolo/blob/main/configs/yolox/README.md#--results-and-models)。
|
| 82 |
+
|
| 83 |
+
#### 6 考虑 ignore 场景避免不确定性标注
|
| 84 |
+
|
| 85 |
+
以 CrowdHuman 为例,其是一个拥挤行人检测数据集,下面是一张典型图片:
|
| 86 |
+
|
| 87 |
+
<div align="center">
|
| 88 |
+
<img src="https://user-images.githubusercontent.com/17425982/224928241-89dac006-392b-445d-87e8-a9e268825401.png" width="1000"/>
|
| 89 |
+
</div>
|
| 90 |
+
|
| 91 |
+
图片来自 [detectron2 issue](https://github.com/facebookresearch/detectron2/issues/1909)。黄色打叉的区域表示 `iscrowd` 标注。原因有两个方面:
|
| 92 |
+
|
| 93 |
+
- 这个区域不是真的人,例如海报上的人
|
| 94 |
+
- 该区域过于拥挤,很难标注
|
| 95 |
+
|
| 96 |
+
在该场景下,你不能简单的将这类标注删掉,因为你一旦删掉就表示当做背景区域来训练了,但是其和背景是不一样的,首先海报上的人和真人很像,并且拥挤区域确实有人只是不好标注。如果你简单的将其当做背景训练,那么会造成漏报。最合适的做法应该是把拥挤区域当做忽略区域即该区域的任何输出都直接忽略,不计算任何 Loss,不强迫模型拟合。
|
| 97 |
+
|
| 98 |
+
MMYOLO 在 YOLOv5 上简单快速的验证了 `iscrowd` 标注的作用,性能如下所示:
|
| 99 |
+
|
| 100 |
+
| Backbone | ignore_iof_thr | box AP50(CrowDHuman Metric) | MR | JI |
|
| 101 |
+
| :------: | :------------: | :-------------------------: | :--: | :---: |
|
| 102 |
+
| YOLOv5-s | -1 | 85.79 | 48.7 | 75.33 |
|
| 103 |
+
| YOLOv5-s | 0.5 | 86.17 | 48.8 | 75.87 |
|
| 104 |
+
|
| 105 |
+
`ignore_iof_thr`为 -1 表示不考虑忽略标签,可以看出性能有一定程度的提升,具体见 [CrowdHuman 结果](https://github.com/open-mmlab/mmyolo/blob/main/configs/yolov5/README.md#crowdhuman)。 如果你的自定义数据集上也有上述情况,则建议你考虑 ignore 场景避免不确定性标注。
|
| 106 |
+
|
| 107 |
+
#### 7 使用知识蒸馏
|
| 108 |
+
|
| 109 |
+
知识蒸馏是一个被广泛使用的技巧,可以将大模型性能转移到小模型上从而提升小模型检测性能。 目前 MMYOLO 和 MMRazor 已支持了该功能,并在 RTMDet 上进行了初步验证。
|
| 110 |
+
|
| 111 |
+
| Model | box AP |
|
| 112 |
+
| :------------: | :---------: |
|
| 113 |
+
| RTMDet-tiny | 41.0 |
|
| 114 |
+
| RTMDet-tiny \* | 41.8 (+0.8) |
|
| 115 |
+
| RTMDet-s | 44.6 |
|
| 116 |
+
| RTMDet-s \* | 45.7 (+1.1) |
|
| 117 |
+
| RTMDet-m | 49.3 |
|
| 118 |
+
| RTMDet-m \* | 50.2 (+0.9) |
|
| 119 |
+
| RTMDet-l | 51.4 |
|
| 120 |
+
| RTMDet-l \* | 52.3 (+0.9) |
|
| 121 |
+
|
| 122 |
+
星号即为采用了大模型蒸馏的结果,详情见 [Distill RTMDet](https://github.com/open-mmlab/mmyolo/tree/main/configs/rtmdet/distillation)。
|
| 123 |
+
|
| 124 |
+
#### 8 更大的模型用更强的增强参数
|
| 125 |
+
|
| 126 |
+
如果你基于默认配置修改了模型或者替换了骨干网络,那么建议你基于此刻模型大小来缩放数据增强参数。 一般来说更大的模型需要使用更强的增强参数,否则可能无法发挥大模型的效果,反之如果小模型应用了较强的增强则可能会欠拟合。 以 RTMDet 为例,我们可以观察其不同模型大小的数据增强参数
|
| 127 |
+
|
| 128 |
+
<div align="center">
|
| 129 |
+
<img src="https://user-images.githubusercontent.com/17425982/224936237-a31add46-77ff-4595-b3d9-c7b574f59c84.png" width="800"/>
|
| 130 |
+
</div>
|
| 131 |
+
|
| 132 |
+
其中 `random_resize_ratio_range` 表示 `RandomResize` 的随机缩放范围,`mosaic_max_cached_images/mixup_max_cached_images`表示 `Mosaic/MixUp` 增强时候缓存的图片个数,可以用于调整增强的强度。 YOLO 系列模型都是遵循同一套参数设置原则。
|
| 133 |
+
|
| 134 |
+
### 加快训练速度
|
| 135 |
+
|
| 136 |
+
#### 1 单尺度训练开启 cudnn_benchmark
|
| 137 |
+
|
| 138 |
+
YOLO 系列算法中大部分网络输入图片大小都是固定的即单尺度,此时可以开启 `cudnn_benchmark` 来加快训练速度。该参数主要针对 PyTorch 的 cuDNN 底层库进行设置, 设置这个标志可以让内置的 cuDNN 自动寻找最适合当前配置的高效算法来优化运行效率。如果是多尺度模式开启该标志则会不断的寻找最优算法,反而会拖慢训练速度。
|
| 139 |
+
|
| 140 |
+
在 MMYOLO 中开启 `cudnn_benchmark`,只需要在配置中设置 `env_cfg = dict(cudnn_benchmark=True)`
|
| 141 |
+
|
| 142 |
+
#### 2 使用带缓存的 Mosaic 和 MixUp
|
| 143 |
+
|
| 144 |
+
如果你的数据增强中应用了 Mosaic 和 MixUp,并且经过排查训练瓶颈来自图片的随机读取,那么建议将常规的 Mosaic 和 MixUp 替换为 RTMDet 中提出的带缓存的版本。
|
| 145 |
+
|
| 146 |
+
| Data Aug | Use cache | ms/100 imgs |
|
| 147 |
+
| :------: | :-------: | :---------: |
|
| 148 |
+
| Mosaic | No | 87.1 |
|
| 149 |
+
| Mosaic | Yes | 24.0 |
|
| 150 |
+
| MixUp | No | 19.3 |
|
| 151 |
+
| MixUp | Yes | 12.4 |
|
| 152 |
+
|
| 153 |
+
Mosaic 和 MixUp 涉及到多张图片的混合,它们的耗时会是普通数据增强的 K 倍(K 为混入图片的数量)。 如在 YOLOv5 中每次做 Mosaic 时, 4 张图片的信息都需要从硬盘中重新加载。 而带缓存的 Mosaic 和 MixUp 只需要重新载入当前的一张图片,其余参与混合增强的图片则从缓存队列中获取,通过牺牲一定内存空间的方式大幅提升了效率。
|
| 154 |
+
|
| 155 |
+
<div align=center>
|
| 156 |
+
<img alt="data cache" src="https://user-images.githubusercontent.com/33799979/192730011-90e2a28d-e163-4399-bf87-d3012007d8c3.png" width=800 />
|
| 157 |
+
</div>
|
| 158 |
+
|
| 159 |
+
如图所示,cache 队列中预先储存了 N 张已加载的图像与标签数据,每一个训练 step 中只需加载一张新的图片及其标签数据并更新到 cache 队列中(cache 队列中的图像可重复,如图中出现两次 img3),同时如果 cache 队列长度超过预设长度,则随机 pop 一张图,当需要进行混合数据增强时,只需要从 cache 中随机选择需要的图像进行拼接等处理,而不需要全部从硬盘中加载,节省了图像加载的时间。
|
| 160 |
+
|
| 161 |
+
### 减少超参
|
| 162 |
+
|
| 163 |
+
YOLOv5 中通过实践提供了一些减少超参数的方法,下面详细说明。
|
| 164 |
+
|
| 165 |
+
#### 1 Loss 权重自适应,少 1 个超参
|
| 166 |
+
|
| 167 |
+
一般来说,对于不同的任务或者不同的类别,可能需要针对性的设置超参,而这通常比较难。YOLOv5 中根据实践提出了一些根据类别数和检测输出层个数来自适应缩放 Loss 权重的方法,如下所示:
|
| 168 |
+
|
| 169 |
+
```python
|
| 170 |
+
# scaled based on number of detection layers
|
| 171 |
+
loss_cls=dict(
|
| 172 |
+
type='mmdet.CrossEntropyLoss',
|
| 173 |
+
use_sigmoid=True,
|
| 174 |
+
reduction='mean',
|
| 175 |
+
loss_weight=loss_cls_weight *
|
| 176 |
+
(num_classes / 80 * 3 / num_det_layers)),
|
| 177 |
+
loss_bbox=dict(
|
| 178 |
+
type='IoULoss',
|
| 179 |
+
iou_mode='ciou',
|
| 180 |
+
bbox_format='xywh',
|
| 181 |
+
eps=1e-7,
|
| 182 |
+
reduction='mean',
|
| 183 |
+
loss_weight=loss_bbox_weight * (3 / num_det_layer
|
| 184 |
+
return_iou=True),
|
| 185 |
+
loss_obj=dict(
|
| 186 |
+
type='mmdet.CrossEntropyLoss',
|
| 187 |
+
use_sigmoid=True,
|
| 188 |
+
reduction='mean',
|
| 189 |
+
loss_weight=loss_obj_weight *
|
| 190 |
+
((img_scale[0] / 640)**2 * 3 / num_det_layers)),
|
| 191 |
+
```
|
| 192 |
+
|
| 193 |
+
`loss_cls` 可以根据自定义类别数和检测层数对 `loss_weight` 进行自适应缩放,`loss_bbox` 可以根据检测层数进行自适应计算,而 `loss_obj` 可以根据输入图片大小和检测层数进行自适应缩放。这种策略可以让用户不用去设置 Loss 权重超参。
|
| 194 |
+
需要说明的是:这个只是经验规则,并不是说是最佳设置组合,只是作为一个参考。
|
| 195 |
+
|
| 196 |
+
#### 2 Weight Decay 和 Loss 输出值基于 Batch Size 自适应,少 2 个超参
|
| 197 |
+
|
| 198 |
+
一般来说,在不同的 `Batch Size` 上进行训练,需要遵循学习率自动缩放规则。但是在各个数据集上验证表明 YOLOv5 实际上在改变 `Batch Size` 时候不缩放学习率也可以取得不错的效果,甚至有时候你缩放了效果还更差。原因就在于代码中存在 `Weight Decay` 和 Loss 输出值基于 `Batch Size` 自适应的技巧。在 YOLOv5 中会基于当前训练的总 `Batch Size` 来缩放 `Weight Decay` 和 Loss 输出值。对应代码为:
|
| 199 |
+
|
| 200 |
+
```python
|
| 201 |
+
# https://github.com/open-mmlab/mmyolo/blob/dev/mmyolo/engine/optimizers/yolov5_optim_constructor.py#L86
|
| 202 |
+
if 'batch_size_per_gpu' in optimizer_cfg:
|
| 203 |
+
batch_size_per_gpu = optimizer_cfg.pop('batch_size_per_gpu')
|
| 204 |
+
# No scaling if total_batch_size is less than
|
| 205 |
+
# base_total_batch_size, otherwise linear scaling.
|
| 206 |
+
total_batch_size = get_world_size() * batch_size_per_gpu
|
| 207 |
+
accumulate = max(
|
| 208 |
+
round(self.base_total_batch_size / total_batch_size), 1)
|
| 209 |
+
scale_factor = total_batch_size * \
|
| 210 |
+
accumulate / self.base_total_batch_size
|
| 211 |
+
if scale_factor != 1:
|
| 212 |
+
weight_decay *= scale_factor
|
| 213 |
+
print_log(f'Scaled weight_decay to {weight_decay}', 'current')
|
| 214 |
+
```
|
| 215 |
+
|
| 216 |
+
```python
|
| 217 |
+
# https://github.com/open-mmlab/mmyolo/blob/dev/mmyolo/models/dense_heads/yolov5_head.py#L635
|
| 218 |
+
_, world_size = get_dist_info()
|
| 219 |
+
return dict(
|
| 220 |
+
loss_cls=loss_cls * batch_size * world_size,
|
| 221 |
+
loss_obj=loss_obj * batch_size * world_size,
|
| 222 |
+
loss_bbox=loss_box * batch_size * world_size)
|
| 223 |
+
```
|
| 224 |
+
|
| 225 |
+
在不同的 Batch Size 下 Loss 的权重是不一样大的,Batch Size 越大,Loss 就越大,梯度就越大,我个人猜测这可以等价于 Batch Size 增大时候,学习率线性增加的场合。
|
| 226 |
+
实际上从 YOLOv5 的 [YOLOv5 Study: mAP vs Batch-Size](https://github.com/ultralytics/yolov5/discussions/2452) 中可以发现确实是希望用户在修改 Batch Size 时���需要修改其他参数也可以相近的性能。上述两个策略是一个非常不错的训练技巧。
|
| 227 |
+
|
| 228 |
+
### 减少训练显存
|
| 229 |
+
|
| 230 |
+
如何减少训练显存是一个经常谈论的问题,所涉及的技术也非常多。 MMYOLO 的训练执行器来自 MMEngine,因此如何减少训练显存可以查阅 MMEngine 的文档。 MMEngine 目前支持梯度累加、梯度检查点和大模型训练技术,详情见
|
| 231 |
+
[节省显存](https://mmengine.readthedocs.io/zh_CN/latest/common_usage/save_gpu_memory.html)。
|
| 232 |
+
|
| 233 |
+
## 测试技巧
|
| 234 |
+
|
| 235 |
+
### 推理速度和测试精度的平衡
|
| 236 |
+
|
| 237 |
+
在模型性能测试时候,我们一般是要求 mAP 越高越好,但是在实际应用或者推理时候我们希望在保证低误报率和漏报率情况下模型推理越快越好,或者说测试只关注 mAP 而忽略了后处理和评估速度,而实际落地应用时候会追求速度和精度的平衡。
|
| 238 |
+
在 YOLO 系列中可以通过控制某些参数实现速度和精度平衡,下面以 YOLOv5 为例对其进行详细描述。
|
| 239 |
+
|
| 240 |
+
#### 1 推理时避免一个检测框输出多个类别
|
| 241 |
+
|
| 242 |
+
YOLOv5 在训练分类分支时候采用的是 BCE Loss 即 `use_sigmoid=True`。假设物体类别数是 4,那么分类分支输出的类别数是 4 而不是 5,并且由于使用的是 sigmoid 而非 softmax 预测模式,很可能在某个位置预测出多个满足过滤阈值的检测框,也就是会出现一个预测 bbox 对应多个预测 label 的情况。如下图所示
|
| 243 |
+
|
| 244 |
+
<div align=center>
|
| 245 |
+
<img alt="multi-label" src="https://user-images.githubusercontent.com/17425982/226282295-8ef53a89-e33e-4fd5-8d60-417db2d5a140.png" width=800 />
|
| 246 |
+
</div>
|
| 247 |
+
|
| 248 |
+
一般在计算 mAP 时候过滤阈值为 0.001,由于 sigmoid 非竞争性预测模式会导致一个框对应多个 label。这种计算方式可以提高 mAP 计算时候的召回率,但是实际落地应用会不方便。
|
| 249 |
+
|
| 250 |
+
一个常用的办法就是提高过滤阈值,但是如果你不需要出现较多漏报,此时推荐你修改 `multi_label` 参数为 False,其位于配置的 `mode.test_cfg.multi_label` 中,默认值是 True 表示允许一个检测框对应多个 label。
|
| 251 |
+
|
| 252 |
+
#### 2 简化 test pipeline
|
| 253 |
+
|
| 254 |
+
注意到 YOLOv5 的 test pipeline 为如下:
|
| 255 |
+
|
| 256 |
+
```python
|
| 257 |
+
test_pipeline = [
|
| 258 |
+
dict(type='LoadImageFromFile'),
|
| 259 |
+
dict(type='YOLOv5KeepRatioResize', scale=img_scale),
|
| 260 |
+
dict(
|
| 261 |
+
type='LetterResize',
|
| 262 |
+
scale=img_scale,
|
| 263 |
+
allow_scale_up=False,
|
| 264 |
+
pad_val=dict(img=114)),
|
| 265 |
+
dict(type='LoadAnnotations', with_bbox=True, _scope_='mmdet'),
|
| 266 |
+
dict(
|
| 267 |
+
type='mmdet.PackDetInputs',
|
| 268 |
+
meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',
|
| 269 |
+
'scale_factor', 'pad_param'))
|
| 270 |
+
]
|
| 271 |
+
```
|
| 272 |
+
|
| 273 |
+
其使用了两个不同功能的 Resize,目的依然是提高评估时候的 mAP 值。在实际落地应用时候你可以简化该 pipeline,如下所示:
|
| 274 |
+
|
| 275 |
+
```python
|
| 276 |
+
test_pipeline = [
|
| 277 |
+
dict(type='LoadImageFromFile'),
|
| 278 |
+
dict(
|
| 279 |
+
type='LetterResize',
|
| 280 |
+
scale=_base_.img_scale,
|
| 281 |
+
allow_scale_up=True,
|
| 282 |
+
use_mini_pad=True),
|
| 283 |
+
dict(type='LoadAnnotations', with_bbox=True),
|
| 284 |
+
dict(
|
| 285 |
+
type='mmdet.PackDetInputs',
|
| 286 |
+
meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',
|
| 287 |
+
'scale_factor', 'pad_param'))
|
| 288 |
+
]
|
| 289 |
+
```
|
| 290 |
+
|
| 291 |
+
实际上 YOLOv5 算法在实际应用时候是采用简化的 pipeline,并将 multi_label 设为 False, score_thr 提高为 0.25, iou_threshold 降低为 0.45。
|
| 292 |
+
在 YOLOv5 配置中我们提供了一套 detect 落地时候的配置参数,具体见 [yolov5_s-v61_syncbn-detect_8xb16-300e_coco.py](https://github.com/open-mmlab/mmyolo/blob/main/configs/yolov5/yolov5_s-v61_syncbn-detect_8xb16-300e_coco.py)。
|
| 293 |
+
|
| 294 |
+
#### 3 Batch Shape 策略加快测试速度
|
| 295 |
+
|
| 296 |
+
Batch Shape 是 YOLOv5 中提出的可以加快推理的一个测试技巧,其思路是不再强制要求整个测试过程图片都是 640x640,而是可以变尺度测试,只需要保证当前 batch 内的 shape 是一样的就行。这种方式可以减少额外的图片像素填充,从而实现加速推理过程。
|
| 297 |
+
Batch Shape 的具体实现可以参考 [链接](https://github.com/open-mmlab/mmyolo/blob/main/mmyolo/datasets/utils.py#L55)。MMYOLO 中几乎所有算法在测试时候都是默认开启了 Batch Shape 策略。 如果用户想关闭该功能,可以设置 `val_dataloader.dataset.batch_shapes_cfg=None`。
|
| 298 |
+
|
| 299 |
+
在实际落地场景下,因为动态 shape 没有固定 shape 快且高效,所以一般会不采用这个策略。
|
| 300 |
+
|
| 301 |
+
### TTA 提升测试精度
|
| 302 |
+
|
| 303 |
+
TTA 测试时增强是一个万能的涨点技巧,在打比赛时候非常有用。MMYOLO 已经支持了 TTA,只需要在测试时候输入 `--tta` 即可开启。详情见 [TTA 说明](https://github.com/open-mmlab/mmyolo/blob/dev/docs/zh_cn/common_usage/tta.md)。
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/troubleshooting_steps.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 常见错误排除步骤
|
| 2 |
+
|
| 3 |
+
本文档收集用户经常碰到的常见错误情况,并提供详细的排查步骤。如果你发现阅读本文你没有找到正确的解决方案,请联系我们或者提 PR 进行更新。提 PR 请参考 [如何给 MMYOLO 贡献代码](../recommended_topics/contributing.md)
|
| 4 |
+
|
| 5 |
+
## xxx is not in the model registry
|
| 6 |
+
|
| 7 |
+
这个错误信息是指某个模块没有被注册到 model 中。 这个错误出现的原因非常多,典型的情况有:
|
| 8 |
+
|
| 9 |
+
1. 你新增的模块没有在类别前面加上注册器装饰器 @MODELS.register_module()
|
| 10 |
+
2. 虽然注册了,但是注册错了位置,例如你实际想注册到 MMYOLO 中,但是你导入的 MODELS 是 MMDet 包里面的
|
| 11 |
+
3. 你注册了且注册正确了,但是没有在对应的 `__init__.py` 中加入导致没有被导入
|
| 12 |
+
4. 以上 3 个步骤都确认没问题,但是你是新增 py 文件来自定义模块的却没有重新安装 MMYOLO 导致没有生效,此时你可以重新安装一遍,即使你是 -e 模式安装也需要重新安装
|
| 13 |
+
5. 如果你是在 mmyolo 包路径下新增了一个 package, 除上述步骤外,你还需要在 [register_all_modules](https://github.com/open-mmlab/mmyolo/blob/main/mmyolo/utils/setup_env.py#L8) 函数中增加其导包代码,否则该 package 不会被自动触发
|
| 14 |
+
6. 你的环境中有多个版本 MMYOLO,你注册的和实际运行的实际上不是同一套代码,导致没有生效。此时你可以在程序运行前输入 `PYTHONPATH="$(dirname $0)/..":$PYTHONPATH` 强行使用当前代码
|
| 15 |
+
|
| 16 |
+
## loss_bbox 始终为 0
|
| 17 |
+
|
| 18 |
+
该原因出现主要有两个原因:
|
| 19 |
+
|
| 20 |
+
1. 训练过程中没有 GT 标注数据
|
| 21 |
+
2. 参数设置不合理导致训练中没有正样本
|
| 22 |
+
|
| 23 |
+
第一种情况出现的概率更大。 `loss_bbox` 通常是只考虑正样本的 loss,如果训练中没有正样本则始终为 0。如果是第一种原因照成的 `loss_bbox` 始终为 0,那么通常意味着你配置不对,特别是 dataset 部分的配置不正确。
|
| 24 |
+
一个非常典型的情况是用户的 `dataset` 中 `metainfo` 设置不正确或者设置了但是没有传给 dataset 导致加载后没有找到对应类别的 GT Bbox 标注。 这种情况请仔细阅读我们提供的 [示例配置](https://github.com/open-mmlab/mmyolo/blob/main/projects/misc/custom_dataset/yolov5_s-v61_syncbn_fast_1xb32-100e_cat.py#L27) 。
|
| 25 |
+
验证 dataset 配置是否正确的一个最直接的途径是运行 [browse_dataset 脚本](https://github.com/open-mmlab/mmyolo/blob/main/tools/analysis_tools/browse_dataset.py),如果可视化效果正确则说明是正确的。
|
| 26 |
+
|
| 27 |
+
## MMCV 安装时间非常久
|
| 28 |
+
|
| 29 |
+
这通常意味着你在自己编译 MMCV 而不是直接下载使用我们提供的预编译包。 MMCV 中包括了大量的自定义的 CUDA 算子,如果从源码安装则需要非常久的时间去编译,并且由于其安装成功依赖于严格的底层环境信息,需要多个库的版本一致才可以。如果用户自己编译大概率会失败。
|
| 30 |
+
我们不推荐用户自己去编译 MMCV 而应该优先选择预编译包。如果你当前的环境中我们没有提供对应的预编译包,那么建议你可以快速换一个 Conda 环境,并安装有预编译包的 Torch。 以 torch1.8.0+cu102 为例,如果你想查看目前查看所有的预编译包,可以查看 https://download.openmmlab.com/mmcv/dist/cu102/torch1.8.0/index.html。
|
| 31 |
+
|
| 32 |
+
## 基于官方配置继承新建的配置出现 unexpected keyword argument
|
| 33 |
+
|
| 34 |
+
这通常是由于你没有删除 base 配置中的额外参数。 可以在你新建配置所修改的字典中增加 `_delete_=True` 删掉 base 中该类之前的所有参数。
|
| 35 |
+
|
| 36 |
+
## The testing results of the whole dataset is empty
|
| 37 |
+
|
| 38 |
+
这通常说明训练效果太差导致网络没有预测出任何符合阈值要求的检测框。 出现这种现象有多个原因,典型的为:
|
| 39 |
+
|
| 40 |
+
1. 当前为前几个 epoch,网络当前训练效果还较差,等后续训练久一点后可能就不会出现该警告了
|
| 41 |
+
2. 配置设置不正确,网络虽然正常训练但是实际上无效训练,例如前面的 `loss_bbox` 始终为 0 就会导致上述警告
|
| 42 |
+
3. 超参设置不合理
|
| 43 |
+
|
| 44 |
+
## ValueError: not enough values to unpack(expected 2, got 0)
|
| 45 |
+
|
| 46 |
+
这个错误通常是在 epoch 切换时候出现。这是 PyTorch 1.7 的已知问题,在 PyTorch 1.8+ 中已经修复。如果在 PyTorch 1.7 中想修复这个问题,可以简单的设置 dataloader 参数 `persistent_workers` 为 False。
|
| 47 |
+
|
| 48 |
+
## ValueError: need at least one array to concatenate
|
| 49 |
+
|
| 50 |
+
这个是一个非常常见的错误,可能出现在训练一开始或者训练正常但是评估时候。不管出现在何阶段,均说明你的配置不对:
|
| 51 |
+
|
| 52 |
+
1. 最常见的错误就是 `num_classes` 参数设置不对。在 MMYOLO 或者 MMDet 中大部分配置都是以 COCO 数据为例,因此配置中默认的 `num_classes` 是 80, 如果用户自定义数据集没有正确修改这个字段则会出现上述错误。
|
| 53 |
+
MMYOLO 中有些算法配置会在多个模块中都需要 `num_classes` 参数,用户经常出现的错误就是仅仅修改了某一个地方的 `num_classes` 而没有将所有的 `num_classes` 都修改。想快速解决这个问题,可以使用 [print_config](https://github.com/open-mmlab/mmyolo/blob/main/tools/misc/print_config.py)
|
| 54 |
+
脚本打印下全配置,然后全局搜索 `num_classes` 确认是否有没有修改的模块。
|
| 55 |
+
2. 该错误还可能会出现在对 `dataset.metainfo.classes` 参数设置不对造成的。当用户希望训练自己的数据集但是未能正确的修改 `dataset.metainfo.classes` 参数,而默认的使用 `COCO` 数据集中的类别时,且用户自定义数据集的所有类别不在 `COCO` 数据集的类别里就会出现该错误。这时需要用户核对并修改正确的 `dataset.metainfo.classes` 信息。
|
| 56 |
+
|
| 57 |
+
## 评估时候 IndexError: list index out of range
|
| 58 |
+
|
| 59 |
+
具体输出信息是
|
| 60 |
+
|
| 61 |
+
```text
|
| 62 |
+
File "site-packages/mmdet/evaluation/metrics/coco_metric.py", line 216, in results2json
|
| 63 |
+
data['category_id'] = self.cat_ids[label]
|
| 64 |
+
IndexError: list index out of range
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
+
可以看出是评估时候类别索引越界,这个通常的原因是配置中的 `num_classes` 设置不正确,默认的 `num_classes` 是 80,如果你自定义类别小于 80,那么就有可能出现类别越界。注意算法配置的 `num_classes` 一般会用到多个模块,你可能只改了某几个而漏掉了一些。想快速解决这个问题,可以使用 [print_config](https://github.com/open-mmlab/mmyolo/blob/main/tools/misc/print_config.py)
|
| 68 |
+
脚本打印下全配置,然后全局搜索 `num_classes` 确认是否有没有修改的模块。
|
| 69 |
+
|
| 70 |
+
## 训练中不打印 loss,但是程序依然正常训练和评估
|
| 71 |
+
|
| 72 |
+
这通常是因为一个训练 epoch 没有超过 50 个迭代,而 MMYOLO 中默认的打印间隔是 50。你可以修改 `default_hooks.logger.interval` 参数。
|
| 73 |
+
|
| 74 |
+
## GPU out of memory
|
| 75 |
+
|
| 76 |
+
1. 存在大量 ground truth boxes 或者大量 anchor 的场景,可能在 assigner 会 OOM。
|
| 77 |
+
2. 使用 --amp 来开启混合精度训练。
|
| 78 |
+
3. 你也可以尝试使用 MMDet 中的 AvoidCUDAOOM 来避免该问题。首先它将尝试调用 torch.cuda.empty_cache()。如果失败,将会尝试把输入类型转换到 FP16。如果仍然失败,将会把输入从 GPUs 转换到 CPUs 进行计算。这里提供了两个使用的例子:
|
| 79 |
+
|
| 80 |
+
```python
|
| 81 |
+
from mmdet.utils import AvoidCUDAOOM
|
| 82 |
+
|
| 83 |
+
output = AvoidCUDAOOM.retry_if_cuda_oom(some_function)(input1, input2)
|
| 84 |
+
```
|
| 85 |
+
|
| 86 |
+
你也可也使用 AvoidCUDAOOM 作为装饰器让代码遇到 OOM 的时候继续运行:
|
| 87 |
+
|
| 88 |
+
```python
|
| 89 |
+
from mmdet.utils import AvoidCUDAOOM
|
| 90 |
+
|
| 91 |
+
@AvoidCUDAOOM.retry_if_cuda_oom
|
| 92 |
+
def function(*args, **kwargs):
|
| 93 |
+
...
|
| 94 |
+
return xxx
|
| 95 |
+
```
|
| 96 |
+
|
| 97 |
+
## Loss goes Nan
|
| 98 |
+
|
| 99 |
+
1. 检查数据的标注是否正常, 长或宽为 0 的框可能会导致回归 loss 变为 nan,一些小尺寸(宽度或高度小于 1)的框在数据增强后也会导致此问题。 因此,可以检查标注并过滤掉那些特别小甚至面积为 0 的框,并关闭一些可能会导致 0 面积框出现数据增强。
|
| 100 |
+
2. 降低学习率:由于某些原因,例如 batch size 大小的变化, 导致当前学习率可能太大。 您可以降低为可以稳定训练模型的值。
|
| 101 |
+
3. 延长 warm up 的时间:一些模型在训练初始时对学习率很敏感。
|
| 102 |
+
4. 添加 gradient clipping: 一些模型需要梯度裁剪来稳定训练过程。 你可以在 config 设置 `optim_wrapper.clip_grad=dict(max_norm=xx)`
|
| 103 |
+
|
| 104 |
+
## 训练中其他不符合预期或者错误
|
| 105 |
+
|
| 106 |
+
如果训练或者评估中出现了不属于上述描述的问题,由于原因不明,现提供常用的排除流程:
|
| 107 |
+
|
| 108 |
+
1. 首先确认配置是否正确,可以使用 [print_config](https://github.com/open-mmlab/mmyolo/blob/main/tools/misc/print_config.py) 脚本打印全部配置,如果运行成功则说明配置语法没有错误
|
| 109 |
+
2. 确认 COCO 格式的 json 标注是否正确,可以使用 [browse_coco_json.py](https://github.com/open-mmlab/mmyolo/blob/main/tools/misc/browse_coco_json.py) 脚本确认
|
| 110 |
+
3. 确认 dataset 部分配置是否正确,这一步骤几乎是必须要提前运行的,可以提前排查很多问题,可以使用 [browse_dataset.py](https://github.com/open-mmlab/mmyolo/blob/main/tools/misc/browse_dataset.py) 脚本确认
|
| 111 |
+
4. 如果以上 3 步都没有问题,那么出问题可能在 model 部分了。这个部分的排除没有特别的办法,你可以单独写一个脚本来仅运行 model 部分并通过调试来确认,如果对于 model 中多个模块的输入构建存在困惑,可以参考对应模块的单元测试写法
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/recommended_topics/visualization.md
ADDED
|
@@ -0,0 +1,542 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 关于可视化的一切
|
| 2 |
+
|
| 3 |
+
本文包括特征图可视化和 Grad-Based 和 Grad-Free CAM 可视化
|
| 4 |
+
|
| 5 |
+
## 特征图可视化
|
| 6 |
+
|
| 7 |
+
<div align=center>
|
| 8 |
+
<img src="https://user-images.githubusercontent.com/89863442/190903635-27bbc619-9bf8-43a8-aea8-ea13b9dad28c.jpg" width="1000" alt="image"/>
|
| 9 |
+
</div>
|
| 10 |
+
可视化可以给深度学习的模型训练和测试过程提供直观解释。
|
| 11 |
+
|
| 12 |
+
MMYOLO 中,将使用 MMEngine 提供的 `Visualizer` 可视化器进行特征图可视化,其具备如下功能:
|
| 13 |
+
|
| 14 |
+
- 支持基础绘图接口以及特征图可视化。
|
| 15 |
+
- 支持选择模型中的不同层来得到特征图,包含 `squeeze_mean` , `select_max` , `topk` 三种显示方式,用户还可以使用 `arrangement` 自定义特征图显示的布局方式。
|
| 16 |
+
|
| 17 |
+
### 特征图绘制
|
| 18 |
+
|
| 19 |
+
你可以调用 `demo/featmap_vis_demo.py` 来简单快捷地得到可视化结果,为了方便理解,将其主要参数的功能梳理如下:
|
| 20 |
+
|
| 21 |
+
- `img`:选择要用于特征图可视化的图片,支持单张图片或者图片路径列表。
|
| 22 |
+
|
| 23 |
+
- `config`:选择算法的配置文件。
|
| 24 |
+
|
| 25 |
+
- `checkpoint`:选择对应算法的权重文件。
|
| 26 |
+
|
| 27 |
+
- `--out-file`:将得到的特征图保存到本地,并指定路径和文件名。
|
| 28 |
+
|
| 29 |
+
- `--device`:指定用于推理图片的硬件,`--device cuda:0` 表示使用第 1 张 GPU 推理,`--device cpu` 表示用 CPU 推理。
|
| 30 |
+
|
| 31 |
+
- `--score-thr`:设置检测框的置信度阈值,只有置信度高于这个值的框才会显示。
|
| 32 |
+
|
| 33 |
+
- `--preview-model`:可以预览模型,方便用户理解模型的特征层结构。
|
| 34 |
+
|
| 35 |
+
- `--target-layers`:对指定层获取可视化的特征图。
|
| 36 |
+
|
| 37 |
+
- 可以单独输出某个层的特征图,例如: `--target-layers backbone` , `--target-layers neck` , `--target-layers backbone.stage4` 等。
|
| 38 |
+
- 参数为列表时,也可以同时输出多个层的特征图,例如: `--target-layers backbone.stage4 neck` 表示同时输出 backbone 的 stage4 层和 neck 的三层一共四层特征图。
|
| 39 |
+
|
| 40 |
+
- `--channel-reduction`:输入的 Tensor 一般是包括多个通道的,`channel_reduction` 参数可以将多个通道压缩为单通道,然后和图片进行叠加显示,有以下三个参数可以设置:
|
| 41 |
+
|
| 42 |
+
- `squeeze_mean`:将输入的 C 维度采用 mean 函数压缩为一个通道,输出维度变成 (1, H, W)。
|
| 43 |
+
- `select_max`:将输入先在空间维度 sum,维度变成 (C, ),然后选择值最大的通道。
|
| 44 |
+
- `None`:表示不需要压缩,此时可以通过 `topk` 参数可选择激活度最高的 `topk` 个特征图显示。
|
| 45 |
+
|
| 46 |
+
- `--topk`:只有在 `channel_reduction` 参数为 `None` 的情况下, `topk` 参数才会生效,其会按照激活度排序选择 `topk` 个通道,然后和图片进行叠加显示,并且此时会通过 `--arrangement` 参数指定显示的布局,该参数表示为一个数组,两个数字需要以空格分开,例如: `--topk 5 --arrangement 2 3` 表示以 `2行 3列` 显示激活度排序最高的 5 张特征图, `--topk 7 --arrangement 3 3` 表示以 `3行 3列` 显示激活度排序最高的 7 张特征图。
|
| 47 |
+
|
| 48 |
+
- 如果 topk 不是 -1,则会按照激活度排序选择 topk 个通道显示。
|
| 49 |
+
- 如果 topk = -1,此时通道 C 必须是 1 或者 3 表示输入数据是图片,否则报错提示用户应该设置 `channel_reduction` 来压缩通道。
|
| 50 |
+
|
| 51 |
+
- 考虑到输入的特征图通常非常小,函数默认将特征图进行上采样后方便进行可视化。
|
| 52 |
+
|
| 53 |
+
**注意:当图片和特征图尺度不一样时候,`draw_featmap` 函数会自动进行上采样对齐。如果你的图片在推理过程中前处理存在类似 Pad 的操作此时得到的特征图也是 Pad 过的,那么直接上采样就可能会出现不对齐问题。**
|
| 54 |
+
|
| 55 |
+
### 用法示例
|
| 56 |
+
|
| 57 |
+
以预训练好的 YOLOv5-s 模型为例:
|
| 58 |
+
|
| 59 |
+
请提前下载 YOLOv5-s 模型权重到本仓库根路径下:
|
| 60 |
+
|
| 61 |
+
```shell
|
| 62 |
+
cd mmyolo
|
| 63 |
+
wget https://download.openmmlab.com/mmyolo/v0/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco/yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
(1) 将多通道特征图采用 `select_max` 参数压缩为单通道并显示, 通过提取 `backbone` 层输出进行特征图可视化,将得到 `backbone` 三个输出层的特征图:
|
| 67 |
+
|
| 68 |
+
```shell
|
| 69 |
+
python demo/featmap_vis_demo.py demo/dog.jpg \
|
| 70 |
+
configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py \
|
| 71 |
+
yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth \
|
| 72 |
+
--target-layers backbone \
|
| 73 |
+
--channel-reduction select_max
|
| 74 |
+
```
|
| 75 |
+
|
| 76 |
+
<div align=center>
|
| 77 |
+
<img src="https://user-images.githubusercontent.com/17425982/198520580-c1b24d50-2e90-4ba5-af51-5a7dcb9db945.png" width="800" alt="image"/>
|
| 78 |
+
</div>
|
| 79 |
+
|
| 80 |
+
实际上上述代码存在图片和特征图不对齐问题,解决办法有两个:
|
| 81 |
+
|
| 82 |
+
1. 修改 YOLOv5 配置,让后处理只是简单的 Resize 即可,这对于可视化是没有啥影响的
|
| 83 |
+
|
| 84 |
+
2. 可视化时��图片应该用前处理后的,而不能用前处理前的
|
| 85 |
+
|
| 86 |
+
**为了简单目前这里采用第一种解决办法,后续会采用第二种方案修复,让大家可以不修改配置即可使用**。具体来说是将原先的 `test_pipeline` 替换为仅仅 Resize 版本。
|
| 87 |
+
|
| 88 |
+
旧的 `test_pipeline` 为:
|
| 89 |
+
|
| 90 |
+
```python
|
| 91 |
+
test_pipeline = [
|
| 92 |
+
dict(
|
| 93 |
+
type='LoadImageFromFile'),
|
| 94 |
+
dict(type='YOLOv5KeepRatioResize', scale=img_scale),
|
| 95 |
+
dict(
|
| 96 |
+
type='LetterResize',
|
| 97 |
+
scale=img_scale,
|
| 98 |
+
allow_scale_up=False,
|
| 99 |
+
pad_val=dict(img=114)),
|
| 100 |
+
dict(type='LoadAnnotations', with_bbox=True, _scope_='mmdet'),
|
| 101 |
+
dict(
|
| 102 |
+
type='mmdet.PackDetInputs',
|
| 103 |
+
meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',
|
| 104 |
+
'scale_factor', 'pad_param'))
|
| 105 |
+
]
|
| 106 |
+
```
|
| 107 |
+
|
| 108 |
+
修改为如下配置:
|
| 109 |
+
|
| 110 |
+
```python
|
| 111 |
+
test_pipeline = [
|
| 112 |
+
dict(
|
| 113 |
+
type='LoadImageFromFile',
|
| 114 |
+
backend_args=_base_.backend_args),
|
| 115 |
+
dict(type='mmdet.Resize', scale=img_scale, keep_ratio=False), # 这里将 LetterResize 修改成 mmdet.Resize
|
| 116 |
+
dict(type='LoadAnnotations', with_bbox=True, _scope_='mmdet'),
|
| 117 |
+
dict(
|
| 118 |
+
type='mmdet.PackDetInputs',
|
| 119 |
+
meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',
|
| 120 |
+
'scale_factor'))
|
| 121 |
+
]
|
| 122 |
+
```
|
| 123 |
+
|
| 124 |
+
正确效果如下:
|
| 125 |
+
|
| 126 |
+
<div align=center>
|
| 127 |
+
<img src="https://user-images.githubusercontent.com/17425982/198521116-dbccade4-370c-4051-92bf-923ca8f60f24.png" width="800" alt="image"/>
|
| 128 |
+
</div>
|
| 129 |
+
|
| 130 |
+
(2) 将多通道特征图采用 `squeeze_mean` 参数压缩为单通道并显示, 通过提取 `neck` 层输出进行特征图可视化,将得到 `neck` 三个输出层的特征图:
|
| 131 |
+
|
| 132 |
+
```shell
|
| 133 |
+
python demo/featmap_vis_demo.py demo/dog.jpg \
|
| 134 |
+
configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py \
|
| 135 |
+
yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth \
|
| 136 |
+
--target-layers neck \
|
| 137 |
+
--channel-reduction squeeze_mean
|
| 138 |
+
```
|
| 139 |
+
|
| 140 |
+
<div align=center>
|
| 141 |
+
<img src="https://user-images.githubusercontent.com/17425982/198521267-20202e3d-b1bc-4559-9085-e0af287636c8.png" width="800" alt="image"/>
|
| 142 |
+
</div>
|
| 143 |
+
|
| 144 |
+
(3) 将多通道特征图采用 `squeeze_mean` 参数压缩为单通道并显示, 通过提取 `backbone.stage4` 和 `backbone.stage3` 层输出进行特征图可视化,将得到两个输出层的特征图:
|
| 145 |
+
|
| 146 |
+
```shell
|
| 147 |
+
python demo/featmap_vis_demo.py demo/dog.jpg \
|
| 148 |
+
configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py \
|
| 149 |
+
yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth \
|
| 150 |
+
--target-layers backbone.stage4 backbone.stage3 \
|
| 151 |
+
--channel-reduction squeeze_mean
|
| 152 |
+
```
|
| 153 |
+
|
| 154 |
+
<div align=center>
|
| 155 |
+
<img src="https://user-images.githubusercontent.com/17425982/198522004-c5782807-166a-45f3-96e3-7e6df5dc70ac.png" width="800" alt="image"/>
|
| 156 |
+
</div>
|
| 157 |
+
|
| 158 |
+
(4) 利用 `--topk 3 --arrangement 2 2` 参数选择多通道特征图中激活度最高的 3 个通道并采用 `2x2` 布局显示, 用户可以通过 `arrangement` 参数选择自己想要的布局,特征图将自动布局,先按每个层中的 `top3` 特征图按 `2x2` 的格式布局,再将每个层按 `2x2` 布局:
|
| 159 |
+
|
| 160 |
+
```shell
|
| 161 |
+
python demo/featmap_vis_demo.py demo/dog.jpg \
|
| 162 |
+
configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py \
|
| 163 |
+
yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth \
|
| 164 |
+
--target-layers backbone.stage3 backbone.stage4 \
|
| 165 |
+
--channel-reduction None \
|
| 166 |
+
--topk 3 \
|
| 167 |
+
--arrangement 2 2
|
| 168 |
+
```
|
| 169 |
+
|
| 170 |
+
<div align=center>
|
| 171 |
+
<img src="https://user-images.githubusercontent.com/17425982/198522489-8adee6ae-9915-4e9d-bf50-167b8a12c275.png" width="800" alt="image"/>
|
| 172 |
+
</div>
|
| 173 |
+
|
| 174 |
+
(5) 存储绘制后的图片,在绘制完成后,可以选择本地窗口显示,也可以存储到本地,只需要加入参数 `--out-file xxx.jpg`:
|
| 175 |
+
|
| 176 |
+
```shell
|
| 177 |
+
python demo/featmap_vis_demo.py demo/dog.jpg \
|
| 178 |
+
configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py \
|
| 179 |
+
yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth \
|
| 180 |
+
--target-layers backbone \
|
| 181 |
+
--channel-reduction select_max \
|
| 182 |
+
--out-file featmap_backbone.jpg
|
| 183 |
+
```
|
| 184 |
+
|
| 185 |
+
## Grad-Based 和 Grad-Free CAM 可视化
|
| 186 |
+
|
| 187 |
+
目标检测 CAM 可视化相比于分类 CAM 复杂很多且差异很大。本文只是简要说明用法,后续会单独开文档详细描述实现原理和注意事项。
|
| 188 |
+
|
| 189 |
+
你可以调用 `demo/boxmap_vis_demo.py` 来简单快捷地得到 Box 级别的 AM 可视化结果,目前已经支持 `YOLOv5/YOLOv6/YOLOX/RTMDet`。
|
| 190 |
+
|
| 191 |
+
以 YOLOv5 为例,和特征图可视化绘制一样,��需要先修改 `test_pipeline`,否则会出现特征图和原图不对齐问题。
|
| 192 |
+
|
| 193 |
+
旧的 `test_pipeline` 为:
|
| 194 |
+
|
| 195 |
+
```python
|
| 196 |
+
test_pipeline = [
|
| 197 |
+
dict(
|
| 198 |
+
type='LoadImageFromFile',
|
| 199 |
+
backend_args=_base_.backend_args),
|
| 200 |
+
dict(type='YOLOv5KeepRatioResize', scale=img_scale),
|
| 201 |
+
dict(
|
| 202 |
+
type='LetterResize',
|
| 203 |
+
scale=img_scale,
|
| 204 |
+
allow_scale_up=False,
|
| 205 |
+
pad_val=dict(img=114)),
|
| 206 |
+
dict(type='LoadAnnotations', with_bbox=True, _scope_='mmdet'),
|
| 207 |
+
dict(
|
| 208 |
+
type='mmdet.PackDetInputs',
|
| 209 |
+
meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',
|
| 210 |
+
'scale_factor', 'pad_param'))
|
| 211 |
+
]
|
| 212 |
+
```
|
| 213 |
+
|
| 214 |
+
修改为如下配置:
|
| 215 |
+
|
| 216 |
+
```python
|
| 217 |
+
test_pipeline = [
|
| 218 |
+
dict(
|
| 219 |
+
type='LoadImageFromFile',
|
| 220 |
+
backend_args=_base_.backend_args),
|
| 221 |
+
dict(type='mmdet.Resize', scale=img_scale, keep_ratio=False), # 这里将 LetterResize 修改成 mmdet.Resize
|
| 222 |
+
dict(type='LoadAnnotations', with_bbox=True, _scope_='mmdet'),
|
| 223 |
+
dict(
|
| 224 |
+
type='mmdet.PackDetInputs',
|
| 225 |
+
meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',
|
| 226 |
+
'scale_factor'))
|
| 227 |
+
]
|
| 228 |
+
```
|
| 229 |
+
|
| 230 |
+
(1) 使用 `GradCAM` 方法可视化 neck 模块的最后一个输出层的 AM 图
|
| 231 |
+
|
| 232 |
+
```shell
|
| 233 |
+
python demo/boxam_vis_demo.py \
|
| 234 |
+
demo/dog.jpg \
|
| 235 |
+
configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py \
|
| 236 |
+
yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth
|
| 237 |
+
|
| 238 |
+
```
|
| 239 |
+
|
| 240 |
+
<div align=center>
|
| 241 |
+
<img src="https://user-images.githubusercontent.com/17425982/203775584-c4aebf11-4ff8-4530-85fe-7dda897e95a8.jpg" width="800" alt="image"/>
|
| 242 |
+
</div>
|
| 243 |
+
|
| 244 |
+
相对应的特征图 AM 图如下:
|
| 245 |
+
|
| 246 |
+
<div align=center>
|
| 247 |
+
<img src="https://user-images.githubusercontent.com/17425982/203774801-1555bcfb-a8f9-4688-8ed6-982d6ad38e1d.jpg" width="800" alt="image"/>
|
| 248 |
+
</div>
|
| 249 |
+
|
| 250 |
+
可以看出 `GradCAM` 效果可以突出 box 级别的 AM 信息。
|
| 251 |
+
|
| 252 |
+
你可以通过 `--topk` 参数选择仅仅可视化预测分值最高的前几个预测框
|
| 253 |
+
|
| 254 |
+
```shell
|
| 255 |
+
python demo/boxam_vis_demo.py \
|
| 256 |
+
demo/dog.jpg \
|
| 257 |
+
configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py \
|
| 258 |
+
yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth \
|
| 259 |
+
--topk 2
|
| 260 |
+
```
|
| 261 |
+
|
| 262 |
+
<div align=center>
|
| 263 |
+
<img src="https://user-images.githubusercontent.com/17425982/203778700-3165aa72-ecaf-40cc-b470-6911646e6046.jpg" width="800" alt="image"/>
|
| 264 |
+
</div>
|
| 265 |
+
|
| 266 |
+
(2) 使用 `AblationCAM` 方法可视化 neck 模块的最后一个输出层的 AM 图
|
| 267 |
+
|
| 268 |
+
```shell
|
| 269 |
+
python demo/boxam_vis_demo.py \
|
| 270 |
+
demo/dog.jpg \
|
| 271 |
+
configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py \
|
| 272 |
+
yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth \
|
| 273 |
+
--method ablationcam
|
| 274 |
+
```
|
| 275 |
+
|
| 276 |
+
<div align=center>
|
| 277 |
+
<img src="https://user-images.githubusercontent.com/17425982/203776978-b5a9b383-93b4-4b35-9e6a-7cac684b372c.jpg" width="800" alt="image"/>
|
| 278 |
+
</div>
|
| 279 |
+
|
| 280 |
+
由于 `AblationCAM` 是通过每个通道对分值的贡献程度来加权,因此无法实现类似 `GradCAM` 的仅仅可视化 box 级别的 AM 信息, 但是你可以使用 `--norm-in-bbox` 来仅仅显示 bbox 内部 AM
|
| 281 |
+
|
| 282 |
+
```shell
|
| 283 |
+
python demo/boxam_vis_demo.py \
|
| 284 |
+
demo/dog.jpg \
|
| 285 |
+
configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py \
|
| 286 |
+
yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth \
|
| 287 |
+
--method ablationcam \
|
| 288 |
+
--norm-in-bbox
|
| 289 |
+
```
|
| 290 |
+
|
| 291 |
+
<div align=center>
|
| 292 |
+
<img src="https://user-images.githubusercontent.com/17425982/203777566-7c74e82f-b477-488e-958f-91e1d10833b9.jpg" width="800" alt="image"/>
|
| 293 |
+
</div>
|
| 294 |
+
|
| 295 |
+
## 可视化 COCO 标签
|
| 296 |
+
|
| 297 |
+
脚本 `tools/analysis_tools/browse_coco_json.py` 能够使用可视化显示 COCO 标签在图片的情况。
|
| 298 |
+
|
| 299 |
+
```shell
|
| 300 |
+
python tools/analysis_tools/browse_coco_json.py [--data-root ${DATA_ROOT}] \
|
| 301 |
+
[--img-dir ${IMG_DIR}] \
|
| 302 |
+
[--ann-file ${ANN_FILE}] \
|
| 303 |
+
[--wait-time ${WAIT_TIME}] \
|
| 304 |
+
[--disp-all] [--category-names CATEGORY_NAMES [CATEGORY_NAMES ...]] \
|
| 305 |
+
[--shuffle]
|
| 306 |
+
```
|
| 307 |
+
|
| 308 |
+
其中,如果图片、标签都在同一个文件夹下的话,可以指定 `--data-root` 到该文件夹,然后 `--img-dir` 和 `--ann-file` 指定该文件夹的相对路径,代码会自动拼接。
|
| 309 |
+
如果图片、标签文件不在同一个文件夹下的话,则无需指定 `--data-root` ,直接指定绝对路径的 `--img-dir` 和 `--ann-file` 即可。
|
| 310 |
+
|
| 311 |
+
例子:
|
| 312 |
+
|
| 313 |
+
1. 查看 `COCO` 全部类别,同时展示 `bbox`、`mask` 等所有类型的标注:
|
| 314 |
+
|
| 315 |
+
```shell
|
| 316 |
+
python tools/analysis_tools/browse_coco_json.py --data-root './data/coco' \
|
| 317 |
+
--img-dir 'train2017' \
|
| 318 |
+
--ann-file 'annotations/instances_train2017.json' \
|
| 319 |
+
--disp-all
|
| 320 |
+
```
|
| 321 |
+
|
| 322 |
+
如果图片、标签不在��一个文件夹下的话,可以使用绝对路径:
|
| 323 |
+
|
| 324 |
+
```shell
|
| 325 |
+
python tools/analysis_tools/browse_coco_json.py --img-dir '/dataset/image/coco/train2017' \
|
| 326 |
+
--ann-file '/label/instances_train2017.json' \
|
| 327 |
+
--disp-all
|
| 328 |
+
```
|
| 329 |
+
|
| 330 |
+
2. 查看 `COCO` 全部类别,同时仅展示 `bbox` 类型的标注,并打乱显示:
|
| 331 |
+
|
| 332 |
+
```shell
|
| 333 |
+
python tools/analysis_tools/browse_coco_json.py --data-root './data/coco' \
|
| 334 |
+
--img-dir 'train2017' \
|
| 335 |
+
--ann-file 'annotations/instances_train2017.json' \
|
| 336 |
+
--shuffle
|
| 337 |
+
```
|
| 338 |
+
|
| 339 |
+
3. 只查看 `bicycle` 和 `person` 类别,同时仅展示 `bbox` 类型的标注:
|
| 340 |
+
|
| 341 |
+
```shell
|
| 342 |
+
python tools/analysis_tools/browse_coco_json.py --data-root './data/coco' \
|
| 343 |
+
--img-dir 'train2017' \
|
| 344 |
+
--ann-file 'annotations/instances_train2017.json' \
|
| 345 |
+
--category-names 'bicycle' 'person'
|
| 346 |
+
```
|
| 347 |
+
|
| 348 |
+
4. 查看 `COCO` 全部类别,同时展示 `bbox`、`mask` 等所有类型的标注,并打乱显示:
|
| 349 |
+
|
| 350 |
+
```shell
|
| 351 |
+
python tools/analysis_tools/browse_coco_json.py --data-root './data/coco' \
|
| 352 |
+
--img-dir 'train2017' \
|
| 353 |
+
--ann-file 'annotations/instances_train2017.json' \
|
| 354 |
+
--disp-all \
|
| 355 |
+
--shuffle
|
| 356 |
+
```
|
| 357 |
+
|
| 358 |
+
## 可视化数据集
|
| 359 |
+
|
| 360 |
+
```shell
|
| 361 |
+
python tools/analysis_tools/browse_dataset.py \
|
| 362 |
+
${CONFIG_FILE} \
|
| 363 |
+
[-o, --output-dir ${OUTPUT_DIR}] \
|
| 364 |
+
[-p, --phase ${DATASET_PHASE}] \
|
| 365 |
+
[-n, --show-number ${NUMBER_IMAGES_DISPLAY}] \
|
| 366 |
+
[-i, --show-interval ${SHOW_INTERRVAL}] \
|
| 367 |
+
[-m, --mode ${DISPLAY_MODE}] \
|
| 368 |
+
[--cfg-options ${CFG_OPTIONS}]
|
| 369 |
+
```
|
| 370 |
+
|
| 371 |
+
**所有参数的说明**:
|
| 372 |
+
|
| 373 |
+
- `config` : 模型配置文件的路径。
|
| 374 |
+
- `-o, --output-dir`: 保存图片文件夹,如果没有指定,默认为 `'./output'`。
|
| 375 |
+
- **`-p, --phase`**: 可视化数据集的阶段,只能为 `['train', 'val', 'test']` 之一,默认为 `'train'`。
|
| 376 |
+
- **`-n, --show-number`**: 可视化样本数量。如果没有指定,默认展示数据集的所有图片。
|
| 377 |
+
- **`-m, --mode`**: 可视化的模式,只能为 `['original', 'transformed', 'pipeline']` 之一。 默认为 `'transformed'`。
|
| 378 |
+
- `--cfg-options` : 对配置文件的修改,参考[学习配置文件](../../tutorials/config.md)。
|
| 379 |
+
|
| 380 |
+
```shell
|
| 381 |
+
`-m, --mode` 用于设置可视化的模式,默认设置为 'transformed'。
|
| 382 |
+
- 如果 `--mode` 设置为 'original',则获取原始图片;
|
| 383 |
+
- 如果 `--mode` 设置为 'transformed',则获取预处理后的图片;
|
| 384 |
+
- 如果 `--mode` 设置为 'pipeline',则获得数据流水线所有中间过程图片。
|
| 385 |
+
```
|
| 386 |
+
|
| 387 |
+
**示例**:
|
| 388 |
+
|
| 389 |
+
1. **'original'** 模式 :
|
| 390 |
+
|
| 391 |
+
```shell
|
| 392 |
+
python ./tools/analysis_tools/browse_dataset.py configs/yolov5/yolov5_balloon.py --phase val --output-dir tmp --mode original
|
| 393 |
+
```
|
| 394 |
+
|
| 395 |
+
- `--phase val`: 可视化验证集, 可简化为 `-p val`;
|
| 396 |
+
- `--output-dir tmp`: 可视化结果保存在 "tmp" 文件夹, 可简化为 `-o tmp`;
|
| 397 |
+
- `--mode original`: 可视化原图, 可简化为 `-m original`;
|
| 398 |
+
- `--show-number 100`: 可视化100张图,可简化为 `-n 100`;
|
| 399 |
+
|
| 400 |
+
2.**'transformed'** 模式 :
|
| 401 |
+
|
| 402 |
+
```shell
|
| 403 |
+
python ./tools/analysis_tools/browse_dataset.py configs/yolov5/yolov5_balloon.py
|
| 404 |
+
```
|
| 405 |
+
|
| 406 |
+
3.**'pipeline'** 模式 :
|
| 407 |
+
|
| 408 |
+
```shell
|
| 409 |
+
python ./tools/analysis_tools/browse_dataset.py configs/yolov5/yolov5_balloon.py -m pipeline
|
| 410 |
+
```
|
| 411 |
+
|
| 412 |
+
<div align=center>
|
| 413 |
+
<img src="https://user-images.githubusercontent.com/45811724/204810831-0fbc7f1c-0951-4be1-a11c-491cf0d194f6.png" alt="Image">
|
| 414 |
+
</div>
|
| 415 |
+
|
| 416 |
+
## 可视化数据集分析
|
| 417 |
+
|
| 418 |
+
脚本 `tools/analysis_tools/dataset_analysis.py` 能够帮助用户得到四种功能的结果图,并将图片保存到当前运行目录下的 `dataset_analysis` 文件夹中。
|
| 419 |
+
|
| 420 |
+
关于该脚本的功能的说明:
|
| 421 |
+
|
| 422 |
+
通过 `main()` 的数据准备,得到每个子函数所需要的数据。
|
| 423 |
+
|
| 424 |
+
功能一:显示类别和 bbox 实例个数的分布图,通过子函数 `show_bbox_num` 生成。
|
| 425 |
+
|
| 426 |
+
<img src="https://user-images.githubusercontent.com/90811472/200314770-4fb21626-72f2-4a4c-be5d-bf860ad830ec.jpg"/>
|
| 427 |
+
|
| 428 |
+
功能二:显示类别和 bbox 实例宽、高的分布图,通过子函数 `show_bbox_wh` 生成。
|
| 429 |
+
|
| 430 |
+
<img src="https://user-images.githubusercontent.com/90811472/200315007-96e8e795-992a-4c72-90fa-f6bc00b3f2c7.jpg"/>
|
| 431 |
+
|
| 432 |
+
功能三:显示类别和 bbox 实例宽/高比例的分布图,通过子函数 `show_bbox_wh_ratio` 生成。
|
| 433 |
+
|
| 434 |
+
<img src="https://user-images.githubusercontent.com/90811472/200315044-4bdedcf6-087a-418e-8fe8-c2d3240ceba8.jpg"/>
|
| 435 |
+
|
| 436 |
+
功能四:基于面积规则下,显示类别和 bbox 实例面积的分布图,通过子函数 `show_bbox_area` 生成。
|
| 437 |
+
|
| 438 |
+
<img src="https://user-images.githubusercontent.com/90811472/200315075-71680fe2-db6f-4981-963e-a035c1281fc1.jpg"/>
|
| 439 |
+
|
| 440 |
+
打印列表显示,通过脚本中子函数 `show_class_list` 和 `show_data_list` 生成。
|
| 441 |
+
|
| 442 |
+
<img src="https://user-images.githubusercontent.com/90811472/200315152-9d6df91c-f2d2-4bba-9f95-b790fac37b62.jpg"/>
|
| 443 |
+
|
| 444 |
+
```shell
|
| 445 |
+
python tools/analysis_tools/dataset_analysis.py ${CONFIG} \
|
| 446 |
+
[-h] \
|
| 447 |
+
[--val-dataset ${TYPE}] \
|
| 448 |
+
[--class-name ${CLASS_NAME}] \
|
| 449 |
+
[--area-rule ${AREA_RULE}] \
|
| 450 |
+
[--func ${FUNC}] \
|
| 451 |
+
[--out-dir ${OUT_DIR}]
|
| 452 |
+
```
|
| 453 |
+
|
| 454 |
+
例子:
|
| 455 |
+
|
| 456 |
+
1. 使用 `config` 文件 `configs/yolov5/voc/yolov5_s-v61_fast_1xb64-50e_voc.py` 分析数据集,其中默认设置:数据加载类型为 `train_dataset` ,面积规则设置为 `[0,32,96,1e5]` ,生成包含所有类的结果图并将图片保存到当前运行目录下 `./dataset_analysis` 文件夹中:
|
| 457 |
+
|
| 458 |
+
```shell
|
| 459 |
+
python tools/analysis_tools/dataset_analysis.py configs/yolov5/voc/yolov5_s-v61_fast_1xb64-50e_voc.py
|
| 460 |
+
```
|
| 461 |
+
|
| 462 |
+
2. 使用 `config` 文件 `configs/yolov5/voc/yolov5_s-v61_fast_1xb64-50e_voc.py` 分析数据集,通过 `--val-dataset` 设置将数据加载类型由默认的 `train_dataset` 改为 `val_dataset`:
|
| 463 |
+
|
| 464 |
+
```shell
|
| 465 |
+
python tools/analysis_tools/dataset_analysis.py configs/yolov5/voc/yolov5_s-v61_fast_1xb64-50e_voc.py \
|
| 466 |
+
--val-dataset
|
| 467 |
+
```
|
| 468 |
+
|
| 469 |
+
3. 使用 `config` 文件 `configs/yolov5/voc/yolov5_s-v61_fast_1xb64-50e_voc.py` 分析数据集,通过 `--class-name` 设置将生成所有类改为特定类显示,以显示 `person` 为例:
|
| 470 |
+
|
| 471 |
+
```shell
|
| 472 |
+
python tools/analysis_tools/dataset_analysis.py configs/yolov5/voc/yolov5_s-v61_fast_1xb64-50e_voc.py \
|
| 473 |
+
--class-name person
|
| 474 |
+
```
|
| 475 |
+
|
| 476 |
+
4. 使用 `config` 文件 `configs/yolov5/voc/yolov5_s-v61_fast_1xb64-50e_voc.py` 分析数据集,通过 `--area-rule` 重新定义面积规则,以 `30 70 125` 为例,面积规则变为 `[0,30,70,125,1e5]`:
|
| 477 |
+
|
| 478 |
+
```shell
|
| 479 |
+
python tools/analysis_tools/dataset_analysis.py configs/yolov5/voc/yolov5_s-v61_fast_1xb64-50e_voc.py \
|
| 480 |
+
--area-rule 30 70 125
|
| 481 |
+
```
|
| 482 |
+
|
| 483 |
+
5. 使用 `config` 文件 `configs/yolov5/voc/yolov5_s-v61_fast_1xb64-50e_voc.py` 分析数据集,通过 `--func` 设置,将显示四个功能效果图改为只显示 `功能一` 为例:
|
| 484 |
+
|
| 485 |
+
```shell
|
| 486 |
+
python tools/analysis_tools/dataset_analysis.py configs/yolov5/voc/yolov5_s-v61_fast_1xb64-50e_voc.py \
|
| 487 |
+
--func show_bbox_num
|
| 488 |
+
```
|
| 489 |
+
|
| 490 |
+
6. 使用 `config` 文件 `configs/yolov5/voc/yolov5_s-v61_fast_1xb64-50e_voc.py` 分析数据集,通过 `--out-dir` 设置修改图片保存地址,以 `work_dirs/dataset_analysis` 地址为例:
|
| 491 |
+
|
| 492 |
+
```shell
|
| 493 |
+
python tools/analysis_tools/dataset_analysis.py configs/yolov5/voc/yolov5_s-v61_fast_1xb64-50e_voc.py \
|
| 494 |
+
--out-dir work_dirs/dataset_analysis
|
| 495 |
+
```
|
| 496 |
+
|
| 497 |
+
## 优化器参数策略可视化
|
| 498 |
+
|
| 499 |
+
`tools/analysis_tools/vis_scheduler.py` 旨在帮助用户检查优化器的超参数调度器(无需训练),支持学习率(learning rate)、动量(momentum)和权值衰减(weight decay)。
|
| 500 |
+
|
| 501 |
+
```shell
|
| 502 |
+
python tools/analysis_tools/vis_scheduler.py \
|
| 503 |
+
${CONFIG_FILE} \
|
| 504 |
+
[-p, --parameter ${PARAMETER_NAME}] \
|
| 505 |
+
[-d, --dataset-size ${DATASET_SIZE}] \
|
| 506 |
+
[-n, --ngpus ${NUM_GPUs}] \
|
| 507 |
+
[-o, --out-dir ${OUT_DIR}] \
|
| 508 |
+
[--title ${TITLE}] \
|
| 509 |
+
[--style ${STYLE}] \
|
| 510 |
+
[--window-size ${WINDOW_SIZE}] \
|
| 511 |
+
[--cfg-options]
|
| 512 |
+
```
|
| 513 |
+
|
| 514 |
+
**所有参数的说明**:
|
| 515 |
+
|
| 516 |
+
- `config` : 模型配置文件的路径。
|
| 517 |
+
- **`-p, parameter`**: 可视化参数名,只能为 `["lr", "momentum", "wd"]` 之一, 默认为 `"lr"`.
|
| 518 |
+
- **`-d, --dataset-size`**: 数据集的大小。如果指定,`DATASETS.build` 将被跳过并使用这个数值作为数据集大小,默认使用 `DATASETS.build` 所得数据集的大小。
|
| 519 |
+
- **`-n, --ngpus`**: 使用 GPU 的数量, 默认为1。
|
| 520 |
+
- **`-o, --out-dir`**: 保存的可视化图片的文件夹路径,默认不保存。
|
| 521 |
+
- `--title`: 可视化图片的标题,默认为配置文件名。
|
| 522 |
+
- `--style`: 可视化图片的风格,默认为 `whitegrid`。
|
| 523 |
+
- `--window-size`: 可视化窗口大小,如果没有指定,默认为 `12*7`。如果需要指定,按照格式 `'W*H'`。
|
| 524 |
+
- `--cfg-options`: 对配置文件的修改,参考[学习配置文件](../tutorials/config.md)。
|
| 525 |
+
|
| 526 |
+
```{note}
|
| 527 |
+
部分数据集在解析标注阶段比较耗时,推荐直接将 `-d, dataset-size` 指定数据集的大小,以节约时间。
|
| 528 |
+
```
|
| 529 |
+
|
| 530 |
+
你可以使用如下命令来绘制配置文件 `configs/rtmdet/rtmdet_s_syncbn_fast_8xb32-300e_coco.py` 将会使用的学习率变化曲线:
|
| 531 |
+
|
| 532 |
+
```shell
|
| 533 |
+
python tools/analysis_tools/vis_scheduler.py \
|
| 534 |
+
configs/rtmdet/rtmdet_s_syncbn_fast_8xb32-300e_coco.py \
|
| 535 |
+
--dataset-size 118287 \
|
| 536 |
+
--ngpus 8 \
|
| 537 |
+
--out-dir ./output
|
| 538 |
+
```
|
| 539 |
+
|
| 540 |
+
<div align=center><img src="https://user-images.githubusercontent.com/27466624/213091635-d322d2b3-6e28-4755-b871-ef0a89a67a6b.jpg" style=" width: auto; height: 40%; "></div>
|
| 541 |
+
|
| 542 |
+
## 大图推理 (TODO)
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/stat.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
import functools as func
|
| 3 |
+
import glob
|
| 4 |
+
import os.path as osp
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
url_prefix = 'https://github.com/open-mmlab/mmyolo/blob/main/'
|
| 10 |
+
|
| 11 |
+
files = sorted(glob.glob('../configs/*/README.md'))
|
| 12 |
+
|
| 13 |
+
stats = []
|
| 14 |
+
titles = []
|
| 15 |
+
num_ckpts = 0
|
| 16 |
+
|
| 17 |
+
for f in files:
|
| 18 |
+
url = osp.dirname(f.replace('../', url_prefix))
|
| 19 |
+
|
| 20 |
+
with open(f) as content_file:
|
| 21 |
+
content = content_file.read()
|
| 22 |
+
|
| 23 |
+
title = content.split('\n')[0].replace('# ', '').strip()
|
| 24 |
+
ckpts = {
|
| 25 |
+
x.lower().strip()
|
| 26 |
+
for x in re.findall(r'\[model\]\((https?.*)\)', content)
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
if len(ckpts) == 0:
|
| 30 |
+
continue
|
| 31 |
+
|
| 32 |
+
_papertype = [x for x in re.findall(r'\[([A-Z]+)\]', content)]
|
| 33 |
+
assert len(_papertype) > 0
|
| 34 |
+
papertype = _papertype[0]
|
| 35 |
+
|
| 36 |
+
paper = {(papertype, title)}
|
| 37 |
+
|
| 38 |
+
titles.append(title)
|
| 39 |
+
num_ckpts += len(ckpts)
|
| 40 |
+
|
| 41 |
+
statsmsg = f"""
|
| 42 |
+
\t* [{papertype}] [{title}]({url}) ({len(ckpts)} ckpts)
|
| 43 |
+
"""
|
| 44 |
+
stats.append((paper, ckpts, statsmsg))
|
| 45 |
+
|
| 46 |
+
allpapers = func.reduce(lambda a, b: a.union(b), [p for p, _, _ in stats])
|
| 47 |
+
msglist = '\n'.join(x for _, _, x in stats)
|
| 48 |
+
|
| 49 |
+
papertypes, papercounts = np.unique([t for t, _ in allpapers],
|
| 50 |
+
return_counts=True)
|
| 51 |
+
countstr = '\n'.join(
|
| 52 |
+
[f' - {t}: {c}' for t, c in zip(papertypes, papercounts)])
|
| 53 |
+
|
| 54 |
+
modelzoo = f"""
|
| 55 |
+
# Model Zoo Statistics
|
| 56 |
+
|
| 57 |
+
* Number of papers: {len(set(titles))}
|
| 58 |
+
{countstr}
|
| 59 |
+
|
| 60 |
+
* Number of checkpoints: {num_ckpts}
|
| 61 |
+
|
| 62 |
+
{msglist}
|
| 63 |
+
"""
|
| 64 |
+
|
| 65 |
+
with open('modelzoo_statistics.md', 'w') as f:
|
| 66 |
+
f.write(modelzoo)
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/switch_language.md
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
## <a href='https://mmyolo.readthedocs.io/en/latest/'>English</a>
|
| 2 |
+
|
| 3 |
+
## <a href='https://mmyolo.readthedocs.io/zh_CN/latest/'>简体中文</a>
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/tutorials/config.md
ADDED
|
@@ -0,0 +1,553 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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Engine 的配置文件系统](https://mmengine.readthedocs.io/zh_cn/latest/tutorials/config.md)。 配置文件使用了模块化和继承设计,以便于进行各类实验。
|
| 4 |
+
|
| 5 |
+
## 配置文件的内容
|
| 6 |
+
|
| 7 |
+
MMYOLO 采用模块化设计,所有功能的模块都可以通过配置文件进行配置。 以 [yolov5_s-v61_syncbn_8xb16-300e_coco.py](https://github.com/open-mmlab/mmyolo/blob/main/configs/yolov5/yolov5_s-v61_syncbn_8xb16-300e_coco.py) 为例,我们将根据不同的功能模块介绍配置文件中的各个字段:
|
| 8 |
+
|
| 9 |
+
### 重要参数
|
| 10 |
+
|
| 11 |
+
如下参数是修改训练配置时经常需要修改的参数。例如缩放因子 `deepen_factor` 和 `widen_factor`,MMYOLO 中的网络基本都使用它们来控制模型的大小。所以我们推荐在配置文件中单独定义这些参数。
|
| 12 |
+
|
| 13 |
+
```python
|
| 14 |
+
img_scale = (640, 640) # 高度,宽度
|
| 15 |
+
deepen_factor = 0.33 # 控制网络结构深度的缩放因子,YOLOv5-s 为 0.33
|
| 16 |
+
widen_factor = 0.5 # 控制网络结构宽度的缩放因子,YOLOv5-s 为 0.5
|
| 17 |
+
max_epochs = 300 # 最大训练轮次 300 轮
|
| 18 |
+
save_epoch_intervals = 10 # 验证间隔,每 10 个 epoch 验证一次
|
| 19 |
+
train_batch_size_per_gpu = 16 # 训练时单个 GPU 的 Batch size
|
| 20 |
+
train_num_workers = 8 # 训练时单个 GPU 分配的数据加载线程数
|
| 21 |
+
val_batch_size_per_gpu = 1 # 验证时单个 GPU 的 Batch size
|
| 22 |
+
val_num_workers = 2 # 验证时单个 GPU 分配的数据加载线程数
|
| 23 |
+
```
|
| 24 |
+
|
| 25 |
+
### 模型配置
|
| 26 |
+
|
| 27 |
+
在 MMYOLO 的配置中,我们使用 `model` 字段来配置检测算法的组件。 除了 `backbone`、`neck` 等神经网络组件外,还需要 `data_preprocessor`、`train_cfg` 和 `test_cfg`。 `data_preprocessor` 负责对 dataloader 输出的每一批数据进行预处理。 模型配置中的 `train_cfg` 和 `test_cfg` 用于设置训练和测试组件的超参数。
|
| 28 |
+
|
| 29 |
+
```python
|
| 30 |
+
anchors = [[(10, 13), (16, 30), (33, 23)], # 多尺度的先验框基本尺寸
|
| 31 |
+
[(30, 61), (62, 45), (59, 119)],
|
| 32 |
+
[(116, 90), (156, 198), (373, 326)]]
|
| 33 |
+
strides = [8, 16, 32] # 先验框生成器的步幅
|
| 34 |
+
|
| 35 |
+
model = dict(
|
| 36 |
+
type='YOLODetector', #检测器名
|
| 37 |
+
data_preprocessor=dict( # 数据预处理器的配置,通常包括图像归一化和 padding
|
| 38 |
+
type='mmdet.DetDataPreprocessor', # 数据预处理器的类型,还可以选择 'YOLOv5DetDataPreprocessor' 训练速度更快
|
| 39 |
+
mean=[0., 0., 0.], # 用于预训练骨干网络的图像归一化通道均值,按 R、G、B 排序
|
| 40 |
+
std=[255., 255., 255.], # 用于预训练骨干网络的图像归一化通道标准差,按 R、G、B 排序
|
| 41 |
+
bgr_to_rgb=True), # 是否将图像通道从 BGR 转为 RGB
|
| 42 |
+
backbone=dict( # 主干网络的配置文件
|
| 43 |
+
type='YOLOv5CSPDarknet', # 主干网络的类别,目前可选用 'YOLOv5CSPDarknet', 'YOLOv6EfficientRep', 'YOLOXCSPDarknet' 3种
|
| 44 |
+
deepen_factor=deepen_factor, # 控制网络结构深度的缩放因子
|
| 45 |
+
widen_factor=widen_factor, # 控制网络结构宽度的缩放因子
|
| 46 |
+
norm_cfg=dict(type='BN', momentum=0.03, eps=0.001), # 归一化层(norm layer)的配置项
|
| 47 |
+
act_cfg=dict(type='SiLU', inplace=True)), # 激活函数(activation function)的配置项
|
| 48 |
+
neck=dict(
|
| 49 |
+
type='YOLOv5PAFPN', # 检测器的 neck 是 YOLOv5FPN,我们同样支持 'YOLOv6RepPAFPN', 'YOLOXPAFPN'
|
| 50 |
+
deepen_factor=deepen_factor, # 控制网络结构深度的缩放因子
|
| 51 |
+
widen_factor=widen_factor, # 控制网络结构宽度的缩放因子
|
| 52 |
+
in_channels=[256, 512, 1024], # 输入通道数,与 Backbone 的输出通道一致
|
| 53 |
+
out_channels=[256, 512, 1024], # 输出通道数,与 Head 的输入通道一致
|
| 54 |
+
num_csp_blocks=3, # CSPLayer 中 bottlenecks 的数量
|
| 55 |
+
norm_cfg=dict(type='BN', momentum=0.03, eps=0.001), # 归一化层(norm layer)的配置项
|
| 56 |
+
act_cfg=dict(type='SiLU', inplace=True)), # 激活函数(activation function)的配置项
|
| 57 |
+
bbox_head=dict(
|
| 58 |
+
type='YOLOv5Head', # bbox_head 的类型是 'YOLOv5Head', 我们目前也支持 'YOLOv6Head', 'YOLOXHead'
|
| 59 |
+
head_module=dict(
|
| 60 |
+
type='YOLOv5HeadModule', # head_module 的类型是 'YOLOv5HeadModule', 我们目前也支持 'YOLOv6HeadModule', 'YOLOXHeadModule'
|
| 61 |
+
num_classes=80, # 分类的类别数量
|
| 62 |
+
in_channels=[256, 512, 1024], # 输入通道数,与 Neck 的输出通道一致
|
| 63 |
+
widen_factor=widen_factor, # 控制网络结构宽度的缩放因子
|
| 64 |
+
featmap_strides=[8, 16, 32], # 多尺度特征图的步幅
|
| 65 |
+
num_base_priors=3), # 在一个点上,先验框的数量
|
| 66 |
+
prior_generator=dict( # 先验框(prior)生成器的配置
|
| 67 |
+
type='mmdet.YOLOAnchorGenerator', # 先验框生成器的类型是 mmdet 中的 'YOLOAnchorGenerator'
|
| 68 |
+
base_sizes=anchors, # 多尺度的先验框基本尺寸
|
| 69 |
+
strides=strides), # 先验框生成器的步幅, 与 FPN 特征步幅一致。如果未设置 base_sizes,则当前步幅值将被视为 base_sizes。
|
| 70 |
+
),
|
| 71 |
+
test_cfg=dict(
|
| 72 |
+
multi_label=True, # 对于多类别预测来说是否考虑多标签,默认设置为 True
|
| 73 |
+
nms_pre=30000, # NMS 前保留的最大检测框数目
|
| 74 |
+
score_thr=0.001, # 过滤类别的分值,低于 score_thr 的检测框当做背景处理
|
| 75 |
+
nms=dict(type='nms', # NMS 的类型
|
| 76 |
+
iou_threshold=0.65), # NMS 的阈值
|
| 77 |
+
max_per_img=300)) # 每张图像 NMS 后保留的最大检测框数目
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
### 数据集和评测器配置
|
| 81 |
+
|
| 82 |
+
在使用 [执行器](https://mmengine.readthedocs.io/zh_CN/latest/tutorials/runner.html) 进行训练、测试、验证时,我们需要配置 [Dataloader](https://pytorch.org/docs/stable/data.html?highlight=data%20loader#torch.utils.data.DataLoader) 。构建数据 dataloader 需要设置数据集(dataset)和数据处理流程(data pipeline)。 由于这部分的配置较为复杂,我们使用中间变量来简化 dataloader 配置的编写。由于 MMYOLO 中各类轻量目标检测算法使用了更加复杂的数据增强方法,因此会比 MMDetection 中的其他模型拥有更多样的数据集配置。
|
| 83 |
+
|
| 84 |
+
YOLOv5 的训练与测试的数据流存在一定差异,这里我们分别进行介绍。
|
| 85 |
+
|
| 86 |
+
```python
|
| 87 |
+
dataset_type = 'CocoDataset' # 数据集类型,这将被用来定义数据集
|
| 88 |
+
data_root = 'data/coco/' # 数据的根路径
|
| 89 |
+
|
| 90 |
+
pre_transform = [ # 训练数据读取流程
|
| 91 |
+
dict(
|
| 92 |
+
type='LoadImageFromFile'), # 第 1 个流程,从文件路径里加载图像
|
| 93 |
+
dict(type='LoadAnnotations', # 第 2 个流程,对于当前图像,加载它的注释信息
|
| 94 |
+
with_bbox=True) # 是否使用标注框(bounding box),目标检测需要设置为 True
|
| 95 |
+
]
|
| 96 |
+
|
| 97 |
+
albu_train_transforms = [ # YOLOv5-v6.1 仓库中,引入了 Albumentation 代码库进行图像的数据增广, 请确保其版本为 1.0.+
|
| 98 |
+
dict(type='Blur', p=0.01), # 图像模糊,模糊概率 0.01
|
| 99 |
+
dict(type='MedianBlur', p=0.01), # 均值模糊,模糊概率 0.01
|
| 100 |
+
dict(type='ToGray', p=0.01), # 随机转换为灰度图像,转灰度概率 0.01
|
| 101 |
+
dict(type='CLAHE', p=0.01) # CLAHE(限制对比度自适应直方图均衡化) 图像增强方法,直方图均衡化概率 0.01
|
| 102 |
+
]
|
| 103 |
+
train_pipeline = [ # 训练数据处理流程
|
| 104 |
+
*pre_transform, # 引入前述定义的训练数据读取流程
|
| 105 |
+
dict(
|
| 106 |
+
type='Mosaic', # Mosaic 数据增强方法
|
| 107 |
+
img_scale=img_scale, # Mosaic 数据增强后的图像尺寸
|
| 108 |
+
pad_val=114.0, # 空区域填充像素值
|
| 109 |
+
pre_transform=pre_transform), # 之前创建的 pre_transform 训练数据读取流程
|
| 110 |
+
dict(
|
| 111 |
+
type='YOLOv5RandomAffine', # YOLOv5 的随机仿射变换
|
| 112 |
+
max_rotate_degree=0.0, # 最大旋转角度
|
| 113 |
+
max_shear_degree=0.0, # 最大错切角度
|
| 114 |
+
scaling_ratio_range=(0.5, 1.5), # 图像缩放系数的范围
|
| 115 |
+
border=(-img_scale[0] // 2, -img_scale[1] // 2), # 从输入图像的高度和宽度两侧调整输出形状的距离
|
| 116 |
+
border_val=(114, 114, 114)), # 边界区域填充像素值
|
| 117 |
+
dict(
|
| 118 |
+
type='mmdet.Albu', # mmdet 中的 Albumentation 数据增强
|
| 119 |
+
transforms=albu_train_transforms, # 之前创建的 albu_train_transforms 数据增强流程
|
| 120 |
+
bbox_params=dict(
|
| 121 |
+
type='BboxParams',
|
| 122 |
+
format='pascal_voc',
|
| 123 |
+
label_fields=['gt_bboxes_labels', 'gt_ignore_flags']),
|
| 124 |
+
keymap={
|
| 125 |
+
'img': 'image',
|
| 126 |
+
'gt_bboxes': 'bboxes'
|
| 127 |
+
}),
|
| 128 |
+
dict(type='YOLOv5HSVRandomAug'), # HSV通道随机增强
|
| 129 |
+
dict(type='mmdet.RandomFlip', prob=0.5), # 随机翻转,翻转概率 0.5
|
| 130 |
+
dict(
|
| 131 |
+
type='mmdet.PackDetInputs', # 将数据转换为检测器输入格式的流程
|
| 132 |
+
meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'flip',
|
| 133 |
+
'flip_direction'))
|
| 134 |
+
]
|
| 135 |
+
train_dataloader = dict( # 训练 dataloader 配置
|
| 136 |
+
batch_size=train_batch_size_per_gpu, # 训练时单个 GPU 的 Batch size
|
| 137 |
+
num_workers=train_num_workers, # 训练时单个 GPU 分配的数据加载线程数
|
| 138 |
+
persistent_workers=True, # 如果设置为 True,dataloader 在迭代完一轮之后不会关闭数据读取的子进程,可以加速训练
|
| 139 |
+
pin_memory=True, # 开启锁页内存,节省 CPU 内存拷贝时间
|
| 140 |
+
sampler=dict( # 训练数据的采样器
|
| 141 |
+
type='DefaultSampler', # 默认的采样器,同时支持分布式和非分布式训练。请参考 https://github.com/open-mmlab/mmengine/blob/main/mmengine/dataset/sampler.py
|
| 142 |
+
shuffle=True), # 随机打乱每个轮次训练数据的顺序
|
| 143 |
+
dataset=dict( # 训练数据集的配置
|
| 144 |
+
type=dataset_type,
|
| 145 |
+
data_root=data_root,
|
| 146 |
+
ann_file='annotations/instances_train2017.json', # 标注文件路径
|
| 147 |
+
data_prefix=dict(img='train2017/'), # 图像路径前缀
|
| 148 |
+
filter_cfg=dict(filter_empty_gt=False, min_size=32), # 图像和标注的过滤配置
|
| 149 |
+
pipeline=train_pipeline)) # 这是由之前创建的 train_pipeline 定义的数据处理流程
|
| 150 |
+
```
|
| 151 |
+
|
| 152 |
+
YOLOv5 测试阶段采用 [Letter Resize](https://github.com/open-mmlab/mmyolo/blob/main/mmyolo/datasets/transforms/transforms.py#L116) 的方法来将所有的测试图像统一到相同尺度,进而有效保留了图像的长宽比。因此我们在验证和评测时,都采用相同的数据流进行推理。
|
| 153 |
+
|
| 154 |
+
```python
|
| 155 |
+
test_pipeline = [ # 测试数据处理流程
|
| 156 |
+
dict(
|
| 157 |
+
type='LoadImageFromFile'), # 第 1 个流程,从文件路径里加载图像
|
| 158 |
+
dict(type='YOLOv5KeepRatioResize', # 第 2 个流程,保持长宽比的图像大小缩放
|
| 159 |
+
scale=img_scale), # 图像缩放的目标尺寸
|
| 160 |
+
dict(
|
| 161 |
+
type='LetterResize', # 第 3 个流程,满足多种步幅要求的图像大小缩放
|
| 162 |
+
scale=img_scale, # 图像缩放的目标尺寸
|
| 163 |
+
allow_scale_up=False, # 当 ratio > 1 时,是否允许放大图像,
|
| 164 |
+
pad_val=dict(img=114)), # 空区域填充像素值
|
| 165 |
+
dict(type='LoadAnnotations', with_bbox=True), # 第 4 个流程,对于当前图像,加载它的注释信息
|
| 166 |
+
dict(
|
| 167 |
+
type='mmdet.PackDetInputs', # 将数据转换为检测器输入格式的流程
|
| 168 |
+
meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',
|
| 169 |
+
'scale_factor', 'pad_param'))
|
| 170 |
+
]
|
| 171 |
+
|
| 172 |
+
val_dataloader = dict(
|
| 173 |
+
batch_size=val_batch_size_per_gpu, # 验证时单个 GPU 的 Batch size
|
| 174 |
+
num_workers=val_num_workers, # 验证时单个 GPU 分配的数据加载线程数
|
| 175 |
+
persistent_workers=True, # 如果设置为 True,dataloader 在迭代完一轮之后不会关闭数据读取的子进程,可以加速训练
|
| 176 |
+
pin_memory=True, # 开启锁页内存,节省 CPU 内存拷贝时间
|
| 177 |
+
drop_last=False, # 是否丢弃最后未能组成一个批次的数据
|
| 178 |
+
sampler=dict(
|
| 179 |
+
type='DefaultSampler', # 默认的采样器,同时支持分布式和非分布式训练
|
| 180 |
+
shuffle=False), # 验证和测试时不打乱数据顺序
|
| 181 |
+
dataset=dict(
|
| 182 |
+
type=dataset_type,
|
| 183 |
+
data_root=data_root,
|
| 184 |
+
test_mode=True, # 开启测试模式,避免数据集过滤图像和标注
|
| 185 |
+
data_prefix=dict(img='val2017/'), # 图像路径前缀
|
| 186 |
+
ann_file='annotations/instances_val2017.json', # 标注文件路径
|
| 187 |
+
pipeline=test_pipeline, # 这是由之前创建的 test_pipeline 定义的数据处理流程
|
| 188 |
+
batch_shapes_cfg=dict( # batch shapes 配置
|
| 189 |
+
type='BatchShapePolicy', # 确保在 batch 推理过程中同一个 batch 内的图像 pad 像素最少,不要求整个验证过程中所有 batch 的图像尺度一样
|
| 190 |
+
batch_size=val_batch_size_per_gpu, # batch shapes 策略的 batch size,等于验证时单个 GPU 的 Batch size
|
| 191 |
+
img_size=img_scale[0], # 图像的尺寸
|
| 192 |
+
size_divisor=32, # padding 后的图像的大小应该可以被 pad_size_divisor 整除
|
| 193 |
+
extra_pad_ratio=0.5))) # 额外需要 pad 的像素比例
|
| 194 |
+
|
| 195 |
+
test_dataloader = val_dataloader
|
| 196 |
+
```
|
| 197 |
+
|
| 198 |
+
[评测器](https://mmengine.readthedocs.io/zh_CN/latest/tutorials/evaluation.html) 用于计算训练模型在验证和测试数据集上的指标。评测器的配置由一个或一组评价指标(Metric)配置组成:
|
| 199 |
+
|
| 200 |
+
```python
|
| 201 |
+
val_evaluator = dict( # 验证过程使用的评测器
|
| 202 |
+
type='mmdet.CocoMetric', # 用于评估检测的 AR、AP 和 mAP 的 coco 评价指标
|
| 203 |
+
proposal_nums=(100, 1, 10), # 用于评估检测任务时,选取的Proposal数量
|
| 204 |
+
ann_file=data_root + 'annotations/instances_val2017.json', # 标注文件路径
|
| 205 |
+
metric='bbox', # 需要计算的评价指标,`bbox` 用于检测
|
| 206 |
+
)
|
| 207 |
+
test_evaluator = val_evaluator # 测试过程使用的评测器
|
| 208 |
+
```
|
| 209 |
+
|
| 210 |
+
由于测试数据集没有标注文件,因此 MMYOLO 中的 `test_dataloader` 和 `test_evaluator` 配置通常等于 `val`。 如果要保存在测试数据集上的检测结果,则可以像这样编写配置:
|
| 211 |
+
|
| 212 |
+
```python
|
| 213 |
+
# 在测试集上推理,
|
| 214 |
+
# 并将检测结果转换格式以用于提交结果
|
| 215 |
+
test_dataloader = dict(
|
| 216 |
+
batch_size=1,
|
| 217 |
+
num_workers=2,
|
| 218 |
+
persistent_workers=True,
|
| 219 |
+
drop_last=False,
|
| 220 |
+
sampler=dict(type='DefaultSampler', shuffle=False),
|
| 221 |
+
dataset=dict(
|
| 222 |
+
type=dataset_type,
|
| 223 |
+
data_root=data_root,
|
| 224 |
+
ann_file=data_root + 'annotations/image_info_test-dev2017.json',
|
| 225 |
+
data_prefix=dict(img='test2017/'),
|
| 226 |
+
test_mode=True,
|
| 227 |
+
pipeline=test_pipeline))
|
| 228 |
+
test_evaluator = dict(
|
| 229 |
+
type='mmdet.CocoMetric',
|
| 230 |
+
ann_file=data_root + 'annotations/image_info_test-dev2017.json',
|
| 231 |
+
metric='bbox',
|
| 232 |
+
format_only=True, # 只将模型输出转换为coco的 JSON 格式并保存
|
| 233 |
+
outfile_prefix='./work_dirs/coco_detection/test') # 要保存的 JSON 文件的前缀
|
| 234 |
+
```
|
| 235 |
+
|
| 236 |
+
### 训练和测试的配置
|
| 237 |
+
|
| 238 |
+
MMEngine 的 Runner 使用 Loop 来控制训练,验证和测试过程。
|
| 239 |
+
用户可以使用这些字段设置最大训练轮次和验证间隔。
|
| 240 |
+
|
| 241 |
+
```python
|
| 242 |
+
max_epochs = 300 # 最大训练轮次 300 轮
|
| 243 |
+
save_epoch_intervals = 10 # 验证间隔,每 10 轮验证一次
|
| 244 |
+
|
| 245 |
+
train_cfg = dict(
|
| 246 |
+
type='EpochBasedTrainLoop', # 训练循环的类型,请参考 https://github.com/open-mmlab/mmengine/blob/main/mmengine/runner/loops.py
|
| 247 |
+
max_epochs=max_epochs, # 最大训练轮次 300 轮
|
| 248 |
+
val_interval=save_epoch_intervals) # 验证间隔,每 10 个 epoch 验证一次
|
| 249 |
+
val_cfg = dict(type='ValLoop') # 验证循环的类型
|
| 250 |
+
test_cfg = dict(type='TestLoop') # 测试循环的类型
|
| 251 |
+
```
|
| 252 |
+
|
| 253 |
+
MMEngine 也支持动态评估间隔,例如你可以在前面 280 epoch 训练阶段中,每间隔 10 个 epoch 验证一次,到最后 20 epoch 训练中每隔 1 个 epoch 验证一次,则配置写法为:
|
| 254 |
+
|
| 255 |
+
```python
|
| 256 |
+
max_epochs = 300 # 最大训练轮次 300 轮
|
| 257 |
+
save_epoch_intervals = 10 # 验证间隔,每 10 轮验证一次
|
| 258 |
+
|
| 259 |
+
train_cfg = dict(
|
| 260 |
+
type='EpochBasedTrainLoop', # 训练循环的类型,请参考 https://github.com/open-mmlab/mmengine/blob/main/mmengine/runner/loops.py
|
| 261 |
+
max_epochs=max_epochs, # 最大训练轮次 300 轮
|
| 262 |
+
val_interval=save_epoch_intervals, # 验证间隔,每 10 个 epoch 验证一次
|
| 263 |
+
dynamic_intervals=[(280, 1)]) # 到 280 epoch 开始切换为间隔 1 的评估方式
|
| 264 |
+
val_cfg = dict(type='ValLoop') # 验证循环的类型
|
| 265 |
+
test_cfg = dict(type='TestLoop') # 测试循环的类型
|
| 266 |
+
```
|
| 267 |
+
|
| 268 |
+
### 优化相关配置
|
| 269 |
+
|
| 270 |
+
`optim_wrapper` 是配置优化相关设置的字段。优化器封装(OptimWrapper)不仅提供了优化器的功能,还支持梯度裁剪、混合精度训练等功能。更多内容请看[优化器封装教程](https://mmengine.readthedocs.io/zh_CN/latest/tutorials/optim_wrapper.html).
|
| 271 |
+
|
| 272 |
+
```python
|
| 273 |
+
optim_wrapper = dict( # 优化器封装的配置
|
| 274 |
+
type='OptimWrapper', # 优化器封装的类型。可以切换至 AmpOptimWrapper 来启用混合精度训练
|
| 275 |
+
optimizer=dict( # 优化器配置。支持 PyTorch 的各种优化器。请参考 https://pytorch.org/docs/stable/optim.html#algorithms
|
| 276 |
+
type='SGD', # 随机梯度下降优化器
|
| 277 |
+
lr=0.01, # 基础学习率
|
| 278 |
+
momentum=0.937, # 带动量的随机梯度下降
|
| 279 |
+
weight_decay=0.0005, # 权重衰减
|
| 280 |
+
nesterov=True, # 开启Nesterov momentum,公式详见 http://www.cs.toronto.edu/~hinton/absps/momentum.pdf
|
| 281 |
+
batch_size_per_gpu=train_batch_size_per_gpu), # 该选项实现了自动权重衰减系数缩放
|
| 282 |
+
clip_grad=None, # 梯度裁剪的配置,设置为 None 关闭梯度裁剪。使用方法请见 https://mmengine.readthedocs.io/zh_CN/latest/tutorials/optim_wrapper.html
|
| 283 |
+
constructor='YOLOv5OptimizerConstructor') # YOLOv5 优化器构建器
|
| 284 |
+
|
| 285 |
+
```
|
| 286 |
+
|
| 287 |
+
`param_scheduler` 字段用于配置参数调度器(Parameter Scheduler)来调整优化器的超参数(例如学习率和动量)。 用户可以组合多个调度器来创建所需的参数调整策略。 在[参数调度器教程](https://mmengine.readthedocs.io/zh_CN/latest/tutorials/param_scheduler.html) 和参数调度器 API 文档 中查找更多信息。在 YOLOv5 中,参数调度实现比较复杂,难以通过 `param_scheduler` 实现。所以我们采用了 `YOLOv5ParamSchedulerHook` 来实现(见下节),这样做更简单但是通用性较差。
|
| 288 |
+
|
| 289 |
+
```python
|
| 290 |
+
param_scheduler = None
|
| 291 |
+
```
|
| 292 |
+
|
| 293 |
+
### 钩子配置
|
| 294 |
+
|
| 295 |
+
用户可以在训练、验证和测试循环上添加钩子,以便在运行期间插入一些操作。配置中有两种不同的钩子字段,一种是 `default_hooks`,另一种是 `custom_hooks`。
|
| 296 |
+
|
| 297 |
+
`default_hooks` 是一个字典,用于配置运行时必须使用的钩子。这些钩子具有默认优先级,如果未设置,runner 将使用默认值。如果要禁用默认钩子,用户可以将其配置设置为 `None`。
|
| 298 |
+
|
| 299 |
+
```python
|
| 300 |
+
default_hooks = dict(
|
| 301 |
+
param_scheduler=dict(
|
| 302 |
+
type='YOLOv5ParamSchedulerHook', # MMYOLO 中默认采用 Hook 方式进行优化器超参数的调节
|
| 303 |
+
scheduler_type='linear',
|
| 304 |
+
lr_factor=0.01,
|
| 305 |
+
max_epochs=max_epochs),
|
| 306 |
+
checkpoint=dict(
|
| 307 |
+
type='CheckpointHook', # 按照给定间隔保存模型的权重的 Hook
|
| 308 |
+
interval=save_epoch_intervals, # 每 10 轮保存 1 次权重文件
|
| 309 |
+
max_keep_ckpts=3)) # 最多保存 3 个权重文件
|
| 310 |
+
```
|
| 311 |
+
|
| 312 |
+
`custom_hooks` 是一个列表。用户可以在这个字段中加入自定义的钩子,例如 `EMAHook`。
|
| 313 |
+
|
| 314 |
+
```python
|
| 315 |
+
custom_hooks = [
|
| 316 |
+
dict(
|
| 317 |
+
type='EMAHook', # 实现权重 EMA(指数移动平均) 更新的 Hook
|
| 318 |
+
ema_type='ExpMomentumEMA', # YOLO 中使用的带动量 EMA
|
| 319 |
+
momentum=0.0001, # EMA 的动量参数
|
| 320 |
+
update_buffers=True, # 是否计算模型的参数和缓冲的 running averages
|
| 321 |
+
priority=49) # 优先级略高于 NORMAL(50)
|
| 322 |
+
]
|
| 323 |
+
```
|
| 324 |
+
|
| 325 |
+
### 运行相关配置
|
| 326 |
+
|
| 327 |
+
```python
|
| 328 |
+
default_scope = 'mmyolo' # 默认的注册器域名,默认从此注册器域中寻找模块。请参考 https://mmengine.readthedocs.io/zh_CN/latest/tutorials/registry.html
|
| 329 |
+
|
| 330 |
+
env_cfg = dict(
|
| 331 |
+
cudnn_benchmark=True, # 是否启用 cudnn benchmark, 推荐单尺度训练时开启,可加速训练
|
| 332 |
+
mp_cfg=dict( # 多进程设置
|
| 333 |
+
mp_start_method='fork', # 使用 fork 来启动多进程。‘fork’ 通常比 ‘spawn’ 更快,但可能存在隐患。请参考 https://github.com/pytorch/pytorch/issues/1355
|
| 334 |
+
opencv_num_threads=0), # 关闭 opencv 的多线程以避免系统超负荷
|
| 335 |
+
dist_cfg=dict(backend='nccl'), # 分布式相关设置
|
| 336 |
+
)
|
| 337 |
+
|
| 338 |
+
vis_backends = [dict(type='LocalVisBackend')] # 可视化后端,请参考 https://mmengine.readthedocs.io/zh_CN/latest/advanced_tutorials/visualization.html
|
| 339 |
+
visualizer = dict(
|
| 340 |
+
type='mmdet.DetLocalVisualizer', vis_backends=vis_backends, name='visualizer')
|
| 341 |
+
log_processor = dict(
|
| 342 |
+
type='LogProcessor', # 日志处理器用于处理运行时日志
|
| 343 |
+
window_size=50, # 日志数值的平滑窗口
|
| 344 |
+
by_epoch=True) # 是否使用 epoch 格式的日志。需要与训练循环的类型保存一致。
|
| 345 |
+
|
| 346 |
+
log_level = 'INFO' # 日志等级
|
| 347 |
+
load_from = None # 从给定路径加载模型检查点作为预训练模型。这不会恢复训练。
|
| 348 |
+
resume = False # 是否从 `load_from` 中定义的检查点恢复。 如果 `load_from` 为 None,它将恢复 `work_dir` 中的最新检查点。
|
| 349 |
+
```
|
| 350 |
+
|
| 351 |
+
## 配置文件继承
|
| 352 |
+
|
| 353 |
+
在 `config/_base_` 文件夹目前有运行时的默认设置(default runtime)。由 `_base_` 下的组件组成的配置,被我们称为 _原始配置(primitive)_。
|
| 354 |
+
|
| 355 |
+
对于同一文件夹下的所有配置,推荐**只有一个**对应的**原始配置**文件。所有其他的配置文件都应该继承自这个**原始配置**文件。这样就能保证配置文件的最大继承深度为 3。
|
| 356 |
+
|
| 357 |
+
为了便于理解,我们建议贡献者继承现有方法。例如,如果在 YOLOv5s 的基础上做了一些修改,比如修改网络深度,用户首先可以通过指定 `_base_ = ./yolov5_s-v61_syncbn_8xb16-300e_coco.py` 来集成基础的 YOLOv5 结构,然后修改配置文件中的必要参数以完成继承。
|
| 358 |
+
|
| 359 |
+
如果你在构建一个与任何现有方法不共享结构的全新方法,那么可以在 `configs` 文件夹下创建一个新的例如 `yolov100` 文件夹。
|
| 360 |
+
|
| 361 |
+
更多细节请参考 [MMEngine 配置文件教程](https://mmengine.readthedocs.io/zh_CN/latest/tutorials/config.html)。
|
| 362 |
+
|
| 363 |
+
通过设置 `_base_` 字段,我们可以设置当前配置文件继承自哪些文件。
|
| 364 |
+
|
| 365 |
+
当 `_base_` 为文件路径字符串时,表示继承一个配置文件的内容。
|
| 366 |
+
|
| 367 |
+
```python
|
| 368 |
+
_base_ = '../_base_/default_runtime.py'
|
| 369 |
+
```
|
| 370 |
+
|
| 371 |
+
当 `_base_` 是多个文件路径的列表时,表示继承多个文件。
|
| 372 |
+
|
| 373 |
+
```python
|
| 374 |
+
_base_ = [
|
| 375 |
+
'./yolov5_s-v61_syncbn_8xb16-300e_coco.py',
|
| 376 |
+
'../_base_/default_runtime.py'
|
| 377 |
+
]
|
| 378 |
+
```
|
| 379 |
+
|
| 380 |
+
如果需要检查配置文件,可以通过运行 `mim run mmdet print_config /PATH/TO/CONFIG` 来查看完整的配置。
|
| 381 |
+
|
| 382 |
+
### 忽略基础配置文件里的部分内容
|
| 383 |
+
|
| 384 |
+
有时,您也许会设置 `_delete_=True` 去忽略基础配置文件里的一些域内容。 您也许可以参照 [MMEngine 配置文件教程](https://mmengine.readthedocs.io/zh_CN/latest/tutorials/config.html) 来获得一些简单的指导。
|
| 385 |
+
|
| 386 |
+
在 MMYOLO 里,例如为了改变 RTMDet 的主干网络的某些内容:
|
| 387 |
+
|
| 388 |
+
```python
|
| 389 |
+
model = dict(
|
| 390 |
+
type='YOLODetector',
|
| 391 |
+
data_preprocessor=dict(...),
|
| 392 |
+
backbone=dict(
|
| 393 |
+
type='CSPNeXt',
|
| 394 |
+
arch='P5',
|
| 395 |
+
expand_ratio=0.5,
|
| 396 |
+
deepen_factor=deepen_factor,
|
| 397 |
+
widen_factor=widen_factor,
|
| 398 |
+
channel_attention=True,
|
| 399 |
+
norm_cfg=dict(type='BN'),
|
| 400 |
+
act_cfg=dict(type='SiLU', inplace=True)),
|
| 401 |
+
neck=dict(...),
|
| 402 |
+
bbox_head=dict(...))
|
| 403 |
+
```
|
| 404 |
+
|
| 405 |
+
如果想把 RTMDet 主干网络的 `CSPNeXt` 改成 `YOLOv6EfficientRep`,因为 `CSPNeXt` 和 `YOLOv6EfficientRep` 中有不同的字段(`channel_attention` 和 `expand_ratio`),这时候就需要使用 `_delete_=True` 将新的键去替换 `backbone` 域内所有老的键。
|
| 406 |
+
|
| 407 |
+
```python
|
| 408 |
+
_base_ = '../rtmdet/rtmdet_l_syncbn_8xb32-300e_coco.py'
|
| 409 |
+
model = dict(
|
| 410 |
+
backbone=dict(
|
| 411 |
+
_delete_=True,
|
| 412 |
+
type='YOLOv6EfficientRep',
|
| 413 |
+
deepen_factor=deepen_factor,
|
| 414 |
+
widen_factor=widen_factor,
|
| 415 |
+
norm_cfg=dict(type='BN', momentum=0.03, eps=0.001),
|
| 416 |
+
act_cfg=dict(type='ReLU', inplace=True)),
|
| 417 |
+
neck=dict(...),
|
| 418 |
+
bbox_head=dict(...))
|
| 419 |
+
```
|
| 420 |
+
|
| 421 |
+
### 使用配置文件里的中间变量
|
| 422 |
+
|
| 423 |
+
配置文件里会使用一些中间变量,例如数据集里的 `train_pipeline`/`test_pipeline`。我们在定义新的 `train_pipeline`/`test_pipeline` 之后,需要将它们传递到 `data` 里。例如,我们想在训练或测试时,改变 YOLOv5 网络的 `img_scale` 训练尺度并在训练时添加 `YOLOv5MixUp` 数据增强,`img_scale/train_pipeline/test_pipeline` 是我们想要修改的中间变量。
|
| 424 |
+
|
| 425 |
+
**注**:使用 `YOLOv5MixUp` 数据增强时,需要将 `YOLOv5MixUp` 之前的训练数据处理流程定义在其 `pre_transform` 中。详细过程和图解可参见 [YOLOv5 原理和实现全解析](../recommended_topics/algorithm_descriptions/yolov5_description.md)。
|
| 426 |
+
|
| 427 |
+
```python
|
| 428 |
+
_base_ = './yolov5_s-v61_syncbn_8xb16-300e_coco.py'
|
| 429 |
+
|
| 430 |
+
img_scale = (1280, 1280) # 高度,宽度
|
| 431 |
+
affine_scale = 0.9 # 仿射变换尺度
|
| 432 |
+
|
| 433 |
+
mosaic_affine_pipeline = [
|
| 434 |
+
dict(
|
| 435 |
+
type='Mosaic',
|
| 436 |
+
img_scale=img_scale,
|
| 437 |
+
pad_val=114.0,
|
| 438 |
+
pre_transform=pre_transform),
|
| 439 |
+
dict(
|
| 440 |
+
type='YOLOv5RandomAffine',
|
| 441 |
+
max_rotate_degree=0.0,
|
| 442 |
+
max_shear_degree=0.0,
|
| 443 |
+
scaling_ratio_range=(1 - affine_scale, 1 + affine_scale),
|
| 444 |
+
border=(-img_scale[0] // 2, -img_scale[1] // 2),
|
| 445 |
+
border_val=(114, 114, 114))
|
| 446 |
+
]
|
| 447 |
+
|
| 448 |
+
train_pipeline = [
|
| 449 |
+
*pre_transform, *mosaic_affine_pipeline,
|
| 450 |
+
dict(
|
| 451 |
+
type='YOLOv5MixUp', # YOLOv5 的 MixUp (图像混合) 数据增强
|
| 452 |
+
prob=0.1, # MixUp 概率
|
| 453 |
+
pre_transform=[*pre_transform,*mosaic_affine_pipeline]), # MixUp 之前的训练数据处理流程,包含 数据预处理流程、 'Mosaic' 和 'YOLOv5RandomAffine'
|
| 454 |
+
dict(
|
| 455 |
+
type='mmdet.Albu',
|
| 456 |
+
transforms=albu_train_transforms,
|
| 457 |
+
bbox_params=dict(
|
| 458 |
+
type='BboxParams',
|
| 459 |
+
format='pascal_voc',
|
| 460 |
+
label_fields=['gt_bboxes_labels', 'gt_ignore_flags']),
|
| 461 |
+
keymap={
|
| 462 |
+
'img': 'image',
|
| 463 |
+
'gt_bboxes': 'bboxes'
|
| 464 |
+
}),
|
| 465 |
+
dict(type='YOLOv5HSVRandomAug'),
|
| 466 |
+
dict(type='mmdet.RandomFlip', prob=0.5),
|
| 467 |
+
dict(
|
| 468 |
+
type='mmdet.PackDetInputs',
|
| 469 |
+
meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'flip',
|
| 470 |
+
'flip_direction'))
|
| 471 |
+
]
|
| 472 |
+
|
| 473 |
+
test_pipeline = [
|
| 474 |
+
dict(
|
| 475 |
+
type='LoadImageFromFile'),
|
| 476 |
+
dict(type='YOLOv5KeepRatioResize', scale=img_scale),
|
| 477 |
+
dict(
|
| 478 |
+
type='LetterResize',
|
| 479 |
+
scale=img_scale,
|
| 480 |
+
allow_scale_up=False,
|
| 481 |
+
pad_val=dict(img=114)),
|
| 482 |
+
dict(type='LoadAnnotations', with_bbox=True),
|
| 483 |
+
dict(
|
| 484 |
+
type='mmdet.PackDetInputs',
|
| 485 |
+
meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',
|
| 486 |
+
'scale_factor', 'pad_param'))
|
| 487 |
+
]
|
| 488 |
+
|
| 489 |
+
train_dataloader = dict(dataset=dict(pipeline=train_pipeline))
|
| 490 |
+
val_dataloader = dict(dataset=dict(pipeline=test_pipeline))
|
| 491 |
+
test_dataloader = dict(dataset=dict(pipeline=test_pipeline))
|
| 492 |
+
```
|
| 493 |
+
|
| 494 |
+
我们首先定义新的 `train_pipeline`/`test_pipeline` 然后传递到 `data` 里。
|
| 495 |
+
|
| 496 |
+
同样的,如果我们想从 `SyncBN` 切换到 `BN` 或者 `MMSyncBN`,我们需要修改配置文件里的每一个 `norm_cfg`。
|
| 497 |
+
|
| 498 |
+
```python
|
| 499 |
+
_base_ = './yolov5_s-v61_syncbn_8xb16-300e_coco.py'
|
| 500 |
+
norm_cfg = dict(type='BN', requires_grad=True)
|
| 501 |
+
model = dict(
|
| 502 |
+
backbone=dict(norm_cfg=norm_cfg),
|
| 503 |
+
neck=dict(norm_cfg=norm_cfg),
|
| 504 |
+
...)
|
| 505 |
+
```
|
| 506 |
+
|
| 507 |
+
### 复用 \_base\_ 文件中的变量
|
| 508 |
+
|
| 509 |
+
如果用户希望在当前配置中复用 `_base_` 文件中的变量,则可以通过使用 `{{_base_.xxx}}` 的方式来获取对应变量的拷贝。而在新版 MMEngine 中,还支持省略 `{{}}` 的写法。例如:
|
| 510 |
+
|
| 511 |
+
```python
|
| 512 |
+
_base_ = '../_base_/default_runtime.py'
|
| 513 |
+
|
| 514 |
+
pre_transform = _base_.pre_transform # 变量 pre_transform 等于 _base_ 中定义的 pre_transform
|
| 515 |
+
```
|
| 516 |
+
|
| 517 |
+
## 通过脚本参数修改配置
|
| 518 |
+
|
| 519 |
+
当运行 `tools/train.py` 和 `tools/test.py` 时,可以通过 `--cfg-options` 来修改配置文件。
|
| 520 |
+
|
| 521 |
+
- 更新字典链中的配置
|
| 522 |
+
|
| 523 |
+
可以按照原始配置文件中的 dict 键顺序地指定配置预选项。例如,使用 `--cfg-options model.backbone.norm_eval=False` 将模型主干网络中的所有 BN 模块都改为 `train` 模式。
|
| 524 |
+
|
| 525 |
+
- 更新配置列表中的键
|
| 526 |
+
|
| 527 |
+
在配置文件里,一些字典型的配置被包含在列表中。例如,数据训练流程 `data.train.pipeline` 通常是一个列表,比如 `[dict(type='LoadImageFromFile'), ...]`。如果需要将 `'LoadImageFromFile'` 改成 `'LoadImageFromNDArray'`,需要写成下述形式:`--cfg-options data.train.pipeline.0.type=LoadImageFromNDArray`.
|
| 528 |
+
|
| 529 |
+
- 更新列表或元组的值
|
| 530 |
+
|
| 531 |
+
如果要更新的值是列表或元组。例如,配置文件通常设置 `model.data_preprocessor.mean=[123.675, 116.28, 103.53]`。如果需要改变这个键,可以通过 `--cfg-options model.data_preprocessor.mean="[127,127,127]"` 来重新设置。需要注意,引号 `"` 是支持列表或元组数据类型所必需的,并且在指定值的引号内**不允许**有空格。
|
| 532 |
+
|
| 533 |
+
## 配置文件名称风格
|
| 534 |
+
|
| 535 |
+
我们遵循以下样式来命名配置文件。建议贡献者遵循相同的风格。
|
| 536 |
+
|
| 537 |
+
```
|
| 538 |
+
{algorithm name}_{model component names [component1]_[component2]_[...]}-[version id]_[norm setting]_[data preprocessor type]_{training settings}_{training dataset information}_[testing dataset information].py
|
| 539 |
+
```
|
| 540 |
+
|
| 541 |
+
文件名分为 8 个部分,其中 4 个必填部分、4 个可选部分。 每个部分用 `_` 连接,每个部分内的单词应该用 `-` 连接。`{}` 表示必填部分,`[]` 表示选填部分。
|
| 542 |
+
|
| 543 |
+
- `{algorithm name}`:算法的名称。 它可以是检测器名称,例如 `yolov5`, `yolov6`, `yolox` 等。
|
| 544 |
+
- `{component names}`:算法中使用的组件名称,如 backbone、neck 等。例如 yolov5_s代表其深度缩放因子`deepen_factor=0.33` 以及其宽度缩放因子 `widen_factor=0.5`。
|
| 545 |
+
- `[version_id]` (可选):由于 YOLO 系列算法迭代速度远快于传统目标检测算法,因此采用 `version id` 来区分不同子版本之间的差异。例如 YOLOv5 的 3.0 版本采用 `Focus` 层作为第一个下采样层,而 6.0 以后的版本采用 `Conv` 层作为第一个下采样层。
|
| 546 |
+
- `[norm_setting]` (可选):`bn` 表示 `Batch Normalization`, `syncbn` 表示 `Synchronized Batch Normalization`。
|
| 547 |
+
- `[data preprocessor type]` (可选):`fast` 表示调用 [YOLOv5DetDataPreprocessor](https://github.com/open-mmlab/mmyolo/blob/main/mmyolo/models/data_preprocessors/data_preprocessor.py#L9) 并配合 [yolov5_collate](https://github.com/open-mmlab/mmyolo/blob/main/mmyolo/datasets/utils.py#L12) 进行数据预处理,训练速度比默认的 `mmdet.DetDataPreprocessor` 更快,但是对多任务处理的灵活性较低。
|
| 548 |
+
- `{training settings}`:训练设置的信息,例如 batch 大小、数据增强、损失、参数调度方式和训练最大轮次/迭代。 例如:`8xb16-300e_coco` 表示使用 8 个 GPU 每个 GPU 16 张图,并训练 300 个 epoch。
|
| 549 |
+
缩写介绍:
|
| 550 |
+
- `{gpu x batch_per_gpu}`:GPU 数和每个 GPU 的样本数。例如 `4x4b` 是 4 个 GPU 每个 GPU 4 张图的缩写。
|
| 551 |
+
- `{schedule}`:训练方案,MMYOLO 中默认为 300 个 epoch。
|
| 552 |
+
- `{training dataset information}`:训练数据集,例如 `coco`, `cityscapes`, `voc-0712`, `wider-face`, `balloon`。
|
| 553 |
+
- `[testing dataset information]` (可选):测试数据集,用于训练和测试在不同数据集上的模型配置。 如果没有注明,则表示训练和测试的数据集类型相同。
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/tutorials/custom_installation.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 自定义安装
|
| 2 |
+
|
| 3 |
+
## CUDA 版本
|
| 4 |
+
|
| 5 |
+
在安装 PyTorch 时,你需要指定 CUDA 的版本。如果你不清楚应该选择哪一个,请遵循我们的建议。
|
| 6 |
+
|
| 7 |
+
- 对于 Ampere 架构的 NVIDIA GPU,例如 GeForce 30 系列 以及 NVIDIA A100,CUDA 11 是必需的。
|
| 8 |
+
- 对于更早的 NVIDIA GPU,CUDA 11 是向后兼容 (backward compatible) 的,但 CUDA 10.2 能够提供更好的兼容性,也更加轻量。
|
| 9 |
+
|
| 10 |
+
请确保你的 GPU 驱动版本满足最低的版本需求,参阅 NVIDIA 官方的 [CUDA 工具箱和相应的驱动版本关系表](https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html#cuda-major-component-versions__table-cuda-toolkit-driver-versions)。
|
| 11 |
+
|
| 12 |
+
```{note}
|
| 13 |
+
如果按照我们的最佳实践进行安装,CUDA 运行时库就足够了,因为我们提供相关 CUDA 代码的预编译,不需要进行本地编译。
|
| 14 |
+
但如果你希望从源码进行 MMCV 的编译,或是进行其他 CUDA 算子的开发,那么就必须安装完整的 CUDA 工具链,参见
|
| 15 |
+
[NVIDIA 官网](https://developer.nvidia.com/cuda-downloads) ,另外还需要确保该 CUDA 工具链的版本与 PyTorch 安装时
|
| 16 |
+
的配置相匹配(如用 `conda install` 安装 PyTorch 时指定的 cudatoolkit 版本)。
|
| 17 |
+
```
|
| 18 |
+
|
| 19 |
+
## 不使用 MIM 安装 MMEngine
|
| 20 |
+
|
| 21 |
+
要使用 pip 而不是 MIM 来安装 MMEngine,请遵照 [MMEngine 安装指南](https://mmengine.readthedocs.io/en/latest/get_started/installation.html)。
|
| 22 |
+
|
| 23 |
+
例如,你可以通过以下命令安装 MMEngine:
|
| 24 |
+
|
| 25 |
+
```shell
|
| 26 |
+
pip install "mmengine>=0.6.0"
|
| 27 |
+
```
|
| 28 |
+
|
| 29 |
+
## 不使用 MIM 安装 MMCV
|
| 30 |
+
|
| 31 |
+
MMCV 包含 C++ 和 CUDA 扩展,因此其对 PyTorch 的依赖比较复杂。MIM 会自动解析这些 依赖,选择合适的 MMCV 预编译包,使安装更简单,但它并不是必需的。
|
| 32 |
+
|
| 33 |
+
要使用 pip 而不是 MIM 来安装 MMCV,请遵照 [MMCV 安装指南](https://mmcv.readthedocs.io/zh_CN/2.x/get_started/installation.html)。
|
| 34 |
+
它需要您用指定 URL 的形式手动指定对应的 PyTorch 和 CUDA 版本。
|
| 35 |
+
|
| 36 |
+
例如,下述命令将会安装基于 PyTorch 1.12.x 和 CUDA 11.6 编译的 mmcv:
|
| 37 |
+
|
| 38 |
+
```shell
|
| 39 |
+
pip install "mmcv>=2.0.0rc4" -f https://download.openmmlab.com/mmcv/dist/cu116/torch1.12.0/index.html
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
## 在 CPU 环境中安装
|
| 43 |
+
|
| 44 |
+
我们的代码能够建立在只使用 CPU 的环境(CUDA 不可用)。
|
| 45 |
+
|
| 46 |
+
在 CPU 模式下,可以进行模型训练(需要 MMCV 版本 >= `2.0.0rc1`)、测试或者推理,然而以下功能将在 CPU 模式下不能使用:
|
| 47 |
+
|
| 48 |
+
- Deformable Convolution
|
| 49 |
+
- Modulated Deformable Convolution
|
| 50 |
+
- ROI pooling
|
| 51 |
+
- Deformable ROI pooling
|
| 52 |
+
- CARAFE: Content-Aware ReAssembly of FEatures
|
| 53 |
+
- SyncBatchNorm
|
| 54 |
+
- CrissCrossAttention: Criss-Cross Attention
|
| 55 |
+
- MaskedConv2d
|
| 56 |
+
- Temporal Interlace Shift
|
| 57 |
+
- nms_cuda
|
| 58 |
+
- sigmoid_focal_loss_cuda
|
| 59 |
+
- bbox_overlaps
|
| 60 |
+
|
| 61 |
+
因此,如果尝试使用包含上述操作的模型进行训练/测试/推理,将会报错。下表列出了由于依赖上述算子而无法在 CPU 上运行的相关模型:
|
| 62 |
+
|
| 63 |
+
| 操作 | 模型 |
|
| 64 |
+
| :-----------------------------------------------------: | :--------------------------------------------------------------------------------------: |
|
| 65 |
+
| Deformable Convolution/Modulated Deformable Convolution | DCN、Guided Anchoring、RepPoints、CentripetalNet、VFNet、CascadeRPN、NAS-FCOS、DetectoRS |
|
| 66 |
+
| MaskedConv2d | Guided Anchoring |
|
| 67 |
+
| CARAFE | CARAFE |
|
| 68 |
+
| SyncBatchNorm | ResNeSt |
|
| 69 |
+
|
| 70 |
+
## 在 Google Colab 中安装
|
| 71 |
+
|
| 72 |
+
[Google Colab](https://colab.research.google.com/) 通常已经包含了 PyTorch 环境,因此我们只需要安装 MMEngine、MMCV、MMDetection 和 MMYOLO 即可,命令如下:
|
| 73 |
+
|
| 74 |
+
**步骤 1.** 使用 [MIM](https://github.com/open-mmlab/mim) 安装 [MMEngine](https://github.com/open-mmlab/mmengine) 、 [MMCV](https://github.com/open-mmlab/mmcv) 和 [MMDetection](https://github.com/open-mmlab/mmdetection) 。
|
| 75 |
+
|
| 76 |
+
```shell
|
| 77 |
+
!pip3 install openmim
|
| 78 |
+
!mim install "mmengine>=0.6.0"
|
| 79 |
+
!mim install "mmcv>=2.0.0rc4,<2.1.0"
|
| 80 |
+
!mim install "mmdet>=3.0.0,<4.0.0"
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
**步骤 2.** 使用源码安装 MMYOLO:
|
| 84 |
+
|
| 85 |
+
```shell
|
| 86 |
+
!git clone https://github.com/open-mmlab/mmyolo.git
|
| 87 |
+
%cd mmyolo
|
| 88 |
+
!pip install -e .
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
**步骤 3.** 验证安装是否成功:
|
| 92 |
+
|
| 93 |
+
```python
|
| 94 |
+
import mmyolo
|
| 95 |
+
print(mmyolo.__version__)
|
| 96 |
+
# 预期输出: 0.1.0 或其他版本号
|
| 97 |
+
```
|
| 98 |
+
|
| 99 |
+
```{note}
|
| 100 |
+
在 Jupyter 中,感叹号 `!` 用于执行外部命令,而 `%cd` 是一个[魔术命令](https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-cd),用于切换 Python 的工作路径。
|
| 101 |
+
```
|
| 102 |
+
|
| 103 |
+
## 使用多个 MMYOLO 版��进行开发
|
| 104 |
+
|
| 105 |
+
训练和测试的脚本已经在 `PYTHONPATH` 中进行了修改,以确保脚本使用当前目录中的 MMYOLO。
|
| 106 |
+
|
| 107 |
+
要使环境中安装默认的 MMYOLO 而不是当前正在在使用的,可以删除出现在相关脚本中的如下代码:
|
| 108 |
+
|
| 109 |
+
```shell
|
| 110 |
+
PYTHONPATH="$(dirname $0)/..":$PYTHONPATH
|
| 111 |
+
```
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/tutorials/data_flow.md
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 混合类图片数据增强更新
|
| 2 |
+
|
| 3 |
+
混合类图片数据增强是指类似 Mosaic 和 MixUp 一样,在运行过程中需要获取多张图片的标注信息进行融合。 在 OpenMMLab 数据增强 pipeline 中一般是获取不到数据集其他索引的。 为了实现上述功能,在 MMDetection 复现的 YOLOX 中提出了 [MultiImageMixDataset](https://github.com/open-mmlab/mmdetection/blob/master/mmdet/datasets/dataset_wrappers.py#L338) 数据集包装器的概念。
|
| 4 |
+
|
| 5 |
+
`MultiImageMixDataset` 数据集包装器会传入一个包括 `Mosaic` 和 `RandAffine` 等数据增强,而 `CocoDataset` 中也需要传入一个包括图片和标注加载的 `pipeline` 。通过这种方式就可以快速的实现混合类数据增强。其配置用法如下所示:
|
| 6 |
+
|
| 7 |
+
```python
|
| 8 |
+
train_pipeline = [
|
| 9 |
+
dict(type='Mosaic', img_scale=img_scale, pad_val=114.0),
|
| 10 |
+
dict(
|
| 11 |
+
type='RandomAffine',
|
| 12 |
+
scaling_ratio_range=(0.1, 2),
|
| 13 |
+
border=(-img_scale[0] // 2, -img_scale[1] // 2)),
|
| 14 |
+
dict(
|
| 15 |
+
type='MixUp',
|
| 16 |
+
img_scale=img_scale,
|
| 17 |
+
ratio_range=(0.8, 1.6),
|
| 18 |
+
pad_val=114.0),
|
| 19 |
+
...
|
| 20 |
+
]
|
| 21 |
+
train_dataset = dict(
|
| 22 |
+
# use MultiImageMixDataset wrapper to support mosaic and mixup
|
| 23 |
+
type='MultiImageMixDataset',
|
| 24 |
+
dataset=dict(
|
| 25 |
+
type='CocoDataset',
|
| 26 |
+
pipeline=[
|
| 27 |
+
dict(type='LoadImageFromFile'),
|
| 28 |
+
dict(type='LoadAnnotations', with_bbox=True)
|
| 29 |
+
]),
|
| 30 |
+
pipeline=train_pipeline)
|
| 31 |
+
|
| 32 |
+
```
|
| 33 |
+
|
| 34 |
+
但是上述实现起来会有一个缺点:对于不熟悉 MMDetection 的用户来说,其经常会忘记 Mosaic 必须要和 `MultiImageMixDataset` 配合使用,而且这样会加大复杂度和理解难度。
|
| 35 |
+
|
| 36 |
+
为了解决这个问题,在 MMYOLO 中进一步进行了简化。直接让 `pipeline` 获取到 `dataset` 对象,此时就可以将 `Mosaic` 等混合类数据增强的实现和使用随机翻转的操作一样,不再需要数据集包装器。新的配置写法为:
|
| 37 |
+
|
| 38 |
+
```python
|
| 39 |
+
pre_transform = [
|
| 40 |
+
dict(type='LoadImageFromFile'),
|
| 41 |
+
dict(type='LoadAnnotations', with_bbox=True)
|
| 42 |
+
]
|
| 43 |
+
train_pipeline = [
|
| 44 |
+
*pre_transform,
|
| 45 |
+
dict(
|
| 46 |
+
type='Mosaic',
|
| 47 |
+
img_scale=img_scale,
|
| 48 |
+
pad_val=114.0,
|
| 49 |
+
pre_transform=pre_transform),
|
| 50 |
+
dict(
|
| 51 |
+
type='mmdet.RandomAffine',
|
| 52 |
+
scaling_ratio_range=(0.1, 2),
|
| 53 |
+
border=(-img_scale[0] // 2, -img_scale[1] // 2)),
|
| 54 |
+
dict(
|
| 55 |
+
type='YOLOXMixUp',
|
| 56 |
+
img_scale=img_scale,
|
| 57 |
+
ratio_range=(0.8, 1.6),
|
| 58 |
+
pad_val=114.0,
|
| 59 |
+
pre_transform=pre_transform),
|
| 60 |
+
...
|
| 61 |
+
]
|
| 62 |
+
```
|
| 63 |
+
|
| 64 |
+
一个稍微复杂点的包括 MixUp 的 YOLOv5-m 配置如下所示:
|
| 65 |
+
|
| 66 |
+
```python
|
| 67 |
+
mosaic_affine_pipeline = [
|
| 68 |
+
dict(
|
| 69 |
+
type='Mosaic',
|
| 70 |
+
img_scale=img_scale,
|
| 71 |
+
pad_val=114.0,
|
| 72 |
+
pre_transform=pre_transform),
|
| 73 |
+
dict(
|
| 74 |
+
type='YOLOv5RandomAffine',
|
| 75 |
+
max_rotate_degree=0.0,
|
| 76 |
+
max_shear_degree=0.0,
|
| 77 |
+
scaling_ratio_range=(1 - affine_scale, 1 + affine_scale),
|
| 78 |
+
border=(-img_scale[0] // 2, -img_scale[1] // 2),
|
| 79 |
+
border_val=(114, 114, 114))
|
| 80 |
+
]
|
| 81 |
+
|
| 82 |
+
# enable mixup
|
| 83 |
+
train_pipeline = [
|
| 84 |
+
*pre_transform, *mosaic_affine_pipeline,
|
| 85 |
+
dict(
|
| 86 |
+
type='YOLOv5MixUp',
|
| 87 |
+
prob=0.1,
|
| 88 |
+
pre_transform=[*pre_transform, *mosaic_affine_pipeline]),
|
| 89 |
+
dict(
|
| 90 |
+
type='mmdet.Albu',
|
| 91 |
+
transforms=albu_train_transforms,
|
| 92 |
+
bbox_params=dict(
|
| 93 |
+
type='BboxParams',
|
| 94 |
+
format='pascal_voc',
|
| 95 |
+
label_fields=['gt_bboxes_labels', 'gt_ignore_flags']),
|
| 96 |
+
keymap={
|
| 97 |
+
'img': 'image',
|
| 98 |
+
'gt_bboxes': 'bboxes'
|
| 99 |
+
}),
|
| 100 |
+
dict(type='YOLOv5HSVRandomAug'),
|
| 101 |
+
dict(type='mmdet.RandomFlip', prob=0.5),
|
| 102 |
+
dict(
|
| 103 |
+
type='mmdet.PackDetInputs',
|
| 104 |
+
meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'flip',
|
| 105 |
+
'flip_direction'))
|
| 106 |
+
]
|
| 107 |
+
```
|
| 108 |
+
|
| 109 |
+
其实现过程非常简单,只需要在 Dataset 中将本身对象传给 pipeline 即可,具体代码如下:
|
| 110 |
+
|
| 111 |
+
```python
|
| 112 |
+
def prepare_data(self, idx) -> Any:
|
| 113 |
+
"""Pass the dataset to the pipeline during training to support mixed
|
| 114 |
+
data augmentation, such as Mosaic and MixUp."""
|
| 115 |
+
if self.test_mode is False:
|
| 116 |
+
data_info = self.get_data_info(idx)
|
| 117 |
+
data_info['dataset'] = self
|
| 118 |
+
return self.pipeline(data_info)
|
| 119 |
+
else:
|
| 120 |
+
return super().prepare_data(idx)
|
| 121 |
+
```
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/tutorials/faq.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 常见问题解答
|
| 2 |
+
|
| 3 |
+
我们在这里列出了使用时的一些常见问题及其相应的解决方案。 如果您发现有一些问题被遗漏,请随时提 PR 丰富这个列表。 如果您无法在此获得帮助,请创建 [issue](https://github.com/open-mmlab/mmyolo/issues/new/choose) 提问,但是请在模板中填写所有必填信息,这有助于我们更快定位问题。
|
| 4 |
+
|
| 5 |
+
## 为什么要推出 MMYOLO?
|
| 6 |
+
|
| 7 |
+
为什么要推出 MMYOLO? 为何要单独开一个仓库而不是直接放到 MMDetection 中? 自从开源后,不断收到社区小伙伴们类似的疑问,答案可以归纳为以下三点:
|
| 8 |
+
|
| 9 |
+
**(1) 统一运行和推理平台**
|
| 10 |
+
|
| 11 |
+
目前目标检测领域出现了非常多 YOLO 的改进算法,并且非常受大家欢迎,但是这类算法基于不同框架不同后端实现,存在较大差异,缺少统一便捷的从训练到部署的公平评测流程。
|
| 12 |
+
|
| 13 |
+
**(2) 协议限制**
|
| 14 |
+
|
| 15 |
+
众所周知,YOLOv5 以及其衍生的 YOLOv6 和 YOLOv7 等算法都是 GPL 3.0 协议,不同于 MMDetection 的 Apache 协议。由于协议问题,无法将 MMYOLO 直接并入 MMDetection 中。
|
| 16 |
+
|
| 17 |
+
**(3) 多任务支持**
|
| 18 |
+
|
| 19 |
+
还有一层深远的原因: **MMYOLO 任务不局限于 MMDetection**,后续会支持更多任务例如基于 MMPose 实现关键点相关的应用,基于 MMTracking 实现追踪相关的应用,因此不太适合直接并入 MMDetection 中。
|
| 20 |
+
|
| 21 |
+
## projects 文件夹是用来干什么的?
|
| 22 |
+
|
| 23 |
+
projects 文件夹是 OpenMMLab 2.0 中引入的一个全新文件夹。其初衷有如下 3 点:
|
| 24 |
+
|
| 25 |
+
1. 便于社区贡献。由于 OpenMMLab 系列代码库对于代码合入有一套规范严谨的流程,这不可避免的会导致算法复现周期很长,不利于社区贡献
|
| 26 |
+
2. 便于快速支持新算法。算法开发周期过长同样会导致用户无法尽快体验最新算法
|
| 27 |
+
3. 便于快速支持新方向和新特性。新发展方向或者一些新的特性可能和现如今代码库中的设计有些不兼容,没法快速合入到代码库中
|
| 28 |
+
|
| 29 |
+
综上所述,projects 文件夹的引入主要是解决算法复现周期过长导致的新算法支持速度较慢,新特性支持较复杂等多个问题。 projects 中每个文件夹属于一个完全独立的工程,社区用户可以通过
|
| 30 |
+
projects 快速支持一些在当前版本中较难支持或者想快速支持的新算法和新特性。等后续设计稳定或者代码符合合入规范,则会考虑合入到主分支中。
|
| 31 |
+
|
| 32 |
+
## YOLOv5 backbone 替换为 Swin 后效果很差
|
| 33 |
+
|
| 34 |
+
在 [轻松更换主干网络](../recommended_topics/replace_backbone.md) 一文中我们提供了大量替换 backbone 的教程,但是该文档只是教用户如何替换 backbone,直接训练不一定能得到比较优异的结果。原因是
|
| 35 |
+
不同 backbone 所需要的训练超参是不一样的,以 Swin 和 YOLOv5 backbone 为例两者差异较大,Swin 属于 transformer 系列算法,而 YOLOv5 backbone 属于卷积系列算法,其训练的优化器、学习率以及其他超参差异较大。
|
| 36 |
+
如果强行将 Swin 作为 YOLOv5 backbone 且想取得不错的效果,需要同时调整诸多参数。
|
| 37 |
+
|
| 38 |
+
## MM 系列开源库中有很多组件,如何在 MMYOLO 中使用?
|
| 39 |
+
|
| 40 |
+
在 OpenMMLab 2.0 中对多个 MM 系列开源库之间的模块跨库调用功能进行增强。目前在 MMYOLO 中可以在配置文件中通过 `MM 算法库 A.模块名` 来之间调用 MM 算法库 A 中已经被注册的任意模块。 具体例子可以参考
|
| 41 |
+
[轻松更换主干网络](../recommended_topics/replace_backbone.md) 中使用在 MMClassification 中实现的主干网络章节,其他模块调用也是相同的用法。
|
| 42 |
+
|
| 43 |
+
## MMYOLO 中是否可以加入纯背景图片进行训练?
|
| 44 |
+
|
| 45 |
+
将纯背景图片加入训练大部分情况可以抑制误报率,是否将纯背景图片加入训练功能已经大部分数据集上支持了。以 `YOLOv5CocoDataset` 为例,核心控制参数是 `train_dataloader.dataset.filter_cfg.filter_empty_gt`,如果 `filter_empty_gt` 为 True 表示将纯背景图片过滤掉不加入训练,
|
| 46 |
+
反之将纯背景图片加入到训练中。 目前 MMYOLO 中大部分算法都是默认将纯背景图片加入训练中。
|
| 47 |
+
|
| 48 |
+
## MMYOLO 是否有计算模型推理 FPS 脚本?
|
| 49 |
+
|
| 50 |
+
MMYOLO 是基于 MMDet 3.x 来开发的,在 MMDet 3.x 中提供了计算模型推理 FPS 的脚本。 具体脚本为 [benchmark](https://github.com/open-mmlab/mmdetection/blob/3.x/tools/analysis_tools/benchmark.py)。我们推荐大家使用 mim 直接跨库启动 MMDet 中的脚本而不是直接复制到 MMYOLO 中。
|
| 51 |
+
关于如果通过 mim 启动 MMDet 中脚本,可以查看 [使用 mim 跨库调用其他 OpenMMLab 仓库的脚本](../common_usage/mim_usage.md)。
|
| 52 |
+
|
| 53 |
+
## MMDeploy 和 EasyDeploy 有啥区别?
|
| 54 |
+
|
| 55 |
+
MMDeploy 是由 OpenMMLab 中部署团队开发的针对 OpenMMLab 系列算法库提供部署支持的开源库,支持各种后端和自定义等等强大功能。 EasyDeploy 是由社区小伙伴提供的一个相比 MMDeploy 更加简单易用的部署 projects。
|
| 56 |
+
EasyDeploy 支持的功能目前没有 MMDeploy 多,但是使用上更加简单。 MMYOLO 中同时提供对 MMDeploy 和 EasyDeploy 的支持,用户可以根据自己需求选择。
|
| 57 |
+
|
| 58 |
+
## COCOMetric 中如何查看每个类的 AP
|
| 59 |
+
|
| 60 |
+
只需要在配置中设置 `test_evaluator.classwise` 为 True,或者在 test.py 运行时候增加 `--cfg-options test_evaluator.classwise=True` 即可。
|
| 61 |
+
|
| 62 |
+
## MMYOLO 中为何没有支持 MMDet 类似的自动学习率缩放功能?
|
| 63 |
+
|
| 64 |
+
原因是实验发现 YOLO 系列算法不是非常满足线性缩放功能。在多个数据集上验证发现会出现不基于 batch size 自动学习率缩放效果好于缩放的情形。因此暂时 MMYOLO 还没有支持自动学习率缩放功能。
|
| 65 |
+
|
| 66 |
+
## 自己训练的模型权重尺寸为啥比官方发布的大?
|
| 67 |
+
|
| 68 |
+
原因是用户自己训练的权重通常包括 `optimizer`、`ema_state_dict` 和 `message_hub` 等额外数据,这部分数据我们会在模型发布时候自动删掉,而用户直接基于框架跑的模型权重是全部保留的,所以用户自己训练的模型权重尺寸会比官方发布的大。
|
| 69 |
+
你可以使用 [publish_model.py](https://github.com/open-mmlab/mmyolo/blob/main/tools/misc/publish_model.py) 脚本删掉额外字段。
|
| 70 |
+
|
| 71 |
+
## RTMDet 为何训练所占显存比 YOLOv5 多很多?
|
| 72 |
+
|
| 73 |
+
训练显存较多的原因主要是 assigner 部分的差异。YOLOv5 采用的是非常简单且高效的 shape 匹配 assigner,而 RTMDet 中采用的是动态的全 batch 计算的 dynamic soft label assigner,其内部的 Cost 矩阵需要消耗比较多的显存,特别是当前 batch 中标注框过多时候。
|
| 74 |
+
后续我们会考虑解决这个问题。
|
| 75 |
+
|
| 76 |
+
## 修改一些代码后是否需要重新安装 MMYOLO
|
| 77 |
+
|
| 78 |
+
在不新增 py 代码情况下, 如果你遵循最佳实践,即使用 `mim install -v -e .` 安装的 MMYOLO,则对本地代码所作的任何修改都会生效,无需重新安装。但是如果你是新增了 py 文件然后在里面新增的代码,则依然需要重新安装即运行 `mim install -v -e .`。
|
| 79 |
+
|
| 80 |
+
## 如何使用多个 MMYOLO 版本进行开发
|
| 81 |
+
|
| 82 |
+
若你拥有多个 MMYOLO 工程文件夹,例如 mmyolo-v1, mmyolo-v2。 在使用不同版本 MMYOLO 时候,你可以在终端运行前设置
|
| 83 |
+
|
| 84 |
+
```shell
|
| 85 |
+
PYTHONPATH="$(dirname $0)/..":$PYTHONPATH
|
| 86 |
+
```
|
| 87 |
+
|
| 88 |
+
使得当前环境生效。如果要使用环境中安装默认的 MMYOLO 而不是当前正在在使用的,可以删除出现上述命令或者通过如下命令重置
|
| 89 |
+
|
| 90 |
+
```shell
|
| 91 |
+
unset PYTHONPATH
|
| 92 |
+
```
|
| 93 |
+
|
| 94 |
+
## 训练中保存最好模型
|
| 95 |
+
|
| 96 |
+
用户可以通过在配置中设置 `default_hooks.checkpoint.save_best` 参数来选择根据什么指标来筛选最优模型。以 `COCO` 数据集检测任务为例,
|
| 97 |
+
`default_hooks.checkpoint.save_best` 可以选择输入的参数有:
|
| 98 |
+
|
| 99 |
+
1. `auto` 将会根据验证集中的第一个评价指标作为筛选条件。
|
| 100 |
+
2. `coco/bbox_mAP` 将会根据 `bbox_mAP` 作为筛选条件。
|
| 101 |
+
3. `coco/bbox_mAP_50` 将会根据 `bbox_mAP_50` 作为筛选条件。
|
| 102 |
+
4. `coco/bbox_mAP_75` 将会根据 `bbox_mAP_75` 作为筛选条件。
|
| 103 |
+
5. `coco/bbox_mAP_s` 将会根据 `bbox_mAP_s` 作为筛选条件。
|
| 104 |
+
6. `coco/bbox_mAP_m` 将会根据 `bbox_mAP_m` 作为筛选条件。
|
| 105 |
+
7. `coco/bbox_mAP_l` 将会根据 `bbox_mAP_l` 作为筛选条件。
|
| 106 |
+
|
| 107 |
+
此外用户还可以选择筛选的逻辑,通过设置配置中的 `default_hooks.checkpoint.rule` 来选择判断逻辑,如:`default_hooks.checkpoint.rule=greater` 表示指标越大越好。更详细的使用可以参考 [checkpoint_hook](https://github.com/open-mmlab/mmengine/blob/main/mmengine/hooks/checkpoint_hook.py) 来修改
|
| 108 |
+
|
| 109 |
+
## 如何进行非正方形输入尺寸训练和测试?
|
| 110 |
+
|
| 111 |
+
在 YOLO 系列算法中默认配置基本上都是 640x640 或者 1280x1280 正方形尺度输入训练的。用户如果想进行非正方形尺度训练,你可以修改配置中 `image_scale` 参数,并将其他对应位置进行修改即可。用户可以参考我们提供的 [yolov5_s-v61_fast_1xb12-40e_608x352_cat.py](https://github.com/open-mmlab/mmyolo/tree/dev/configs/yolov5/yolov5_s-v61_fast_1xb12-40e_608x352_cat.py) 配置。
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/tutorials/rotated_detection.md
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 旋转目标检测
|
| 2 |
+
|
| 3 |
+
旋转目标检测(Rotated Object Detection),又称为有向目标检测(Oriented Object Detection),试图在检测出目标位置的同时得到目标的方向信息。它通过重新定义目标表示形式,以及增加回归自由度数量的操作,实现旋转矩形、四边形甚至任意形状的目标检测。旋转目标检测在人脸识别、场景文字、遥感影像、自动驾驶、医学图像、机器人抓取等领域都有广泛应用。
|
| 4 |
+
|
| 5 |
+
关于旋转目标检测的详细介绍请参考文档 [MMRotate 基础知识](https://mmrotate.readthedocs.io/zh_CN/1.x/overview.html)
|
| 6 |
+
|
| 7 |
+
MMYOLO 中的旋转目标检测依赖于 MMRotate 1.x,请参考文档 [开始你的第一步](https://mmrotate.readthedocs.io/zh_CN/1.x/get_started.html) 安装 MMRotate 1.x。
|
| 8 |
+
|
| 9 |
+
本教程将介绍如何在 MMYOLO 中训练和使用旋转目标检测模型,目前支持了 RTMDet-R。
|
| 10 |
+
|
| 11 |
+
## 数据集准备
|
| 12 |
+
|
| 13 |
+
对于旋转目标检测数据集,目前最常用的数据集是 DOTA 数据集,由于DOTA数据集中的图像分辨率较大,因此需要进行切片处理,数据集准备请参考 [Preparing DOTA Dataset](https://github.com/open-mmlab/mmyolo/tools/dataset_converters/dota_split).
|
| 14 |
+
|
| 15 |
+
处理后的数据集结构如下:
|
| 16 |
+
|
| 17 |
+
```none
|
| 18 |
+
mmyolo
|
| 19 |
+
├── data
|
| 20 |
+
│ ├── split_ss_dota
|
| 21 |
+
│ │ ├── trainval
|
| 22 |
+
│ │ │ ├── images
|
| 23 |
+
│ │ │ ├── annfiles
|
| 24 |
+
│ │ ├── test
|
| 25 |
+
│ │ │ ├── images
|
| 26 |
+
│ │ │ ├── annfiles
|
| 27 |
+
│ ├── split_ms_dota
|
| 28 |
+
│ │ ├── trainval
|
| 29 |
+
│ │ │ ├── images
|
| 30 |
+
│ │ │ ├── annfiles
|
| 31 |
+
│ │ ├── test
|
| 32 |
+
│ │ │ ├── images
|
| 33 |
+
│ │ │ ├── annfiles
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
其中 `split_ss_dota` 是单尺度切片,`split_ms_dota` 是多尺度切片,可以根据需要选择。
|
| 37 |
+
|
| 38 |
+
对于自定义数据集,我们建议将数据转换为 DOTA 格式并离线进行转换,如此您只需在数据转换后修改 config 的数据标注路径和类别即可。
|
| 39 |
+
|
| 40 |
+
为了方便使用,我们同样提供了基于 COCO 格式的旋转标注格式,将多边形检测框储存在 COCO 标注的 segmentation 标签中,示例如下:
|
| 41 |
+
|
| 42 |
+
```json
|
| 43 |
+
{
|
| 44 |
+
"id": 131,
|
| 45 |
+
"image_id": 72,
|
| 46 |
+
"bbox": [123, 167, 11, 37],
|
| 47 |
+
"area": 271.5,
|
| 48 |
+
"category_id": 1,
|
| 49 |
+
"segmentation": [[123, 167, 128, 204, 134, 201, 132, 167]],
|
| 50 |
+
"iscrowd": 0,
|
| 51 |
+
}
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
## 配置文件
|
| 55 |
+
|
| 56 |
+
这里以 RTMDet-R 为例介绍旋转目标检测的配置文件,其中大部分和水平检测模型相同,主要介绍它们的差异,包括数据集和评测器配置、检测头、可视化等。
|
| 57 |
+
|
| 58 |
+
得益于 MMEngine 的配置文件系统,大部分模块都可以调用 MMRotate 中的模块。
|
| 59 |
+
|
| 60 |
+
### 数据集和评测器配置
|
| 61 |
+
|
| 62 |
+
关于配置文件的基础请先阅读 [学习 YOLOV5 配置文件](./config.md). 下面介绍旋转目标检测的一些必要设置。
|
| 63 |
+
|
| 64 |
+
```python
|
| 65 |
+
dataset_type = 'YOLOv5DOTADataset' # 数据集类型,这将被用来定义数据集
|
| 66 |
+
data_root = 'data/split_ss_dota/' # 数据的根路径
|
| 67 |
+
|
| 68 |
+
angle_version = 'le90' # 角度范围的定义,目前支持 oc, le90 和 le135
|
| 69 |
+
|
| 70 |
+
train_pipeline = [
|
| 71 |
+
# 训练数据读取流程
|
| 72 |
+
dict(
|
| 73 |
+
type='LoadImageFromFile'), # 第 1 个流程,从文件路径里加载图像
|
| 74 |
+
dict(type='LoadAnnotations', # 第 2 个流程,对于当前图像,加载它的注释信息
|
| 75 |
+
with_bbox=True, # 是否使用标注框 (bounding box),目标检测需要设置为 True
|
| 76 |
+
box_type='qbox'), # 指定读取的标注格式,旋转框数据集默认的数据格式为四边形
|
| 77 |
+
dict(type='mmrotate.ConvertBoxType', # 第 3 个流程,转换标注格式
|
| 78 |
+
box_type_mapping=dict(gt_bboxes='rbox')), # 将四边形标注转化为旋转框标注
|
| 79 |
+
|
| 80 |
+
# 训练数据处理流程
|
| 81 |
+
dict(type='mmdet.Resize', scale=(1024, 1024), keep_ratio=True),
|
| 82 |
+
dict(type='mmdet.RandomFlip',
|
| 83 |
+
prob=0.75,
|
| 84 |
+
direction=['horizontal', 'vertical', 'diagonal']),
|
| 85 |
+
dict(type='mmrotate.RandomRotate', # 旋转数据增强
|
| 86 |
+
prob=0.5, # 旋转概率 0.5
|
| 87 |
+
angle_range=180, # 旋转范围 180
|
| 88 |
+
rotate_type='mmrotate.Rotate', # 旋转方法
|
| 89 |
+
rect_obj_labels=[9, 11]), # 由于 DOTA 数据集中标号为 9 的 'storage-tank' 和标号 11 的 'roundabout' 两类为正方形标注,无需角度信息,旋转中将这两类保持为水平
|
| 90 |
+
dict(type='mmdet.Pad', size=img_scale, pad_val=dict(img=(114, 114, 114))),
|
| 91 |
+
dict(type='RegularizeRotatedBox', # 统一旋转框表示形式
|
| 92 |
+
angle_version=angle_version), # 根据角度的定义方式进行
|
| 93 |
+
dict(type='mmdet.PackDetInputs')
|
| 94 |
+
]
|
| 95 |
+
|
| 96 |
+
train_dataloader = dict(
|
| 97 |
+
batch_size=train_batch_size_per_gpu,
|
| 98 |
+
num_workers=train_num_workers,
|
| 99 |
+
persistent_workers=persistent_workers,
|
| 100 |
+
pin_memory=True,
|
| 101 |
+
collate_fn=dict(type='yolov5_collate'),
|
| 102 |
+
sampler=dict(type='DefaultSampler', shuffle=True),
|
| 103 |
+
dataset=dict( # 训练数据集的配置
|
| 104 |
+
type=dataset_type,
|
| 105 |
+
data_root=data_root,
|
| 106 |
+
ann_file='trainval/annfiles/', # 标注文件夹路径
|
| 107 |
+
data_prefix=dict(img_path='trainval/images/'), # 图像路径前缀
|
| 108 |
+
img_shape=(1024, 1024), # 图像大小
|
| 109 |
+
filter_cfg=dict(filter_empty_gt=True), # 标注的过滤配置
|
| 110 |
+
pipeline=train_pipeline)) # 这是由之前创建的 train_pipeline 定义的数据处理流程
|
| 111 |
+
```
|
| 112 |
+
|
| 113 |
+
RTMDet-R 保持论文内的配置,默认仅采用随机旋转增强,得益于 BoxType 设计,在数据增强阶段,大部分增强无需改动代码即可直接支持,例如 MixUp 和 Mosaic 等,可以直接在 pipeline 中使用。
|
| 114 |
+
|
| 115 |
+
```{Warning}
|
| 116 |
+
目前已知 Albu 数据增强仅支持水平框,在使用其他的数据增强时建议先使用 可视化数据集脚本 `browse_dataset.py` 验证数据增强是否正确。
|
| 117 |
+
```
|
| 118 |
+
|
| 119 |
+
RTMDet-R 测试阶段仅采用 Resize 和 Pad,在验证和评测时,都采用相同的数据流进行推理。
|
| 120 |
+
|
| 121 |
+
```python
|
| 122 |
+
val_pipeline = [
|
| 123 |
+
dict(type='LoadImageFromFile', backend_args=_base_.backend_args),
|
| 124 |
+
dict(type='mmdet.Resize', scale=(1024, 1024), keep_ratio=True),
|
| 125 |
+
dict(
|
| 126 |
+
type='mmdet.Pad', size=(1024, 1024),
|
| 127 |
+
pad_val=dict(img=(114, 114, 114))),
|
| 128 |
+
# 和训练时一致,先读取标注再转换标注格式
|
| 129 |
+
dict(
|
| 130 |
+
type='LoadAnnotations',
|
| 131 |
+
with_bbox=True,
|
| 132 |
+
box_type='qbox',
|
| 133 |
+
_scope_='mmdet'),
|
| 134 |
+
dict(
|
| 135 |
+
type='mmrotate.ConvertBoxType',
|
| 136 |
+
box_type_mapping=dict(gt_bboxes='rbox')),
|
| 137 |
+
dict(
|
| 138 |
+
type='mmdet.PackDetInputs',
|
| 139 |
+
meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape',
|
| 140 |
+
'scale_factor'))
|
| 141 |
+
]
|
| 142 |
+
|
| 143 |
+
val_dataloader = dict(
|
| 144 |
+
batch_size=val_batch_size_per_gpu,
|
| 145 |
+
num_workers=val_num_workers,
|
| 146 |
+
persistent_workers=persistent_workers,
|
| 147 |
+
pin_memory=True,
|
| 148 |
+
drop_last=False,
|
| 149 |
+
sampler=dict(type='DefaultSampler', shuffle=False),
|
| 150 |
+
dataset=dict(
|
| 151 |
+
type=dataset_type,
|
| 152 |
+
data_root=data_root,
|
| 153 |
+
ann_file='trainval/annfiles/',
|
| 154 |
+
data_prefix=dict(img_path='trainval/images/'),
|
| 155 |
+
img_shape=(1024, 1024),
|
| 156 |
+
test_mode=True,
|
| 157 |
+
batch_shapes_cfg=batch_shapes_cfg,
|
| 158 |
+
pipeline=val_pipeline))
|
| 159 |
+
```
|
| 160 |
+
|
| 161 |
+
[评测器](https://mmengine.readthedocs.io/zh_CN/latest/tutorials/evaluation.html) 用于计算训练模型在验证和测试数据集上的指标。评测器的配置由一个或一组评价指标(Metric)配置组成:
|
| 162 |
+
|
| 163 |
+
```python
|
| 164 |
+
val_evaluator = dict( # 验证过程使用的评测器
|
| 165 |
+
type='mmrotate.DOTAMetric', # 用于评估旋转目标检测的 mAP 的 dota 评价指标
|
| 166 |
+
metric='mAP' # 需要计算的评价指标
|
| 167 |
+
)
|
| 168 |
+
test_evaluator = val_evaluator # 测试过程使用的评测器
|
| 169 |
+
```
|
| 170 |
+
|
| 171 |
+
由于 DOTA 测试数据集没有标注文件, 如果要保存在测试数据集上的检测结果,则可以像这样编写配置:
|
| 172 |
+
|
| 173 |
+
```python
|
| 174 |
+
# 在测试集上推理,
|
| 175 |
+
# 并将检测结果转换格式以用于提交结果
|
| 176 |
+
test_dataloader = dict(
|
| 177 |
+
batch_size=val_batch_size_per_gpu,
|
| 178 |
+
num_workers=val_num_workers,
|
| 179 |
+
persistent_workers=True,
|
| 180 |
+
drop_last=False,
|
| 181 |
+
sampler=dict(type='DefaultSampler', shuffle=False),
|
| 182 |
+
dataset=dict(
|
| 183 |
+
type=dataset_type,
|
| 184 |
+
data_root=data_root,
|
| 185 |
+
data_prefix=dict(img_path='test/images/'),
|
| 186 |
+
img_shape=(1024, 1024),
|
| 187 |
+
test_mode=True,
|
| 188 |
+
batch_shapes_cfg=batch_shapes_cfg,
|
| 189 |
+
pipeline=test_pipeline))
|
| 190 |
+
test_evaluator = dict(
|
| 191 |
+
type='mmrotate.DOTAMetric',
|
| 192 |
+
format_only=True, # 只将模型输出转换为 DOTA 的 txt 提交格式并压缩成 zip
|
| 193 |
+
merge_patches=True, # 将切片结果合并成大图检测结果
|
| 194 |
+
outfile_prefix='./work_dirs/dota_detection/submission') # 输出测试文件夹的路径
|
| 195 |
+
```
|
| 196 |
+
|
| 197 |
+
如果使用基于 COCO 格式的旋转框标注,只需要修改 pipeline 中数据读取流程和训练数据集的配置,以训练数据为例:
|
| 198 |
+
|
| 199 |
+
```python
|
| 200 |
+
|
| 201 |
+
dataset_type='YOLOv5CocoDataset'
|
| 202 |
+
|
| 203 |
+
train_pipeline = [
|
| 204 |
+
# 训练数据读取流程
|
| 205 |
+
dict(
|
| 206 |
+
type='LoadImageFromFile'), # 第 1 个流程,从文件路径里加载图像
|
| 207 |
+
dict(type='LoadAnnotations', # 第 2 个流程,对于当前图像,加载它的注释信息
|
| 208 |
+
with_bbox=True, # 是否使用标注框 (bounding box),目标检测需要设置为 True
|
| 209 |
+
with_mask=True, # 读取储存在 segmentation 标注中的多边形标注
|
| 210 |
+
poly2mask=False) # 不执行 poly2mask,后续会将 poly 转化成检测框
|
| 211 |
+
dict(type='ConvertMask2BoxType', # 第 3 个流程,将 mask 标注转化为 boxtype
|
| 212 |
+
box_type='rbox'), # 目标类型是 rbox 旋转框
|
| 213 |
+
# 剩余的其他 pipeline
|
| 214 |
+
...
|
| 215 |
+
]
|
| 216 |
+
|
| 217 |
+
metainfo = dict( # DOTA 数据集的 metainfo
|
| 218 |
+
classes=('plane', 'baseball-diamond', 'bridge', 'ground-track-field',
|
| 219 |
+
'small-vehicle', 'large-vehicle', 'ship', 'tennis-court',
|
| 220 |
+
'basketball-court', 'storage-tank', 'soccer-ball-field',
|
| 221 |
+
'roundabout', 'harbor', 'swimming-pool', 'helicopter'))
|
| 222 |
+
|
| 223 |
+
train_dataloader = dict(
|
| 224 |
+
dataset=dict( # 训练数据集的配置
|
| 225 |
+
type=dataset_type,
|
| 226 |
+
metainfo=metainfo,
|
| 227 |
+
data_root=data_root,
|
| 228 |
+
ann_file='train/train.json', # 标注文件路径
|
| 229 |
+
data_prefix=dict(img='train/images/'), # 图像路径前缀
|
| 230 |
+
filter_cfg=dict(filter_empty_gt=True), # 标注的过滤配置
|
| 231 |
+
pipeline=train_pipeline), # 数据处理流程
|
| 232 |
+
)
|
| 233 |
+
```
|
| 234 |
+
|
| 235 |
+
### 模型配置
|
| 236 |
+
|
| 237 |
+
对于旋转目标检测器,在模型配置中 backbone 和 neck 的配置和其他模型是一致的,主要差异在检测头上。目前仅支持 RTMDet-R 旋转目标检测,下面介绍新增的参数:
|
| 238 |
+
|
| 239 |
+
1. `angle_version` 角度范围,用于在训练时限制角度的范围,可选的角度范围有 `le90`, `le135` 和 `oc`。
|
| 240 |
+
|
| 241 |
+
2. `angle_coder` 角度编码器,和 bbox coder 类似,用于编码和解码角度。
|
| 242 |
+
|
| 243 |
+
默认使用的角度编码器是 `PseudoAngleCoder`,即”伪角度编码器“,并不进行编解码,直接回归角度参数。这样设计的目标是能更好的自定义角度编码方式,而无需重写代码,例如 CSL,DCL,PSC 等方法。
|
| 244 |
+
|
| 245 |
+
3. `use_hbbox_loss` 是否使用水平框 loss。考虑到部分角度编码解码过程不可导,直接使用旋转框的损失函数无法学习角度,因此引入该参数用于将框和角度分开训练。
|
| 246 |
+
|
| 247 |
+
4. `loss_angle` 角度损失函数。在设定`use_hbbox_loss=True` 时必须设定,而使用旋转框损失时可选,此时可以作为回归损失的辅助。
|
| 248 |
+
|
| 249 |
+
通过组合 `use_hbbox_loss` 和 `loss_angle` 可以控制旋转框训练时的回归损失计算方式,共有三种组合方式:
|
| 250 |
+
|
| 251 |
+
- `use_hbbox_loss=False` 且 `loss_angle` 为 None.
|
| 252 |
+
|
| 253 |
+
此时框预测和角度预测进行合并,直接对旋转框预测进行回归,此时 `loss_bbox` 应当设定为旋转框损失,例如 `RotatedIoULoss`。
|
| 254 |
+
这种方案和水平检测模型的回归方式基本一致,只是多了额外的角度编解码过程。
|
| 255 |
+
|
| 256 |
+
```
|
| 257 |
+
bbox_pred────(tblr)───┐
|
| 258 |
+
▼
|
| 259 |
+
angle_pred decode──►rbox_pred──(xywha)─►loss_bbox
|
| 260 |
+
│ ▲
|
| 261 |
+
└────►decode──(a)─┘
|
| 262 |
+
```
|
| 263 |
+
|
| 264 |
+
- `use_hbbox_loss=False`,同时设定 `loss_angle`.
|
| 265 |
+
|
| 266 |
+
此时会增加额外的角度回归和分类损失,具体的角度损失类型需要根据角度编码器 `angle_code` 进行选择。
|
| 267 |
+
|
| 268 |
+
```
|
| 269 |
+
bbox_pred────(tblr)───┐
|
| 270 |
+
▼
|
| 271 |
+
angle_pred decode──►rbox_pred──(xywha)─►loss_bbox
|
| 272 |
+
│ ▲
|
| 273 |
+
├────►decode──(a)─┘
|
| 274 |
+
│
|
| 275 |
+
└───────────────────────────────────────────►loss_angle
|
| 276 |
+
```
|
| 277 |
+
|
| 278 |
+
- `use_hbbox_loss=True` 且 `loss_angle` 为 None.
|
| 279 |
+
|
| 280 |
+
此时框预测和角度预测完全分离,将两个分支视作两个任务进行训练。
|
| 281 |
+
此时 `loss_bbox` 要设定为水平框的损失函数,例如 `IoULoss` 。
|
| 282 |
+
|
| 283 |
+
```
|
| 284 |
+
bbox_pred──(tblr)──►decode──►hbox_pred──(xyxy)──►loss_bbox
|
| 285 |
+
|
| 286 |
+
angle_pred──────────────────────────────────────►loss_angle
|
| 287 |
+
```
|
| 288 |
+
|
| 289 |
+
除了检测头中的参数,在test_cfg中还增加了 `decoded_with_angle` 参数用来控制推理时角度的处理逻辑,默认设定为 True 。
|
| 290 |
+
设计这个参数的目标是让训练过程和推理过程的逻辑对齐,该参数会影响最终的精度。
|
| 291 |
+
|
| 292 |
+
当 `decoded_with_angle=True` 时,将框和角度同时送入 `bbox_coder` 中。
|
| 293 |
+
此时要使用旋转框的编解码器,例如`DistanceAnglePointCoder`。
|
| 294 |
+
|
| 295 |
+
```
|
| 296 |
+
bbox_pred────(tblr)───┐
|
| 297 |
+
▼
|
| 298 |
+
angle_pred decode──(xywha)──►rbox_pred
|
| 299 |
+
│ ▲
|
| 300 |
+
└────►decode──(a)─┘
|
| 301 |
+
```
|
| 302 |
+
|
| 303 |
+
当 `decoded_with_angle=False` 时,首先解码出水平检测框,之后将角度 concat 到检测框。
|
| 304 |
+
此时要使用水平框的编解码器,例如`DistancePointBBoxCoder`。
|
| 305 |
+
|
| 306 |
+
```
|
| 307 |
+
bbox_pred──(tblr)─►decode
|
| 308 |
+
│ (xyxy)
|
| 309 |
+
▼
|
| 310 |
+
format───(xywh)──►concat──(xywha)──►rbox_pred
|
| 311 |
+
▲
|
| 312 |
+
angle_pred────────►decode────(a)───────┘
|
| 313 |
+
```
|
| 314 |
+
|
| 315 |
+
### 可视化器
|
| 316 |
+
|
| 317 |
+
由于旋转框和水平框的差异,旋转目标检测模型需要使用 MMRotate 中的 `RotLocalVisualizer`,配置如下:
|
| 318 |
+
|
| 319 |
+
```python
|
| 320 |
+
vis_backends = [dict(type='LocalVisBackend')] # 可视化后端,请参考 https://mmengine.readthedocs.io/zh_CN/latest/advanced_tutorials/visualization.html
|
| 321 |
+
visualizer = dict(
|
| 322 |
+
type='mmrotate.RotLocalVisualizer', vis_backends=vis_backends, name='visualizer')
|
| 323 |
+
```
|
| 324 |
+
|
| 325 |
+
## 实用工具
|
| 326 |
+
|
| 327 |
+
目前测试可用的工具包括:
|
| 328 |
+
|
| 329 |
+
[可视化数据集](../useful_tools/browse_dataset.md)
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/tutorials/warning_notes.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 常见警告说明
|
| 2 |
+
|
| 3 |
+
本文档收集用户经常疑惑的警告信息说明,方便大家理解。
|
| 4 |
+
|
| 5 |
+
## xxx registry in mmyolo did not set import location
|
| 6 |
+
|
| 7 |
+
完整信息为 The xxx registry in mmyolo did not set import location. Fallback to call `mmyolo.utils.register_all_modules` instead.。
|
| 8 |
+
这个警告的含义说某个模块在导入时候发现没有设置导入的 location,导致无法确定其位置,因此会自动调用 `mmyolo.utils.register_all_modules` 触发包的导入。这个警告属于 MMEngine 中非常底层的模块警告,
|
| 9 |
+
用户理解起来可能比较困难,不过对大家使用没有任何影响,可以直接忽略。
|
| 10 |
+
|
| 11 |
+
## save_param_schedulers is true but self.param_schedulers is None
|
| 12 |
+
|
| 13 |
+
以 YOLOv5 算法为例,这是因为 YOLOv5 中重新写了参数调度器策略 `YOLOv5ParamSchedulerHook`,因此 MMEngine 中设计的 ParamScheduler 是没有使用的,但是 YOLOv5 配置中也没有设置 `save_param_schedulers` 为 False。
|
| 14 |
+
首先这个警告对性能和恢复训练没有任何影响,用户如果觉得这个警告会影响体验,可以设置 `default_hooks.checkpoint.save_param_scheduler` 为 False 或者训练时候通过命令行设置 `--cfg-options default_hooks.checkpoint.save_param_scheduler=False` 即可。
|
| 15 |
+
|
| 16 |
+
## The loss_cls will be 0. This is a normal phenomenon.
|
| 17 |
+
|
| 18 |
+
这个和具体算法有关。以 YOLOv5 为例,其分类 loss 是只考虑正样本的,如果类别是 1,那么分类 loss 和 obj loss 就是功能重复的了,因此在设计上当类别是 1 的时候 loss_cls 是不计算的,因此始终是 0,这是正常现象。
|
| 19 |
+
|
| 20 |
+
## The model and loaded state dict do not match exactly
|
| 21 |
+
|
| 22 |
+
这个警告是否会影响性能要根据进一步的打印信息来确定。如果是在微调模式下,由于用户自定义类别不一样无法加载 Head 模块的 COCO 预训练权重,这是一个正常现象,不会影响性能。
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/useful_tools/browse_coco_json.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 可视化 COCO 标签
|
| 2 |
+
|
| 3 |
+
脚本 `tools/analysis_tools/browse_coco_json.py` 能够使用可视化显示 COCO 标签在图片的情况。
|
| 4 |
+
|
| 5 |
+
```shell
|
| 6 |
+
python tools/analysis_tools/browse_coco_json.py [--data-root ${DATA_ROOT}] \
|
| 7 |
+
[--img-dir ${IMG_DIR}] \
|
| 8 |
+
[--ann-file ${ANN_FILE}] \
|
| 9 |
+
[--wait-time ${WAIT_TIME}] \
|
| 10 |
+
[--disp-all] [--category-names CATEGORY_NAMES [CATEGORY_NAMES ...]] \
|
| 11 |
+
[--shuffle]
|
| 12 |
+
```
|
| 13 |
+
|
| 14 |
+
其中,如果图片、标签都在同一个文件夹下的话,可以指定 `--data-root` 到该文件夹,然后 `--img-dir` 和 `--ann-file` 指定该文件夹的相对路径,代码会自动拼接。
|
| 15 |
+
如果图片、标签文件不在同一个文件夹下的话,则无需指定 `--data-root` ,直接指定绝对路径的 `--img-dir` 和 `--ann-file` 即可。
|
| 16 |
+
|
| 17 |
+
例子:
|
| 18 |
+
|
| 19 |
+
1. 查看 `COCO` 全部类别,同时展示 `bbox`、`mask` 等所有类型的标注:
|
| 20 |
+
|
| 21 |
+
```shell
|
| 22 |
+
python tools/analysis_tools/browse_coco_json.py --data-root './data/coco' \
|
| 23 |
+
--img-dir 'train2017' \
|
| 24 |
+
--ann-file 'annotations/instances_train2017.json' \
|
| 25 |
+
--disp-all
|
| 26 |
+
```
|
| 27 |
+
|
| 28 |
+
如果图片、标签不在同一个文件夹下的话,可以使用绝对路径:
|
| 29 |
+
|
| 30 |
+
```shell
|
| 31 |
+
python tools/analysis_tools/browse_coco_json.py --img-dir '/dataset/image/coco/train2017' \
|
| 32 |
+
--ann-file '/label/instances_train2017.json' \
|
| 33 |
+
--disp-all
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
2. 查看 `COCO` 全部类别,同时仅展示 `bbox` 类型的标注,并打乱显示:
|
| 37 |
+
|
| 38 |
+
```shell
|
| 39 |
+
python tools/analysis_tools/browse_coco_json.py --data-root './data/coco' \
|
| 40 |
+
--img-dir 'train2017' \
|
| 41 |
+
--ann-file 'annotations/instances_train2017.json' \
|
| 42 |
+
--shuffle
|
| 43 |
+
```
|
| 44 |
+
|
| 45 |
+
3. 只查看 `bicycle` 和 `person` 类别,同时仅展示 `bbox` 类型的标注:
|
| 46 |
+
|
| 47 |
+
```shell
|
| 48 |
+
python tools/analysis_tools/browse_coco_json.py --data-root './data/coco' \
|
| 49 |
+
--img-dir 'train2017' \
|
| 50 |
+
--ann-file 'annotations/instances_train2017.json' \
|
| 51 |
+
--category-names 'bicycle' 'person'
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
4. 查看 `COCO` 全部类别,同时展示 `bbox`、`mask` 等所有类型的标注,并打乱显示:
|
| 55 |
+
|
| 56 |
+
```shell
|
| 57 |
+
python tools/analysis_tools/browse_coco_json.py --data-root './data/coco' \
|
| 58 |
+
--img-dir 'train2017' \
|
| 59 |
+
--ann-file 'annotations/instances_train2017.json' \
|
| 60 |
+
--disp-all \
|
| 61 |
+
--shuffle
|
| 62 |
+
```
|
video_gen_14d/third_party/VBench/VBench-2.0/vbench2/third_party/ViTDetector/third_party/YOLO-World/mmyolo/docs/zh_cn/useful_tools/browse_dataset.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 可视化数据集
|
| 2 |
+
|
| 3 |
+
```shell
|
| 4 |
+
python tools/analysis_tools/browse_dataset.py \
|
| 5 |
+
${CONFIG_FILE} \
|
| 6 |
+
[-o, --out-dir ${OUTPUT_DIR}] \
|
| 7 |
+
[-p, --phase ${DATASET_PHASE}] \
|
| 8 |
+
[-n, --show-number ${NUMBER_IMAGES_DISPLAY}] \
|
| 9 |
+
[-i, --show-interval ${SHOW_INTERRVAL}] \
|
| 10 |
+
[-m, --mode ${DISPLAY_MODE}] \
|
| 11 |
+
[--cfg-options ${CFG_OPTIONS}]
|
| 12 |
+
```
|
| 13 |
+
|
| 14 |
+
**所有参数的说明**:
|
| 15 |
+
|
| 16 |
+
- `config` : 模型配置文件的路径。
|
| 17 |
+
- `-o, --out-dir`: 保存图片文件夹,如果没有指定,默认为 `'./output'`。
|
| 18 |
+
- **`-p, --phase`**: 可视化数据集的阶段,只能为 `['train', 'val', 'test']` 之一,默认为 `'train'`。
|
| 19 |
+
- **`-n, --show-number`**: 可视化样本数量。如果没有指定,默认展示数据集的所有图片。
|
| 20 |
+
- **`-m, --mode`**: 可视化的模式,只能为 `['original', 'transformed', 'pipeline']` 之一。 默认为 `'transformed'`。
|
| 21 |
+
- `--cfg-options` : 对配置文件的修改,参考[学习配置文件](../tutorials/config.md)。
|
| 22 |
+
|
| 23 |
+
```shell
|
| 24 |
+
`-m, --mode` 用于设置可视化的模式,默认设置为 'transformed'。
|
| 25 |
+
- 如果 `--mode` 设置为 'original',则获取原始图片;
|
| 26 |
+
- 如果 `--mode` 设置为 'transformed',则获取预处理后的图片;
|
| 27 |
+
- 如果 `--mode` 设置为 'pipeline',则获得数据流水线所有中间过程图片。
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
**示例**:
|
| 31 |
+
|
| 32 |
+
1. **'original'** 模式 :
|
| 33 |
+
|
| 34 |
+
```shell
|
| 35 |
+
python ./tools/analysis_tools/browse_dataset.py configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py --phase val --out-dir tmp --mode original
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
- `--phase val`: 可视化验证集, 可简化为 `-p val`;
|
| 39 |
+
- `--out-dir tmp`: 可视化结果保存在 "tmp" 文件夹, 可简化为 `-o tmp`;
|
| 40 |
+
- `--mode original`: 可视化原图, 可简化为 `-m original`;
|
| 41 |
+
- `--show-number 100`: 可视化100张图,可简化为 `-n 100`;
|
| 42 |
+
|
| 43 |
+
2. **'transformed'** 模式 :
|
| 44 |
+
|
| 45 |
+
```shell
|
| 46 |
+
python ./tools/analysis_tools/browse_dataset.py configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
+
3. **'pipeline'** 模式 :
|
| 50 |
+
|
| 51 |
+
```shell
|
| 52 |
+
python ./tools/analysis_tools/browse_dataset.py configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py -m pipeline
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
<div align=center>
|
| 56 |
+
<img src="https://user-images.githubusercontent.com/45811724/204810831-0fbc7f1c-0951-4be1-a11c-491cf0d194f6.png" alt="Image">
|
| 57 |
+
</div>
|