mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-11-02 20:54:03 +08:00
Update ppmatting directory
This commit is contained in:
43
examples/vision/segmentation/ppmatting/cpu-gpu/README.md
Normal file
43
examples/vision/segmentation/ppmatting/cpu-gpu/README.md
Normal file
@@ -0,0 +1,43 @@
|
||||
# PaddleSeg Matting模型高性能全场景部署方案-FastDeploy
|
||||
|
||||
PaddleSeg通过[FastDeploy](https://github.com/PaddlePaddle/FastDeploy)支持在NVIDIA GPU、X86 CPU、飞腾CPU、ARM CPU、Intel GPU(独立显卡/集成显卡)、昆仑芯、华为昇腾硬件上部署Matting模型
|
||||
|
||||
## 模型版本说明
|
||||
|
||||
- [PaddleSeg](https://github.com/PaddlePaddle/PaddleSeg/tree/develop)
|
||||
>> **注意**:支持PaddleSeg高于2.6版本的Matting模型
|
||||
|
||||
目前FastDeploy支持如下模型的部署
|
||||
|
||||
- [PP-Matting系列模型](https://github.com/PaddlePaddle/PaddleSeg/tree/develop/Matting)
|
||||
- [PP-HumanMatting系列模型](https://github.com/PaddlePaddle/PaddleSeg/tree/develop/Matting)
|
||||
- [ModNet系列模型](https://github.com/PaddlePaddle/PaddleSeg/tree/develop/Matting)
|
||||
|
||||
|
||||
## 准备PaddleSeg部署模型
|
||||
在部署前,需要先将Matting模型导出成部署模型,导出步骤参考文档[导出模型](https://github.com/PaddlePaddle/PaddleSeg/tree/develop/Matting)
|
||||
|
||||
**注意**
|
||||
- PaddleSeg导出的模型包含`model.pdmodel`、`model.pdiparams`和`deploy.yaml`三个文件,FastDeploy会从yaml文件中获取模型在推理时需要的预处理信息
|
||||
|
||||
## 预导出的推理模型
|
||||
|
||||
为了方便开发者的测试,下面提供了PP-Matting导出的各系列模型,开发者可直接下载使用。
|
||||
|
||||
其中精度指标来源于PP-Matting中对各模型的介绍(未提供精度数据),详情各参考PP-Matting中的说明。
|
||||
|
||||
>> **注意**`deploy.yaml`文件记录导出模型的`input_shape`以及预处理信息,若不满足要求,用户可重新导出相关模型
|
||||
|
||||
| 模型 | 参数大小 | 精度 | 备注 |
|
||||
|:---------------------------------------------------------------- |:----- |:----- | :------ |
|
||||
| [PP-Matting-512](https://bj.bcebos.com/paddlehub/fastdeploy/PP-Matting-512.tgz) | 106MB | - |
|
||||
| [PP-Matting-1024](https://bj.bcebos.com/paddlehub/fastdeploy/PP-Matting-1024.tgz) | 106MB | - |
|
||||
| [PP-HumanMatting](https://bj.bcebos.com/paddlehub/fastdeploy/PPHumanMatting.tgz) | 247MB | - |
|
||||
| [Modnet-ResNet50_vd](https://bj.bcebos.com/paddlehub/fastdeploy/PPModnet_ResNet50_vd.tgz) | 355MB | - |
|
||||
| [Modnet-MobileNetV2](https://bj.bcebos.com/paddlehub/fastdeploy/PPModnet_MobileNetV2.tgz) | 28MB | - |
|
||||
| [Modnet-HRNet_w18](https://bj.bcebos.com/paddlehub/fastdeploy/PPModnet_HRNet_w18.tgz) | 51MB | - |
|
||||
|
||||
## 详细部署文档
|
||||
|
||||
- [Python部署](python)
|
||||
- [C++部署](cpp)
|
||||
@@ -0,0 +1,14 @@
|
||||
PROJECT(infer_demo C CXX)
|
||||
CMAKE_MINIMUM_REQUIRED (VERSION 3.10)
|
||||
|
||||
# 指定下载解压后的fastdeploy库路径
|
||||
option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.")
|
||||
|
||||
include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake)
|
||||
|
||||
# 添加FastDeploy依赖头文件
|
||||
include_directories(${FASTDEPLOY_INCS})
|
||||
|
||||
add_executable(infer_demo ${PROJECT_SOURCE_DIR}/infer.cc)
|
||||
# 添加FastDeploy库依赖
|
||||
target_link_libraries(infer_demo ${FASTDEPLOY_LIBS})
|
||||
60
examples/vision/segmentation/ppmatting/cpu-gpu/cpp/README.md
Normal file
60
examples/vision/segmentation/ppmatting/cpu-gpu/cpp/README.md
Normal file
@@ -0,0 +1,60 @@
|
||||
[English](README.md) | 简体中文
|
||||
# PP-Matting C++部署示例
|
||||
|
||||
本目录下提供`infer.cc`快速完成PP-Matting在CPU/GPU、昆仑芯、华为昇腾以及GPU上通过Paddle-TensorRT加速部署的示例。
|
||||
|
||||
在部署前,需确认软硬件环境,同时下载预编译部署库,参考文档[FastDeploy预编译库安装](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/build_and_install)
|
||||
|
||||
>> **注意** 只有CPU、GPU提供预编译库,华为昇腾以及昆仑芯需要参考以上文档自行编译部署环境
|
||||
|
||||
以Linux上推理为例,在本目录执行如下命令即可完成编译测试,支持此模型需保证FastDeploy版本1.0.0以上(x.x.x>=1.0.0)
|
||||
|
||||
```bash
|
||||
mkdir build
|
||||
cd build
|
||||
# 下载FastDeploy预编译库,用户可在上文提到的`FastDeploy预编译库`中自行选择合适的版本使用
|
||||
wget https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-linux-x64-x.x.x.tgz
|
||||
tar xvf fastdeploy-linux-x64-x.x.x.tgz
|
||||
cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-x.x.x
|
||||
make -j
|
||||
|
||||
# 下载PP-Matting模型文件和测试图片
|
||||
wget https://bj.bcebos.com/paddlehub/fastdeploy/PP-Matting-512.tgz
|
||||
tar -xvf PP-Matting-512.tgz
|
||||
wget https://bj.bcebos.com/paddlehub/fastdeploy/matting_input.jpg
|
||||
wget https://bj.bcebos.com/paddlehub/fastdeploy/matting_bgr.jpg
|
||||
|
||||
|
||||
# CPU推理
|
||||
./infer_demo PP-Matting-512 matting_input.jpg matting_bgr.jpg 0
|
||||
# GPU推理
|
||||
./infer_demo PP-Matting-512 matting_input.jpg matting_bgr.jpg 1
|
||||
# GPU上TensorRT推理
|
||||
./infer_demo PP-Matting-512 matting_input.jpg matting_bgr.jpg 2
|
||||
# 昆仑芯XPU推理
|
||||
./infer_demo PP-Matting-512 matting_input.jpg matting_bgr.jpg 3
|
||||
```
|
||||
>> ***注意** 以上示例未提供华为昇腾的示例,在编译好昇腾部署环境后,只需改造一行代码,将示例文件中KunlunXinInfer方法的`option.UseKunlunXin()`为`option.UseAscend()`就可以完成在华为昇腾上的推理部署
|
||||
|
||||
运行完成可视化结果如下图所示
|
||||
<div width="840">
|
||||
<img width="200" height="200" float="left" src="https://user-images.githubusercontent.com/67993288/186852040-759da522-fca4-4786-9205-88c622cd4a39.jpg">
|
||||
<img width="200" height="200" float="left" src="https://user-images.githubusercontent.com/67993288/186852587-48895efc-d24a-43c9-aeec-d7b0362ab2b9.jpg">
|
||||
<img width="200" height="200" float="left" src="https://user-images.githubusercontent.com/67993288/186852116-cf91445b-3a67-45d9-a675-c69fe77c383a.jpg">
|
||||
<img width="200" height="200" float="left" src="https://user-images.githubusercontent.com/67993288/186852554-6960659f-4fd7-4506-b33b-54e1a9dd89bf.jpg">
|
||||
</div>
|
||||
|
||||
以上命令只适用于Linux或MacOS, Windows下SDK的使用方式请参考:
|
||||
- [如何在Windows中使用FastDeploy C++ SDK](../../../../../docs/cn/faq/use_sdk_on_windows.md)
|
||||
|
||||
## 快速链接
|
||||
- [PaddleSeg C++ API文档](https://www.paddlepaddle.org.cn/fastdeploy-api-doc/cpp/html/namespacefastdeploy_1_1vision_1_1segmentation.html)
|
||||
- [FastDeploy部署PaddleSeg模型概览](../../)
|
||||
- [Python部署](../python)
|
||||
|
||||
## 常见问题
|
||||
- [如何切换模型推理后端引擎](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/faq/how_to_change_backend.md)
|
||||
- [Intel GPU(独立显卡/集成显卡)的使用](https://github.com/PaddlePaddle/FastDeploy/blob/develop/tutorials/intel_gpu/README.md)
|
||||
- [编译CPU部署库](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/build_and_install/cpu.md)
|
||||
- [编译GPU部署库](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/build_and_install/gpu.md)
|
||||
- [编译Jetson部署库](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/build_and_install/jetson.md)
|
||||
174
examples/vision/segmentation/ppmatting/cpu-gpu/cpp/infer.cc
Normal file
174
examples/vision/segmentation/ppmatting/cpu-gpu/cpp/infer.cc
Normal file
@@ -0,0 +1,174 @@
|
||||
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "fastdeploy/vision.h"
|
||||
|
||||
#ifdef WIN32
|
||||
const char sep = '\\';
|
||||
#else
|
||||
const char sep = '/';
|
||||
#endif
|
||||
|
||||
void CpuInfer(const std::string& model_dir, const std::string& image_file,
|
||||
const std::string& background_file) {
|
||||
auto model_file = model_dir + sep + "model.pdmodel";
|
||||
auto params_file = model_dir + sep + "model.pdiparams";
|
||||
auto config_file = model_dir + sep + "deploy.yaml";
|
||||
auto option = fastdeploy::RuntimeOption();
|
||||
option.UseCpu();
|
||||
auto model = fastdeploy::vision::matting::PPMatting(model_file, params_file,
|
||||
config_file, option);
|
||||
if (!model.Initialized()) {
|
||||
std::cerr << "Failed to initialize." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
auto im = cv::imread(image_file);
|
||||
cv::Mat bg = cv::imread(background_file);
|
||||
fastdeploy::vision::MattingResult res;
|
||||
if (!model.Predict(&im, &res)) {
|
||||
std::cerr << "Failed to predict." << std::endl;
|
||||
return;
|
||||
}
|
||||
auto vis_im = fastdeploy::vision::VisMatting(im, res);
|
||||
auto vis_im_with_bg = fastdeploy::vision::SwapBackground(im, bg, res);
|
||||
cv::imwrite("visualized_result.jpg", vis_im_with_bg);
|
||||
cv::imwrite("visualized_result_fg.jpg", vis_im);
|
||||
std::cout << "Visualized result save in ./visualized_result_replaced_bg.jpg "
|
||||
"and ./visualized_result_fg.jpg"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
void KunlunXinInfer(const std::string& model_dir, const std::string& image_file,
|
||||
const std::string& background_file) {
|
||||
auto model_file = model_dir + sep + "model.pdmodel";
|
||||
auto params_file = model_dir + sep + "model.pdiparams";
|
||||
auto config_file = model_dir + sep + "deploy.yaml";
|
||||
auto option = fastdeploy::RuntimeOption();
|
||||
option.UseKunlunXin();
|
||||
auto model = fastdeploy::vision::matting::PPMatting(model_file, params_file,
|
||||
config_file, option);
|
||||
if (!model.Initialized()) {
|
||||
std::cerr << "Failed to initialize." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
auto im = cv::imread(image_file);
|
||||
cv::Mat bg = cv::imread(background_file);
|
||||
fastdeploy::vision::MattingResult res;
|
||||
if (!model.Predict(&im, &res)) {
|
||||
std::cerr << "Failed to predict." << std::endl;
|
||||
return;
|
||||
}
|
||||
auto vis_im = fastdeploy::vision::VisMatting(im, res);
|
||||
auto vis_im_with_bg = fastdeploy::vision::SwapBackground(im, bg, res);
|
||||
cv::imwrite("visualized_result.jpg", vis_im_with_bg);
|
||||
cv::imwrite("visualized_result_fg.jpg", vis_im);
|
||||
std::cout << "Visualized result save in ./visualized_result_replaced_bg.jpg "
|
||||
"and ./visualized_result_fg.jpg"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
void GpuInfer(const std::string& model_dir, const std::string& image_file,
|
||||
const std::string& background_file) {
|
||||
auto model_file = model_dir + sep + "model.pdmodel";
|
||||
auto params_file = model_dir + sep + "model.pdiparams";
|
||||
auto config_file = model_dir + sep + "deploy.yaml";
|
||||
|
||||
auto option = fastdeploy::RuntimeOption();
|
||||
option.UseGpu();
|
||||
option.UsePaddleInferBackend();
|
||||
auto model = fastdeploy::vision::matting::PPMatting(model_file, params_file,
|
||||
config_file, option);
|
||||
if (!model.Initialized()) {
|
||||
std::cerr << "Failed to initialize." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
auto im = cv::imread(image_file);
|
||||
cv::Mat bg = cv::imread(background_file);
|
||||
fastdeploy::vision::MattingResult res;
|
||||
if (!model.Predict(&im, &res)) {
|
||||
std::cerr << "Failed to predict." << std::endl;
|
||||
return;
|
||||
}
|
||||
auto vis_im = fastdeploy::vision::VisMatting(im, res);
|
||||
auto vis_im_with_bg = fastdeploy::vision::SwapBackground(im, bg, res);
|
||||
cv::imwrite("visualized_result.jpg", vis_im_with_bg);
|
||||
cv::imwrite("visualized_result_fg.jpg", vis_im);
|
||||
std::cout << "Visualized result save in ./visualized_result_replaced_bg.jpg "
|
||||
"and ./visualized_result_fg.jpg"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
void TrtInfer(const std::string& model_dir, const std::string& image_file,
|
||||
const std::string& background_file) {
|
||||
auto model_file = model_dir + sep + "model.pdmodel";
|
||||
auto params_file = model_dir + sep + "model.pdiparams";
|
||||
auto config_file = model_dir + sep + "deploy.yaml";
|
||||
|
||||
auto option = fastdeploy::RuntimeOption();
|
||||
option.UseGpu();
|
||||
option.UseTrtBackend();
|
||||
// If use original Tensorrt, not Paddle-TensorRT,
|
||||
// comment the following two lines
|
||||
option.EnablePaddleToTrt();
|
||||
option.EnablePaddleTrtCollectShape();
|
||||
option.SetTrtInputShape("img", {1, 3, 512, 512});
|
||||
auto model = fastdeploy::vision::matting::PPMatting(model_file, params_file,
|
||||
config_file, option);
|
||||
if (!model.Initialized()) {
|
||||
std::cerr << "Failed to initialize." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
auto im = cv::imread(image_file);
|
||||
cv::Mat bg = cv::imread(background_file);
|
||||
fastdeploy::vision::MattingResult res;
|
||||
if (!model.Predict(&im, &res)) {
|
||||
std::cerr << "Failed to predict." << std::endl;
|
||||
return;
|
||||
}
|
||||
auto vis_im = fastdeploy::vision::VisMatting(im, res);
|
||||
auto vis_im_with_bg = fastdeploy::vision::SwapBackground(im, bg, res);
|
||||
cv::imwrite("visualized_result.jpg", vis_im_with_bg);
|
||||
cv::imwrite("visualized_result_fg.jpg", vis_im);
|
||||
std::cout << "Visualized result save in ./visualized_result_replaced_bg.jpg "
|
||||
"and ./visualized_result_fg.jpg"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc < 5) {
|
||||
std::cout
|
||||
<< "Usage: infer_demo path/to/model_dir path/to/image run_option, "
|
||||
"e.g ./infer_model ./PP-Matting-512 ./test.jpg ./test_bg.jpg 0"
|
||||
<< std::endl;
|
||||
std::cout << "The data type of run_option is int, 0: run with cpu; 1: run "
|
||||
"with gpu; 2: run with gpu and use tensorrt backend, 3: run "
|
||||
"with kunlunxin."
|
||||
<< std::endl;
|
||||
return -1;
|
||||
}
|
||||
if (std::atoi(argv[4]) == 0) {
|
||||
CpuInfer(argv[1], argv[2], argv[3]);
|
||||
} else if (std::atoi(argv[4]) == 1) {
|
||||
GpuInfer(argv[1], argv[2], argv[3]);
|
||||
} else if (std::atoi(argv[4]) == 2) {
|
||||
TrtInfer(argv[1], argv[2], argv[3]);
|
||||
} else if (std::atoi(argv[4]) == 3) {
|
||||
KunlunXinInfer(argv[1], argv[2], argv[3]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
[English](README.md) | 简体中文
|
||||
# PP-Matting Python部署示例
|
||||
|
||||
本目录下提供`infer.py`快速完成PP-Matting在CPU/GPU、昆仑芯、华为昇腾,以及GPU上通过Paddle-TensorRT加速部署的示例。执行如下脚本即可完成
|
||||
|
||||
## 部署环境准备
|
||||
|
||||
在部署前,需确认软硬件环境,同时下载预编译python wheel 包,参考文档[FastDeploy预编译库安装](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/build_and_install)
|
||||
>> **注意** 只有CPU、GPU提供预编译库,华为昇腾以及昆仑芯需要参考以上文档自行编译部署环境
|
||||
|
||||
|
||||
```bash
|
||||
#下载部署示例代码
|
||||
git clone https://github.com/PaddlePaddle/FastDeploy.git
|
||||
cd FastDeploy/examples/vision/matting/ppmatting/python
|
||||
|
||||
# 下载PP-Matting模型文件和测试图片
|
||||
wget https://bj.bcebos.com/paddlehub/fastdeploy/PP-Matting-512.tgz
|
||||
tar -xvf PP-Matting-512.tgz
|
||||
wget https://bj.bcebos.com/paddlehub/fastdeploy/matting_input.jpg
|
||||
wget https://bj.bcebos.com/paddlehub/fastdeploy/matting_bgr.jpg
|
||||
# CPU推理
|
||||
python infer.py --model PP-Matting-512 --image matting_input.jpg --bg matting_bgr.jpg --device cpu
|
||||
# GPU推理
|
||||
python infer.py --model PP-Matting-512 --image matting_input.jpg --bg matting_bgr.jpg --device gpu
|
||||
# GPU上使用TensorRT推理 (注意:TensorRT推理第一次运行,有序列化模型的操作,有一定耗时,需要耐心等待)
|
||||
python infer.py --model PP-Matting-512 --image matting_input.jpg --bg matting_bgr.jpg --device gpu --use_trt True
|
||||
# 昆仑芯XPU推理
|
||||
python infer.py --model PP-Matting-512 --image matting_input.jpg --bg matting_bgr.jpg --device kunlunxin
|
||||
```
|
||||
>> ***注意** 以上示例未提供华为昇腾的示例,在编译好昇腾部署环境后,只需改造一行代码,将示例文件中的`option.use_kunlunxin()`为`option.use_ascend()`就可以完成在华为昇腾上的推理部署
|
||||
|
||||
运行完成可视化结果如下图所示
|
||||
<div width="840">
|
||||
<img width="200" height="200" float="left" src="https://user-images.githubusercontent.com/67993288/186852040-759da522-fca4-4786-9205-88c622cd4a39.jpg">
|
||||
<img width="200" height="200" float="left" src="https://user-images.githubusercontent.com/67993288/186852587-48895efc-d24a-43c9-aeec-d7b0362ab2b9.jpg">
|
||||
<img width="200" height="200" float="left" src="https://user-images.githubusercontent.com/67993288/186852116-cf91445b-3a67-45d9-a675-c69fe77c383a.jpg">
|
||||
<img width="200" height="200" float="left" src="https://user-images.githubusercontent.com/67993288/186852554-6960659f-4fd7-4506-b33b-54e1a9dd89bf.jpg">
|
||||
</div>
|
||||
|
||||
## 快速链接
|
||||
- [PaddleSeg python API文档](https://www.paddlepaddle.org.cn/fastdeploy-api-doc/python/html/semantic_segmentation.html)
|
||||
- [FastDeploy部署PaddleSeg模型概览](..)
|
||||
- [PaddleSeg C++部署](../cpp)
|
||||
|
||||
## 常见问题
|
||||
- [如何将模型预测结果SegmentationResult转为numpy格式](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/faq/vision_result_related_problems.md)
|
||||
- [如何切换模型推理后端引擎](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/faq/how_to_change_backend.md)
|
||||
- [Intel GPU(独立显卡/集成显卡)的使用](https://github.com/PaddlePaddle/FastDeploy/blob/develop/tutorials/intel_gpu/README.md)
|
||||
- [编译CPU部署库](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/build_and_install/cpu.md)
|
||||
- [编译GPU部署库](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/build_and_install/gpu.md)
|
||||
- [编译Jetson部署库](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/build_and_install/jetson.md)
|
||||
74
examples/vision/segmentation/ppmatting/cpu-gpu/python/infer.py
Executable file
74
examples/vision/segmentation/ppmatting/cpu-gpu/python/infer.py
Executable file
@@ -0,0 +1,74 @@
|
||||
import fastdeploy as fd
|
||||
import cv2
|
||||
import os
|
||||
|
||||
|
||||
def parse_arguments():
|
||||
import argparse
|
||||
import ast
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
"--model", required=True, help="Path of PaddleSeg model.")
|
||||
parser.add_argument(
|
||||
"--image", type=str, required=True, help="Path of test image file.")
|
||||
parser.add_argument(
|
||||
"--bg",
|
||||
type=str,
|
||||
required=True,
|
||||
default=None,
|
||||
help="Path of test background image file.")
|
||||
parser.add_argument(
|
||||
"--device",
|
||||
type=str,
|
||||
default='cpu',
|
||||
help="Type of inference device, support 'cpu', 'kunlunxin' or 'gpu'.")
|
||||
parser.add_argument(
|
||||
"--use_trt",
|
||||
type=ast.literal_eval,
|
||||
default=False,
|
||||
help="Wether to use tensorrt.")
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def build_option(args):
|
||||
option = fd.RuntimeOption()
|
||||
if args.device.lower() == "gpu":
|
||||
option.use_gpu()
|
||||
option.use_paddle_infer_backend()
|
||||
|
||||
if args.use_trt:
|
||||
option.use_trt_backend()
|
||||
# If use original Tensorrt, not Paddle-TensorRT,
|
||||
# comment the following two lines
|
||||
option.enable_paddle_to_trt()
|
||||
option.enable_paddle_trt_collect_shape()
|
||||
option.set_trt_input_shape("img", [1, 3, 512, 512])
|
||||
|
||||
if args.device.lower() == "kunlunxin":
|
||||
option.use_kunlunxin()
|
||||
return option
|
||||
|
||||
|
||||
args = parse_arguments()
|
||||
|
||||
# 配置runtime,加载模型
|
||||
runtime_option = build_option(args)
|
||||
model_file = os.path.join(args.model, "model.pdmodel")
|
||||
params_file = os.path.join(args.model, "model.pdiparams")
|
||||
config_file = os.path.join(args.model, "deploy.yaml")
|
||||
model = fd.vision.matting.PPMatting(
|
||||
model_file, params_file, config_file, runtime_option=runtime_option)
|
||||
|
||||
# 预测图片抠图结果
|
||||
im = cv2.imread(args.image)
|
||||
bg = cv2.imread(args.bg)
|
||||
result = model.predict(im)
|
||||
print(result)
|
||||
# 可视化结果
|
||||
vis_im = fd.vision.vis_matting(im, result)
|
||||
vis_im_with_bg = fd.vision.swap_background(im, bg, result)
|
||||
cv2.imwrite("visualized_result_fg.jpg", vis_im)
|
||||
cv2.imwrite("visualized_result_replaced_bg.jpg", vis_im_with_bg)
|
||||
print(
|
||||
"Visualized result save in ./visualized_result_replaced_bg.jpg and ./visualized_result_fg.jpg"
|
||||
)
|
||||
Reference in New Issue
Block a user