feature_extraction.md 4.6 KB
Newer Older
S
stephon 已提交
1
# 特征提取
S
stephon 已提交
2 3 4
## 1. 简介
特征提取是图像识别中的关键一环,它的作用是将输入的图片转化为固定维度的特征向量,用于后续的[向量检索](./vector_search.md)。好的特征需要具备相似度保持性,即在特征空间中,相似度高的图片对其特征相似度要比较高(距离比较近);相似度低的图片对,其特征相似度要比较小(距离比较远)。[Deep Metric Learning](../algorithm_introduction/metric_learning.md)用以研究如何通过深度学习的方法获得具有强表征能力的特征。

B
Bin Lu 已提交
5
## 2. 网络结构
B
Bin Lu 已提交
6
为了图像识别任务的灵活定制,我们将整个网络分为Backbone, Neck, Head以及Loss部分,整体结构如下图所示,下面分别介绍各自的功能:
S
stephon 已提交
7
![](../../images/feature_extraction_framework.png)
B
Bin Lu 已提交
8 9 10 11
- **Backbone**:   指定所使用的骨干网络。 值得注意的是,PaddleClas提供的基于ImageNet的预训练模型,最后一层的输出为1000, 我们需要依据所需的特征维度定制最后一层的输出。
- **Neck**:  用以特征增强及特征维度变换。  这儿的Neck,可以是一个简单的Linear Layer,用来做特征维度变换;也可以是较复杂的FPN结构,用以做特征增强。
- **Head**:  用来将feature转化为logits。 除了常用的Fc Layer外,还可以替换为cosmargin, arcmargin, circlemargin等模块。
- **Loss**:  指定所使用的Loss函数。  我们将Loss设计为组合loss的形式, 可以方便得将Classification Loss和Pair_wise Loss组合在一起。
S
stephon 已提交
12

S
stephon 已提交
13 14 15 16 17 18 19 20 21 22 23 24
## 3. 通用识别模型
在PP-Shitu中, 我们采用[PP_LCNet_x2_5]作为骨干网络, Neck部分选用Linear Layer, Head部分选用ArcMargin, Loss部分选用CELoss, 详细的配置文件见[通用商品识别配置文件](../../ppcls/configs/GeneralRecognition/GeneralRecognition_PPLCNet_x2_5.yaml)。其中,训练数据为如下7个公开数据集的汇总:
| 数据集       | 数据量   | 类别数   | 场景  | 数据集地址 |
| :------------:  | :-------------: | :-------: | :-------: | :--------: |
| Aliproduct | 2498771 | 50030 | 商品 | [地址](https://www.objects365.org/overview.html) |
| GLDv2 | 1580470 | 81313  | 地标 | [地址](https://cocodataset.org/) |
| VeRI-Wild | 277797 | 30671 | 车辆 | [地址](https://github.com/luxiangju-PersonAI/iCartoonFace) |
| LogoDet-3K | 155427 | 3000 | Logo | [地址](https://github.com/Wangjing1551/LogoDet-3K-Dataset) |
| iCartoonFace | 389678 | 5013  | 动漫人物 | [地址](https://rpc-dataset.github.io/) |
| SOP | 59551 | 11318  | 商品 | [地址](https://rpc-dataset.github.io/) |
| Inshop | 25882 | 3997  | 商品 | [地址](https://rpc-dataset.github.io/) |
| **Total** | **5M** | **185K**  | ---- | [地址](https://rpc-dataset.github.io/) |
S
stephon 已提交
25

S
stephon 已提交
26
最终的模型效果如下表所示:
S
stephon 已提交
27

S
stephon 已提交
28
# 4. 自定义识别
B
Bin Lu 已提交
29
## 4.1 数据准备
S
stephon 已提交
30
首先,需要基于任务的需要,定制自己的数据集。数据的制作方法,参见
B
Bin Lu 已提交
31

S
stephon 已提交
32
## 4.2 模型训练
B
Bin Lu 已提交
33
- 单机单卡训练
S
stephon 已提交
34 35 36
```shell
export CUDA_VISIBLE_DEVICES=0
python tools/train.py -c ppcls/configs/GeneralRecognition/GeneralRecognition_PPLCNet_x2_5.yaml
S
stephon 已提交
37
```
B
Bin Lu 已提交
38
- 单机多卡训练
S
stephon 已提交
39 40
```shell
export CUDA_VISIBLE_DEVICES=0,1,2,3
S
stephon 已提交
41
python -m paddle.distributed.launch 
S
stephon 已提交
42 43
    --gpus="0,1,2,3" tools/train.py
    -c ppcls/configs/GeneralRecognition/GeneralRecognition_PPLCNet_x2_5.yaml
S
stephon 已提交
44
```
S
stephon 已提交
45 46
注意: 配置文件中默认采用`在线评估`的方式,如果你想加快训练速度,去除`在线评估`, 只需要在上述命令后面, 增加`-o eval_during_train=False`
训练完毕之后, 在output目录下会生成最终模型文件`latest.pd*`,`best_model.pd*`和训练日志文件`train.log`
S
stephon 已提交
47

S
stephon 已提交
48
## 4.3 模型评估
B
Bin Lu 已提交
49
- 单卡评估
S
stephon 已提交
50 51 52 53
```shell
export CUDA_VISIBLE_DEVICES=0
python tools/eval.py -c ppcls/configs/GeneralRecognition/GeneralRecognition_PPLCNet_x2_5.yaml \
\ -o Global.pretrained_model="output/RecModel/best_model"
S
stephon 已提交
54
```
S
stephon 已提交
55

B
Bin Lu 已提交
56
- 多卡评估
S
stephon 已提交
57 58
```shell
export CUDA_VISIBLE_DEVICES=0,1,2,3
S
stephon 已提交
59
python -m paddle.distributed.launch 
S
stephon 已提交
60 61
    --gpus="0,1,2,3" tools/eval.py
    -c  ppcls/configs/GeneralRecognition/GeneralRecognition_PPLCNet_x2_5.yaml
S
stephon 已提交
62
    -o  Global.pretrained_model="output/RecModel/best_model"
S
stephon 已提交
63
```
S
stephon 已提交
64 65

## 4.4 模型推理
S
stephon 已提交
66
推理过程包括两个步骤: 1)导出推理模型, 2)获取特征向量
B
Bin Lu 已提交
67
### 4.4.1 导出推理模型
S
stephon 已提交
68
```
S
stephon 已提交
69
python tools/export_model -c ppcls/configs/ResNet50_vd_SOP.yaml -o Global.pretrained_model="output/RecModel/best_model"
S
stephon 已提交
70
```
B
Bin Lu 已提交
71
生成的推理模型位于`inference`目录,名字为`inference.pd*`
S
stephon 已提交
72

B
Bin Lu 已提交
73
### 4.4.2 获取特征向量
S
stephon 已提交
74 75
```
cd deploy
S
stephon 已提交
76
python python/predict_rec.py -c configs/inference_rec.yaml -o Global.rec_inference_model_dir="../inference"
B
Bin Lu 已提交
77
```
S
stephon 已提交
78
最终得到的模型向量,如下图所示: