diff --git a/README.md b/README.md index 9795eea78..13ae15495 100644 --- a/README.md +++ b/README.md @@ -64,9 +64,10 @@ Res2Net200_vd预训练模型Top-1精度高达85.1%。 - 模型训练/评估 - [图像分类任务](./docs/zh_CN/tutorials/getting_started.md) - [特征学习任务](./docs/zh_CN/tutorials/getting_started_retrieval.md) -- 模型预测(当前只支持图像分类任务,图像识别更新中) - - [基于Python预测引擎预测推理](./docs/zh_CN/tutorials/getting_started.md) - - [基于C++预测引擎预测推理](./deploy/cpp_infer/readme.md) +- 模型预测 + - [基于Python预测引擎预测推理](./docs/zh_CN/inference.md) + - [基于C++预测引擎预测推理](./deploy/cpp/readme.md)(当前只支持图像分类任务,图像识别更新中) +- 模型部署(当前只支持图像分类任务,图像识别更新中) - [服务化部署](./deploy/hubserving/readme.md) - [端侧部署](./deploy/lite/readme.md) - [whl包预测](./docs/zh_CN/whl.md) diff --git a/deploy/configs/inference_cls.yaml b/deploy/configs/inference_cls.yaml index cd8ac8bde..577c77d35 100644 --- a/deploy/configs/inference_cls.yaml +++ b/deploy/configs/inference_cls.yaml @@ -1,7 +1,6 @@ - Global: - infer_imgs: "../docs/images/whl/demo.jpg" - inference_model_dir: "../inference/" + infer_imgs: "./images/ILSVRC2012_val_00000010.jpeg" + inference_model_dir: "./models" batch_size: 1 use_gpu: True enable_mkldnn: True diff --git a/deploy/configs/inference_det.yaml b/deploy/configs/inference_det.yaml new file mode 100644 index 000000000..d5d6d6790 --- /dev/null +++ b/deploy/configs/inference_det.yaml @@ -0,0 +1,33 @@ +Global: + infer_imgs: "./images/wangzai.jpg" + det_inference_model_dir: "./models/ppyolov2_r50vd_dcn_mainbody_v1.0_infer" + batch_size: 1 + image_shape: [3, 640, 640] + threshold: 0.2 + max_det_results: 1 + labe_list: + - foreground + + # inference engine config + use_gpu: True + enable_mkldnn: True + cpu_num_threads: 100 + enable_benchmark: True + use_fp16: False + ir_optim: True + use_tensorrt: False + gpu_mem: 8000 + enable_profile: False + +DetPreProcess: + transform_ops: + - DetResize: + interp: 2 + keep_ratio: false + target_size: [640, 640] + - DetNormalizeImage: + is_scale: true + mean: [0.485, 0.456, 0.406] + std: [0.229, 0.224, 0.225] + - DetPermute: {} +DetPostProcess: {} diff --git a/deploy/configs/inference_rec.yaml b/deploy/configs/inference_rec.yaml index ba64b6d8e..2293752c5 100644 --- a/deploy/configs/inference_rec.yaml +++ b/deploy/configs/inference_rec.yaml @@ -1,8 +1,6 @@ Global: - infer_imgs: "images/coco_000000570688.jpg" - # infer_imgs: "../docs/images/whl/demo.jpg" - det_inference_model_dir: "./ppyolov2_r50vd_dcn_365e_mainbody_infer/" - rec_inference_model_dir: "./MobileNetV1_infer/" + infer_imgs: "./images/wangzai.jpg" + rec_inference_model_dir: "./models/product_ResNet50_vd_aliproduct_v1.0_infer" batch_size: 1 image_shape: [3, 640, 640] threshold: 0.5 @@ -21,21 +19,6 @@ Global: gpu_mem: 8000 enable_profile: False -DetPreProcess: - transform_ops: - - DetResize: - interp: 2 - keep_ratio: false - target_size: [640, 640] - - DetNormalizeImage: - is_scale: true - mean: [0.485, 0.456, 0.406] - std: [0.229, 0.224, 0.225] - - DetPermute: {} - -DetPostProcess: {} - - RecPreProcess: transform_ops: - ResizeImage: @@ -50,20 +33,3 @@ RecPreProcess: - ToCHWImage: RecPostProcess: null - - -# indexing engine config -IndexProcess: - build: - enable: True - index_path: "./logo_index/" - image_root: "dataset/LogoDet-3K-crop/train" - data_file: "dataset/LogoDet-3K-crop/LogoDet-3K+train.txt" - spacer: " " - dist_type: "IP" - pq_size: 100 - embedding_size: 1000 - infer: - index_path: "./logo_index/" - search_budget: 100 - return_k: 10 diff --git a/deploy/images/ILSVRC2012_val_00000010.jpeg b/deploy/images/ILSVRC2012_val_00000010.jpeg new file mode 100644 index 000000000..6fcafb4d9 Binary files /dev/null and b/deploy/images/ILSVRC2012_val_00000010.jpeg differ diff --git a/deploy/images/ILSVRC2012_val_00010010.jpeg b/deploy/images/ILSVRC2012_val_00010010.jpeg new file mode 100644 index 000000000..4908ac446 Binary files /dev/null and b/deploy/images/ILSVRC2012_val_00010010.jpeg differ diff --git a/deploy/images/ILSVRC2012_val_00020010.jpeg b/deploy/images/ILSVRC2012_val_00020010.jpeg new file mode 100644 index 000000000..9c6f87795 Binary files /dev/null and b/deploy/images/ILSVRC2012_val_00020010.jpeg differ diff --git a/deploy/images/ILSVRC2012_val_00030010.jpeg b/deploy/images/ILSVRC2012_val_00030010.jpeg new file mode 100644 index 000000000..0a073fe49 Binary files /dev/null and b/deploy/images/ILSVRC2012_val_00030010.jpeg differ diff --git a/deploy/images/anmuxi.jpg b/deploy/images/anmuxi.jpg new file mode 100644 index 000000000..735ea5a4c Binary files /dev/null and b/deploy/images/anmuxi.jpg differ diff --git a/deploy/images/wangzai.jpg b/deploy/images/wangzai.jpg new file mode 100644 index 000000000..fe7aa77d9 Binary files /dev/null and b/deploy/images/wangzai.jpg differ diff --git a/deploy/python/predict_rec.py b/deploy/python/predict_rec.py index 64c18e02e..de293bf00 100644 --- a/deploy/python/predict_rec.py +++ b/deploy/python/predict_rec.py @@ -74,7 +74,7 @@ def main(config): output = rec_predictor.predict(img) if rec_predictor.postprocess is not None: output = rec_predictor.postprocess(output) - print(output.shape) + print(output) return diff --git a/docs/zh_CN/inference.md b/docs/zh_CN/inference.md new file mode 100644 index 000000000..fffa80716 --- /dev/null +++ b/docs/zh_CN/inference.md @@ -0,0 +1,189 @@ + +# 基于Python预测引擎推理 + +inference 模型(`paddle.jit.save`保存的模型) +一般是模型训练,把模型结构和模型参数保存在文件中的固化模型,多用于预测部署场景。 +训练过程中保存的模型是checkpoints模型,保存的只有模型的参数,多用于恢复训练等。 +与checkpoints模型相比,inference 模型会额外保存模型的结构信息,在预测部署、加速推理上性能优越,灵活方便,适合于实际系统集成。 + +接下来首先介绍如何将训练的模型转换成inference模型,然后依次介绍主体检测模型、特征提取模型在CPU、GPU上的预测方法, +之后介绍主体检测、特征提取、特征检索串联的预测方法,最后介绍图像分类的预测方法。 + + +- [一、训练模型转inference模型](#训练模型转inference模型) + - [1.1 特征提取模型转inference模型](#特征提取模型转inference模型) + - [1.2 分类模型转inference模型](#分类模型转inference模型) + +- [二、主体检测模型推理](#主体检测模型推理) + +- [三、特征提取模型推理](#特征提取模型推理) + +- [四、主体检测、特征提取和向量检索串联](#主体检测、特征提取和向量检索串联) + +- [五、图像分类模型推理](#图像分类模型推理) + + + +## 一、训练模型转inference模型 + + +### 1.1 特征提取模型转inference模型 +以下命令请在PaddleClas的根目录执行。以商品识别特征提取模型模型为例,首先下载预训练模型: + +```shell script +wget -P ./product_pretrain/ https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/models/pretrain/product_ResNet50_vd_Aliproduct_v1.0_pretrained.pdparams +``` + +上述模型是ResNet50_vd在AliProduct上训练的模型,训练使用的配置文件为ppcls/configs/Products/ResNet50_vd_Aliproduct.yaml +将训练好的模型转换成inference模型只需要运行如下命令: +``` shell script +# -c 后面设置训练算法的yml配置文件 +# -o 配置可选参数 +# Global.pretrained_model 参数设置待转换的训练模型地址,不用添加文件后缀 .pdmodel,.pdopt或.pdparams。 +# Global.save_inference_dir参数设置转换的模型将保存的地址。 + +python3.7 tools/export_model.py -c ppcls/configs/Products/ResNet50_vd_Aliproduct.yaml -o Global.pretrained_model=./product_pretrain/product_ResNet50_vd_Aliproduct_v1.0_pretrained -o Global.save_inference_dir=./deploy/models/product_ResNet50_vd_aliproduct_v1.0_infer +``` + +这里也可以使用自己训练的模型。转inference模型时,使用的配置文件和训练时使用的配置文件相同。另外,还需要设置配置文件中的`Global.pretrained_model`参数,其指向训练中保存的模型参数文件。 +转换成功后,在模型保存目录下有三个文件: +``` +├── product_ResNet50_vd_aliproduct_v1.0_infer +│ ├── inference.pdiparams # 识别inference模型的参数文件 +│ ├── inference.pdiparams.info # 识别inference模型的参数信息,可忽略 +│ └── inference.pdmodel # 识别inference模型的program文件 +``` + + +### 1.2 分类模型转inference模型 + +下载预训练模型: +``` shell script +wget -P ./cls_pretrain/ https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/ResNet50_vd_pretrained.pdparams +``` + +上述模型是使用ResNet50_vd在ImageNet上训练的模型,使用的配置文件为`ppcls/configs/ImageNet/ResNet/ResNet50_vd.yaml`。 +转inference模型与特征提取模型的方式相同,如下: +``` +# -c 后面设置训练算法的yml配置文件 +# -o 配置可选参数 +# Global.pretrained_model 参数设置待转换的训练模型地址,不用添加文件后缀 .pdmodel,.pdopt或.pdparams。 +# Global.save_inference_dir参数设置转换的模型将保存的地址。 + +python3.7 tools/export_model.py -c ppcls/configs/ImageNet/ResNet/ResNet50_vd.yaml -o Global.pretrained_model=./cls_pretrain/ResNet50_vd_pretrained -o Global.save_inference_dir=./deploy/models/class_ResNet50_vd_ImageNet_infer +``` + +转换成功后,在目录下有三个文件: +``` +├── class_ResNet50_vd_ImageNet_infer +│ ├── inference.pdiparams # 识别inference模型的参数文件 +│ ├── inference.pdiparams.info # 识别inference模型的参数信息,可忽略 +│ └── inference.pdmodel # 识别inference模型的program文件 +``` + + +## 二、主体检测模型推理 + +下面介绍主体检测模型推理,以下命令请进入PaddleClas的deploy目录执行: +```shell script +cd deploy +``` +使用PaddleClas提供的主体检测Inference模型进行推理,可以执行: + +```shell script +mkdir -p models +cd models +# 下载通用检测inference模型并解压 +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 +cd .. + +# 用下载的inference模型进行预测 +python3.7 python/predict_det.py -c configs/inference_det.yaml +``` +输入的图像如下所示。 +[](../images/recognition/product_demo/wangzai.jpg) +最终输出结果如下: +```text +[{'class_id': 0, 'score': 0.4762245, 'bbox': array([305.55115, 226.05322, 776.61084, 930.42395], dtype=float32), 'label_name': 'foreground'}] +``` +检测的可视化结果如下: +[](../images/recognition/product_demo/wangzai_det_result.jpg) + +如果想要修改图像,可以在configs/inference_det.yaml中,修改infer_imgs的值,或使用-o Global.infer_imgs修改, +例如,要使用`images/anmuxi.jpg`可以运行: + +```shell script +python3.7 python/predict_det.py -c configs/inference_det.yaml -o Global.infer_imgs=images/anmuxi.jpg +``` + +如果想使用CPU进行预测,可以将配置文件中use_gpu选项设置为False,或者执行命令: +``` +python3.7 python/predict_det.py -c configs/inference_det.yaml -o Global.use_gpu=False +``` + + +## 三、特征提取模型推理 + +下面以商品特征提取为例,介绍特征提取模型推理。其他应用可以参考图像识别快速开始中的[模型地址](./tutorials/quick_start_recognition.md#2-%E5%9B%BE%E5%83%8F%E8%AF%86%E5%88%AB%E4%BD%93%E9%AA%8C), +将链接替换为相应模型的链接。以下命令请进入PaddleClas的deploy目录执行: +```shell script +cd deploy +``` +使用PaddleClas提供的商品特征提取Inference模型进行推理,可以执行: + +```shell script +mkdir -p models +cd models +# 下载商品特征提取inference模型并解压 +wget https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/models/inference/product_ResNet50_vd_aliproduct_v1.0_infer.tar && tar -xf product_ResNet50_vd_aliproduct_v1.0_infer.tar +cd .. + +# 用下载的inference模型进行预测 +python3.7 python/predict_rec.py -c configs/inference_rec.yaml +``` +预测可以得到一个512特征向量,直接在命令行输出显示。 + +如果想要修改图像,可以在configs/inference_det.yaml中,修改infer_imgs的值,或使用-o Global.infer_imgs修改, +例如,要使用`images/anmuxi.jpg`可以运行: + +```shell script +python3.7 python/predict_rec.py -c configs/inference_rec.yaml -o Global.infer_imgs=images/anmuxi.jpg +``` + +如果想使用CPU进行预测,可以将配置文件中use_gpu选项设置为False,或者执行命令: +``` +python3.7 python/predict_rec.py -c configs/inference_rec.yaml -o Global.use_gpu=False +``` + + +## 四、主体检测、特征提取和向量检索串联 +主体检测、特征提取和向量检索的串联预测,可以参考[图像识别快速体验](./tutorials/quick_start_recognition.md) + + +## 五、图像分类模型推理 + +下面介绍图像分类模型推理,以下命令请进入PaddleClas的deploy目录执行: +```shell script +cd deploy +``` +使用PaddleClas提供的商品特征提取Inference模型进行推理,首先请下载预训练模型并导出inference模型,具体参见[2. 分类模型转inference模型](#分类模型转inference模型)。 + +导出inference模型后,可以使用下面的命令预测: +```shell script + +python3.7 python/predict_cls.py -c configs/inference_cls.yaml +``` + +如果想要修改图像,可以在configs/inference_det.yaml中,修改infer_imgs的值,或使用-o Global.infer_imgs修改, +例如,要使用`images/ILSVRC2012_val_00010010.jpeg`可以运行: + +```shell script +python3.7 python/predict_cls.py -c configs/inference_cls.yaml -o Global.infer_imgs=images/ILSVRC2012_val_00010010.jpeg + +``` + +如果想使用CPU进行预测,可以将配置文件中use_gpu选项设置为False,或者执行命令: +``` +python3.7 python/predict_cls.py -c configs/inference_cls.yaml -o Global.use_gpu=False +``` +