# mmdetection
**Repository Path**: KaffeeCat/mmdetection
## Basic Information
- **Project Name**: mmdetection
- **Description**: mmdetection学习篇
- **Primary Language**: Python
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: https://github.com/open-mmlab/mmdetection
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 12
- **Created**: 2022-03-17
- **Last Updated**: 2022-05-12
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
MMDetection学习篇
## 1. mmdetection环境准备
```bash
pip install mmcv
# mmcv小巧但不带cuda op,也可以通过如下命令来安装完整版:pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/{cu_version}/{torch_version}/index.html
git clone https://gitee.com/KaffeeCat/mmdetection.git
cd mmdetection
pip install -r requirements/build.txt
pip install -v -e .
```
## 2. 数据集的准备 [[知乎]](https://zhuanlan.zhihu.com/p/56223200)
以 COCO2017 和 VOC2007 数据为例,下载数据集
- VOC2007 training/validation data (450MB tar file) [[Download]](http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar)
- VOC2007 annotated test data (430MB tar file) [[Download]](http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar)
- COCO2017 in Kaggle (28GB file) [[Download]](https://www.kaggle.com/awsaf49/coco-2017-dataset)
将COCO和VOC数据集按照如下目录形式存储:
```bash
mmdetection
├── mmdet
├── tools
├── configs
├── data
│ ├── coco
│ │ ├── annotations
│ │ ├── train2017
│ │ ├── val2017
│ │ ├── test2017
│ ├── VOCdevkit
│ │ ├── VOC2007
│ │ │ ├── Annotations
│ │ │ ├── JPEGImages
│ │ │ ├── ImageSets
│ │ │ │ ├── Main
│ │ │ │ │ ├── test.txt
│ │ │ │ │ ├── trainval.txt
```
推荐以软链接的方式创建,以VOC2007举例:
```bash
mkdir data
ln -s $VOC2007_ROOT data/VOCdevkit
```
## 3. 构建流程 [[知乎]](https://zhuanlan.zhihu.com/p/444201503)
mmdetection主要对外提供三个API接口,定义在`tools`和`mmdet/api`文件夹下
- train: 通过mmcv的Runner管对训练生命周期进行管理
- test: 在测试集上计算各种metric指标
- inference: 前向推理,输入若干图片,得到推理结果

## 4. MMDetection整体框架流程 [[知乎]](https://zhuanlan.zhihu.com/p/341954021)

1. __Dataset__: 用于迭代输出数据;
2. __Pipeline__: 在迭代输出时,对数据进行各种处理,例如训练数据增强;
3. __Sampler__: 控制迭代输出数据的顺序(例:随机采样器`RandomSampler`)。由于Dataset中输出图片大小不一样,为了尽可能减少后续构建Batch时`padding`的像素个数,使用分组采样器`GroupSampler`和`DistributedGroupSampler`,在随机采样器的基础上,根据图片宽高比进行分组。
4. __DataLoader__: 输出组成Batch的数据,作为model的输入;
5. __Model__: 具有两个上层封装,`MMDataParallel `(单机版) + `MMDistributedDataParallel` (分布式单机/多机多卡);
6. __Loss__: 运行model后输出loss以及其他信息,通过`logger`进行保持和可视化;
7. __Runner__: 全生命周期管理,通过`Hook`方便的获取、修改、拦截任何生命周期数据流。
## 5. Model部分构建流程和思想 [[知乎]](https://zhuanlan.zhihu.com/p/337375549)
所有目标检测算法都按照以下流程进行配置:

1. __backbone__: 对Batch图片进行特征提取(例如ResNet);
2. __neck__: 特征融合或增强(例如FPN);
3. __head__: 输出分类和回归分支;
4. __Enhance__: 在整个网络中引入一些即插即用的增强算子,增强特征提取能力(例如SPP);
5. __BBox Assigner & Sampler__: 对于分类任务,存在严重的正负样本不平衡,通过分配和采样策略进行控制;
6. __Loss__:计算分类和回归Loss,进行训练;
7. __Training & Test Tricks__: 训练和测试过程中的tricks,比如优化器选择、参数调节等;
## 6. 学习材料路径
1. 安装mmdetection环境 [[Link]](https://gitee.com/KaffeeCat/mmdetection/blob/master/docs/zh_cn/get_started.md)
2. 使用已有模型在标准数据集上进行推理 [[Link]](https://gitee.com/KaffeeCat/mmdetection/blob/master/docs/zh_cn/1_exist_data_model.md)
3. 学习配置文件 [[Link]](https://gitee.com/KaffeeCat/mmdetection/blob/master/docs/zh_cn/tutorials/config.md)
4. 在自定义数据集上进行训练(将数据集重新组织为 COCO 格式) [[Link1]](https://gitee.com/KaffeeCat/mmdetection/blob/master/docs/zh_cn/2_new_data_model.md) [[Link2]](https://gitee.com/KaffeeCat/mmdetection/blob/master/docs/zh_cn/tutorials/customize_dataset.md)
5. 在标准数据集上训练自定义模型 [[Link1]](https://gitee.com/KaffeeCat/mmdetection/blob/master/docs/zh_cn/3_exist_data_new_model.md) [[Link2]](https://gitee.com/KaffeeCat/mmdetection/blob/master/docs/zh_cn/tutorials/customize_models.md)
6. 自定义数据预处理流程 [[Link]](https://gitee.com/KaffeeCat/mmdetection/blob/master/docs/zh_cn/tutorials/data_pipeline.md)
7. 权重初始化 [[Link]](https://gitee.com/KaffeeCat/mmdetection/blob/master/docs/zh_cn/tutorials/init_cfg.md)
8. 自定义损失函数 [[Link]])(https://gitee.com/KaffeeCat/mmdetection/blob/master/docs/zh_cn/tutorials/customize_losses.md)
9. ONNX 到 TensorRT 的模型转换 [[Link]](https://gitee.com/KaffeeCat/mmdetection/blob/master/docs/zh_cn/tutorials/onnx2tensorrt.md)
## 7. 相关资源链接
1. 预训练模型库[[Link]](https://gitee.com/KaffeeCat/mmdetection/blob/master/docs/zh_cn/model_zoo.md)