refine description in quick_start_recognition.md and fix typo in feature_extraction.md

pull/2254/head
HydrogenSulfate 2022-09-04 07:20:16 +00:00
parent fc6a15d292
commit a40aff49cd
2 changed files with 51 additions and 49 deletions

View File

@ -35,10 +35,10 @@
![](../../images/feature_extraction_framework.png)
图中各个模块的功能为:
- **Backbone**: 用于提取输入图像初步特征的骨干网络,一般由配置文件中的 [`B.//ppcls/configs/GeneralRecognitionV2/GeneralRecognitionV2_PPLCNetV2_base.yaml#L26-L29) 以及 [`B.//ppcls/configs/GeneralRecognitionV2/GeneralRecognitionV2_PPLCNetV2_base.yaml#L30-L31) 字段共同指定。
- **Neck**: 用以特征增强及特征维度变换。可以是一个简单的 FC Layer用来做特征维度变换也可以是较复杂的 FPN 结构,用以做特征增强,一般由配置文件中的 [`N.//ppcls/configs/GeneralRecognitionV2/GeneralRecognitionV2_PPLCNetV2_base.yaml#L32-L35)字段指定。
- **Head**: 用来将 `Neck` 的输出 feature 转化为 logits让模型在训练阶段能以分类任务的形式进行训练。除了常用的 FC Layer 外,还可以替换为 [CosMargin](../../../ppcls/arch/gears/cosmargin.py), [ArcMargin](../../../ppcls/arch/gears/arcmargin.py), [CircleMargin](../../../ppcls/arch/gears/circlemargin.py) 等模块,一般由配置文件中的 [`H.//ppcls/configs/GeneralRecognitionV2/GeneralRecognitionV2_PPLCNetV2_base.yaml#L36-L41) 字段指定。
- **Loss**: 指定所使用的 Loss 函数。我们将 Loss 设计为组合 loss 的形式,可以方便地将 Classification Loss 和 Metric learning Loss 组合在一起,一般由配置文件中的 [`L.//ppcls/configs/GeneralRecognitionV2/GeneralRecognitionV2_PPLCNetV2_base.yaml#L44-L50) 字段指定。
- **Backbone**: 用于提取输入图像初步特征的骨干网络,一般由配置文件中的 [Backbone](../../../ppcls/configs/GeneralRecognitionV2/GeneralRecognitionV2_PPLCNetV2_base.yaml#L33-L37) 以及 [BackboneStopLayer](../../../ppcls/configs/GeneralRecognitionV2/GeneralRecognitionV2_PPLCNetV2_base.yaml#L38-L39) 字段共同指定。
- **Neck**: 用以特征增强及特征维度变换。可以是一个简单的 FC Layer用来做特征维度变换也可以是较复杂的 FPN 结构,用以做特征增强,一般由配置文件中的 [Neck](../../../ppcls/configs/GeneralRecognitionV2/GeneralRecognitionV2_PPLCNetV2_base.yaml#L40-L51) 字段指定。
- **Head**: 用来将 `Neck` 的输出 feature 转化为 logits让模型在训练阶段能以分类任务的形式进行训练。除了常用的 FC Layer 外,还可以替换为 [CosMargin](../../../ppcls/arch/gears/cosmargin.py), [ArcMargin](../../../ppcls/arch/gears/arcmargin.py), [CircleMargin](../../../ppcls/arch/gears/circlemargin.py) 等模块,一般由配置文件中的 [Head](`../../../ppcls/configs/GeneralRecognitionV2/GeneralRecognitionV2_PPLCNetV2_base.yaml#L52-L60) 字段指定。
- **Loss**: 指定所使用的 Loss 函数。我们将 Loss 设计为组合 loss 的形式,可以方便地将 Classification Loss 和 Metric learning Loss 组合在一起,一般由配置文件中的 [Loss](../../../ppcls/configs/GeneralRecognitionV2/GeneralRecognitionV2_PPLCNetV2_base.yaml#L63-L77) 字段指定。
<a name="3"></a>
@ -116,7 +116,7 @@ Loss 部分选用 [Cross entropy loss](../../../ppcls/loss/celoss.py) 和 [Tripl
### 5.1 数据准备
首先需要基于任务定制自己的数据集。数据集格式与文件结构详见[数据集格式说明](../data_preparation/recognition_dataset.md)。
首先需要基于任务定制自己的数据集。数据集格式与文件结构详见 [数据集格式说明](../data_preparation/recognition_dataset.md)。
准备完毕之后还需要在配置文件中修改数据配置相关的内容, 主要包括数据集的地址以及类别数量。对应到配置文件中的位置如下所示:
@ -173,8 +173,8 @@ Loss 部分选用 [Cross entropy loss](../../../ppcls/loss/celoss.py) 和 [Tripl
- 单机多卡训练
```shell
export CUDA_VISIBLE_DEVICES=0,1,2,3
python3.7 -m paddle.distributed.launch \
--gpus="0,1,2,3" tools/train.py \
python3.7 -m paddle.distributed.launch --gpus="0,1,2,3" \
tools/train.py \
-c ./ppcls/configs/GeneralRecognitionV2/GeneralRecognitionV2_PPLCNetV2_base.yaml
```
**注意:**
@ -192,8 +192,8 @@ Loss 部分选用 [Cross entropy loss](../../../ppcls/loss/celoss.py) 和 [Tripl
- 单机多卡断点恢复训练
```shell
export CUDA_VISIBLE_DEVICES=0,1,2,3
python3.7 -m paddle.distributed.launch \
--gpus="0,1,2,3" tools/train.py \
python3.7 -m paddle.distributed.launch --gpus="0,1,2,3" \
tools/train.py \
-c ./ppcls/configs/GeneralRecognitionV2/GeneralRecognitionV2_PPLCNetV2_base.yaml \
-o Global.checkpoint="output/RecModel/latest"
```
@ -215,8 +215,8 @@ Loss 部分选用 [Cross entropy loss](../../../ppcls/loss/celoss.py) 和 [Tripl
- 多卡评估
```shell
export CUDA_VISIBLE_DEVICES=0,1,2,3
python3.7 -m paddle.distributed.launch \
--gpus="0,1,2,3" tools/eval.py \
python3.7 -m paddle.distributed.launch --gpus="0,1,2,3" \
tools/eval.py \
-c ./ppcls/configs/GeneralRecognitionV2/GeneralRecognitionV2_PPLCNetV2_base.yaml \
-o Global.pretrained_model="output/RecModel/best_model"
```
@ -260,7 +260,7 @@ wangzai.jpg: [-4.72979844e-02 3.40038240e-02 -4.06982675e-02 2.46225717e-03
-3.14170569e-02 7.35917836e-02 -3.09373233e-02 -2.31755469e-02]
```
在实际使用过程中,仅仅得到特征可能并不能满足业务需求。如果想进一步通过特征检索来进行图像识别,可以参照文档[向量检索](./vector_search.md)。
在实际使用过程中,仅仅得到特征可能并不能满足业务需求。如果想进一步通过特征检索来进行图像识别,可以参照文档 [向量检索](./vector_search.md)。
<a name="6"></a>

View File

@ -19,7 +19,7 @@
- [1.3.2 图像加库](#132-图像加库)
- [1.3.3 保存检索库](#133-保存检索库)
- [1.3.4 初始化检索库](#134-初始化检索库)
- [1.3.5 查看检索库标签](#135-查看检索库标签)
- [1.3.5 查看检索库标签](#135-查看检索库标签)
- [2. PP-ShiTu PC端 demo 快速体验](#2-pp-shitu-pc端-demo-快速体验)
- [2.1 环境配置](#21-环境配置)
- [2.2 图像识别体验](#22-图像识别体验)
@ -48,7 +48,7 @@
<a name="功能体验"></a>
### 1.2 功能体验
目前 PP-ShiTu android demo 具有图像检索、图像加库、保存检索库、更换检索库、查看检索库内的标签等基本功能,接下来介绍如何体验这几个功能。
目前 PP-ShiTu android demo 具有图像检索、图像加库、保存检索库、初始化检索库、查看检索库标签等基本功能,接下来介绍如何体验这几个功能。
#### 1.2.1 图像检索
点击下方的“拍照识别”按钮<img src="../../images/quick_start/android_demo/paizhaoshibie_100.png" width="25" height="25"/>或者“本地识别”按钮<img src="../../images/quick_start/android_demo/bendishibie_100.png" width="25" height="25"/>即可拍摄一张图像或者选中一张图像然后等待几秒钟APP便会将图像中的主体框标注出来并且在图像下方给出预测的类别以及预测时间等信息。
@ -89,8 +89,8 @@
#### 1.3.4 初始化检索库
初始化库时会自动将检索库和标签库切换成 `original.index``original.txt`,并自动删除手机中的 `latest.index``latest.txt`(如果存在的话)。
#### 1.3.5 查看检索库标签
可按照[功能体验-查看检索库标签](#125-查看检索库标签)中说明进行查看当检索标签库过多如本demo自带的196类检索标签库可在弹窗中滑动查看。
#### 1.3.5 查看检索库标签
可按照 [功能体验-查看检索库标签](#125-查看检索库标签) 中说明进行查看当检索标签库过多如本demo自带的196类检索标签库可在弹窗中滑动查看。
## 2. PP-ShiTu PC端 demo 快速体验
@ -117,14 +117,14 @@
| 模型简介 | 推荐场景 | inference 模型 | 预测配置文件 |
| ---------------------- | -------- | ----------- | ------------ |
| 轻量级通用主体检测模型 | 通用场景 | [tar 格式下载链接](https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/models/inference/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_infer.tar) [zip 格式下载链接](https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/models/inference/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_infer.zip) | - |
| 轻量级通用识别模型 | 通用场景 | [tar 格式下载链接](https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/models/inference/PP-ShiTuV2/general_PPLCNetV2_base_pretrained_v1.0_infer.tar) [zip 格式下载链接](https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/models/inference/PP-ShiTuV2/general_PPLCNetV2_base_pretrained_v1.0_infer.zip) | [inference_general.yaml](../../../deploy/configs/inference_general.yaml) |
| 轻量级通用主体检测模型 | 通用场景 | [tar 格式下载链接](https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/models/inference/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_infer.tar) \| [zip 格式下载链接](https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/models/inference/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_infer.zip) | - |
| 轻量级通用识别模型 | 通用场景 | [tar 格式下载链接](https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/models/inference/PP-ShiTuV2/general_PPLCNetV2_base_pretrained_v1.0_infer.tar) \| [zip 格式下载链接](https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/models/inference/PP-ShiTuV2/general_PPLCNetV2_base_pretrained_v1.0_infer.zip) | [inference_general.yaml](../../../deploy/configs/inference_general.yaml) |
注意:由于部分解压缩软件在解压上述 `tar` 格式文件时存在问题,建议非命令行用户下载 `zip` 格式文件并解压。`tar` 格式文件建议使用命令 `tar -xf xxx.tar` 解压。
本章节 demo 数据下载地址如下: [瓶装饮料数据下载链接](https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/data/drink_dataset_v2.0.tar)。
本章节 demo 数据下载地址如下: [drink_dataset_v2.0.tar(瓶装饮料数据)](https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/data/drink_dataset_v2.0.tar)。
如果希望体验服务端主体检测和各垂类方向的识别模型,可以参考[2.4 服务端识别模型列表](#24-服务端识别模型列表)
如果希望体验服务端主体检测和各垂类方向的识别模型,可以参考 [2.4 服务端识别模型列表](#24-服务端识别模型列表)
**注意**
@ -188,8 +188,9 @@ wget https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/data/drink_da
如果使用服务端通用识别模型Demo 数据需要重新提取特征、够建索引,方式如下:
```shell
# 下面是使用下载的服务端商品识别模型进行索引库构建
python3.7 python/build_gallery.py -c configs/inference_general.yaml -o Global.rec_inference_model_dir=./models/general_PPLCNetV2_base_pretrained_v1.0_infer
python3.7 python/build_gallery.py \
-c configs/inference_general.yaml \
-o Global.rec_inference_model_dir=./models/general_PPLCNetV2_base_pretrained_v1.0_infer
```
<a name="瓶装饮料识别与检索"></a>
@ -212,17 +213,18 @@ python3.7 -m pip install faiss-cpu==1.7.1post2
运行下面的命令,对图像 `./drink_dataset_v2.0/test_images/100.jpeg` 进行识别与检索
待检索图像如下所示
![](../../images/recognition/drink_data_demo/test_images/100.jpeg)
```shell
# 使用下面的命令使用 GPU 进行预测
python3.7 python/predict_system.py -c configs/inference_general.yaml
# 使用下面的命令使用 CPU 进行预测
python3.7 python/predict_system.py -c configs/inference_general.yaml -o Global.use_gpu=False
```
待检索图像如下所示。
![](../../images/recognition/drink_data_demo/test_images/100.jpeg)
最终输出结果如下。
@ -232,7 +234,7 @@ python3.7 python/predict_system.py -c configs/inference_general.yaml -o Global.u
其中 `bbox` 表示检测出的主体所在位置,`rec_docs` 表示索引库中与检测框最为相似的类别,`rec_scores` 表示对应的置信度。
检测的可视化结果保存在 `output` 文件夹下,对于本张图像,识别结果可视化如下所示。
检测的可视化结果默认保存在 `output` 文件夹下,对于本张图像,识别结果可视化如下所示。
![](../../images/recognition/drink_data_demo/output/100.jpeg)
@ -274,31 +276,32 @@ Inference: 37.95266151428223 ms per batch image
### 2.3 未知类别的图像识别体验
对图像 `./drink_dataset_v2.0/test_images/mosilian.jpeg` 进行识别,命令如下
对图像 `./drink_dataset_v2.0/test_images/mosilian.jpeg` 进行识别
待检索图像如下
![](../../images/recognition/drink_data_demo/test_images/mosilian.jpeg)
执行如下识别命令
```shell
# 使用下面的命令使用 GPU 进行预测,如果希望使用 CPU 预测,可以在命令后面添加 -o Global.use_gpu=False
python3.7 python/predict_system.py -c configs/inference_general.yaml -o Global.infer_imgs="./drink_dataset_v2.0/test_images/mosilian.jpeg"
```
待检索图像如下所示。
![](../../images/recognition/drink_data_demo/test_images/mosilian.jpeg)
输出结果为空。
可以发现输出结果为空
由于默认的索引库中不包含对应的索引信息,所以这里的识别结果有误,此时我们可以通过构建新的索引库的方式,完成未知类别的图像识别。
当索引库中的图像无法覆盖我们实际识别的场景时,即在预测未知类别的图像时,我们需要将对应类别的相似图像添加到索引库中,从而完成对未知类别的图像识别,这一过程是不需要重新训练的
当索引库中的图像无法覆盖我们实际识别的场景时,即识别未知类别的图像前,我们需要将该未知类别的相似图像(至少一张)添加到索引库中,从而完成对未知类别的图像识别。这一过程不需要重新训练模型,以识别 `mosilian.jpeg` 为例,只需按以下步骤重新构建新的索引库即可。
<a name="准备新的数据与标签"></a>
#### 2.3.1 准备新的数据与标签
首先需要将与待检索图像相似的图像列表拷贝到索引库原始图像的文件夹。这里 PaddleClas 已经将所有的图像数据都放在文件夹 `drink_dataset_v2.0/gallery/` 中。
首先需要将与待检索图像相似的图像列表拷贝到索引库原始图像的文件夹。这里 PaddleClas 已经将所有的图像数据都放在文件夹 `drink_dataset_v2.0/gallery/` 中。
然后需要编辑记录了图像路径和标签信息的文本文件,这里 PaddleClas 将更后的标签信息文件放在了 `drink_dataset_v2.0/gallery/drink_label_all.txt` 文件中。可以与默认的 `drink_dataset_v2.0/gallery/drink_label.txt` 标签文件进行对比,添加了光明和三元系列牛奶的索引图像。
然后需要编辑记录了图像路径和标签信息的文本文件,这里 PaddleClas 将更后的标签信息文件放在了 `drink_dataset_v2.0/gallery/drink_label_all.txt` 文件中。与原始的 `drink_dataset_v2.0/gallery/drink_label.txt` 标签文件进行对比,可以发现新增了光明和三元系列牛奶的索引图像。
每一行的文本中,第一个字段表示图像的相对路径,第二个字段表示图像对应的标签信息,中间用 `\t` 键分隔开(注意:有些编辑器会将 `tab` 自动转换为 `空格`,这种情况下会导致文件解析报错)。
@ -306,23 +309,23 @@ python3.7 python/predict_system.py -c configs/inference_general.yaml -o Global.i
#### 2.3.2 建立新的索引库
使用下面的命令构建 `index` 索引,加速识别后的检索过程
使用下面的命令构建新的索引库 `index_all`。
```shell
python3.7 python/build_gallery.py -c configs/inference_general.yaml -o IndexProcess.data_file="./drink_dataset_v2.0/gallery/drink_label_all.txt" -o IndexProcess.index_dir="./drink_dataset_v2.0/index_all"
```
最终新的索引信息保存在文件夹 `./drink_dataset_v2.0/index_all`。具体 `yaml` 请参考[向量检索文档](../image_recognition_pipeline/vector_search.md)。
最终构建完毕的新的索引库保存在文件夹 `./drink_dataset_v2.0/index_all`。具体 `yaml` 请参考[向量检索文档](../image_recognition_pipeline/vector_search.md)。
<a name="基于新的索引库的图像识别"></a>
#### 2.3.3 基于新的索引库的图像识别
使用新的索引库,对上述图像进行识别,运行命令如下。
使用新的索引库,重新对 `mosilian.jpeg` 图像进行识别,运行命令如下。
```shell
# 使用下面的命令使用 GPU 进行预测,如果希望使用 CPU 预测,可以在命令后面添加 -o Global.use_gpu=False
python3.7 python/predict_system.py -c configs/inference_general.yaml -o Global.infer_imgs="././drink_dataset_v2.0/test_images/mosilian.jpeg" -o IndexProcess.index_dir="./drink_dataset_v2.0/index_all"
python3.7 python/predict_system.py -c configs/inference_general.yaml -o Global.infer_imgs="./drink_dataset_v2.0/test_images/mosilian.jpeg" -o IndexProcess.index_dir="./drink_dataset_v2.0/index_all"
```
输出结果如下。
@ -331,7 +334,7 @@ python3.7 python/predict_system.py -c configs/inference_general.yaml -o Global.i
[{'bbox': [290, 297, 564, 919], 'rec_docs': '光明_莫斯利安', 'rec_scores': 0.59137374}]
```
最终识别结果为`光明_莫斯利安`,识别正确,识别结果可视化如下所示。
最终识别结果为 `光明_莫斯利安` ,识别正确,识别结果可视化如下所示。
![](../../images/recognition/drink_data_demo/output/mosilian.jpeg)
@ -351,20 +354,19 @@ python3.7 python/predict_system.py -c configs/inference_general.yaml -o Global.i
| 商品识别模型 | 商品场景 | [模型下载链接](https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/models/inference/product_ResNet50_vd_aliproduct_v1.0_infer.tar) | [inference_product.yaml](../../../deploy/configs/inference_product.yaml) |
| 车辆 ReID 模型 | 车辆 ReID 场景 | [模型下载链接](https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/models/inference/vehicle_reid_ResNet50_VERIWild_v1.0_infer.tar) | [inference_vehicle.yaml](../../../deploy/configs/inference_vehicle.yaml) |
可以按照如下命令下载上述模型到 `deploy/models` 文件夹中,以供识别任务使用
```shell
cd PaddleClas/deploy/
cd ./deploy
mkdir -p models
```
```shell
cd ./models
# 下载通用主体检测模型并解压
# 下载服务器端通用主体检测模型并解压
wget https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/models/inference/ppyolov2_r50vd_dcn_mainbody_v1.0_infer.tar && tar -xf ppyolov2_r50vd_dcn_mainbody_v1.0_infer.tar
# 下载识别模型并解压
# 下载通用识别模型并解压
wget {识别模型下载链接地址} && tar -xf {压缩包的名称}
```
使用如下命令下载各方向识别模型的测试数据:
然后使用如下命令下载各个识别场景的测试数据:
```shell
# 回到 deploy 目录下
@ -388,6 +390,6 @@ wget https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/data/recognit
├── ...
```
按照上述步骤下载模型和测试数据后,您可以进行相关方向识别模型的测试。
按照上述步骤下载模型和测试数据后,您可以重新建立索引库,并进行相关方向识别模型的测试。
* 更多关于主体检测的介绍可以参考:[主体检测教程文档](../image_recognition_pipeline/mainbody_detection.md);关于特征提取的介绍可以参考:[特征提取教程文档](../image_recognition_pipeline/feature_extraction.md);关于向量检索的介绍可以参考:[向量检索教程文档](../image_recognition_pipeline/vector_search.md)。