未验证 提交 1eb496cd 编写于 作者: H HinGwenWoong 提交者: GitHub

[DOCS] Improve docs (#48)

* Improve docs

* Improve zh_cn docs

* Improve zh_cn docs

* Improve en docs

* Improve docs
上级 8f9cf4ba
......@@ -21,5 +21,5 @@ If this PR introduces a new feature, it is better to list some use cases here, a
1. Pre-commit or other linting tools are used to fix the potential lint issues.
2. The modification is covered by complete unit tests. If not, please add more unit test to ensure the correctness.
3. If the modification has potential influence on downstream projects, this PR should be tested with downstream projects, like MMDet or MMCls.
3. If the modification has potential influence on downstream projects, this PR should be tested with downstream projects, like MMDetection or MMClassification.
4. The documentation has been modified accordingly, like docstring or example tutorials.
......@@ -76,7 +76,7 @@ The master branch works with **PyTorch 1.6+**.
- Unified component interfaces based on [OpenMMLab 2.0](https://github.com/open-mmlab) and [MMDetection 3.0](https://github.com/open-mmlab/mmdetection/tree/3.x)
- Support for YOLOv5/YOLOX training and deployment, support for YOLOv6 inference and deployment
- Refactored YOLOX for MMDet to provide faster training and inference
- Refactored YOLOX for MMDetection to provide faster training and inference
- Detailed introductory and advanced tutorials are provided, see the [English tutorial](https://mmyolo.readthedocs.io/en/latest)
For release history and update details, please refer to [changelog](https://mmyolo.readthedocs.io/en/latest/notes/changelog.html).
......
......@@ -76,7 +76,7 @@ MMYOLO 是一个基于 PyTorch 的 YOLO 系列算法开源工具箱。它是 [Op
- 基于 [OpenMMLab 2.0](https://github.com/open-mmlab)[MMDetection 3.0](https://github.com/open-mmlab/mmdetection/tree/3.x) 统一了各组件接口。
- 支持 YOLOv5/YOLOX 训练和部署,支持 YOLOv6 推理和部署
- 重构了 MMDet 的 YOLOX,提供了更快的训练和推理速度
- 重构了 MMDetection 的 YOLOX,提供了更快的训练和推理速度
- 提供了详细入门和进阶教程,详见 [中文教程](https://mmyolo.readthedocs.io/zh_CN/latest)
发布历史和更新细节请参考 [更新日志](https://mmyolo.readthedocs.io/zh_CN/latest/notes/changelog.html)
......
# Mixed image data augmentation update
Mixed image data augmentation is similar to Mosaic and MixUp, in which the annotation information of multiple images needs to be fused during the runtime. In the OpenMMLab data augmentation pipeline, the other indexes of the dataset are generally not available. In order to achieve the above function, in the [MultiImageMixDataset](https://github.com/open-mmlab/mmdetection/blob/master/mmdet/datasets/dataset_wrappers.py#L338) the concept of dataset wrapper is proposed in YOLOX, which is reproduced in MMDet.
Mixed image data augmentation is similar to Mosaic and MixUp, in which the annotation information of multiple images needs to be fused during the runtime. In the OpenMMLab data augmentation pipeline, the other indexes of the dataset are generally not available. In order to achieve the above function, in the [MultiImageMixDataset](https://github.com/open-mmlab/mmdetection/blob/master/mmdet/datasets/dataset_wrappers.py#L338) the concept of dataset wrapper is proposed in YOLOX, which is reproduced in MMDetection.
`MultiImageMixDataset` dataset wrapper will include some data augmentation methods such as `Mosaic` and `RandAffine`, while `CocoDataset` will also include the `pipeline` to achieve the image and annotation loading function. In this way, we can achieve mixed data augmentation quickly. The configuration method is as follows:
......@@ -31,7 +31,7 @@ train_dataset = dict(
```
But above method will cause a problem: the users who are not familiar with MMDet, will forget to match data augmentation methods like Mosaic together with `MultiImageMixDataset`, which could extremely increase the Complexity, and it could be hard to understand.
But above method will cause a problem: the users who are not familiar with MMDetection, will forget to match data augmentation methods like Mosaic together with `MultiImageMixDataset`, which could extremely increase the Complexity, and it could be hard to understand.
To solve this problem we make a simplification in MMYOLO, which directly make `pipeline` catch the `dataset`, and make the data augmentation methods like `Mosaic` be achieved and used as random flip, without data wrapper anymore. The new configuration method is as follows:
......
......@@ -3,21 +3,23 @@
## YOLO series model basic class
The structural Graph is provided by RangeKing@GitHub. Thank you RangeKing!
![base class](https://user-images.githubusercontent.com/27466624/190986949-01414a91-baae-4228-8828-c59db58dcf36.jpg)
<div align=center>
<img src="https://user-images.githubusercontent.com/27466624/190986949-01414a91-baae-4228-8828-c59db58dcf36.jpg" width=800 alt="base class">
</div>
Most of the YOLO series algorithms adopt a unified algorithm building structure, typically as Darknet + PAFPN. In order to let users quickly understand the YOLO series algorithm architecture, we deliberately designed the BaseBackbone + BaseYOLONeck structure as shown in the above graph.
Most of the YOLO series algorithms adopt a unified algorithm building structure, typically as Darknet + PAFPN. In order to let users quickly understand the YOLO series algorithm architecture, we deliberately designed the `BaseBackbone` + `BaseYOLONeck` structure as shown in the above graph.
The benefit of abstract BaseBackbone includes:
The benefit of abstract `BaseBackbone` includes:
1. Subclasses do not need to concern about the forward process, just build the model as the builder pattern.
2. It can be configured to achieve custom plug-in functions, the users can easily insert some similar attention module.
3. All subclasses automatically support frozen certain stage and frozen bn functions.
BaseYOLONeck has the same benefit as BaseBackbone.
`BaseYOLONeck` has the same benefit as `BaseBackbone`.
### BaseBackbone
We can see in the above graph,as for P5,BaseBackbone include 1 stem layer and 4 stage layers which are similar to the basic structural of ResNet. Different backbone network algorithms inheritance the BaseBackbone, users can achieve construction of every layer of the network by using self-custom basic module through `build_xx` method.
We can see in the above graph,as for P5,`BaseBackbone` include 1 stem layer and 4 stage layers which are similar to the basic structural of ResNet. Different backbone network algorithms inheritance the `BaseBackbone`, users can achieve construction of every layer of the network by using self-custom basic module through `build_xx` method.
### BaseYOLONeck
......@@ -25,22 +27,22 @@ We reproduce the YOLO series Neck component by the similar method of the BaseBac
### BaseDenseHead
The YOLO series uses the BaseDenseHead designed in MMDet as the base class of the Head structure. Take YOLOv5 as an example, [HeadModule](https://github.com/open-mmlab/mmyolo/blob/main/mmyolo/models/dense_heads/yolov5_head.py#L2) class's forward function replace original forward method.
The YOLO series uses the BaseDenseHead designed in MMDetection as the base class of the Head structure. Take YOLOv5 as an example, [HeadModule](https://github.com/open-mmlab/mmyolo/blob/main/mmyolo/models/dense_heads/yolov5_head.py#L2) class's forward function replace original forward method.
## HeadModule
<div align=center>
<img src="https://user-images.githubusercontent.com/33799979/190407754-c725fe85-a71b-4e45-912b-34513d1ff128.png" width=800>
<img src="https://user-images.githubusercontent.com/33799979/190407754-c725fe85-a71b-4e45-912b-34513d1ff128.png" width=800 alt="image">
</div>
Methods implementation in the [MMDetection](https://github.com/open-mmlab/mmdetection) is shown in the above graphThe solid line is the implementation in [MMYOLO](https://github.com/open-mmlab/mmyolo/blob/main/mmyolo/models/dense_heads/yolov5_head.py), which has the following advantages over the original implementation:
Methods implementation in the [MMDetection](https://github.com/open-mmlab/mmdetection) is shown in the above graph. The solid line is the implementation in [MMYOLO](https://github.com/open-mmlab/mmyolo/blob/main/mmyolo/models/dense_heads/yolov5_head.py), which has the following advantages over the original implementation:
1. MMDet in the bbox_head split into assigner + box coder + sampler three large components, but for the generality of passing through the 3 components , the model need to encapsulate additional objects to handle, and after the unification, the user needn't separate them. The benefits of not deliberately forcing the division of the three components are: no longer need to data encapsulation of internal data, simplifying the code logic, reducing the difficulty of use and the difficulty of algorithm implementation.
1. MMDetection in the `bbox_head` split into `assigner` + `box coder` + `sampler` three large components, but for the generality of passing through the 3 components , the model need to encapsulate additional objects to handle, and after the unification, the user needn't separate them. The benefits of not deliberately forcing the division of the three components are: no longer need to data encapsulation of internal data, simplifying the code logic, reducing the difficulty of use and the difficulty of algorithm implementation.
2. MMYOLO is Faster, the user can customize the implementation of the algorithm when the original framework does not depend on the deep optimization of part of the code.
In general, in the MMYOLO, they only need to implement the decouple of the model + loss_by_feat parts, and users can achieve any model with any `loss_by_feat` calculation process through modify the configuration. For example, applying the YOLOX `loss_by_feat` to the YOLOv5 model, etc.
In general, in the MMYOLO, they only need to implement the decouple of the model + `loss_by_feat` parts, and users can achieve any model with any `loss_by_feat` calculation process through modify the configuration. For example, applying the YOLOX `loss_by_feat` to the YOLOv5 model, etc.
Taking the YOLOX configuration in MMDet as an example, the Head module configuration is written as follows:
Taking the YOLOX configuration in MMDetection as an example, the Head module configuration is written as follows:
```python
bbox_head=dict(
......
......@@ -143,7 +143,7 @@ pip install "mmcv>=2.0.0rc1" -f https://download.openmmlab.com/mmcv/dist/cu116/t
#### Install on CPU-only platforms
MMDetection can be built for the CPU-only environment. In CPU mode you can train (requires MMCV version >= 2.0.0rc1), test, or infer a model.
MMDetection can be built for the CPU-only environment. In CPU mode you can train (requires MMCV version >= `2.0.0rc1`), test, or infer a model.
However, some functionalities are gone in this mode:
......@@ -217,7 +217,8 @@ docker build -t mmyolo docker/
Run it with
```shell
docker run --gpus all --shm-size=8g -it -v {DATA_DIR}:/mmyolo/data mmyolo
DATA_DIR=/path/to/your/dataset
docker run --gpus all --shm-size=8g -it -v ${DATA_DIR}:/mmyolo/data mmyolo
```
### Troubleshooting
......@@ -227,7 +228,7 @@ You may [open an issue](https://github.com/open-mmlab/mmyolo/issues/new/choose)
### Develop using multiple MMYOLO versions
The training and testing scripts have been modified in PYTHONPATH to ensure that the scripts use MMYOLO in the current directory.
The training and testing scripts have been modified in `PYTHONPATH` to ensure that the scripts use MMYOLO in the current directory.
To have the default MMYOLO installed in your environment instead of what is currently in use, you can remove the code that appears in the relevant script:
......
......@@ -6,6 +6,6 @@ We have released MMYOLO open source library, which is based on MMEngine, MMCV 2.
### Highlights
1. Support for YOLOv5/YOLOX training and deployment, support for YOLOv6 inference and deployment
2. Refactored YOLOX for MMDet to provide faster training and inference
3. Detailed introductory and advanced tutorials are provided, see the [English tutorial](https://mmyolo.readthedocs.io/en/latest)
1. Support for YOLOv5/YOLOX training and deployment, support for YOLOv6 inference and deployment.
2. Refactored YOLOX for MMDetection to provide faster training and inference.
3. Detailed introductory and advanced tutorials are provided, see the [English tutorial](https://mmyolo.readthedocs.io/en/latest).
......@@ -8,7 +8,7 @@ This chapter introduces you to the overall framework of MMYOLO and provides link
MMYOLO is a YOLO series algorithm toolbox, which currently implements only the target detection task and will subsequently support various tasks such as instance segmentation, panoramic segmentation and key point detection. It includes a rich set of target detection algorithms and related components and modules, and the following is its overall framework.
MMYOLO file structure is identical to the MMDetection. To allow full reuse of the MMDetection code, MMYOLO includes only custom content, which consists of 3 main parts:datasets、models、engine.
MMYOLO file structure is identical to the MMDetection. To allow full reuse of the MMDetection code, MMYOLO includes only custom content, which consists of 3 main parts: `datasets`, `models`, `engine`.
- **datasets** supports a variety of data sets for target detection
- **transforms** includes various data enhancement transforms
......
......@@ -357,7 +357,7 @@ resume = False # Whether to resume from the checkpoint defined in `load_from`.
For all configs under the same folder, it is recommended to have only **one** _primitive_ config. All other configs should inherit from the _primitive_ config. In this way, the maximum of inheritance level is 3.
For easy understanding, we recommend contributors inherit from existing methods.
For example, if some modification is made based on YOLOv5s, such as modifying the depth of the network, users may first inherit the `_base_ = ./yolov5_s-v61_syncbn_8xb16-300e_coco.py `, then modify the necessary fields in the config files.
For example, if some modification is made based on YOLOv5-s, such as modifying the depth of the network, users may first inherit the `_base_ = ./yolov5_s-v61_syncbn_8xb16-300e_coco.py `, then modify the necessary fields in the config files.
If you are building an entirely new method that does not share the structure with any of the existing methods, you may create a folder `yolov100` under `configs`,
......@@ -509,7 +509,9 @@ model = dict(
### Reuse variables in \_base\_ file
If the users want to reuse the variables in the base file, they can get a copy of the corresponding variable by using `{{_base_.xxx}}`. The latest version of MMEngine also support reusing variables without `{{}}` usage. E.g:
If the users want to reuse the variables in the base file, they can get a copy of the corresponding variable by using `{{_base_.xxx}}`. The latest version of MMEngine also support reusing variables without `{{}}` usage.
E.g:
```python
_base_ = '../_base_/default_runtime.py'
......
......@@ -61,7 +61,7 @@ train_pipeline = [
]
```
一个稍微复杂点的包括 MixUp的 YOLOv5-m 配置如下所示:
一个稍微复杂点的包括 MixUp 的 YOLOv5-m 配置如下所示:
```python
mosaic_affine_pipeline = [
......
......@@ -4,21 +4,23 @@
下图为 RangeKing@GitHub 提供,非常感谢!
![基类](https://user-images.githubusercontent.com/27466624/190986949-01414a91-baae-4228-8828-c59db58dcf36.jpg)
<div align=center>
<img src="https://user-images.githubusercontent.com/27466624/190986949-01414a91-baae-4228-8828-c59db58dcf36.jpg" alt="基类">
</div>
YOLO 系列算法大部分采用了统一的算法搭建结构,典型的如 Darknet + PAFPN。为了让用户快速理解 YOLO 系列算法架构,我们特意设计了如上图中的 BaseBackbone + BaseYOLONeck 结构。
抽象 BaseBackbone 的好处包括:
1. 子类不需要关心 forward 过程,只要类似建造者模式一样构建模型即可
2. 可以通过配置实现定制插件功能,用户可以很方便的插入一些类似注意力模块
3. 所有子类自动支持 frozen 某些 stage 和 frozen bn 功能
1. 子类不需要关心 forward 过程,只要类似建造者模式一样构建模型即可
2. 可以通过配置实现定制插件功能,用户可以很方便的插入一些类似注意力模块
3. 所有子类自动支持 frozen 某些 stage 和 frozen bn 功能
抽取 BaseYOLONeck 也有同样好处。
### BaseBackbone
如上图所示,对于 P5 而言,BaseBackbone 包括1个 stem 层 + 4 个 stage 层的类似 ResNet 的基础结构,不同算法的主干网络继承 BaseBackbone,用户可以通过实现内部的 `build_xx` 方法,使用自定义的基础模块来构建每一层的内部结构。
如上图所示,对于 P5 而言,BaseBackbone 包括 1 个 stem 层 + 4 个 stage 层的类似 ResNet 的基础结构,不同算法的主干网络继承 BaseBackbone,用户可以通过实现内部的 `build_xx` 方法,使用自定义的基础模块来构建每一层的内部结构。
### BaseYOLONeck
......@@ -31,17 +33,17 @@ MMYOLO 系列沿用 MMDetection 中设计的 `BaseDenseHead` 作为 Head 结构
## HeadModule
<div align=center>
<img src="https://user-images.githubusercontent.com/33799979/190985845-ed303ad4-3a77-447b-83f9-1feba38d5e24.png" width=800>
<img src="https://user-images.githubusercontent.com/33799979/190985845-ed303ad4-3a77-447b-83f9-1feba38d5e24.png" width=800 alt="HeadModule">
</div>
如上图所示,虚线部分为 [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) 中的实现。与原先的实现相比具备以下优势:
1. MMDet 中将 bbox_head 拆分为 assigner + box coder + sampler 三个大的组件,但由于 3 个组件之间的传递为了通用性,需要封装额外的对象来处理,统一之后用户可以不用进行拆分。不刻意强求划分三大组件的好处为:不再需要对内部数据进行数据封装,简化了代码逻辑,减轻了社区使用难度和算法复现难度。
1. MMDetection 中将 `bbox_head` 拆分为 `assigner` + `box coder` + `sampler` 三个大的组件,但由于 3 个组件之间的传递为了通用性,需要封装额外的对象来处理,统一之后用户可以不用进行拆分。不刻意强求划分三大组件的好处为:不再需要对内部数据进行数据封装,简化了代码逻辑,减轻了社区使用难度和算法复现难度
2. 速度更快,用户在自定义实现算法时候,可以不依赖于原有框架,对部分代码进行深度优化。
总的来说,在 MMYOLO 中只需要做到 model + loss_by_feat 部分解耦,用户可以通过修改配置实现任意模型配合任意 `loss_by_feat` 计算过程。例如将 YOLOv5 模型应用 YOLOX 的 `loss_by_feat` 等。
总的来说,在 MMYOLO 中只需要做到 `model` + `loss_by_feat` 部分解耦,用户可以通过修改配置实现任意模型配合任意 `loss_by_feat` 计算过程。例如将 YOLOv5 模型应用 YOLOX 的 `loss_by_feat` 等。
以 MMDet 中 YOLOX 配置为例,其 Head 模块配置写法为:
以 MMDetection 中 YOLOX 配置为例,其 Head 模块配置写法为:
```python
bbox_head=dict(
......@@ -64,7 +66,7 @@ bbox_head=dict(
train_cfg=dict(assigner=dict(type='SimOTAAssigner', center_radius=2.5)),
```
在 MMYOLO 中抽取 head_module 后,新的配置写法为:
在 MMYOLO 中抽取 `head_module` 后,新的配置写法为:
```python
bbox_head=dict(
......
......@@ -131,12 +131,12 @@ MixUp 和 Mosaic 类似,也是属于混合图片类增强,其是随机从另
- **HSV 颜色空间增强**
- **随机水平翻转**
MMDet 开源库中已经对 Albu 第三方数据增强库进行了封装,使得用户可以简单的通过配置即可使用 Albu 库中提供的任何数据增强功能。
MMDetection 开源库中已经对 Albu 第三方数据增强库进行了封装,使得用户可以简单的通过配置即可使用 Albu 库中提供的任何数据增强功能。
而 HSV 颜色空间增强和随机水平翻转都是属于比较常规的数据增强,不需要特殊介绍。
#### 1.1.5 MMYOLO 实现解析
常规的单图数据增强例如随机翻转等比较容易实现,而 Mosiac 类的混合数据增强则不太容易。在 MMDet 复现的 YOLOX 算法中
常规的单图数据增强例如随机翻转等比较容易实现,而 Mosiac 类的混合数据增强则不太容易。在 MMDetection 复现的 YOLOX 算法中
提出了 MultiImageMixDataset 数据集包装器的概念,其实现过程如下:
<div align=center >
......@@ -144,7 +144,7 @@ MMDet 开源库中已经对 Albu 第三方数据增强库进行了封装,使
</div>
对于 Mosiac 等混合类数据增强,会额外实现一个 `get_indexes` 方法用来获取其他图片索引,然后得到 4 张图片信息后就可以进行 Mosiac 增强了。
以 MMDet 中实现的 YOLOX 为例,其配置文件写法如下所示:
以 MMDetection 中实现的 YOLOX 为例,其配置文件写法如下所示:
```python
train_pipeline = [
......@@ -177,7 +177,7 @@ MultiImageMixDataset 数据集包装其传入一个包括 Mosaic 和 RandAffine
标注加载的 pipeline。通过这种方式就可以快速的实现混合类数据增强。
但是上述实现有一个缺点:
**对于不熟悉 MMDet 的用户来说,其经常会忘记 Mosaic 必须要和 MultiImageMixDataset 配合使用,否则会报错,而且这样会加大复杂度和理解难度**
**对于不熟悉 MMDetection 的用户来说,其经常会忘记 Mosaic 必须要和 MultiImageMixDataset 配合使用,否则会报错,而且这样会加大复杂度和理解难度**
为了解决这个问题,在 MMYOLO 中进一步进行了简化。直接让 pipeline 能够获取到 dataset 对象,此时就可以将 Mosaic 等混合类数据增强的实现
和使用变成和随机翻转一样。此时在 MMYOLO 中 YOLOX 的配置写法变成如下所示:
......@@ -315,16 +315,16 @@ YOLOV5 的匹配策略简单总结为:**采用了 anchor 和 gt_bbox 的 shape
YOLOv5 是 Anchor-based 的目标检测算法,Anchor size 的获取方式与 YOLOv3 相同,是使用 kmeans 算法进行聚类获得。
在用户更换了数据集后,可以使用 MMDet 里带有的 Anchor 分析工具,对自己的数据集进行分析,确定合适的 Anchor size。
在用户更换了数据集后,可以使用 MMDetection 里带有的 Anchor 分析工具,对自己的数据集进行分析,确定合适的 Anchor size。
若你的 MMDet 通过 mim 安装,可使用以下命令分析 Anchor:
若你的 MMDetection 通过 mim 安装,可使用以下命令分析 Anchor:
```shell
mim run mmdet optimize_anchors ${CONFIG} --algorithm k-means
--input-shape ${INPUT_SHAPE [WIDTH HEIGHT]} --output-dir ${OUTPUT_DIR}
```
若 MMDet 为其他方式安装,可进入 MMDet 所在目录,使用以下命令分析 Anchor:
若 MMDetection 为其他方式安装,可进入 MMDetection 所在目录,使用以下命令分析 Anchor:
```shell
python tools/analysis_tools/optimize_anchors.py ${CONFIG} --algorithm k-means
......
......@@ -76,14 +76,18 @@ mim install "mmyolo"
mim download mmyolo --config yolov5_s-v61_syncbn_fast_8xb16-300e_coco --dest .
```
载将需要几秒钟或更长时间,这取决于你的网络环境。完成后,你会在当前文件夹中发现两个文件 `yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py` and `yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth`
下载将需要几秒钟或更长时间,这取决于你的网络环境。完成后,你会在当前文件夹中发现两个文件 `yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py` and `yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth`
**步骤 2.** 推理验证
方案 1. 如果你通过源码安装的 MMYOLO,那么直接运行以下命令进行验证:
```shell
python demo/image_demo.py demo/demo.jpg yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth --device cpu --out-file result.jpg
python demo/image_demo.py demo/demo.jpg \
yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py \
yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth \
--device cpu \
--out-file result.jpg
```
你会在当前文件夹中看到一个新的图像 `result.jpg`,图像中包含有网络预测的检测框。
......@@ -101,7 +105,7 @@ model = init_detector(config_file, checkpoint_file, device='cpu') # or device='
inference_detector(model, 'demo/demo.jpg')
```
你将会看到一个包含 `DetDataSample` 的列表,预测结果在 `pred_instance` 里,包含有预测框,预测分数和预测类别。
你将会看到一个包含 `DetDataSample` 的列表,预测结果在 `pred_instance` 里,包含有预测框、预测分数 和 预测类别。
### 自定义安装
......@@ -112,7 +116,7 @@ inference_detector(model, 'demo/demo.jpg')
- 对于 Ampere 架构的 NVIDIA GPU,例如 GeForce 30 系列 以及 NVIDIA A100,CUDA 11 是必需的。
- 对于更早的 NVIDIA GPU,CUDA 11 是向后兼容 (backward compatible) 的,但 CUDA 10.2 能够提供更好的兼容性,也更加轻量。
请确保你的 GPU 驱动版本满足最低的版本需求,参阅 NVIDIA 官方的 [CUDA工具箱和相应的驱动版本关系表](https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html#cuda-major-component-versions__table-cuda-toolkit-driver-versions)
请确保你的 GPU 驱动版本满足最低的版本需求,参阅 NVIDIA 官方的 [CUDA工具箱和相应的驱动版本关系表](https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html#cuda-major-component-versions__table-cuda-toolkit-driver-versions)
```{note}
如果按照我们的最佳实践进行安装,CUDA 运行时库就足够了,因为我们提供相关 CUDA 代码的预编译,不需要进行本地编译。
......@@ -123,9 +127,9 @@ inference_detector(model, 'demo/demo.jpg')
#### 不使用 MIM 安装 MMEngine
要使用 pip 而不是 MIM 来安装 MMEngine,请遵照 [MMEngine 安装指南](https://mmengine.readthedocs.io/en/latest/get_started/installation.html)
要使用 pip 而不是 MIM 来安装 MMEngine,请遵照 [MMEngine 安装指南](https://mmengine.readthedocs.io/en/latest/get_started/installation.html)
例如,你可以通过以下命令安装 MMEngine
例如,你可以通过以下命令安装 MMEngine
```shell
pip install mmengine
......@@ -133,13 +137,12 @@ pip install mmengine
#### 不使用 MIM 安装 MMCV
MMCV 包含 C++ 和 CUDA 扩展,因此其对 PyTorch 的依赖比较复杂。MIM 会自动解析这些
依赖,选择合适的 MMCV 预编译包,使安装更简单,但它并不是必需的。
MMCV 包含 C++ 和 CUDA 扩展,因此其对 PyTorch 的依赖比较复杂。MIM 会自动解析这些 依赖,选择合适的 MMCV 预编译包,使安装更简单,但它并不是必需的。
要使用 pip 而不是 MIM 来安装 MMCV,请遵照 [MMCV 安装指南](https://mmcv.readthedocs.io/zh_CN/2.x/get_started/installation.html)
要使用 pip 而不是 MIM 来安装 MMCV,请遵照 [MMCV 安装指南](https://mmcv.readthedocs.io/zh_CN/2.x/get_started/installation.html)
它需要您用指定 URL 的形式手动指定对应的 PyTorch 和 CUDA 版本。
例如,下述命令将会安装基于 PyTorch 1.12.x 和 CUDA 11.6 编译的 mmcv
例如,下述命令将会安装基于 PyTorch 1.12.x 和 CUDA 11.6 编译的 mmcv
```shell
pip install "mmcv>=2.0.0rc1" -f https://download.openmmlab.com/mmcv/dist/cu116/torch1.12.0/index.html
......@@ -149,7 +152,7 @@ pip install "mmcv>=2.0.0rc1" -f https://download.openmmlab.com/mmcv/dist/cu116/t
我们的代码能够建立在只使用 CPU 的环境(CUDA 不可用)。
在 CPU 模式下,可以进行模型训练(需要 MMCV 版本 >= 2.0.0rc1)、测试或者推理,然而以下功能将在 CPU 模式下不能使用:
在 CPU 模式下,可以进行模型训练(需要 MMCV 版本 >= `2.0.0rc1`)、测试或者推理,然而以下功能将在 CPU 模式下不能使用:
- Deformable Convolution
- Modulated Deformable Convolution
......@@ -175,7 +178,7 @@ pip install "mmcv>=2.0.0rc1" -f https://download.openmmlab.com/mmcv/dist/cu116/t
#### 在 Google Colab 中安装
[Google Colab](https://colab.research.google.com/) 通常已经包含了 PyTorch 环境,因此我们只需要安装 MMEngine, MMCV,MMDetection 和 MMYOLO 即可,命令如下:
[Google Colab](https://colab.research.google.com/) 通常已经包含了 PyTorch 环境,因此我们只需要安装 MMEngine、MMCV、MMDetection 和 MMYOLO 即可,命令如下:
**步骤 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)
......@@ -186,7 +189,7 @@ pip install "mmcv>=2.0.0rc1" -f https://download.openmmlab.com/mmcv/dist/cu116/t
!mim install "mmdet>=3.0.0.rc0"
```
**步骤 2.** 使用源码安装 MMYOLO
**步骤 2.** 使用源码安装 MMYOLO
```shell
!git clone https://github.com/open-mmlab/mmyolo.git
......@@ -194,7 +197,7 @@ pip install "mmcv>=2.0.0rc1" -f https://download.openmmlab.com/mmcv/dist/cu116/t
!pip install -e .
```
**步骤 3.** 验证安装是否成功
**步骤 3.** 验证安装是否成功
```python
import mmyolo
......@@ -208,7 +211,7 @@ print(mmyolo.__version__)
#### 通过 Docker 使用 MMYOLO
们提供了一个 [Dockerfile](https://github.com/open-mmlab/mmyolo/blob/master/docker/Dockerfile) 来构建一个镜像。请确保你的 [docker版本](https://docs.docker.com/engine/install/) >=19.03
我们提供了一个 [Dockerfile](https://github.com/open-mmlab/mmyolo/blob/master/docker/Dockerfile) 来构建一个镜像。请确保你的 [docker版本](https://docs.docker.com/engine/install/) >=`19.03`
```shell
# build an image with PyTorch 1.6, CUDA 10.1
......@@ -219,18 +222,19 @@ docker build -t mmyolo docker/
用以下命令运行 Docker 镜像:
```shell
docker run --gpus all --shm-size=8g -it -v {DATA_DIR}:/mmyolo/data mmyolo
DATA_DIR=/path/to/your/dataset
docker run --gpus all --shm-size=8g -it -v ${DATA_DIR}:/mmyolo/data mmyolo
```
### 排除故障
如果你在安装过程中遇到一些问题,请先查看 [FAQ](notes/faq.md) 页面。
如果没有找到解决方案,你也可以在 GitHub 上 [打开一个问题](https://github.com/open-mmlab/mmyolo/issues/new/choose)
如果没有找到解决方案,你也可以在 GitHub 上 [打开一个问题](https://github.com/open-mmlab/mmyolo/issues/new/choose)
### 使用多个 MMYOLO 版本进行开发
训练和测试的脚本已经在 PYTHONPATH 中进行了修改,以确保脚本使用当前目录中的 MMYOLO。
训练和测试的脚本已经在 `PYTHONPATH` 中进行了修改,以确保脚本使用当前目录中的 MMYOLO。
要使环境中安装默认的 MMYOLO 而不是当前正在在使用的,可以删除出现在相关脚本中的代码:
......
......@@ -7,5 +7,5 @@
### 亮点
1. 支持 YOLOv5/YOLOX 训练和部署,支持 YOLOv6 推理和部署
2. 重构了 MMDet 的 YOLOX,提供了更快的训练和推理速度
2. 重构了 MMDetection 的 YOLOX,提供了更快的训练和推理速度
3. 提供了详细入门和进阶教程, 包括 YOLOv5 从入门到部署、YOLOv5 算法原理和实现全解析、 特征图可视化等教程
......@@ -4,11 +4,13 @@
## 什么是 MMYOLO
![图片](https://user-images.githubusercontent.com/12907710/137271636-56ba1cd2-b110-4812-8221-b4c120320aa9.png)
<div align=center>
<img src="https://user-images.githubusercontent.com/12907710/137271636-56ba1cd2-b110-4812-8221-b4c120320aa9.png" alt="图片"/>
</div>
MMYOLO 是一个 YOLO 系列的算法工具箱,目前仅实现了目标检测任务,后续会支持实例分割、全景分割和关键点检测等多种任务。其包括丰富的目标检测算法以及相关的组件和模块,下面是它的整体框架:
MMYOLO 文件结构和 MMDetection 完全一致。为了能够充分复用 MMDetection 代码,MMYOLO 仅包括定制内容,其由 3 个主要部分组成:datasets、models、engine
MMYOLO 文件结构和 MMDetection 完全一致。为了能够充分复用 MMDetection 代码,MMYOLO 仅包括定制内容,其由 3 个主要部分组成:`datasets``models``engine`
- **datasets** 支持用于目标检测的各种数据集。
- **transforms** 包含各种数据增强变换。
......
......@@ -210,7 +210,7 @@ val_evaluator = dict( # 验证过程使用的评测器
test_evaluator = val_evaluator # 测试过程使用的评测器
```
由于测试数据集没有标注文件,因此 MMYOLO 中的 test_dataloader 和 test_evaluator 配置通常等于 val。 如果要保存在测试数据集上的检测结果,则可以像这样编写配置:
由于测试数据集没有标注文件,因此 MMYOLO 中的 `test_dataloader``test_evaluator` 配置通常等于 `val`。 如果要保存在测试数据集上的检测结果,则可以像这样编写配置:
```python
# 在测试集上推理,
......@@ -297,7 +297,7 @@ param_scheduler = None
用户可以在训练、验证和测试循环上添加钩子,以便在运行期间插入一些操作。配置中有两种不同的钩子字段,一种是 `default_hooks`,另一种是 `custom_hooks`
`default_hooks` 是一个字典,用于配置运行时必须使用的钩子。这些钩子具有默认优先级,如果未设置,runner 将使用默认值。如果要禁用默认钩子,用户可以将其配置设置为` None`
`default_hooks` 是一个字典,用于配置运行时必须使用的钩子。这些钩子具有默认优先级,如果未设置,runner 将使用默认值。如果要禁用默认钩子,用户可以将其配置设置为 `None`
```python
default_hooks = dict(
......@@ -357,7 +357,7 @@ resume = False # 是否从 `load_from` 中定义的检查点恢复。 如果 `l
对于同一文件夹下的所有配置,推荐**只有一个**对应的**原始配置**文件。所有其他的配置文件都应该继承自这个**原始配置**文件。这样就能保证配置文件的最大继承深度为 3。
为了便于理解,我们建议贡献者继承现有方法。例如,如果在 YOLOv5s 的基础上做了一些修改,比如修改网络深度,用户首先可以通过指定`_base_ = ./yolov5_s-v61_syncbn_8xb16-300e_coco.py `来集成基础的 YOLOv5 结构,然后修改配置文件中的必要参数以完成继承。
为了便于理解,我们建议贡献者继承现有方法。例如,如果在 YOLOv5s 的基础上做了一些修改,比如修改网络深度,用户首先可以通过指定 `_base_ = ./yolov5_s-v61_syncbn_8xb16-300e_coco.py` 来集成基础的 YOLOv5 结构,然后修改配置文件中的必要参数以完成继承。
如果你在构建一个与任何现有方法不共享结构的全新方法,那么可以在 `configs` 文件夹下创建一个新的例如 `yolov100` 文件夹。
......@@ -507,7 +507,7 @@ model = dict(
### 复用 \_base\_ 文件中的变量
如果用户希望在当前配置中复用 base 文件中的变量,则可以通过使用 `{{_base_.xxx}}` 的方式来获取对应变量的拷贝。而在新版 MMEngine 中,还支持省略 `{{}}` 的写法。例如:
如果用户希望在当前配置中复用 `_base_` 文件中的变量,则可以通过使用 `{{_base_.xxx}}` 的方式来获取对应变量的拷贝。而在新版 MMEngine 中,还支持省略 `{{}}` 的写法。例如:
```python
_base_ = '../_base_/default_runtime.py'
......@@ -526,11 +526,11 @@ pre_transform = _base_.pre_transform # 变量 pre_transform 等于 _base_ 中定
- 更新配置列表中的键
在配置文件里,一些字典型的配置被包含在列表中。例如,数据训练流程 `data.train.pipeline` 通常是一个列表,比如 `[dict(type='LoadImageFromFile'), ...]`。如果需要将 `'LoadImageFromFile'` 改成 `'LoadImageFromWebcam'`,需要写成下述形式: `--cfg-options data.train.pipeline.0.type=LoadImageFromNDArray`.
在配置文件里,一些字典型的配置被包含在列表中。例如,数据训练流程 `data.train.pipeline` 通常是一个列表,比如 `[dict(type='LoadImageFromFile'), ...]`。如果需要将 `'LoadImageFromFile'` 改成 `'LoadImageFromWebcam'`,需要写成下述形式:`--cfg-options data.train.pipeline.0.type=LoadImageFromNDArray`.
- 更新列表或元组的值
如果要更新的值是列表或元组。例如,配置文件通常设置 `model.data_preprocessor.mean=[123.675, 116.28, 103.53]`. 如果需要改变这个键,可以通过 `--cfg-options model.data_preprocessor.mean="[127,127,127]"` 来重新设置。需要注意,引号 " 是支持列表或元组数据类型所必需的,并且在指定值的引号内**不允许**有空格。
如果要更新的值是列表或元组。例如,配置文件通常设置 `model.data_preprocessor.mean=[123.675, 116.28, 103.53]`。如果需要改变这个键,可以通过 `--cfg-options model.data_preprocessor.mean="[127,127,127]"` 来重新设置。需要注意,引号 `"` 是支持列表或元组数据类型所必需的,并且在指定值的引号内**不允许**有空格。
## 配置文件名称风格
......
......@@ -74,7 +74,7 @@ python tools/analysis_tools/browse_dataset.py 'configs/yolov5/yolov5_s-v61_syncb
--output-dir 'work-dir/browse_dataset'
```
2. 使用 `config` 文件 `configs/yolov5/yolov5_s-v61_syncbn_8xb16-300e_coco.py` 可视化图片,图片直接弹出显示,每张图片持续`10`秒,同时保存到目录 `work-dir/browse_dataset`
2. 使用 `config` 文件 `configs/yolov5/yolov5_s-v61_syncbn_8xb16-300e_coco.py` 可视化图片,图片直接弹出显示,每张图片持续 `10` 秒,同时保存到目录 `work-dir/browse_dataset`
```shell
python tools/analysis_tools/browse_dataset.py 'configs/yolov5/yolov5_s-v61_syncbn_8xb16-300e_coco.py' \
......@@ -82,7 +82,7 @@ python tools/analysis_tools/browse_dataset.py 'configs/yolov5/yolov5_s-v61_syncb
--show-interval 10
```
3. 使用 `config` 文件 `configs/yolov5/yolov5_s-v61_syncbn_8xb16-300e_coco.py` 可视化图片,图片直接弹出显示,每张图片持续`10`秒,图片不进行保存:
3. 使用 `config` 文件 `configs/yolov5/yolov5_s-v61_syncbn_8xb16-300e_coco.py` 可视化图片,图片直接弹出显示,每张图片持续 `10` 秒,图片不进行保存:
```shell
python tools/analysis_tools/browse_dataset.py 'configs/yolov5/yolov5_s-v61_syncbn_8xb16-300e_coco.py' \
......@@ -126,7 +126,7 @@ python tools/misc/download_dataset.py --dataset-name balloon [--save-dir ${SAVE_
下面以转换 `yolov5s.pt` 为例:
1.`YOLOv5` 官方代码克隆到本地(目前支持的最高版本为`v6.1`
1.YOLOv5 官方代码克隆到本地(目前支持的最高版本为 `v6.1`
```shell
git clone -b v6.1 https://github.com/ultralytics/yolov5.git
......@@ -139,7 +139,7 @@ cd yolov5
wget https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5s.pt
```
3.`tools/model_converters/yolov5_to_mmyolo.py` 文件复制到 `YOLOv5` 官方代码克隆的路径
3.`tools/model_converters/yolov5_to_mmyolo.py` 文件复制到 YOLOv5 官方代码克隆的路径
```shell
cp ${MMDET_YOLO_PATH}/tools/model_converters/yolov5_to_mmyolo.py yolov5_to_mmyolo.py
......@@ -151,7 +151,7 @@ cp ${MMDET_YOLO_PATH}/tools/model_converters/yolov5_to_mmyolo.py yolov5_to_mmyol
python yolov5_to_mmyolo.py --src ${WEIGHT_FILE_PATH} --dst mmyolov5.pt
```
转换好的 `mmyolov5.pt` 就可以为 `MMYOLO` 所用。 YOLOv6 官方权重转化也是采用一样的使用方式。
转换好的 `mmyolov5.pt` 即可以为 MMYOLO 所用。 YOLOv6 官方权重转化也是采用一样的使用方式。
### YOLOX
......@@ -169,4 +169,4 @@ wget https://github.com/Megvii-BaseDetection/YOLOX/releases/download/0.1.1rc0/yo
python tools/model_converters/yolox_to_mmyolo.py --src yolox_s.pth --dst mmyolox.pt
```
转换好的 `mmyolox.pt` 既可以在 `MMYOLO` 中使用。
转换好的 `mmyolox.pt` 即可以在 MMYOLO 中使用。
......@@ -3,104 +3,128 @@
## 特征图可视化
<div align=center>
<img src="https://user-images.githubusercontent.com/89863442/190903635-27bbc619-9bf8-43a8-aea8-ea13b9dad28c.jpg" width="1000"/>
<img src="https://user-images.githubusercontent.com/89863442/190903635-27bbc619-9bf8-43a8-aea8-ea13b9dad28c.jpg" width="1000" alt="image"/>
</div>
可视化可以给深度学习的模型训练和测试过程提供直观解释。
MMYOLO 中,将使用 MMEngine 提供的 `Visualizer` 可视化器进行特征图可视化,其具备如下功能:
- 支持基础绘图接口以及特征图可视化
- 支持基础绘图接口以及特征图可视化
- 支持选择模型中的不同层来得到特征图,包含 `squeeze_mean``select_max``topk` 三种显示方式,用户还可以使用 `arrangement` 自定义特征图显示的布局方式。
## 特征图绘制
你可以调用 `demo/featmap_vis_demo.py` 来简单快捷地得到可视化结果,为了方便理解,将其主要参数的功能梳理如下:
- `img` 选择要用于特征图可视化的图片,支持单张图片或者图片路径列表
- `img`:选择要用于特征图可视化的图片,支持单张图片或者图片路径列表。
- `config` 选择算法的配置文件
- `config`:选择算法的配置文件。
- `checkpoint` 选择对应算法的权重文件
- `checkpoint`:选择对应算法的权重文件。
- `--out-file` 将得到的特征图保存到本地,并指定路径和文件名
- `--out-file`:将得到的特征图保存到本地,并指定路径和文件名。
- `--device` 指定用于推理图片的硬件,`--device cuda:0` 表示使用第 1 张 GPU 推理,`--device cpu` 表示用 CPU 推理
- `--device`:指定用于推理图片的硬件,`--device cuda:0` 表示使用第 1 张 GPU 推理,`--device cpu` 表示用 CPU 推理。
- `--score-thr` 设置检测框的置信度阈值,只有置信度高于这个值的框才会显示
- `--score-thr`:设置检测框的置信度阈值,只有置信度高于这个值的框才会显示。
- `--preview-model` 可以预览模型,方便用户理解模型的特征层结构
- `--preview-model`:可以预览模型,方便用户理解模型的特征层结构。
- `--target-layers` 对指定层获取可视化的特征图
- `--target-layers`:对指定层获取可视化的特征图。
- 可以单独输出某个层的特征图,例如: `--target-layers backbone` , `--target-layers neck` , `--target-layers backbone.stage4`
- 参数为列表时,也可以同时输出多个层的特征图,例如: `--target-layers backbone.stage4 neck` 表示同时输出 backbone 的 stage4 层和 neck 的三层一共四层特征图
- 可以单独输出某个层的特征图,例如: `--target-layers backbone` , `--target-layers neck` , `--target-layers backbone.stage4`
- 参数为列表时,也可以同时输出多个层的特征图,例如: `--target-layers backbone.stage4 neck` 表示同时输出 backbone 的 stage4 层和 neck 的三层一共四层特征图
- `--channel-reduction` 输入的 Tensor 一般是包括多个通道的,channel_reduction 参数可以将多个通道压缩为单通道,然后和图片进行叠加显示,有以下三个参数可以设置
- `--channel-reduction`:输入的 Tensor 一般是包括多个通道的,`channel_reduction` 参数可以将多个通道压缩为单通道,然后和图片进行叠加显示,有以下三个参数可以设置:
- `squeeze_mean` 将输入的 C 维度采用 mean 函数压缩为一个通道,输出维度变成 (1, H, W)
- `select_max` 从输入的 C 维度中先在空间维度 sum,维度变成 (C, ),然后选择值最大的通道
- `None` 表示不需要压缩,此时可以通过 topk 参数可选择激活度最高的 topk 个特征图显示
- `squeeze_mean`:将输入的 C 维度采用 mean 函数压缩为一个通道,输出维度变成 (1, H, W)。
- `select_max`:从输入的 C 维度中先在空间维度 sum,维度变成 (C, ),然后选择值最大的通道。
- `None`:表示不需要压缩,此时可以通过 `topk` 参数可选择激活度最高的 `topk` 个特征图显示。
- `--topk` 只有在 `channel_reduction` 参数为 None 的情况下, topk 参数才会生效,其会按照激活度排序选择 topk 个通道,然后和图片进行叠加显示,并且此时会通过 `--arrangement` 参数指定显示的布局,该参数表示为一个数组,两个数字需要以空格分开,例如: `--topk 5 --arrangement 2 3` 表示以2行3列显示激活度排序最高的5张特征图, `--topk 7 --arrangement 3 3` 表示以3行3列显示激活度排序最高的7张特征图
- `--topk`:只有在 `channel_reduction` 参数为 `None` 的情况下, `topk` 参数才会生效,其会按照激活度排序选择 `topk` 个通道,然后和图片进行叠加显示,并且此时会通过 `--arrangement` 参数指定显示的布局,该参数表示为一个数组,两个数字需要以空格分开,例如: `--topk 5 --arrangement 2 3` 表示以 `2行 3列` 显示激活度排序最高的 5 张特征图, `--topk 7 --arrangement 3 3` 表示以 `3行 3列` 显示激活度排序最高的 7 张特征图。
- 如果 topk 不是 -1,则会按照激活度排序选择 topk 个通道显示
- 如果 topk 不是 -1,则会按照激活度排序选择 topk 个通道显示
- 如果 topk = -1,此时通道 C 必须是 1 或者 3 表示输入数据是图片,否则报错提示用户应该设置 `channel_reduction` 来压缩通道。
- 考虑到输入的特征图通常非常小,函数默认将特征图进行上采样后方便进行可视化。
## 用法示例
以预训练好的 YOLOv5_s 模型为例:
以预训练好的 YOLOv5-s 模型为例:
请提前下载 YOLOv5_s 模型权重到本仓库根路径下:
请提前下载 YOLOv5-s 模型权重到本仓库根路径下:
```shell
cd mmyolo
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
```
(1) 将多通道特征图采用 `select_max` 参数压缩为单通道并显示, 通过提取 backbone 层输出进行特征图可视化,将得到 backbone 三个输出层的特征图
(1) 将多通道特征图采用 `select_max` 参数压缩为单通道并显示, 通过提取 `backbone` 层输出进行特征图可视化,将得到 `backbone` 三个输出层的特征图:
```python
python demo/featmap_vis_demo.py demo/dog.jpg configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth --target-layers backbone --channel-reduction select_max
```shell
python demo/featmap_vis_demo.py demo/dog.jpg \
configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py \
yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth \
--target-layers backbone \
--channel-reduction select_max
```
<div align=center>
<img src="https://user-images.githubusercontent.com/89863442/190939711-c498060d-ed98-4dca-aa02-b41ca82f51b0.jpg" width="800"/>
<img src="https://user-images.githubusercontent.com/89863442/190939711-c498060d-ed98-4dca-aa02-b41ca82f51b0.jpg" width="800" alt="image"/>
</div>
(2) 将多通道特征图采用 `squeeze_mean` 参数压缩为单通道并显示, 通过提取 neck 层输出进行特征图可视化,将得到 neck 三个输出层的特征图
(2) 将多通道特征图采用 `squeeze_mean` 参数压缩为单通道并显示, 通过提取 `neck` 层输出进行特征图可视化,将得到 `neck` 三个输出层的特征图:
```python
python demo/featmap_vis_demo.py demo/dog.jpg configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth --target-layers neck --channel-reduction squeeze_mean
```shell
python demo/featmap_vis_demo.py demo/dog.jpg \
configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py \
yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth \
--target-layers neck \
--channel-reduction squeeze_mean
```
<div align=center>
<img src="https://user-images.githubusercontent.com/89863442/190939718-b320239e-87f0-4a07-8525-b4087372e3fd.jpg" width="800"/>
<img src="https://user-images.githubusercontent.com/89863442/190939718-b320239e-87f0-4a07-8525-b4087372e3fd.jpg" width="800" alt="image"/>
</div>
(3) 将多通道特征图采用 `squeeze_mean` 参数压缩为单通道并显示, 通过提取 backbone.stage4 和 backbone.stage3 层输出进行特征图可视化,将得到两个输出层的特征图
(3) 将多通道特征图采用 `squeeze_mean` 参数压缩为单通道并显示, 通过提取 `backbone.stage4``backbone.stage3` 层输出进行特征图可视化,将得到两个输出层的特征图:
```python
python demo/featmap_vis_demo.py demo/dog.jpg configs/yolov5/yolov5_s-v61_fast_syncbn_8xb16-300e_coco.py yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth --target-layers backbone.stage4 backbone.stage3 --channel-reduction squeeze_mean
```shell
python demo/featmap_vis_demo.py demo/dog.jpg \
configs/yolov5/yolov5_s-v61_fast_syncbn_8xb16-300e_coco.py \
yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth \
--target-layers backbone.stage4 backbone.stage3 \
--channel-reduction squeeze_mean
```
<div align=center>
<img src="https://user-images.githubusercontent.com/89863442/190939720-cf829954-cc15-4b31-9be3-229d4d95e458.jpg" width="800"/>
<img src="https://user-images.githubusercontent.com/89863442/190939720-cf829954-cc15-4b31-9be3-229d4d95e458.jpg" width="800" alt="image"/>
</div>
(4) 利用 `--topk 3 --arrangement 2 2` 参数选择多通道特征图中激活度最高的 3 个通道并采用 2x2 布局显示, 用户可以通过 `arrangement` 参数选择自己想要的布局,特征图将自动布局,先按每个层中的 top3 特征图按 2x2 的格式布局,再将每个层按 2x2 布局
(4) 利用 `--topk 3 --arrangement 2 2` 参数选择多通道特征图中激活度最高的 3 个通道并采用 `2x2` 布局显示, 用户可以通过 `arrangement` 参数选择自己想要的布局,特征图将自动布局,先按每个层中的 `top3` 特征图按 `2x2` 的格式布局,再将每个层按 `2x2` 布局:
```python
python demo/featmap_vis_demo.py demo/dog.jpg configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth --target-layers backbone.stage3 backbone.stage4 --channel-reduction None --topk 3 --arrangement 2 2 --out-file 4.jpg
```shell
python demo/featmap_vis_demo.py demo/dog.jpg \
configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py \
yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth \
--target-layers backbone.stage3 backbone.stage4 \
--channel-reduction None \
--topk 3 \
--arrangement 2 2 \
--out-file 4.jpg
```
<div align=center>
<img src="https://user-images.githubusercontent.com/89863442/190939723-911c5e9b-dd33-42eb-be4a-ba45f03110a0.jpg" width="1200"/>
<img src="https://user-images.githubusercontent.com/89863442/190939723-911c5e9b-dd33-42eb-be4a-ba45f03110a0.jpg" width="1200" alt="image"/>
</div>
(5) 存储绘制后的图片,在绘制完成后,可以选择本地窗口显示,也可以存储到本地,只需要加入参数 `--out-file xxx.jpg`
(5) 存储绘制后的图片,在绘制完成后,可以选择本地窗口显示,也可以存储到本地,只需要加入参数 `--out-file xxx.jpg`
```python
python demo/featmap_vis_demo.py demo/dog.jpg configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth --target-layers backbone --channel-reduction select_max --out-file featmap_backbone
```shell
python demo/featmap_vis_demo.py demo/dog.jpg \
configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py \
yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth \
--target-layers backbone \
--channel-reduction select_max \
--out-file featmap_backbone
```
......@@ -30,13 +30,15 @@ python tools/misc/download_dataset.py --dataset-name balloon --save-dir data --
python tools/dataset_converters/balloon2coco.py
```
执行以上命令,下载数据集并转化格式后,balloon 数据集在 data 文件夹中准备好了,train.json 和 val.json 便是 coco 格式的标注文件了。
执行以上命令,下载数据集并转化格式后,balloon 数据集在 `data` 文件夹中准备好了,`train.json``val.json` 便是 coco 格式的标注文件了。
![](https://cdn.vansin.top/img/20220912105312.png)
<div align=center>
<img src="https://cdn.vansin.top/img/20220912105312.png" alt="image"/>
</div>
## config 文件准备
configs/yolov5 文件夹下新建 yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py 配置文件,并把以下内容复制配置文件中。
`configs/yolov5` 文件夹下新建 `yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py` 配置文件,并把以下内容复制配置文件中。
```python
_base_ = './yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py'
......@@ -82,7 +84,7 @@ default_hooks = dict(logger=dict(interval=1))
```
以上配置从 `./yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py` 中继承,并根据 balloon 数据的特点更新了 `data_root``metainfo``train_dataloader``val_dataloader``num_classes` 等配置。
我们将 logger 的 interval 设置为 1 的原因是,每进行 interval 次 iteration 会输出一次 loss 相关的日志,而我们选取气球数据集比较小,interval 太大我们将看不到 loss 相关日志的输出。
我们将 logger 的 `interval` 设置为 1 的原因是,每进行 `interval` 次 iteration 会输出一次 loss 相关的日志,而我们选取气球数据集比较小,`interval` 太大我们将看不到 loss 相关日志的输出。
## 训练
......@@ -92,11 +94,13 @@ python tools/train.py configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.
运行以上训练命令,`work_dirs/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon` 文件夹会被自动生成,权重文件以及此次的训练配置文件将会保存在此文件夹中。
![](https://cdn.vansin.top/img/20220913213846.png)
<div align=center>
<img src="https://cdn.vansin.top/img/20220913213846.png" alt="image"/>
</div>
### 中断后恢复训练
如果训练中途停止,在训练命令最后加上 --resume ,程序会自动从 work_dirs 中加载最新的权重文件恢复训练。
如果训练中途停止,在训练命令最后加上 `--resume` ,程序会自动从 `work_dirs` 中加载最新的权重文件恢复训练。
```shell
python tools/train.py configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py --resume
......@@ -104,7 +108,7 @@ python tools/train.py configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.
### 加载预训练权重微调
经过测试,相比不加载预训练模型,加载 YOLOv5_s 预训练模型在气球数据集上训练和验证 coco/bbox_mAP 能涨 30 多个百分点。
经过测试,相比不加载预训练模型,加载 YOLOv5-s 预训练模型在气球数据集上训练和验证 coco/bbox_mAP 能涨 30 多个百分点。
1. 下载 COCO 数据集预训练权重
......@@ -117,7 +121,8 @@ wget https://download.openmmlab.com/mmyolo/v0/yolov5/yolov5_s-v61_syncbn_fast_8x
```shell
cd mmyolo
python tools/train.py configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py --cfg-options load_from='yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth' custom_hooks.0.strict_load=False
python tools/train.py configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py \
--cfg-options load_from='yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth' custom_hooks.0.strict_load=False
```
注意: 必须要设置 `custom_hooks.0.strict_load=False`, 将 EMAHook 的 strict_load 初始化参数设置为 False,否则会报权重不匹配的错误。
......@@ -129,14 +134,15 @@ python tools/train.py configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.
```shell
# 命令行中设置 model.backbone.frozen_stages=4
cd mmyolo
python tools/train.py configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py --cfg-options load_from='yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth' model.backbone.frozen_stages=4
python tools/train.py configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py \
--cfg-options load_from='yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth' model.backbone.frozen_stages=4
```
### 可视化相关
#### 验证阶段可视化
我们将 configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py 中的 default_hooks 的 visualization 进行修改,设置 draw 为 True,interval 为 2
我们将 `configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py` 中的 `default_hooks``visualization` 进行修改,设置 `draw``True``interval``2`
```shell
default_hooks = dict(
......@@ -145,13 +151,15 @@ default_hooks = dict(
)
```
重新运行以下训练命令,在验证评估的过程中,每 interval 张图片就会保存一张标注结果和预测结果的拼图到 work_dirs/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon/{timestamp}/vis_data/vis_image 文件夹中了。
重新运行以下训练命令,在验证评估的过程中,每 `interval` 张图片就会保存一张标注结果和预测结果的拼图到 `work_dirs/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon/{timestamp}/vis_data/vis_image` 文件夹中了。
```shell
python tools/train.py configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py
```
![](https://moonstarimg.oss-cn-hangzhou.aliyuncs.com/img/20220920094007.png)
<div align=center>
<img src="https://moonstarimg.oss-cn-hangzhou.aliyuncs.com/img/20220920094007.png" alt="image"/>
</div>
#### wandb 可视化后端使用
......@@ -159,7 +167,9 @@ MMEngine 支持本地、TensorBoard 以及 wandb 等多种后端, 本节以 wand
wandb 官网注册并在 https://wandb.ai/settings 获取到 wandb 的 API Keys。
![](https://cdn.vansin.top/img/20220913212628.png)
<div align=center>
<img src="https://cdn.vansin.top/img/20220913212628.png" alt="image"/>
</div>
```shell
pip install wandb
......@@ -179,14 +189,20 @@ visualizer = dict(vis_backends = [dict(type='LocalVisBackend'), dict(type='Wandb
python tools/train.py configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py
```
![](https://cdn.vansin.top/img/20220913213221.png)
<div align=center>
<img src="https://cdn.vansin.top/img/20220913213221.png" alt="image"/>
</div>
### 模型推理
```shell
python tools/test.py configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py work_dirs/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon/epoch_300.pth --show-dir show_results
python tools/test.py configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py \
work_dirs/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon/epoch_300.pth \
--show-dir show_results
```
运行以上推理命令,推理结果图片会自动保存至 `work_dirs/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon/{timestamp}/show_results` 文件夹中。下面为其中一张结果图片,左图为实际标注,右图为模型推理结果。
![result_img](https://user-images.githubusercontent.com/27466624/190913272-f99709e5-c798-46b8-aede-30f4e91683a3.jpg)
<div align=center>
<img src="https://user-images.githubusercontent.com/27466624/190913272-f99709e5-c798-46b8-aede-30f4e91683a3.jpg" alt="result_img"/>
</div>
......@@ -13,7 +13,7 @@ from mmyolo.utils import register_all_modules
# TODO: support fuse_conv_bn and format_only
def parse_args():
parser = argparse.ArgumentParser(
description='MMDet test (and eval) a model')
description='MMYOLO test (and eval) a model')
parser.add_argument('config', help='test config file path')
parser.add_argument('checkpoint', help='checkpoint file')
parser.add_argument(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册